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

Description

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

Contributors: Youngdae Kim & Steve Dirkse, June 2016


Small Model of Type : GAMS


Category : GAMS Test library


Main file : emp18.gms

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

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

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

set i agents  / 1*2 /;
alias (i,j);
* j indicates who owns the copy of the var or equation

* The scalar files generated depend on the variable declaration order,
* so do not change the declarations lightly
variables
  obj(i)
  zExp(i,j) 'copy of z_i owned by agent j'
  z(i)      'z_i done with implicit variables'
  ;
positive variables t(i);

equations
  defFExp(i,j)    'copy of F_i for agent j'
  defF(i)         'F_i done with implicit variables'
  defobjExp(i)
  defobj(i)
  ;
defobjExp(i)..
    obj(i) =E= zExp(i,i) + t(i);

defFExp(i,j)..
    (zExp('1',j) - sqr(t('1')-1))$sameas(i,'1') +
    (- exp(zExp('1',j)-t('2')) + zExp('2',j) + 1)$sameas(i,'2') =E= 0;

model me 'explicit formulation' / defobjExp, defFExp /;

put info 'equilibrium' ;
loop(i,
    put / 'min', obj(i), t(i);
    loop(j, put zExp(j,i););

    put / defobjExp(i);
    loop(j, put defFExp(j,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('1')-.75) > tol ]           'bad obj.l("1")', obj.l;
abort$[ abs(obj.l('2')-.25) > tol ]           'bad obj.l("2")', obj.l;
abort$[smax{j, abs(zExp.l('1',j)-.25)} > tol] 'bad zExp.l("1",j)', zExp.l;
abort$[smax{j, abs(zExp.l('2',j)-  0)} > tol] 'bad zExp.l("2",j)', zExp.l;
abort$[ abs(t.l('1')-.50) > tol ]             'bad t.l("1")', t.l;
abort$[ abs(t.l('2')-.25) > tol ]             'bad t.l("2")', t.l;
obj.l(i) = 0;
zExp.l(i,j) = 0;
t.l(i) = 0;

$onEcho > expl_gms
***********************************************
* written by GAMS/JAMS at 11/21/16 19:52:06
* for more information use JAMS option "Dict"
***********************************************

Variables  x3,x4,x5,x6,x7,x8,u3,u4,u5,u6;

Positive Variables  x7,x8;

Equations  e3,e4,e5,e6,dL_dx3,dL_dx4,dL_dx5,dL_dx6,dL_dx7,dL_dx8;


e3.. -sqr((-1) + x7) + x3 =E= 0;

e4.. -sqr((-1) + x7) + x4 =E= 0;

e5.. -exp(x3 - x8) + x5 =E= -1;

e6.. -exp(x4 - x8) + x6 =E= -1;

dL_dx3.. 1 - u3 - (-exp(x3 - x8))*u5 =E= 0;

dL_dx4..  - u4 - (-exp(x4 - x8))*u6 =E= 0;

dL_dx5..  - u5 + eps*x5 =E= 0;

dL_dx6.. 1 - u6 + eps*x6 =E= 0;

dL_dx7.. 1 - (-2*(-1 + x7))*u3 =G= 0;

dL_dx8.. 1 - (exp(x4 - x8))*u6 =G= 0;

Model m / e3.u3,e4.u4,e5.u5,e6.u6,dL_dx3.x3,dL_dx4.x4,dL_dx5.x5,dL_dx6.x6
         ,dL_dx7.x7,dL_dx8.x8 /;

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 12:35:34

********************************************************************************
 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('1') t('1') zExp('1','1') zExp('2','1')
     3:defobjExp('1') defFExp('1','1') defFExp('2','1')
     4:min obj('2') t('2') zExp('1','2') zExp('2','2')
     5:defobjExp('2') defFExp('1','2') defFExp('2','2')


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


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

      {reformulated}  defobjExp(1)
      {reformulated}  defobjExp(2)
                  e3  defFExp(1,1)
                  e4  defFExp(1,2)
                  e5  defFExp(2,1)
                  e6  defFExp(2,2)
              dL_dx3  {new}
              dL_dx4  {new}
              dL_dx5  {new}
              dL_dx6  {new}
              dL_dx7  {new}
              dL_dx8  {new}


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

      {reformulated}  obj(1)
      {reformulated}  obj(2)
                  x3  zExp(1,1)
                  x4  zExp(1,2)
                  x5  zExp(2,1)
                  x6  zExp(2,2)
                  x7  t(1)
                  x8  t(2)
                  u3  {new}
                  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';

defobj(i)..
    obj(i) =E= z(i) + t(i);

defF(i)..
    (z('1') - sqr(t('1')-1))$sameas(i,'1') +
    (- exp(z('1')-t('2')) + z('2') + 1)$sameas(i,'2') =E= 0;

model mi 'implicit formulation' / defobj, defF /;

put info 'equilibrium' /;
put 'implicit z defF' /;
loop(i,
    put 'min', obj(i), t(i), 'z', defobj(i) /;
);
putclose;
putclose opt
  'Dict         implDict.txt' /
  'FileName     impl.gms'     /
  'ImplVarModel substitution' ;

mi.optfile = 1;
solve mi using emp;
abort$[mi.solvestat <> %solveStat.normalCompletion%]
   'wrong mi.solvestat', mi.solvestat;
abort$[mi.modelstat  > %modelStat.locallyOptimal%]
   'wrong mi.modelstat', mi.modelstat;
abort$[ abs(obj.l('1')-.75) > tol ]   'bad obj.l("1")', obj.l;
abort$[ abs(obj.l('2')-.25) > tol ]   'bad obj.l("2")', obj.l;
abort$[ abs(z.l('1')-.25) > tol]      'bad z.l("1")', z.l;
abort$[ abs(z.l('2')-  0) > tol]      'bad z.l("2")', z.l;
abort$[ abs(t.l('1')-.50) > tol ]     'bad t.l("1")', t.l;
abort$[ abs(t.l('2')-.25) > tol ]     'bad t.l("2")', t.l;
obj.l(i) = 0;
z.l(i) = 0;
t.l(i) = 0;
defobj.m(i) = 0;
defF.m(i) = 0;

$onEchoV > impl_gms
***********************************************
* written by GAMS/JAMS at 08/01/16 10:47:43
* for more information use JAMS option "Dict"
***********************************************

Variables  x3,x4,x5,x6,dx3_dx5,dx3_dx6,dx4_dx5,dx4_dx6;

Positive Variables  x5,x6;

Equations  e3,e4,IMPL_de3_dx5,IMPL_de3_dx6,IMPL_de4_dx5,IMPL_de4_dx6,dL_dx5
          ,dL_dx6;


e3.. -sqr((-1) + x5) + x3 =E= 0;

e4.. -exp(x3 - x6) + x4 =E= -1;

* equations below are for newly defined implicit variables

IMPL_de3_dx5.. (1)*(dx3_dx5) + (-2*(-1 + x5)) =E= 0;

IMPL_de3_dx6.. (1)*(dx3_dx6) =E= 0;

IMPL_de4_dx5.. (-exp(x3 - x6))*(dx3_dx5) + (1)*(dx4_dx5) =E= 0;

IMPL_de4_dx6.. (-exp(x3 - x6))*(dx3_dx6) + (1)*(dx4_dx6) + (exp(x3 - x6))
                =E= 0;

dL_dx5.. 1 + (1)*(dx3_dx5) + eps*x5 =G= 0;

dL_dx6.. 1 + (1)*(dx4_dx6) + eps*x6 =G= 0;

Model m / e3.x3,e4.x4,IMPL_de3_dx5.dx3_dx5,IMPL_de3_dx6.dx3_dx6
         ,IMPL_de4_dx5.dx4_dx5,IMPL_de4_dx6.dx4_dx6,dL_dx5.x5,dL_dx6.x6 /;

File implvarfile / '%gams.scrdir%implvar.dat' /;
Put implvarfile;

$onPutV
x3
x4
dx3_dx5
dx3_dx6
dx4_dx5
dx4_dx6
$offPut

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

Solve m using MCP;
$offEcho

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

$onEcho > impl_dict
* written by GAMS/JAMS at 12/03/16 12:40:04

********************************************************************************
 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:implicit z defF
     3:min obj('1') t('1') z defobj('1')
     4:min obj('2') t('2') z defobj('2')


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


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

      {reformulated}  defobj(1)
      {reformulated}  defobj(2)
                  e3  defF(1)
                  e4  defF(2)
        IMPL_de3_dx5  {new}
        IMPL_de3_dx6  {new}
        IMPL_de4_dx5  {new}
        IMPL_de4_dx6  {new}
              dL_dx5  {new}
              dL_dx6  {new}


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

      {reformulated}  obj(1)
      {reformulated}  obj(2)
                  x3  z(1)
                  x4  z(2)
                  x5  t(1)
                  x6  t(2)
             dx3_dx5  {new}
             dx3_dx6  {new}
             dx4_dx5  {new}
             dx4_dx6  {new}
$offEcho

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

* testing with the adjoint method
putclose opt
  'Dict         implSwitchingDict.txt' /
  'FileName     implSwitching.gms'     /
  'ImplVarModel switching'           ;

solve mi using emp;
abort$[mi.solvestat <> %solveStat.normalCompletion%]
   'wrong mi.solvestat', mi.solvestat;
abort$[mi.modelstat  > %modelStat.locallyOptimal%]
   'wrong mi.modelstat', mi.modelstat;
abort$[ abs(obj.l('1')-.75) > tol ]   'bad obj.l("1")', obj.l;
abort$[ abs(obj.l('2')-.25) > tol ]   'bad obj.l("2")', obj.l;
abort$[ abs(z.l('1')-.25) > tol]      'bad z.l("1")', z.l;
abort$[ abs(z.l('2')-  0) > tol]      'bad z.l("2")', z.l;
abort$[ abs(t.l('1')-.50) > tol ]     'bad t.l("1")', t.l;
abort$[ abs(t.l('2')-.25) > tol ]     'bad t.l("2")', t.l;
obj.l(i) = 0;
z.l(i) = 0;
t.l(i) = 0;
defobj.m(i) = 0;
defF.m(i) = 0;

$onEchoV > implSwitching_gms
***********************************************
* for more information use JAMS option "Dict"
***********************************************

Variables  x3,x4,x5,x6,u3_4,u4_4,u3_5,u4_5;

Positive Variables  x5,x6;

Equations  e3,e4,dL_dx3_4,dL_dx4_4,dL_dx5,dL_dx6,dL_dx3_5,dL_dx4_5;


e3.. -sqr((-1) + x5) + x3 =E= 0;

e4.. -exp(x3 - x6) + x4 =E= -1;

dL_dx3_4.. 1 - u3_4 - (-exp(x3 - x6))*u4_4 =E= 0;

dL_dx4_4..  - u4_4 + eps*x4 =E= 0;

dL_dx5.. 1 - (-2*(-1 + x5))*u3_4 =G= 0;

dL_dx6.. 1 - (exp(x3 - x6))*u4_5 =G= 0;

dL_dx3_5..  - u3_5 - (-exp(x3 - x6))*u4_5 =E= 0;

dL_dx4_5.. 1 - u4_5 + eps*x4 =E= 0;

Model m / e3.x3,e4.x4,dL_dx3_4.u3_4,dL_dx4_4.u4_4,dL_dx5.x5,dL_dx6.x6
         ,dL_dx3_5.u3_5,dL_dx4_5.u4_5 /;

File implvarfile / '%gams.scrdir%implvar.dat' /;
Put implvarfile;

$onPutV
x3
x4
u3_4
u3_5
u4_4
u4_5
$offPut

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

Solve m using MCP;
$offEcho

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

$onEcho > implSwitching_dict

********************************************************************************
 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:implicit z defF
     3:min obj('1') t('1') z defobj('1')
     4:min obj('2') t('2') z defobj('2')


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


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

      {reformulated}  defobj(1)
      {reformulated}  defobj(2)
                  e3  defF(1)
                  e4  defF(2)
            dL_dx3_4  {new}
            dL_dx4_4  {new}
              dL_dx5  {new}
              dL_dx6  {new}
            dL_dx3_5  {new}
            dL_dx4_5  {new}


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

      {reformulated}  obj(1)
      {reformulated}  obj(2)
                  x3  z(1)
                  x4  z(2)
                  x5  t(1)
                  x6  t(2)
                u3_4  {new}
                u4_4  {new}
                u3_5  {new}
                u4_5  {new}
$offEcho

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

* testing with replication
putclose opt
  'Dict         implRepDict.txt' /
  'FileName     implRep.gms'     /
  'ImplVarModel replication'     ;

solve mi using emp;
abort$[mi.solvestat <> %solveStat.normalCompletion%]
   'wrong mi.solvestat', mi.solvestat;
abort$[mi.modelstat  > %modelStat.locallyOptimal%]
   'wrong mi.modelstat', mi.modelstat;
abort$[ abs(obj.l('1')-.75) > tol ]   'bad obj.l("1")', obj.l;
abort$[ abs(obj.l('2')-.25) > tol ]   'bad obj.l("2")', obj.l;
abort$[ abs(z.l('1')-.25) > tol]      'bad z.l("1")', z.l;
abort$[ abs(z.l('2')-  0) > tol]      'bad z.l("2")', z.l;
abort$[ abs(t.l('1')-.50) > tol ]     'bad t.l("1")', t.l;
abort$[ abs(t.l('2')-.25) > tol ]     'bad t.l("2")', t.l;

obj.l(i) = 0;
z.l(i) = 0;
t.l(i) = 0;
defobj.m(i) = 0;
defF.m(i) = 0;

* The variable ordering below is curious: why is x5,x6 in the middle??
$onEchoV > implRep_gms
***********************************************
* written by GAMS/JAMS at 11/17/17 15:24:06
* for more information use JAMS option "Dict"
***********************************************

Variables  x3_4,x4_4,x5,x6,x3_5,x4_5,u3_4,u4_4,u3_5,u4_5;

Positive Variables  x5,x6;

Equations  e3_4,e4_4,e3_5,e4_5,dL_dx3_4,dL_dx4_4,dL_dx5,dL_dx6,dL_dx3_5
          ,dL_dx4_5;


e3_4.. -sqr((-1) + x5) + x3_4 =E= 0;

e4_4.. -exp(x3_4 - x6) + x4_4 =E= -1;

e3_5.. -sqr((-1) + x5) + x3_5 =E= 0;

e4_5.. -exp(x3_5 - x6) + x4_5 =E= -1;

dL_dx3_4.. 1 - u3_4 - (-exp(x3_4 - x6))*u4_4 =E= 0;

dL_dx4_4..  - u4_4 + eps*x4_4 =E= 0;

dL_dx5.. 1 - (-2*(-1 + x5))*u3_4 =G= 0;

dL_dx6.. 1 - (exp(x3_5 - x6))*u4_5 =G= 0;

dL_dx3_5..  - u3_5 - (-exp(x3_5 - x6))*u4_5 =E= 0;

dL_dx4_5.. 1 - u4_5 + eps*x4_5 =E= 0;

Model m / e3_4.u3_4,e4_4.u4_4,e3_5.u3_5,e4_5.u4_5,dL_dx3_4.x3_4
         ,dL_dx4_4.x4_4,dL_dx5.x5,dL_dx6.x6,dL_dx3_5.x3_5,dL_dx4_5.x4_5 /;

File implvarfile / '%gams.scrdir%implvar.dat' /;
Put implvarfile;

$onPutV
x3_4
x3_5
x4_4
x4_5
u3_4
u3_5
u4_4
u4_5
$offPut

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

Solve m using MCP;
$offEcho

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

$onEcho > implRep_dict
* written by GAMS/JAMS at 11/17/17 15:26:23

********************************************************************************
 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:implicit z defF
     3:min obj('1') t('1') z defobj('1')
     4:min obj('2') t('2') z defobj('2')


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


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

      {reformulated}  defobj(1)
      {reformulated}  defobj(2)
                e3_4  defF(1)
                e4_4  defF(2)
                e3_5  defF(1)
                e4_5  defF(2)
            dL_dx3_4  {new}
            dL_dx4_4  {new}
              dL_dx5  {new}
              dL_dx6  {new}
            dL_dx3_5  {new}
            dL_dx4_5  {new}


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

      {reformulated}  obj(1)
      {reformulated}  obj(2)
                x3_4  z(1)
                x4_4  z(2)
                  x5  t(1)
                  x6  t(2)
                u3_4  {new}
                u4_4  {new}
                u3_5  {new}
                u4_5  {new}
                x3_5  z(1)
                x4_5  z(2)
$offEcho

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