emp20.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 : emp20.gms

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

$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 j                      agents / 1*3 /
    i(j)  variables and optagents / 1*2 /
    ;

alias(i,ii);

* i has dual purposes: variable index and optimization agent index
* 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'
  x         'VI agent variable'
  ;
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)
  defviExp
  defvi
  ;

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;

defviExp..
    x =E= sum(i, zExp(i,'3'));

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

put info 'equilibrium';
loop(i,
    put / 'min', obj(i), t(i);
    loop(j$(i.val eq j.val), loop(ii, put zExp(ii,j);););

    put / defobjExp(i);
    loop(j$(i.val eq j.val), loop(ii, put defFExp(ii,j);););
);
put / 'vi',  defviExp, x;
put / 'vi', loop(i, put defFexp(i,'3'), zExp(i,'3'););
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;
abort$[ abs(x.l-.5) > tol ]                  'bad x.l', x.l;

obj.l(i) = 0;
zExp.l(i,j) = 0;
t.l(i) = 0;
x.l = 0;

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

Variables  x3,x4,x5,x6,x7,x8,x9,x10,x11,u3,u4,u6,u7;

Positive Variables  x10,x11;

Equations  e3,e4,e5,e6,e7,e8,e9,dL_dx3,dL_dx4,dL_dx6,dL_dx7,dL_dx10,dL_dx11;


e3..    2*x3 + x6 + x10 - x11 =E= 0;

e4..    2*x4 + x7 + x10 - x11 =E= 0;

e5..    2*x5 + x8 + x10 - x11 =E= 0;

e6..  - 2*x3 + 3*x6 - x10 - 3*x11 =E= 0;

e7..  - 2*x4 + 3*x7 - x10 - 3*x11 =E= 0;

e8..  - 2*x5 + 3*x8 - x10 - 3*x11 =E= 0;

e9..  - x5 - x8 + x9 =E= 0;

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

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

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

dL_dx7.. (-2*x7)/(-1) - u4 - 3*u7 =E= 0;

dL_dx10.. -0.5 - u3 + u6 + eps*x10 =G= 0;

dL_dx11.. -2 + u4 + 3*u7 + eps*x11 =G= 0;

Model m / e3.u3,e4.u4,e5.x5,e6.u6,e7.u7,e8.x8,e9.x9,dL_dx3.x3,dL_dx4.x4
         ,dL_dx6.x6,dL_dx7.x7,dL_dx10.x10,dL_dx11.x11 /;

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 23:15:28

********************************************************************************
 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')
     6:vi defviExp x
     7:vi defFExp('1','3') zExp('1','3') defFExp('2','3') zExp('2','3')


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

   Number of VI functions specified = 3
   * e5 is VI Func and perpendicular to x5
   * e8 is VI Func and perpendicular to x8
   * e9 is VI Func and perpendicular to x9


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

      {reformulated}  defobjExp(1)
      {reformulated}  defobjExp(2)
                  e3  defFExp(1,1)
                  e4  defFExp(1,2)
                  e5  defFExp(1,3)
                  e6  defFExp(2,1)
                  e7  defFExp(2,2)
                  e8  defFExp(2,3)
                  e9  defviExp
              dL_dx3  {new}
              dL_dx4  {new}
              dL_dx6  {new}
              dL_dx7  {new}
             dL_dx10  {new}
             dL_dx11  {new}


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

      {reformulated}  obj(1)
      {reformulated}  obj(2)
                  x3  zExp(1,1)
                  x4  zExp(1,2)
                  x5  zExp(1,3)
                  x6  zExp(2,1)
                  x7  zExp(2,2)
                  x8  zExp(2,3)
                  x9  x
                 x10  t(1)
                 x11  t(2)
                  u3  {new}
                  u4  {new}
                  u6  {new}
                  u7  {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;

defvi..
    x =E= sum(i, z(i));

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

put info 'equilibrium' /;
put 'implicit z defF' /;
loop(i,
    put 'min', obj(i), t(i), 'z', defobj(i) /;
);
put 'vi', defvi, x;

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;
abort$[ abs(x.l-0.5) > tol ]      'bad x.l', x.l;

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

$onEchoV > impl_gms
***********************************************
* written by GAMS/JAMS at 12/03/16 23:21:59
* for more information use JAMS option "Dict"
***********************************************

Variables  x3,x4,x5,x6,x7,dx3_dx6,dx3_dx7,dx4_dx6,dx4_dx7;

Positive Variables  x6,x7;

Equations  e3,e4,e5,IMPL_de3_dx6,IMPL_de3_dx7,IMPL_de4_dx6,IMPL_de4_dx7,dL_dx6
          ,dL_dx7;


e3..    2*x3 + x4 + x6 - x7 =E= 0;

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

e5..  - x3 - x4 + x5 =E= 0;

* equations below are for newly defined implicit variables

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

IMPL_de3_dx7.. (2)*(dx3_dx7) + (1)*(dx4_dx7) + (-1) =E= 0;

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

IMPL_de4_dx7.. (-2)*(dx3_dx7) + (3)*(dx4_dx7) + (-3) =E= 0;

dL_dx6.. -0.5 + ((-2*x3)/(-1))*(dx3_dx6) + eps*x6 =G= 0;

dL_dx7.. -2 + ((-2*x4)/(-1))*(dx4_dx7) + eps*x7 =G= 0;

Model m / e3.x3,e4.x4,e5.x5,IMPL_de3_dx6.dx3_dx6,IMPL_de3_dx7.dx3_dx7
         ,IMPL_de4_dx6.dx4_dx6,IMPL_de4_dx7.dx4_dx7,dL_dx6.x6,dL_dx7.x7 /;

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

$onPutV
x3
x4
dx3_dx6
dx3_dx7
dx4_dx6
dx4_dx7
$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 23:21:59

********************************************************************************
 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')
     5:vi defvi x


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

   Number of VI functions specified = 1
   * e5 is VI Func and perpendicular to x5


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

      {reformulated}  defobj(1)
      {reformulated}  defobj(2)
                  e3  defF(1)
                  e4  defF(2)
                  e5  defvi
        IMPL_de3_dx6  {new}
        IMPL_de3_dx7  {new}
        IMPL_de4_dx6  {new}
        IMPL_de4_dx7  {new}
              dL_dx6  {new}
              dL_dx7  {new}


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

      {reformulated}  obj(1)
      {reformulated}  obj(2)
                  x3  z(1)
                  x4  z(2)
                  x5  x
                  x6  t(1)
                  x7  t(2)
             dx3_dx6  {new}
             dx3_dx7  {new}
             dx4_dx6  {new}
             dx4_dx7  {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;
abort$[ abs(x.l-.5) > tol ]       'bad x.l', x.l;

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

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

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

Positive Variables  x6,x7;

Equations  e3,e4,e5,dL_dx3_4,dL_dx4_4,dL_dx6,dL_dx7,dL_dx3_5,dL_dx4_5;


e3..    2*x3 + x4 + x6 - x7 =E= 0;

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

e5..  - x3 - x4 + x5 =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_dx6.. -0.5 - u3_4 + u4_4 + eps*x6 =G= 0;

dL_dx7.. -2 + u3_5 + 3*u4_5 + eps*x7 =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,e5.x5,dL_dx3_4.u3_4,dL_dx4_4.u4_4,dL_dx6.x6,dL_dx7.x7
         ,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')
     5:vi defvi x


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

   Number of VI functions specified = 1
   * e5 is VI Func and perpendicular to x5


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

      {reformulated}  defobj(1)
      {reformulated}  defobj(2)
                  e3  defF(1)
                  e4  defF(2)
                  e5  defvi
            dL_dx3_4  {new}
            dL_dx4_4  {new}
              dL_dx6  {new}
              dL_dx7  {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  x
                  x6  t(1)
                  x7  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';