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

Description

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

Contributors: Youngdae Kim & Steve Dirkse, Dec 2016


Small Model of Type : GAMS


Category : GAMS Test library


Main file : emp19.gms

$TITLE 'Test JAMS/EMP on implicit variable handling' (EMP19,SEQ=701)

$ontext
Test JAMS behavior on a small equilibrium model with implicit variables.

Contributors: Youngdae Kim & Steve Dirkse, Dec 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=
    (sqr(zExp(i,i)) - 0.5*t(i) + 0.25)$sameas(i,'1') +
    (sqr(zExp(i,i)) - 2*t(i) + 1)$sameas(i,'2');

defFExp(i,j)..
    (2*zExp('1',j) + zExp('2',j) + t('1') - t('2'))$sameas(i,'1') +
    (-2*zExp('1',j) + 3*zExp('2',j) - t('1') - 3*t('2'))$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')-0) > tol ]            'bad obj.l("1")', obj.l;
abort$[ abs(obj.l('2')-0) > tol ]            'bad obj.l("2")', obj.l;
abort$[smax{j, abs(zExp.l('1',j)+.5)} > tol] 'bad zExp.l("1",j)', zExp.l;
abort$[smax{j, abs(zExp.l('2',j)-1)} > tol]  'bad zExp.l("2",j)', zExp.l;
abort$[ abs(t.l('1')-1) > tol ]              'bad t.l("1")', t.l;
abort$[ abs(t.l('2')-1) > 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 12/03/16 20:43:34
* 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..    2*x3 + x5 + x7 - x8 =E= 0;

e4..    2*x4 + x6 + x7 - x8 =E= 0;

e5..  - 2*x3 + 3*x5 - x7 - 3*x8 =E= 0;

e6..  - 2*x4 + 3*x6 - x7 - 3*x8 =E= 0;

dL_dx3.. (-2*x3)/(-1) - 2*u3 + 2*u5 =E= 0;

dL_dx4..  - 2*u4 + 2*u6 + eps*x4 =E= 0;

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

dL_dx6.. (-2*x6)/(-1) - u4 - 3*u6 =E= 0;

dL_dx7.. -0.5 - u3 + u5 + eps*x7 =G= 0;

dL_dx8.. -2 + u4 + 3*u6 + eps*x8 =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 20:43: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=
    (sqr(z(i)) - 0.5*t(i) + 0.25)$sameas(i,'1') +
    (sqr(z(i)) - 2*t(i) + 1)$sameas(i,'2');

defF(i)..
    (2*z('1') + z('2') + t('1') - t('2'))$sameas(i,'1') +
    (-2*z('1') + 3*z('2') - t('1') - 3*t('2'))$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;

* testing with implicit
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')-0) > tol ] 'bad obj.l("1")', obj.l;
abort$[ abs(obj.l('2')-0) > tol ] 'bad obj.l("2")', obj.l;
abort$[ abs(z.l('1')+.5) > tol]   'bad z.l("1")', z.l;
abort$[ abs(z.l('2')-1) > tol]    'bad z.l("2")', z.l;
abort$[ abs(t.l('1')-1) > tol ]   'bad t.l("1")', t.l;
abort$[ abs(t.l('2')-1) > 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 12/03/16 20:47:56
* 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..    2*x3 + x4 + x5 - x6 =E= 0;

e4..  - 2*x3 + 3*x4 - x5 - 3*x6 =E= 0;

* equations below are for newly defined implicit variables

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

IMPL_de3_dx6.. (2)*(dx3_dx6) + (1)*(dx4_dx6) + (-1) =E= 0;

IMPL_de4_dx5.. (-2)*(dx3_dx5) + (3)*(dx4_dx5) + (-1) =E= 0;

IMPL_de4_dx6.. (-2)*(dx3_dx6) + (3)*(dx4_dx6) + (-3) =E= 0;

dL_dx5.. -0.5 + ((-2*x3)/(-1))*(dx3_dx5) + eps*x5 =G= 0;

dL_dx6.. -2 + ((-2*x4)/(-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 20:47: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: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 adjoint

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')-0) > tol ] 'bad obj.l("1")', obj.l;
abort$[ abs(obj.l('2')-0) > tol ] 'bad obj.l("2")', obj.l;
abort$[ abs(z.l('1')+.5) > tol]   'bad z.l("1")', z.l;
abort$[ abs(z.l('2')-1) > tol]    'bad z.l("2")', z.l;
abort$[ abs(t.l('1')-1) > tol ]   'bad t.l("1")', t.l;
abort$[ abs(t.l('2')-1) > 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..    2*x3 + x4 + x5 - x6 =E= 0;

e4..  - 2*x3 + 3*x4 - x5 - 3*x6 =E= 0;

dL_dx3_4.. (-2*x3)/(-1) - 2*u3_4 + 2*u4_4 =E= 0;

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

dL_dx5.. -0.5 - u3_4 + u4_4 + eps*x5 =G= 0;

dL_dx6.. -2 + u3_5 + 3*u4_5 + eps*x6 =G= 0;

dL_dx3_5..  - 2*u3_5 + 2*u4_5 + eps*x3 =E= 0;

dL_dx4_5.. (-2*x4)/(-1) - u3_5 - 3*u4_5 =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')-0) > tol ] 'bad obj.l("1")', obj.l;
abort$[ abs(obj.l('2')-0) > tol ] 'bad obj.l("2")', obj.l;
abort$[ abs(z.l('1')+.5) > tol]   'bad z.l("1")', z.l;
abort$[ abs(z.l('2')-1) > tol]    'bad z.l("2")', z.l;
abort$[ abs(t.l('1')-1) > tol ]   'bad t.l("1")', t.l;
abort$[ abs(t.l('2')-1) > 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 > implRep_gms
***********************************************
* written by GAMS/JAMS at 11/17/17 15:27: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..    2*x3_4 + x4_4 + x5 - x6 =E= 0;

e4_4..  - 2*x3_4 + 3*x4_4 - x5 - 3*x6 =E= 0;

e3_5..    2*x3_5 + x4_5 + x5 - x6 =E= 0;

e4_5..  - 2*x3_5 + 3*x4_5 - x5 - 3*x6 =E= 0;

dL_dx3_4.. (-2*x3_4)/(-1) - 2*u3_4 + 2*u4_4 =E= 0;

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

dL_dx5.. -0.5 - u3_4 + u4_4 + eps*x5 =G= 0;

dL_dx6.. -2 + u3_5 + 3*u4_5 + eps*x6 =G= 0;

dL_dx3_5..  - 2*u3_5 + 2*u4_5 + eps*x3_5 =E= 0;

dL_dx4_5.. (-2*x4_5)/(-1) - u3_5 - 3*u4_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:27:40

********************************************************************************
 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';