emp25.gms : Test JAMS/EMP on implicit variable handling

Description

Test JAMS behavior on a small equilibrium model with shared constraints.

Contributors: Youngdae Kim & Steve Dirkse, Dec 2016


Small Model of Type : GAMS


Category : GAMS Test library


Main file : emp25.gms

$title 'Test JAMS/EMP on implicit variable handling' (EMP25,SEQ=707)

$onText
Test JAMS behavior on a small equilibrium model with shared constraints.

Contributors: Youngdae Kim & Steve Dirkse, Dec 2016
$offText
$if not set TESTTOL $set TESTTOL 1e-3
scalars tol / %TESTTOL% /;
file opt  / 'jams.opt' /;
file info / '%emp.info%' /;

set i firms           / i1*i3 /
    b production tech / b1*b2 /;

alias(i,ii);
alias(b,bb);

* -----------------------------------------------------------------------
* Parameters and variables to define problems.
* -----------------------------------------------------------------------

table C(i,b) variable costs
      b1      b2
i1    0.55    0.81
i2    0.62    1.25
i3    0.78    1.35
;


table K(i,b) process capacities
      b1      b2
i1    21000   16000
i2    17000   22000
i3    18000   14000
;

* The inverse demand function is (alpha - beta * (sum_{(i,b)} x(i,b))).
parameter alpha inverse demand intercept / 2.5 /
          beta  inverse demand slope     / 0.0000166666667 /
;

parameter L  coal yard size / 60000 /;

positive variable
    x(i,b) production by firm i from process b
;

variable
    obj(i) objective value of firm i
;

x.up(i,b) = K(i,b);

* -----------------------------------------------------------------------
* Define and solve a Nash-Cournot game where all firms are price-makers.
* -----------------------------------------------------------------------

equations
    defobj(i)          'objective definition of firm i for Nash-Cournot game'
    defyardlimitExp(i) 'the sum of productions should not exceed L - explicit'
    defyardlimit       'the sum of productions should not exceed L - shared'
    defobjFlip1(i)     'objective definition for flipping test'
    defobjFlip2(i)     'objective definition for flipping test'    
    ;

defobj(i)..
    obj(i) =E=
    sum(b, C(i,b)*x(i,b) - (alpha - beta*sum((ii,bb), x(ii,bb)))*x(i,b));

defyardlimitExp(i)..
    sum((ii,b), x(ii,b)) =L= L;

model me / defobj, defyardlimitExp /;

put info 'equilibrium';
loop(i,
    put / 'min', obj(i);
    loop(b, put x(i,b));
    put / defobj(i), defyardlimitExp(i);
);
putclose;

putclose opt
   'Dict     explDict.txt' /
   'FileName expl.gms' ;

me.optfile = 1;
solve me using emp;
abort$[me.solvestat <> %solveStat.normalCompletion%]
     'wrong me.solvestat', me.solvestat;
abort$[me.modelstat <> %modelStat.locallyOptimal%]
     'wrong me.modelstat', me.modelstat;
abort$[ abs(obj.l('i1')+22710) > tol ]    'bad obj.l("i1")', obj.l;
abort$[ abs(obj.l('i2')+14960) > tol ]    'bad obj.l("i2")', obj.l;
abort$[ abs(obj.l('i3')+12960) > tol ]    'bad obj.l("i3")', obj.l;
abort$[ abs(x.l('i1','b1')-21000) > tol ] 'bad x.l("i1","b1")', x.l;
abort$[ abs(x.l('i1','b2')- 4000) > tol ] 'bad x.l("i1","b2")', x.l;
abort$[ abs(x.l('i2','b1')-17000) > tol ] 'bad x.l("i2","b1")', x.l;
abort$[ abs(x.l('i2','b2')-    0) > tol ] 'bad x.l("i2","b2")', x.l;
abort$[ abs(x.l('i3','b1')-18000) > tol ] 'bad x.l("i3","b1")', x.l;
abort$[ abs(x.l('i3','b2')-    0) > tol ] 'bad x.l("i3","b2")', x.l;
abort$[ smax{i, abs(defyardlimitExp.m(i)+.273)} > tol ]
     'bad defyardlimitExp.m(i)', defyardlimitExp.m;

obj.l(i) = 0;
x.l(i,b) = 0;
defobj.m(i) = 0;

$onEcho > expl_gms
***********************************************
* written by GAMS/JAMS at 12/03/16 19:49:19
* for more information use JAMS option "Dict"
***********************************************

Variables  x1,x2,x3,x4,x5,x6,u4,u5,u6;

Negative Variables  u4,u5,u6;

Positive Variables  x1,x2,x3,x4,x5,x6;

Equations  e4,e5,e6,dL_dx1,dL_dx2,dL_dx3,dL_dx4,dL_dx5,dL_dx6;


e4..    x1 + x2 + x3 + x4 + x5 + x6 =L= 60000;

e5..    x1 + x2 + x3 + x4 + x5 + x6 =L= 60000;

e6..    x1 + x2 + x3 + x4 + x5 + x6 =L= 60000;

dL_dx1.. (-(0.55 - (2.5 - 1.66666667e-5*x1 - 1.66666667e-5*x2 - 1.66666667e-5*
         x3 - 1.66666667e-5*x4 - 1.66666667e-5*x5 - 1.66666667e-5*x6 - 
         1.66666667e-5*x1) + 1.66666667e-5*x2))/(-1) - u4 =N= 0;

dL_dx2.. (-(1.66666667e-5*x1 + 0.81 - (2.5 - 1.66666667e-5*x1 - 1.66666667e-5*
         x2 - 1.66666667e-5*x3 - 1.66666667e-5*x4 - 1.66666667e-5*x5 - 
         1.66666667e-5*x6 - 1.66666667e-5*x2)))/(-1) - u4 =N= 0;

dL_dx3.. (-(0.62 - (2.5 - 1.66666667e-5*x1 - 1.66666667e-5*x2 - 1.66666667e-5*
         x3 - 1.66666667e-5*x4 - 1.66666667e-5*x5 - 1.66666667e-5*x6 - 
         1.66666667e-5*x3) + 1.66666667e-5*x4))/(-1) - u5 =N= 0;

dL_dx4.. (-(1.66666667e-5*x3 + 1.25 - (2.5 - 1.66666667e-5*x1 - 1.66666667e-5*
         x2 - 1.66666667e-5*x3 - 1.66666667e-5*x4 - 1.66666667e-5*x5 - 
         1.66666667e-5*x6 - 1.66666667e-5*x4)))/(-1) - u5 =N= 0;

dL_dx5.. (-(0.78 - (2.5 - 1.66666667e-5*x1 - 1.66666667e-5*x2 - 1.66666667e-5*
         x3 - 1.66666667e-5*x4 - 1.66666667e-5*x5 - 1.66666667e-5*x6 - 
         1.66666667e-5*x5) + 1.66666667e-5*x6))/(-1) - u6 =N= 0;

dL_dx6.. (-(1.66666667e-5*x5 + 1.35 - (2.5 - 1.66666667e-5*x1 - 1.66666667e-5*
         x2 - 1.66666667e-5*x3 - 1.66666667e-5*x4 - 1.66666667e-5*x5 - 
         1.66666667e-5*x6 - 1.66666667e-5*x6)))/(-1) - u6 =N= 0;

* set non-default bounds
x1.up = 21000;
x2.up = 16000;
x3.up = 17000;
x4.up = 22000;
x5.up = 18000;
x6.up = 14000;

Model m / e4.u4,e5.u5,e6.u6,dL_dx1.x1,dL_dx2.x2,dL_dx3.x3,dL_dx4.x4
         ,dL_dx5.x5,dL_dx6.x6 /;

m.limrow=0; m.limcol=0;

Solve m using MCP;
$offEcho

execute 'grep -v " written by GAMS" expl_gms > expl.gms.want'
execute 'grep -v " written by GAMS" expl.gms > expl.gms.got'
execute '=diff -I reslim -bw expl.gms.want expl.gms.got'
abort$errorlevel 'Files expl.gms.want and expl.gms.got differ';

$onEcho > expl_dict
* written by GAMS/JAMS at 12/03/16 22:51:53

********************************************************************************
 Contents
 1 Content of EMP Information File
 2 Processed EMP Information
 3 Dictionary
  3.1 Constraints
  3.2 Variables
********************************************************************************


 1 Content of EMP Information File
 ---------------------------------

     1:equilibrium
     2:min obj('i1') x('i1','b1') x('i1','b2')
     3:defobj('i1') defyardlimitExp('i1')
     4:min obj('i2') x('i2','b1') x('i2','b2')
     5:defobj('i2') defyardlimitExp('i2')
     6:min obj('i3') x('i3','b1') x('i3','b2')
     7:defobj('i3') defyardlimitExp('i3')


 2 Processed EMP Information
 ---------------------------


 3 Dictionary
 ------------
  3.1 Constraints
  ---------------

      {reformulated}  defobj(i1)
      {reformulated}  defobj(i2)
      {reformulated}  defobj(i3)
                  e4  defyardlimitExp(i1)
                  e5  defyardlimitExp(i2)
                  e6  defyardlimitExp(i3)
              dL_dx1  {new}
              dL_dx2  {new}
              dL_dx3  {new}
              dL_dx4  {new}
              dL_dx5  {new}
              dL_dx6  {new}


  3.2 Variables
 --------------

                  x1  x(i1,b1)
                  x2  x(i1,b2)
                  x3  x(i2,b1)
                  x4  x(i2,b2)
                  x5  x(i3,b1)
                  x6  x(i3,b2)
      {reformulated}  obj(i1)
      {reformulated}  obj(i2)
      {reformulated}  obj(i3)
                  u4  {new}
                  u5  {new}
                  u6  {new}
$offEcho

execute 'grep -v " written by GAMS" expl_dict > expldict.txt.want'
execute 'grep -v " written by GAMS" explDict.txt > expldict.txt.got'
execute '=diff -bw expldict.txt.want expldict.txt.got'
abort$errorlevel 'Files expldict.txt.want and expldict.txt.got differ';

* test with GNEP type shared constraint

defyardlimit..
    sum((i,b), x(i,b)) =L= L;

model ms / defobj, defyardlimit /;

put info 'equilibrium' /;
loop(i,
    put 'min', obj(i);
    loop(b, put x(i,b));
    put defobj(i), defyardlimit /;
);
putclose;

putclose opt
   'Dict     sharedGNEPDict.txt' /
   'FileName sharedGNEP.gms' /
   'SharedEqu' ;

ms.optfile = 1;
solve ms using emp;
abort$[ms.solvestat <> %solveStat.normalCompletion%]
     'wrong ms.solvestat', ms.solvestat;
abort$[ms.modelstat <> %modelStat.locallyOptimal%]
     'wrong ms.modelstat', ms.modelstat;

* Load multipliers of defyardlimit constraints

set ag / 1*3 /;
parameter defyardlimit_m(ag);
execute_load '%gams.scrdir%/ugdx.dat', defyardlimit_m=defyardlimit;

abort$[ abs(obj.l('i1')+22710) > tol ]    'bad obj.l("i1")', obj.l;
abort$[ abs(obj.l('i2')+14960) > tol ]    'bad obj.l("i2")', obj.l;
abort$[ abs(obj.l('i3')+12960) > tol ]    'bad obj.l("i3")', obj.l;
abort$[ abs(x.l('i1','b1')-21000) > tol ] 'bad x.l("i1","b1")', x.l;
abort$[ abs(x.l('i1','b2')- 4000) > tol ] 'bad x.l("i1","b2")', x.l;
abort$[ abs(x.l('i2','b1')-17000) > tol ] 'bad x.l("i2","b1")', x.l;
abort$[ abs(x.l('i2','b2')-    0) > tol ] 'bad x.l("i2","b2")', x.l;
abort$[ abs(x.l('i3','b1')-18000) > tol ] 'bad x.l("i3","b1")', x.l;
abort$[ abs(x.l('i3','b2')-    0) > tol ] 'bad x.l("i3","b2")', x.l;
abort$[ smax{ag, abs(defyardlimit_m(ag)+.273)} > tol ]
     'bad defyardlimit_m(agent_num)', defyardlimit_m;

obj.l(i) = 0;
x.l(i,b) = 0;
defobj.m(i) = 0;
defyardlimit.m = 0;

$onEcho > sharedGNEP_gms
***********************************************
* written by GAMS/JAMS at 12/05/16 14:44:06
* for more information use JAMS option "Dict"
***********************************************

Variables  x1,x2,x3,x4,x5,x6,u4_4,u4_5,u4_6;

Negative Variables  u4_4,u4_5,u4_6;

Positive Variables  x1,x2,x3,x4,x5,x6;

Equations  e4_4,e4_5,e4_6,dL_dx1,dL_dx2,dL_dx3,dL_dx4,dL_dx5,dL_dx6;


e4_4..    x1 + x2 + x3 + x4 + x5 + x6 =L= 60000;

e4_5..   x1 + x2 + x3 + x4 + x5 + x6 =L= 60000;

e4_6..   x1 + x2 + x3 + x4 + x5 + x6 =L= 60000;

dL_dx1.. (-(0.55 - (2.5 - 1.66666667e-5*x1 - 1.66666667e-5*x2 - 1.66666667e-5*
         x3 - 1.66666667e-5*x4 - 1.66666667e-5*x5 - 1.66666667e-5*x6 - 
         1.66666667e-5*x1) + 1.66666667e-5*x2))/(-1) - u4_4 =N= 0;

dL_dx2.. (-(1.66666667e-5*x1 + 0.81 - (2.5 - 1.66666667e-5*x1 - 1.66666667e-5*
         x2 - 1.66666667e-5*x3 - 1.66666667e-5*x4 - 1.66666667e-5*x5 - 
         1.66666667e-5*x6 - 1.66666667e-5*x2)))/(-1) - u4_4 =N= 0;

dL_dx3.. (-(0.62 - (2.5 - 1.66666667e-5*x1 - 1.66666667e-5*x2 - 1.66666667e-5*
         x3 - 1.66666667e-5*x4 - 1.66666667e-5*x5 - 1.66666667e-5*x6 - 
         1.66666667e-5*x3) + 1.66666667e-5*x4))/(-1) - u4_5 =N= 0;

dL_dx4.. (-(1.66666667e-5*x3 + 1.25 - (2.5 - 1.66666667e-5*x1 - 1.66666667e-5*
         x2 - 1.66666667e-5*x3 - 1.66666667e-5*x4 - 1.66666667e-5*x5 - 
         1.66666667e-5*x6 - 1.66666667e-5*x4)))/(-1) - u4_5 =N= 0;

dL_dx5.. (-(0.78 - (2.5 - 1.66666667e-5*x1 - 1.66666667e-5*x2 - 1.66666667e-5*
         x3 - 1.66666667e-5*x4 - 1.66666667e-5*x5 - 1.66666667e-5*x6 - 
         1.66666667e-5*x5) + 1.66666667e-5*x6))/(-1) - u4_6 =N= 0;

dL_dx6.. (-(1.66666667e-5*x5 + 1.35 - (2.5 - 1.66666667e-5*x1 - 1.66666667e-5*
         x2 - 1.66666667e-5*x3 - 1.66666667e-5*x4 - 1.66666667e-5*x5 - 
         1.66666667e-5*x6 - 1.66666667e-5*x6)))/(-1) - u4_6 =N= 0;

* set non-default bounds
x1.up = 21000;
x2.up = 16000;
x3.up = 17000;
x4.up = 22000;
x5.up = 18000;
x6.up = 14000;

Model m / e4_4.u4_4,e4_5.u4_5,e4_6.u4_6,dL_dx1.x1,dL_dx2.x2,dL_dx3.x3
         ,dL_dx4.x4,dL_dx5.x5,dL_dx6.x6 /;

m.limrow=0; m.limcol=0;

Solve m using MCP;
$offEcho

execute 'grep -v " written by GAMS" sharedGNEP_gms > sharedGNEP.gms.want'
execute 'grep -v " written by GAMS" sharedGNEP.gms > sharedGNEP.gms.got'
execute '=diff -I reslim -bw sharedGNEP.gms.want sharedGNEP.gms.got'
abort$errorlevel 'Files sharedGNEP.gms.want and sharedGNEP.gms.got differ';

$onEcho > sharedGNEP_dict
* written by GAMS/JAMS at 12/05/16 14:44:06

********************************************************************************
 Contents
 1 Content of EMP Information File
 2 Processed EMP Information
 3 Dictionary
  3.1 Constraints
  3.2 Variables
********************************************************************************


 1 Content of EMP Information File
 ---------------------------------

     1:equilibrium
     2:min obj('i1') x('i1','b1') x('i1','b2') defobj('i1') defyardlimit
     3:min obj('i2') x('i2','b1') x('i2','b2') defobj('i2') defyardlimit
     4:min obj('i3') x('i3','b1') x('i3','b2') defobj('i3') defyardlimit


 2 Processed EMP Information
 ---------------------------


 3 Dictionary
 ------------
  3.1 Constraints
  ---------------

      {reformulated}  defobj(i1)
      {reformulated}  defobj(i2)
      {reformulated}  defobj(i3)
                e4_4  defyardlimit
                e4_5  defyardlimit
                e4_6  defyardlimit
              dL_dx1  {new}
              dL_dx2  {new}
              dL_dx3  {new}
              dL_dx4  {new}
              dL_dx5  {new}
              dL_dx6  {new}


  3.2 Variables
 --------------

                  x1  x(i1,b1)
                  x2  x(i1,b2)
                  x3  x(i2,b1)
                  x4  x(i2,b2)
                  x5  x(i3,b1)
                  x6  x(i3,b2)
      {reformulated}  obj(i1)
      {reformulated}  obj(i2)
      {reformulated}  obj(i3)
                u4_4  {new}
                u4_5  {new}
                u4_6  {new}
$offEcho

execute 'grep -v " written by GAMS" sharedGNEP_dict > sharedGNEPdict.txt.want'
execute 'grep -v " written by GAMS" sharedGNEPDict.txt > sharedGNEPdict.txt.got'
execute '=diff -bw sharedGNEPdict.txt.want sharedGNEPdict.txt.got'
abort$errorlevel 'Files sharedGNEPdict.txt.want and sharedGNEPdict.txt.got differ';

* test with VI type shared constraint
put info 'equilibrium' /;
put 'visol defyardlimit' /;
loop(i,
    put 'min', obj(i);
    loop(b, put x(i,b));
    put defobj(i), defyardlimit /;
);
putclose;

putclose opt
   'Dict     sharedVIDict.txt' /
   'FileName sharedVI.gms'     /
   'SharedEqu';

solve ms using emp;
abort$[ms.solvestat <> %solveStat.normalCompletion%]
     'wrong ms.solvestat', ms.solvestat;
abort$[ms.modelstat <> %modelStat.locallyOptimal%]
     'wrong ms.modelstat', ms.modelstat;
abort$[ abs(obj.l('i1')+22710) > tol ]    'bad obj.l("i1")', obj.l;
abort$[ abs(obj.l('i2')+14960) > tol ]    'bad obj.l("i2")', obj.l;
abort$[ abs(obj.l('i3')+12960) > tol ]    'bad obj.l("i3")', obj.l;
abort$[ abs(x.l('i1','b1')-21000) > tol ] 'bad x.l("i1","b1")', x.l;
abort$[ abs(x.l('i1','b2')- 4000) > tol ] 'bad x.l("i1","b2")', x.l;
abort$[ abs(x.l('i2','b1')-17000) > tol ] 'bad x.l("i2","b1")', x.l;
abort$[ abs(x.l('i2','b2')-    0) > tol ] 'bad x.l("i2","b2")', x.l;
abort$[ abs(x.l('i3','b1')-18000) > tol ] 'bad x.l("i3","b1")', x.l;
abort$[ abs(x.l('i3','b2')-    0) > tol ] 'bad x.l("i3","b2")', x.l;
abort$[ abs(defyardlimit.m+.273) > tol ] 'bad defyardlimit.m', defyardlimit.m;

obj.l(i) = 0;
x.l(i,b) = 0;
defobj.m(i) = 0;
defyardlimit.m = 0;

$onEcho > sharedVI_gms
***********************************************
* written by GAMS/JAMS at 12/04/16 22:35:48
* for more information use JAMS option "Dict"
***********************************************

Variables  x1,x2,x3,x4,x5,x6,u4;

Negative Variables  u4;

Positive Variables  x1,x2,x3,x4,x5,x6;

Equations  e4,dL_dx1,dL_dx2,dL_dx3,dL_dx4,dL_dx5,dL_dx6;


e4..    x1 + x2 + x3 + x4 + x5 + x6 =L= 60000;

dL_dx1.. (-(0.55 - (2.5 - 1.66666667e-5*x1 - 1.66666667e-5*x2 - 1.66666667e-5*
         x3 - 1.66666667e-5*x4 - 1.66666667e-5*x5 - 1.66666667e-5*x6 - 
         1.66666667e-5*x1) + 1.66666667e-5*x2))/(-1) - u4 =N= 0;

dL_dx2.. (-(1.66666667e-5*x1 + 0.81 - (2.5 - 1.66666667e-5*x1 - 1.66666667e-5*
         x2 - 1.66666667e-5*x3 - 1.66666667e-5*x4 - 1.66666667e-5*x5 - 
         1.66666667e-5*x6 - 1.66666667e-5*x2)))/(-1) - u4 =N= 0;

dL_dx3.. (-(0.62 - (2.5 - 1.66666667e-5*x1 - 1.66666667e-5*x2 - 1.66666667e-5*
         x3 - 1.66666667e-5*x4 - 1.66666667e-5*x5 - 1.66666667e-5*x6 - 
         1.66666667e-5*x3) + 1.66666667e-5*x4))/(-1) - u4 =N= 0;

dL_dx4.. (-(1.66666667e-5*x3 + 1.25 - (2.5 - 1.66666667e-5*x1 - 1.66666667e-5*
         x2 - 1.66666667e-5*x3 - 1.66666667e-5*x4 - 1.66666667e-5*x5 - 
         1.66666667e-5*x6 - 1.66666667e-5*x4)))/(-1) - u4 =N= 0;

dL_dx5.. (-(0.78 - (2.5 - 1.66666667e-5*x1 - 1.66666667e-5*x2 - 1.66666667e-5*
         x3 - 1.66666667e-5*x4 - 1.66666667e-5*x5 - 1.66666667e-5*x6 - 
         1.66666667e-5*x5) + 1.66666667e-5*x6))/(-1) - u4 =N= 0;

dL_dx6.. (-(1.66666667e-5*x5 + 1.35 - (2.5 - 1.66666667e-5*x1 - 1.66666667e-5*
         x2 - 1.66666667e-5*x3 - 1.66666667e-5*x4 - 1.66666667e-5*x5 - 
         1.66666667e-5*x6 - 1.66666667e-5*x6)))/(-1) - u4 =N= 0;

* set non-default bounds
x1.up = 21000;
x2.up = 16000;
x3.up = 17000;
x4.up = 22000;
x5.up = 18000;
x6.up = 14000;

Model m / e4.u4,dL_dx1.x1,dL_dx2.x2,dL_dx3.x3,dL_dx4.x4,dL_dx5.x5,dL_dx6.x6 /;

m.limrow=0; m.limcol=0;

Solve m using MCP;
$offEcho

execute 'grep -v " written by GAMS" sharedVI_gms > sharedVI.gms.want'
execute 'grep -v " written by GAMS" sharedVI.gms > sharedVI.gms.got'
execute '=diff -I reslim -bw sharedVI.gms.want sharedVI.gms.got'
abort$errorlevel 'Files sharedVI.gms.want and sharedVI.gms.got differ';

$onEcho > sharedVI_dict
* written by GAMS/JAMS at 12/04/16 22:35:48

********************************************************************************
 Contents
 1 Content of EMP Information File
 2 Processed EMP Information
 3 Dictionary
  3.1 Constraints
  3.2 Variables
********************************************************************************


 1 Content of EMP Information File
 ---------------------------------

     1:equilibrium
     2:visol defyardlimit
     3:min obj('i1') x('i1','b1') x('i1','b2') defobj('i1') defyardlimit
     4:min obj('i2') x('i2','b1') x('i2','b2') defobj('i2') defyardlimit
     5:min obj('i3') x('i3','b1') x('i3','b2') defobj('i3') defyardlimit


 2 Processed EMP Information
 ---------------------------


 3 Dictionary
 ------------
  3.1 Constraints
  ---------------

      {reformulated}  defobj(i1)
      {reformulated}  defobj(i2)
      {reformulated}  defobj(i3)
                  e4  defyardlimit
              dL_dx1  {new}
              dL_dx2  {new}
              dL_dx3  {new}
              dL_dx4  {new}
              dL_dx5  {new}
              dL_dx6  {new}


  3.2 Variables
 --------------

                  x1  x(i1,b1)
                  x2  x(i1,b2)
                  x3  x(i2,b1)
                  x4  x(i2,b2)
                  x5  x(i3,b1)
                  x6  x(i3,b2)
      {reformulated}  obj(i1)
      {reformulated}  obj(i2)
      {reformulated}  obj(i3)
                  u4  {new}
$offEcho

execute 'grep -v " written by GAMS" sharedVI_dict > sharedVIdict.txt.want'
execute 'grep -v " written by GAMS" sharedVIDict.txt > sharedVIdict.txt.got'
execute '=diff -bw sharedVIdict.txt.want sharedVIdict.txt.got'
abort$errorlevel 'Files sharedVIdict.txt.want and sharedVIdict.txt.got differ';

* objective functions to test with flipping:
* In defobjFlip1, the second agent maximizes.
* In defobjFlip2, the second and third agents maximizes.
* These changes are made to make the multiplier sign consistent for VI case.

defobjFlip1(i)..
    (obj(i))$(ord(i) ne 2) + (-obj(i))$(ord(i) eq 2) =E=
    sum(b, C(i,b)*x(i,b) - (alpha - beta*sum((ii,bb), x(ii,bb)))*x(i,b));

defobjFlip2(i)..
    (obj(i))$(ord(i) eq 1) + (-obj(i))$(ord(i) ne 1) =E=
    sum(b, C(i,b)*x(i,b) - (alpha - beta*sum((ii,bb), x(ii,bb)))*x(i,b));

model mf1 / defobjFlip1, defyardlimit /;
model mf2 / defobjFlip2, defyardlimit /;

mf1.optfile = 1;
mf2.optfile = 1;

* testing GNEP constraint with flipping:
* In whatever way we flip, it should give us a correct solution.

put info 'equilibrium' /;

put 'min', obj('i1');
loop(b, put x('i1',b););
put defobjFlip1('i1'), '-defyardlimit' /;

put 'max', obj('i2');
loop(b, put x('i2',b););
put defobjFlip1('i2'), '-defyardlimit' /;

put 'min', obj('i3');
loop(b, put x('i3',b););
put defobjFlip1('i3'), defyardlimit /;

putclose;

putclose opt
   'Dict     sharedGNEPFlip1Dict.txt' /
   'FileName sharedGNEPFlip1.gms' /
   'SharedEqu' ;

solve mf1 using emp;

obj.l(i)$(ord(i) eq 2) = -obj.l(i);

abort$[mf1.solvestat <> %solveStat.normalCompletion%]
     'wrong mf1.solvestat', mf1.solvestat;
abort$[mf1.modelstat <> %modelStat.locallyOptimal%]
     'wrong mf1.modelstat', mf1.modelstat;
abort$[ abs(obj.l('i1')+22710) > tol ]    'bad obj.l("i1")', obj.l;
abort$[ abs(obj.l('i2')+14960) > tol ]    'bad obj.l("i2")', obj.l;
abort$[ abs(obj.l('i3')+12960) > tol ]    'bad obj.l("i3")', obj.l;
abort$[ abs(x.l('i1','b1')-21000) > tol ] 'bad x.l("i1","b1")', x.l;
abort$[ abs(x.l('i1','b2')- 4000) > tol ] 'bad x.l("i1","b2")', x.l;
abort$[ abs(x.l('i2','b1')-17000) > tol ] 'bad x.l("i2","b1")', x.l;
abort$[ abs(x.l('i2','b2')-    0) > tol ] 'bad x.l("i2","b2")', x.l;
abort$[ abs(x.l('i3','b1')-18000) > tol ] 'bad x.l("i3","b1")', x.l;
abort$[ abs(x.l('i3','b2')-    0) > tol ] 'bad x.l("i3","b2")', x.l;

execute_load '%gams.scrdir%/ugdx.dat', defyardlimit_m=defyardlimit;

abort$[ abs(defyardlimit_m('1')-.273) > tol ]
     'bad.defyardlimit_m("1")', defyardlimit_m;
abort$[ abs(defyardlimit_m('2')+.273) > tol ]
     'bad.defyardlimit_m("2")', defyardlimit_m;
abort$[ abs(defyardlimit_m('3')+.273) > tol ]
     'bad.defyardlimit_m("3")', defyardlimit_m;

obj.l(i) = 0;
x.l(i,b) = 0;
defobjFlip1.m(i) = 0;
defobjFlip2.m(i) = 0;
defyardlimit.m = 0;

$onEcho > sharedGNEPFlip1_gms
***********************************************
* written by GAMS/JAMS at 12/06/16 13:36:56
* for more information use JAMS option "Dict"
***********************************************

Variables  x1,x2,x3,x4,x5,x6,u4_4,u4_5,u4_6;

Negative Variables  u4_5,u4_6;

Positive Variables  x1,x2,x3,x4,x5,x6;

Positive Variables  u4_4;

Equations  e4_4,e4_5,e4_6,dL_dx1,dL_dx2,dL_dx3,dL_dx4,dL_dx5,dL_dx6;


e4_4.. 60000 =G=    x1 + x2 + x3 + x4 + x5 + x6;

e4_5..   x1 + x2 + x3 + x4 + x5 + x6 =L= 60000;

e4_6..   x1 + x2 + x3 + x4 + x5 + x6 =L= 60000;

dL_dx1.. (-(0.55 - (2.5 - 1.66666667e-5*x1 - 1.66666667e-5*x2 - 1.66666667e-5*
         x3 - 1.66666667e-5*x4 - 1.66666667e-5*x5 - 1.66666667e-5*x6 - 
         1.66666667e-5*x1) + 1.66666667e-5*x2))/(-1) + u4_4 =N= 0;

dL_dx2.. (-(1.66666667e-5*x1 + 0.81 - (2.5 - 1.66666667e-5*x1 - 1.66666667e-5*
         x2 - 1.66666667e-5*x3 - 1.66666667e-5*x4 - 1.66666667e-5*x5 - 
         1.66666667e-5*x6 - 1.66666667e-5*x2)))/(-1) + u4_4 =N= 0;

dL_dx3.. (-(0.62 - (2.5 - 1.66666667e-5*x1 - 1.66666667e-5*x2 - 1.66666667e-5*
         x3 - 1.66666667e-5*x4 - 1.66666667e-5*x5 - 1.66666667e-5*x6 - 
         1.66666667e-5*x3) + 1.66666667e-5*x4))/(-1) - u4_5 =N= 0;

dL_dx4.. (-(1.66666667e-5*x3 + 1.25 - (2.5 - 1.66666667e-5*x1 - 1.66666667e-5*
         x2 - 1.66666667e-5*x3 - 1.66666667e-5*x4 - 1.66666667e-5*x5 - 
         1.66666667e-5*x6 - 1.66666667e-5*x4)))/(-1) - u4_5 =N= 0;

dL_dx5.. (-(0.78 - (2.5 - 1.66666667e-5*x1 - 1.66666667e-5*x2 - 1.66666667e-5*
         x3 - 1.66666667e-5*x4 - 1.66666667e-5*x5 - 1.66666667e-5*x6 - 
         1.66666667e-5*x5) + 1.66666667e-5*x6))/(-1) - u4_6 =N= 0;

dL_dx6.. (-(1.66666667e-5*x5 + 1.35 - (2.5 - 1.66666667e-5*x1 - 1.66666667e-5*
         x2 - 1.66666667e-5*x3 - 1.66666667e-5*x4 - 1.66666667e-5*x5 - 
         1.66666667e-5*x6 - 1.66666667e-5*x6)))/(-1) - u4_6 =N= 0;

* set non-default bounds
x1.up = 21000;
x2.up = 16000;
x3.up = 17000;
x4.up = 22000;
x5.up = 18000;
x6.up = 14000;

Model m / e4_4.u4_4,e4_5.u4_5,e4_6.u4_6,dL_dx1.x1,dL_dx2.x2,dL_dx3.x3
         ,dL_dx4.x4,dL_dx5.x5,dL_dx6.x6 /;

m.limrow=0; m.limcol=0;

Solve m using MCP;
$offEcho

execute 'grep -v " written by GAMS" sharedGNEPFlip1_gms > sharedGNEPFlip1.gms.want'
execute 'grep -v " written by GAMS" sharedGNEPFlip1.gms > sharedGNEPFlip1.gms.got'
execute '=diff -I reslim -bw sharedGNEPFlip1.gms.want sharedGNEPFlip1.gms.got'
abort$errorlevel 'Files sharedGNEPFlip1.gms.want and sharedGNEPFlip1.gms.got differ';

$onEcho > sharedGNEPFlip1_dict
* written by GAMS/JAMS at 12/06/16 13:36:56

********************************************************************************
 Contents
 1 Content of EMP Information File
 2 Processed EMP Information
 3 Dictionary
  3.1 Constraints
  3.2 Variables
********************************************************************************


 1 Content of EMP Information File
 ---------------------------------

     1:equilibrium
     2:min obj('i1') x('i1','b1') x('i1','b2') defobjFlip1('i1') -defyardlimit
     3:max obj('i2') x('i2','b1') x('i2','b2') defobjFlip1('i2') -defyardlimit
     4:min obj('i3') x('i3','b1') x('i3','b2') defobjFlip1('i3') defyardlimit


 2 Processed EMP Information
 ---------------------------

   Number of constraints to be flipped = 2
   * e4_4 will be flipped internally
   * e4_5 will be flipped internally


 3 Dictionary
 ------------
  3.1 Constraints
  ---------------

      {reformulated}  defobjFlip1(i1)
      {reformulated}  defobjFlip1(i2)
      {reformulated}  defobjFlip1(i3)
                e4_4  defyardlimit
                e4_5  defyardlimit
                e4_6  defyardlimit
              dL_dx1  {new}
              dL_dx2  {new}
              dL_dx3  {new}
              dL_dx4  {new}
              dL_dx5  {new}
              dL_dx6  {new}


  3.2 Variables
 --------------

                  x1  x(i1,b1)
                  x2  x(i1,b2)
                  x3  x(i2,b1)
                  x4  x(i2,b2)
                  x5  x(i3,b1)
                  x6  x(i3,b2)
      {reformulated}  obj(i1)
      {reformulated}  obj(i2)
      {reformulated}  obj(i3)
                u4_4  {new}
                u4_5  {new}
                u4_6  {new}
$offEcho

execute 'grep -v " written by GAMS" sharedGNEPFlip1_dict > sharedGNEPFlip1dict.txt.want'
execute 'grep -v " written by GAMS" sharedGNEPFlip1Dict.txt > sharedGNEPFlip1dict.txt.got'
execute '=diff -bw sharedGNEPFlip1dict.txt.want sharedGNEPFlip1dict.txt.got'
abort$errorlevel 'Files sharedGNEPFlip1dict.txt.want and sharedGNEPFlip1dict.txt.got differ';

* another testing GNEP constraint with flipping:
* In whatever way we flip, it should give us a correct solution.

put info 'equilibrium' /;

put 'min', obj('i1');
loop(b, put x('i1',b););
put defobjFlip2('i1'), '-defyardlimit' /;

put 'max', obj('i2');
loop(b, put x('i2',b););
put defobjFlip2('i2'), '-defyardlimit' /;

put 'max', obj('i3');
loop(b, put x('i3',b););
put defobjFlip2('i3'), defyardlimit /;

putclose;

putclose opt
   'Dict     sharedGNEPFlip2Dict.txt' /
   'FileName sharedGNEPFlip2.gms' /
   'SharedEqu' ;

solve mf2 using emp;

obj.l(i)$(ord(i) ne 1) = -obj.l(i);

abort$[mf2.solvestat <> %solveStat.normalCompletion%]
     'wrong mf2.solvestat', mf2.solvestat;
abort$[mf2.modelstat <> %modelStat.locallyOptimal%]
     'wrong mf2.modelstat', mf2.modelstat;
abort$[ abs(obj.l('i1')+22710) > tol ]    'bad obj.l("i1")', obj.l;
abort$[ abs(obj.l('i2')+14960) > tol ]    'bad obj.l("i2")', obj.l;
abort$[ abs(obj.l('i3')+12960) > tol ]    'bad obj.l("i3")', obj.l;
abort$[ abs(x.l('i1','b1')-21000) > tol ] 'bad x.l("i1","b1")', x.l;
abort$[ abs(x.l('i1','b2')- 4000) > tol ] 'bad x.l("i1","b2")', x.l;
abort$[ abs(x.l('i2','b1')-17000) > tol ] 'bad x.l("i2","b1")', x.l;
abort$[ abs(x.l('i2','b2')-    0) > tol ] 'bad x.l("i2","b2")', x.l;
abort$[ abs(x.l('i3','b1')-18000) > tol ] 'bad x.l("i3","b1")', x.l;
abort$[ abs(x.l('i3','b2')-    0) > tol ] 'bad x.l("i3","b2")', x.l;

execute_load '%gams.scrdir%/ugdx.dat', defyardlimit_m=defyardlimit;

abort$[ abs(defyardlimit_m('1')-.273) > tol ]
     'bad.defyardlimit_m("1")', defyardlimit_m;
abort$[ abs(defyardlimit_m('2')+.273) > tol ]
     'bad.defyardlimit_m("2")', defyardlimit_m;
abort$[ abs(defyardlimit_m('3')-.273) > tol ]
     'bad.defyardlimit_m("3")', defyardlimit_m;

obj.l(i) = 0;
x.l(i,b) = 0;
defobjFlip1.m(i) = 0;
defobjFlip2.m(i) = 0;
defyardlimit.m = 0;

$onEcho > sharedGNEPFlip2_gms
***********************************************
* written by GAMS/JAMS at 12/06/16 13:44:14
* for more information use JAMS option "Dict"
***********************************************

Variables  x1,x2,x3,x4,x5,x6,u4_4,u4_5,u4_6;

Negative Variables  u4_5;

Positive Variables  x1,x2,x3,x4,x5,x6;

Positive Variables  u4_4,u4_6;

Equations  e4_4,e4_5,e4_6,dL_dx1,dL_dx2,dL_dx3,dL_dx4,dL_dx5,dL_dx6;


e4_4.. 60000 =G=    x1 + x2 + x3 + x4 + x5 + x6;

e4_5..   x1 + x2 + x3 + x4 + x5 + x6 =L= 60000;

e4_6..60000 =G=    x1 + x2 + x3 + x4 + x5 + x6;

dL_dx1.. (-(0.55 - (2.5 - 1.66666667e-5*x1 - 1.66666667e-5*x2 - 1.66666667e-5*
         x3 - 1.66666667e-5*x4 - 1.66666667e-5*x5 - 1.66666667e-5*x6 - 
         1.66666667e-5*x1) + 1.66666667e-5*x2))/(-1) + u4_4 =N= 0;

dL_dx2.. (-(1.66666667e-5*x1 + 0.81 - (2.5 - 1.66666667e-5*x1 - 1.66666667e-5*
         x2 - 1.66666667e-5*x3 - 1.66666667e-5*x4 - 1.66666667e-5*x5 - 
         1.66666667e-5*x6 - 1.66666667e-5*x2)))/(-1) + u4_4 =N= 0;

dL_dx3.. (-(0.62 - (2.5 - 1.66666667e-5*x1 - 1.66666667e-5*x2 - 1.66666667e-5*
         x3 - 1.66666667e-5*x4 - 1.66666667e-5*x5 - 1.66666667e-5*x6 - 
         1.66666667e-5*x3) + 1.66666667e-5*x4))/(-1) - u4_5 =N= 0;

dL_dx4.. (-(1.66666667e-5*x3 + 1.25 - (2.5 - 1.66666667e-5*x1 - 1.66666667e-5*
         x2 - 1.66666667e-5*x3 - 1.66666667e-5*x4 - 1.66666667e-5*x5 - 
         1.66666667e-5*x6 - 1.66666667e-5*x4)))/(-1) - u4_5 =N= 0;

dL_dx5.. (-(0.78 - (2.5 - 1.66666667e-5*x1 - 1.66666667e-5*x2 - 1.66666667e-5*
         x3 - 1.66666667e-5*x4 - 1.66666667e-5*x5 - 1.66666667e-5*x6 - 
         1.66666667e-5*x5) + 1.66666667e-5*x6))/(-1) + u4_6 =N= 0;

dL_dx6.. (-(1.66666667e-5*x5 + 1.35 - (2.5 - 1.66666667e-5*x1 - 1.66666667e-5*
         x2 - 1.66666667e-5*x3 - 1.66666667e-5*x4 - 1.66666667e-5*x5 - 
         1.66666667e-5*x6 - 1.66666667e-5*x6)))/(-1) + u4_6 =N= 0;

* set non-default bounds
x1.up = 21000;
x2.up = 16000;
x3.up = 17000;
x4.up = 22000;
x5.up = 18000;
x6.up = 14000;

Model m / e4_4.u4_4,e4_5.u4_5,e4_6.u4_6,dL_dx1.x1,dL_dx2.x2,dL_dx3.x3
         ,dL_dx4.x4,dL_dx5.x5,dL_dx6.x6 /;

m.limrow=0; m.limcol=0;

Solve m using MCP;
$offEcho

execute 'grep -v " written by GAMS" sharedGNEPFlip2_gms > sharedGNEPFlip2.gms.want'
execute 'grep -v " written by GAMS" sharedGNEPFlip2.gms > sharedGNEPFlip2.gms.got'
execute '=diff -I reslim -bw sharedGNEPFlip2.gms.want sharedGNEPFlip2.gms.got'
abort$errorlevel 'Files sharedGNEPFlip2.gms.want and sharedGNEPFlip2.gms.got differ';

$onEcho > sharedGNEPFlip2_dict
* written by GAMS/JAMS at 12/06/16 13:44:14

********************************************************************************
 Contents
 1 Content of EMP Information File
 2 Processed EMP Information
 3 Dictionary
  3.1 Constraints
  3.2 Variables
********************************************************************************


 1 Content of EMP Information File
 ---------------------------------

     1:equilibrium
     2:min obj('i1') x('i1','b1') x('i1','b2') defobjFlip2('i1') -defyardlimit
     3:max obj('i2') x('i2','b1') x('i2','b2') defobjFlip2('i2') -defyardlimit
     4:max obj('i3') x('i3','b1') x('i3','b2') defobjFlip2('i3') defyardlimit


 2 Processed EMP Information
 ---------------------------

   Number of constraints to be flipped = 2
   * e4_4 will be flipped internally
   * e4_5 will be flipped internally


 3 Dictionary
 ------------
  3.1 Constraints
  ---------------

      {reformulated}  defobjFlip2(i1)
      {reformulated}  defobjFlip2(i2)
      {reformulated}  defobjFlip2(i3)
                e4_4  defyardlimit
                e4_5  defyardlimit
                e4_6  defyardlimit
              dL_dx1  {new}
              dL_dx2  {new}
              dL_dx3  {new}
              dL_dx4  {new}
              dL_dx5  {new}
              dL_dx6  {new}


  3.2 Variables
 --------------

                  x1  x(i1,b1)
                  x2  x(i1,b2)
                  x3  x(i2,b1)
                  x4  x(i2,b2)
                  x5  x(i3,b1)
                  x6  x(i3,b2)
      {reformulated}  obj(i1)
      {reformulated}  obj(i2)
      {reformulated}  obj(i3)
                u4_4  {new}
                u4_5  {new}
                u4_6  {new}
$offEcho

execute 'grep -v " written by GAMS" sharedGNEPFlip2_dict > sharedGNEPFlip2dict.txt.want'
execute 'grep -v " written by GAMS" sharedGNEPFlip2Dict.txt > sharedGNEPFlip2dict.txt.got'
execute '=diff -bw sharedGNEPFlip2dict.txt.want sharedGNEPFlip2dict.txt.got'
abort$errorlevel 'Files sharedGNEPFlip2dict.txt.want and sharedGNEPFlip2dict.txt.got differ';

* testing VI constraint with flipping:
* The second agent flips defyardlimit and its objective function now
* is a maximization. Hence, it should have a consistent sign.

put info 'equilibrium' /;
put 'visol', defyardlimit /;
loop(i,
    if (ord(i) ne 2,
        put 'min', obj(i);
        loop(b, put x(i,b););
        put defobjFlip1(i), defyardlimit /;
    else
        put 'max', obj(i);
        loop(b, put x(i,b););
        put defobjFlip1(i), '-defyardlimit' /;
    );
);
putclose;

putclose opt
   'Dict     sharedVIFlip1Dict.txt' /
   'FileName sharedVIFlip1.gms'     /
   'SharedEqu';

solve mf1 using emp;

* Now the second agent computed the maximum.
* Thus we need to change the sign of the objective value.
obj.l('i2') = -obj.l('i2');

abort$[mf1.solvestat <> %solveStat.normalCompletion%]
     'wrong mf1.solvestat', mf1.solvestat;
abort$[mf1.modelstat <> %modelStat.locallyOptimal%]
     'wrong mf1.modelstat', mf1.modelstat;
abort$[ abs(obj.l('i1')+22710) > tol ]    'bad obj.l("i1")', obj.l;
abort$[ abs(obj.l('i2')+14960) > tol ]    'bad obj.l("i2")', obj.l;
abort$[ abs(obj.l('i3')+12960) > tol ]    'bad obj.l("i3")', obj.l;
abort$[ abs(x.l('i1','b1')-21000) > tol ] 'bad x.l("i1","b1")', x.l;
abort$[ abs(x.l('i1','b2')- 4000) > tol ] 'bad x.l("i1","b2")', x.l;
abort$[ abs(x.l('i2','b1')-17000) > tol ] 'bad x.l("i2","b1")', x.l;
abort$[ abs(x.l('i2','b2')-    0) > tol ] 'bad x.l("i2","b2")', x.l;
abort$[ abs(x.l('i3','b1')-18000) > tol ] 'bad x.l("i3","b1")', x.l;
abort$[ abs(x.l('i3','b2')-    0) > tol ] 'bad x.l("i3","b2")', x.l;
abort$[ abs(defyardlimit.m+.273) > tol ] 'bad defyardlimit.m', defyardlimit.m;

obj.l(i) = 0;
x.l(i,b) = 0;
defobjFlip1.m(i) = 0;
defobjFlip2.m(i) = 0;
defyardlimit.m = 0;

$onEcho > sharedVIFlip1_gms
***********************************************
* written by GAMS/JAMS at 12/06/16 11:23:09
* for more information use JAMS option "Dict"
***********************************************

Variables  x1,x2,x3,x4,x5,x6,u4;

Negative Variables  u4;

Positive Variables  x1,x2,x3,x4,x5,x6;

Equations  e4,dL_dx1,dL_dx2,dL_dx3,dL_dx4,dL_dx5,dL_dx6;


e4..    x1 + x2 + x3 + x4 + x5 + x6 =L= 60000;

dL_dx1.. (-(0.55 - (2.5 - 1.66666667e-5*x1 - 1.66666667e-5*x2 - 1.66666667e-5*
         x3 - 1.66666667e-5*x4 - 1.66666667e-5*x5 - 1.66666667e-5*x6 - 
         1.66666667e-5*x1) + 1.66666667e-5*x2))/(-1) - u4 =N= 0;

dL_dx2.. (-(1.66666667e-5*x1 + 0.81 - (2.5 - 1.66666667e-5*x1 - 1.66666667e-5*
         x2 - 1.66666667e-5*x3 - 1.66666667e-5*x4 - 1.66666667e-5*x5 - 
         1.66666667e-5*x6 - 1.66666667e-5*x2)))/(-1) - u4 =N= 0;

dL_dx3.. (-(0.62 - (2.5 - 1.66666667e-5*x1 - 1.66666667e-5*x2 - 1.66666667e-5*
         x3 - 1.66666667e-5*x4 - 1.66666667e-5*x5 - 1.66666667e-5*x6 - 
         1.66666667e-5*x3) + 1.66666667e-5*x4))/(-1) - u4 =N= 0;

dL_dx4.. (-(1.66666667e-5*x3 + 1.25 - (2.5 - 1.66666667e-5*x1 - 1.66666667e-5*
         x2 - 1.66666667e-5*x3 - 1.66666667e-5*x4 - 1.66666667e-5*x5 - 
         1.66666667e-5*x6 - 1.66666667e-5*x4)))/(-1) - u4 =N= 0;

dL_dx5.. (-(0.78 - (2.5 - 1.66666667e-5*x1 - 1.66666667e-5*x2 - 1.66666667e-5*
         x3 - 1.66666667e-5*x4 - 1.66666667e-5*x5 - 1.66666667e-5*x6 - 
         1.66666667e-5*x5) + 1.66666667e-5*x6))/(-1) - u4 =N= 0;

dL_dx6.. (-(1.66666667e-5*x5 + 1.35 - (2.5 - 1.66666667e-5*x1 - 1.66666667e-5*
         x2 - 1.66666667e-5*x3 - 1.66666667e-5*x4 - 1.66666667e-5*x5 - 
         1.66666667e-5*x6 - 1.66666667e-5*x6)))/(-1) - u4 =N= 0;

* set non-default bounds
x1.up = 21000;
x2.up = 16000;
x3.up = 17000;
x4.up = 22000;
x5.up = 18000;
x6.up = 14000;

Model m / e4.u4,dL_dx1.x1,dL_dx2.x2,dL_dx3.x3,dL_dx4.x4,dL_dx5.x5,dL_dx6.x6 /;

m.limrow=0; m.limcol=0;

Solve m using MCP;
$offEcho

execute 'grep -v " written by GAMS" sharedVIFlip1_gms > sharedVIFlip1.gms.want'
execute 'grep -v " written by GAMS" sharedVIFlip1.gms > sharedVIFlip1.gms.got'
execute '=diff -I reslim -bw sharedVIFlip1.gms.want sharedVIFlip1.gms.got'
abort$errorlevel 'Files sharedVIFlip1.gms.want and sharedVIFlip1.gms.got differ';

$onEcho > sharedVIFlip1_dict
* written by GAMS/JAMS at 12/06/16 11:23:09

********************************************************************************
 Contents
 1 Content of EMP Information File
 2 Processed EMP Information
 3 Dictionary
  3.1 Constraints
  3.2 Variables
********************************************************************************


 1 Content of EMP Information File
 ---------------------------------

     1:equilibrium
     2:visol defyardlimit
     3:min obj('i1') x('i1','b1') x('i1','b2') defobjFlip1('i1') defyardlimit
     4:max obj('i2') x('i2','b1') x('i2','b2') defobjFlip1('i2') -defyardlimit
     5:min obj('i3') x('i3','b1') x('i3','b2') defobjFlip1('i3') defyardlimit


 2 Processed EMP Information
 ---------------------------


 3 Dictionary
 ------------
  3.1 Constraints
  ---------------

      {reformulated}  defobjFlip1(i1)
      {reformulated}  defobjFlip1(i2)
      {reformulated}  defobjFlip1(i3)
                  e4  defyardlimit
              dL_dx1  {new}
              dL_dx2  {new}
              dL_dx3  {new}
              dL_dx4  {new}
              dL_dx5  {new}
              dL_dx6  {new}


  3.2 Variables
 --------------

                  x1  x(i1,b1)
                  x2  x(i1,b2)
                  x3  x(i2,b1)
                  x4  x(i2,b2)
                  x5  x(i3,b1)
                  x6  x(i3,b2)
      {reformulated}  obj(i1)
      {reformulated}  obj(i2)
      {reformulated}  obj(i3)
                  u4  {new}
$offEcho

execute 'grep -v " written by GAMS" sharedVIFlip1_dict > sharedVIFlip1dict.txt.want'
execute 'grep -v " written by GAMS" sharedVIFlip1Dict.txt > sharedVIFlip1dict.txt.got'
execute '=diff -bw sharedVIFlip1dict.txt.want sharedVIFlip1dict.txt.got'
abort$errorlevel 'Files sharedVIFlip1dict.txt.want and sharedVIFlip1dict.txt.got differ';

* another testing VI constraint with flipping:
* The first agent flips defyardlimit. Then the multiplier associated with
* defyardlimit has a positive sign in the scalar model.
* By changing the objective function directions of other agents,
* we should have a consistent sign.

put info 'equilibrium' /;
put 'visol', defyardlimit /;
loop(i,
    if (ord(i) eq 1,
        put 'min', obj(i);
        loop(b, put x(i,b););
        put defobjFlip2(i), '-defyardlimit' /;
    else
        put 'max', obj(i);
        loop(b, put x(i,b););
        put defobjFlip2(i), defyardlimit /;
    );
);
putclose;

putclose opt
   'Dict     sharedVIFlip2Dict.txt' /
   'FileName sharedVIFlip2.gms'     /
   'SharedEqu';

mf2.optfile = 1;
solve mf2 using emp;

obj.l(i)$(ord(i) ne 1) = -obj.l(i);

abort$[mf2.solvestat <> %solveStat.normalCompletion%]
     'wrong mf2.solvestat', mf2.solvestat;
abort$[mf2.modelstat <> %modelStat.locallyOptimal%]
     'wrong mf2.modelstat', mf2.modelstat;
abort$[ abs(obj.l('i1')+22710) > tol ]    'bad obj.l("i1")', obj.l;
abort$[ abs(obj.l('i2')+14960) > tol ]    'bad obj.l("i2")', obj.l;
abort$[ abs(obj.l('i3')+12960) > tol ]    'bad obj.l("i3")', obj.l;
abort$[ abs(x.l('i1','b1')-21000) > tol ] 'bad x.l("i1","b1")', x.l;
abort$[ abs(x.l('i1','b2')- 4000) > tol ] 'bad x.l("i1","b2")', x.l;
abort$[ abs(x.l('i2','b1')-17000) > tol ] 'bad x.l("i2","b1")', x.l;
abort$[ abs(x.l('i2','b2')-    0) > tol ] 'bad x.l("i2","b2")', x.l;
abort$[ abs(x.l('i3','b1')-18000) > tol ] 'bad x.l("i3","b1")', x.l;
abort$[ abs(x.l('i3','b2')-    0) > tol ] 'bad x.l("i3","b2")', x.l;
abort$[ abs(defyardlimit.m+.273) > tol ] 'bad defyardlimit.m', defyardlimit.m;

obj.l(i) = 0;
x.l(i,b) = 0;
defobjFlip1.m(i) = 0;
defobjFlip2.m(i) = 0;
defyardlimit.m = 0;

$onEcho > sharedVIFlip2_gms
***********************************************
* written by GAMS/JAMS at 12/06/16 11:27:08
* for more information use JAMS option "Dict"
***********************************************

Variables  x1,x2,x3,x4,x5,x6,u4;

Positive Variables  x1,x2,x3,x4,x5,x6;

Positive Variables  u4;

Equations  e4,dL_dx1,dL_dx2,dL_dx3,dL_dx4,dL_dx5,dL_dx6;


e4.. 60000 =G=    x1 + x2 + x3 + x4 + x5 + x6;

dL_dx1.. (-(0.55 - (2.5 - 1.66666667e-5*x1 - 1.66666667e-5*x2 - 1.66666667e-5*
         x3 - 1.66666667e-5*x4 - 1.66666667e-5*x5 - 1.66666667e-5*x6 - 
         1.66666667e-5*x1) + 1.66666667e-5*x2))/(-1) + u4 =N= 0;

dL_dx2.. (-(1.66666667e-5*x1 + 0.81 - (2.5 - 1.66666667e-5*x1 - 1.66666667e-5*
         x2 - 1.66666667e-5*x3 - 1.66666667e-5*x4 - 1.66666667e-5*x5 - 
         1.66666667e-5*x6 - 1.66666667e-5*x2)))/(-1) + u4 =N= 0;

dL_dx3.. (-(0.62 - (2.5 - 1.66666667e-5*x1 - 1.66666667e-5*x2 - 1.66666667e-5*
         x3 - 1.66666667e-5*x4 - 1.66666667e-5*x5 - 1.66666667e-5*x6 - 
         1.66666667e-5*x3) + 1.66666667e-5*x4))/(-1) + u4 =N= 0;

dL_dx4.. (-(1.66666667e-5*x3 + 1.25 - (2.5 - 1.66666667e-5*x1 - 1.66666667e-5*
         x2 - 1.66666667e-5*x3 - 1.66666667e-5*x4 - 1.66666667e-5*x5 - 
         1.66666667e-5*x6 - 1.66666667e-5*x4)))/(-1) + u4 =N= 0;

dL_dx5.. (-(0.78 - (2.5 - 1.66666667e-5*x1 - 1.66666667e-5*x2 - 1.66666667e-5*
         x3 - 1.66666667e-5*x4 - 1.66666667e-5*x5 - 1.66666667e-5*x6 - 
         1.66666667e-5*x5) + 1.66666667e-5*x6))/(-1) + u4 =N= 0;

dL_dx6.. (-(1.66666667e-5*x5 + 1.35 - (2.5 - 1.66666667e-5*x1 - 1.66666667e-5*
         x2 - 1.66666667e-5*x3 - 1.66666667e-5*x4 - 1.66666667e-5*x5 - 
         1.66666667e-5*x6 - 1.66666667e-5*x6)))/(-1) + u4 =N= 0;

* set non-default bounds
x1.up = 21000;
x2.up = 16000;
x3.up = 17000;
x4.up = 22000;
x5.up = 18000;
x6.up = 14000;

Model m / e4.u4,dL_dx1.x1,dL_dx2.x2,dL_dx3.x3,dL_dx4.x4,dL_dx5.x5,dL_dx6.x6 /;

m.limrow=0; m.limcol=0;

Solve m using MCP;
$offEcho

execute 'grep -v " written by GAMS" sharedVIFlip2_gms > sharedVIFlip2.gms.want'
execute 'grep -v " written by GAMS" sharedVIFlip2.gms > sharedVIFlip2.gms.got'
execute '=diff -I reslim -bw sharedVIFlip2.gms.want sharedVIFlip2.gms.got'
abort$errorlevel 'Files sharedVIFlip2.gms.want and sharedVIFlip2.gms.got differ';

$onEcho > sharedVIFlip2_dict
* written by GAMS/JAMS at 12/06/16 11:27:08

********************************************************************************
 Contents
 1 Content of EMP Information File
 2 Processed EMP Information
 3 Dictionary
  3.1 Constraints
  3.2 Variables
********************************************************************************


 1 Content of EMP Information File
 ---------------------------------

     1:equilibrium
     2:visol defyardlimit
     3:min obj('i1') x('i1','b1') x('i1','b2') defobjFlip2('i1') -defyardlimit
     4:max obj('i2') x('i2','b1') x('i2','b2') defobjFlip2('i2') defyardlimit
     5:max obj('i3') x('i3','b1') x('i3','b2') defobjFlip2('i3') defyardlimit


 2 Processed EMP Information
 ---------------------------

   Number of constraints to be flipped = 1
   * e4 will be flipped internally


 3 Dictionary
 ------------
  3.1 Constraints
  ---------------

      {reformulated}  defobjFlip2(i1)
      {reformulated}  defobjFlip2(i2)
      {reformulated}  defobjFlip2(i3)
                  e4  defyardlimit
              dL_dx1  {new}
              dL_dx2  {new}
              dL_dx3  {new}
              dL_dx4  {new}
              dL_dx5  {new}
              dL_dx6  {new}


  3.2 Variables
 --------------

                  x1  x(i1,b1)
                  x2  x(i1,b2)
                  x3  x(i2,b1)
                  x4  x(i2,b2)
                  x5  x(i3,b1)
                  x6  x(i3,b2)
      {reformulated}  obj(i1)
      {reformulated}  obj(i2)
      {reformulated}  obj(i3)
                  u4  {new}
$offEcho

execute 'grep -v " written by GAMS" sharedVIFlip2_dict > sharedVIFlip2dict.txt.want'
execute 'grep -v " written by GAMS" sharedVIFlip2Dict.txt > sharedVIFlip2dict.txt.got'
execute '=diff -bw sharedVIFlip2dict.txt.want sharedVIFlip2dict.txt.got'
abort$errorlevel 'Files sharedVIFlip2dict.txt.want and sharedVIFlip2dict.txt.got differ';