22 #ifndef _SGSOLVER_JYC_HPP
23 #define _SGSOLVER_JYC_HPP
25 #include "sgcommon.hpp"
28 #include "sgexception.hpp"
29 #include "gurobi_c++.h"
68 levels(vector< vector<double> > (
game.getNumStates(),
69 vector<double>(_numDirections,0))),
73 env.set(GRB_IntParam_Method,2);
74 env.set(GRB_IntParam_Crossover,0);
75 env.set(GRB_DoubleParam_BarConvTol,1e-12);
76 env.set(GRB_DoubleParam_OptimalityTol,1e-9);
77 env.set(GRB_DoubleParam_MarkowitzTol,0.999);
78 env.set(GRB_DoubleParam_FeasibilityTol,1e-9);
79 env.set(GRB_IntParam_OutputFlag,0);
105 double errorTol = 1e-8;
107 int numIterations = 0;
111 while (error > errorTol)
114 cout <<
"Iteration: " << numIterations
115 <<
", error: " << error << endl;
135 else if (numPlayers==3)
141 double a = 4.0*PI/
static_cast<double>(numDirsApprox);
143 int Mpsi = round(PI/d);
144 double dpsi = PI/
static_cast<double>(Mpsi);
145 double dphi = a/dpsi;
149 vector< list<vector< double> > :: const_iterator> playerMinLvls(numPlayers);
150 for (
int mpsi = 0; mpsi < Mpsi; mpsi++)
152 double psi = PI*(
static_cast<double>(mpsi)+0.5)/
static_cast<double>(Mpsi);
153 int Mphi = round(2.0*PI*sin(psi)/dpsi);
155 for (
int mphi = 0; mphi < Mphi; mphi++)
157 double phi = 2.0*PI*
static_cast<double>(mphi)/
static_cast<double>(Mphi);
159 SGPoint newDir(numPlayers,0.0);
160 newDir[0]=sin(psi)*cos(phi);
161 newDir[1]=sin(psi)*sin(phi);
180 for (
int varCtr = 0; varCtr <
model.get(GRB_IntAttr_NumVars); varCtr++)
182 var[varCtr].set(GRB_DoubleAttr_LB,-1e20);
192 SGPoint NW(SW[0],NE[1]), SE(NE[0],SW[1]);
215 for (
int p = 0; p < numPlayers; p++)
217 lhs += var[numPlayers*state+p]*
directions[dir][p];
226 for (
int p = 0; p < numPlayers; p++)
243 vector<int> actions, deviations;
254 GRBVar * var =
model.getVars();
256 GRBConstr * constr =
model.getConstrs();
259 for (
int state = 0; state < numStates; state++)
269 vector< vector<double> >
272 -numeric_limits<double>::max()));
273 for (
int state = 0; state < numStates; state++)
275 for (
int action = 0; action < numActions_total[state]; action++)
278 if (!eqActions[state].empty() && !eqActions[state][action])
283 deviations = actions;
286 for (
int player = 0; player < numPlayers; player ++)
290 lhs += (1-delta)*payoffs[state][action][player];
291 for (
int sp = 0; sp < numStates; sp++)
292 lhs += delta*prob[state][action][sp]*var[numPlayers*sp+player];
294 for (
int dev = 0; dev < numActions[state][player]; dev++)
296 if (dev == actions[player])
299 deviations[player] = dev;
304 rhs += (1-delta)*payoffs[state][deviation][player];
305 for (
int sp = 0; sp < numStates; sp++)
307 rhs += delta*prob[state][deviation][sp]*var[numPlayers*numStates
308 +numPlayers*sp+player];
311 model.addConstr(lhs >= rhs);
314 deviations[player] = actions[player];
322 for (
int sp = 0; sp < numStates; sp++)
324 for (
int player = 0; player < numPlayers; player++)
325 obj +=
directions[dir][player]*prob[state][action][sp]
326 *var[numPlayers*sp+player];
330 model.setObjective(obj);
331 model.getEnv().set(GRB_IntParam_OutputFlag,0);
332 model.set(GRB_IntAttr_ModelSense,-1);
335 if (
model.get(GRB_IntAttr_Status)==GRB_OPTIMAL)
337 double val = (1-delta)*payoffs[state][action]*
directions[dir]
338 + delta *
model.get(GRB_DoubleAttr_ObjVal);
340 if (val > newLevels[state][dir])
341 newLevels[state][dir] = val;
348 constr =
model.getConstrs();
350 constrCtr <
model.get(GRB_IntAttr_NumConstrs);
352 model.remove(constr[constrCtr]);
362 for (
int state = 0; state < numStates; state++)
365 dist = std::max(dist,abs(
levels[state][dir]-newLevels[state][dir]));