GAMS [ Home | Downloads | Documentation | Solvers | APIs | Tools | Model Libraries | Resources | Sales | Support | Contact Us | Search ]

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

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

$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-3
scalars tol / %TESTTOL% /;
file opt  / 'jams.opt' /;
file info / '%emp.info%' /;

sets i agents   / 1*2 /;

* The scalar files generated depend on the variable declaration order,
* so do not change the declarations lightly

* The objective variable is a shared variable among agents.
* We set the objective variable as an implicit variable to implement shared
* objective variable.

variable
    objExp(i)
    obj
    x
    ;

positive variable p;

equation
    defobjExp(i)
    defobj
    defxlo
    defxup
    ;


defobjExp(i)..
    objExp(i) =E= sqr(p) - p - sqr(x) - x;

defxlo..
    x =G= -p;

defxup..
    x =L= p;

model me 'explicit formulation' / defobjExp, defxlo, defxup /;
put info 'equilibrium';
put / 'min', objExp('1'), p, defobjExp('1');
put / 'max', objExp('2'), x, defobjExp('2'), defxlo, defxup;
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(objExp.l('1')-0) > tol ]   'bad objExp.l("1")', objExp.l;
abort$[ abs(objExp.l('2')-0) > tol ]   'bad objExp.l("2")', objExp.l;
abort$[ abs(x.l+.5) > tol ]            'bad x.l', x.l;
abort$[ abs(p.l-.5) > tol ]            'bad p.l', p.l;

x.l = 0;
p.l = 0;
defxlo.m = 0;
defxup.m = 0;

$onecho > expl_gms
***********************************************
* written by GAMS/JAMS at 12/04/16 16:32:21
* for more information use JAMS option "Dict"
***********************************************

Variables  x3,x4,u3,u4;

Negative Variables  u3;

Positive Variables  x4;

Positive Variables  u4;

Equations  e3,e4,dL_dx3,dL_dx4;


e3.. 0 =L=    x3 + x4;

e4.. 0 =G=    x3 - x4;

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

dL_dx4.. (-(-1 + 2*x4))/(-1) =G= 0;

Model m / e3.u3,e4.u4,dL_dx3.x3,dL_dx4.x4 /;

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 -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/04/16 16:32:21

********************************************************************************
 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 objExp('1') p defobjExp('1')
     3:max objExp('2') x defobjExp('2') defxlo defxup


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


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

      {reformulated}  defobjExp(1)
      {reformulated}  defobjExp(2)
                  e3  defxlo
                  e4  defxup
              dL_dx3  {new}
              dL_dx4  {new}


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

      {reformulated}  objExp(1)
      {reformulated}  objExp(2)
                  x3  x
                  x4  p
                  u3  {new}
                  u4  {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..
    obj =E= sqr(p) - p - sqr(x) - x;

model mi 'implicit formulation' / defobj, defxlo, defxup /;

put info 'equilibrium';
put / 'implicit obj defobj';
put / 'min', obj, p;
put / 'max', obj, x, defxlo, defxup;
putclose;

* testing with implicit
putclose opt
  'Dict     implDict.txt' /
  'FileName impl.gms' ;

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-0) > tol ]   'bad obj.l', obj.l;
abort$[ abs(x.l+.5) > tol ]    'bad x.l', x.l;
abort$[ abs(p.l-.5) > tol ]    'bad p.l', p.l;

obj.l = 0;
x.l = 0;
p.l = 0;
defobj.m = 0;
defxlo.m = 0;
defxup.m = 0;

$onecho > impl_gms
***********************************************
* written by GAMS/JAMS at 12/04/16 16:33:55
* for more information use JAMS option "Dict"
***********************************************

Variables  x2,x3,u2,u3;

Negative Variables  u2;

Positive Variables  x3;

Positive Variables  u3;

Equations  e2,e3,dL_dx2,dL_dx3;


e2.. 0 =L=    x2 + x3;

e3.. 0 =G=    x2 - x3;

dL_dx2.. (-1)*((-(-1 - 2*x2))/(-1)) + u2 + u3 + eps*x2 =E= 0;

dL_dx3.. (1)*((-(-1 + 2*x3))/(-1)) + eps*x3 =G= 0;

Model m / e2.u2,e3.u3,dL_dx2.x2,dL_dx3.x3 /;

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 -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/04/16 16:33:55

********************************************************************************
 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 obj defobj
     3:min obj p
     4:max obj x defxlo defxup


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


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

      {reformulated}  defobj
                  e2  defxlo
                  e3  defxup
              dL_dx2  {new}
              dL_dx3  {new}


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

      {reformulated}  obj
                  x2  x
                  x3  p
                  u2  {new}
                  u3  {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     implAdjointDict.txt' /
  'FileName implAdjoint.gms' /
  'Adjoint' / ;

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-0) > tol ]   'bad obj.l', obj.l;
abort$[ abs(x.l+.5) > tol ]    'bad x.l', x.l;
abort$[ abs(p.l-.5) > tol ]    'bad p.l', p.l;

obj.l = 0;
x.l = 0;
p.l = 0;
defobj.m = 0;
defxlo.m = 0;
defxup.m = 0;

$onecho > implAdjoint_gms
***********************************************
* written by GAMS/JAMS at 12/05/16 14:37:34
* for more information use JAMS option "Dict"
***********************************************

Variables  x2,x3,u2,u3,IMPL1_a4_x1,IMPL1_a5_x1;

Negative Variables  u2;

Positive Variables  x3;

Positive Variables  u3;

Equations  e2,e3,IMPL1_a4_e1,IMPL1_a5_e1,dL_dx2,dL_dx3;


e2.. 0 =L=    x2 + x3;

e3.. 0 =G=    x2 - x3;

IMPL1_a4_e1.. (1)*(IMPL1_a4_x1) + (-1) =E= 0;

IMPL1_a5_e1.. (1)*(IMPL1_a5_x1) + (1) =E= 0;

dL_dx2.. (-(-1 - 2*x2))*(IMPL1_a5_x1) + u2 + u3 + eps*x2 =E= 0;

dL_dx3.. (-(-1 + 2*x3))*(-IMPL1_a4_x1) + eps*x3 =G= 0;

Model m / e2.u2,e3.u3,IMPL1_a4_e1.IMPL1_a4_x1,IMPL1_a5_e1.IMPL1_a5_x1
         ,dL_dx2.x2,dL_dx3.x3 /;

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

Solve m using MCP;
$offecho

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

$onecho > implAdjoint_dict
* written by GAMS/JAMS at 12/05/16 14:37: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:implicit obj defobj
     3:min obj p
     4:max obj x defxlo defxup


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


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

      {reformulated}  defobj
                  e2  defxlo
                  e3  defxup
         IMPL1_a4_e1  {new}
         IMPL1_a5_e1  {new}
              dL_dx2  {new}
              dL_dx3  {new}


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

      {reformulated}  obj
                  x2  x
                  x3  p
                  u2  {new}
                  u3  {new}
         IMPL1_a4_x1  {new}
         IMPL1_a5_x1  {new}
$offecho

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

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

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-0) > tol ]   'bad obj.l', obj.l;
abort$[ abs(x.l+.5) > tol ]    'bad x.l', x.l;
abort$[ abs(p.l-.5) > tol ]    'bad p.l', p.l;

obj.l = 0;
x.l = 0;
p.l = 0;
defobj.m = 0;
defxlo.m = 0;
defxup.m = 0;

$onechoV > implRep_gms
***********************************************
* written by GAMS/JAMS at 12/05/16 14:38:29
* for more information use JAMS option "Dict"
***********************************************

Variables  x1_4,x2,x3,x1_5;

Negative Variables  u2;

Positive Variables  x3;

Positive Variables  u3;

Equations  e1_4,e2,e3,e1_5;


e1_4.. -(sqr(x3) - x3 - sqr(x2) - x2) + x1_4 =E= 0;

e2.. 0 =L=    x2 + x3;

e3.. 0 =G=    x2 - x3;

e1_5.. -(sqr(x3) - x3 - sqr(x2) - x2) + x1_5 =E= 0;

Model m / e1_4,e2,e3,e1_5 /;

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


File empinfo / '%emp.info%' /;

Put empinfo 'equilibrium' /;
Put 'min',x1_4,x3,e1_4 /;
Put 'max',x1_5,x2,e2,e3,e1_5 /;
Putclose empinfo;

m.optfile = 0;

Solve m using EMP;
$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 -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 12/05/16 14:38:29

********************************************************************************
 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 obj defobj
     3:min obj p
     4:max obj x defxlo defxup


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


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

                e1_4  defobj
                  e2  defxlo
                  e3  defxup
                e1_5  defobj


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

                x1_4  obj
                  x2  x
                  x3  p
                x1_5  obj
$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';

* testing with replication and matching
putclose opt
  'Dict     implRepMMDict.txt' /
  'FileName implRepMM.gms' /
  'CopyImplVar' /
  'MixMatch' /
  ;

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-0) > tol ]   'bad obj.l', obj.l;
abort$[ abs(x.l+.5) > tol ]    'bad x.l', x.l;
abort$[ abs(p.l-.5) > tol ]    'bad p.l', p.l;

obj.l = 0;
x.l = 0;
p.l = 0;
defobj.m = 0;
defxlo.m = 0;
defxup.m = 0;

$onechoV > implRepMM_gms
***********************************************
* written by GAMS/JAMS at 12/05/16 14:39:30
* for more information use JAMS option "Dict"
***********************************************

Variables  x1_4,x2,x3,x1_5;

Negative Variables  u2;

Positive Variables  x3;

Positive Variables  u3;

Equations  e1_4,e2,e3,e1_5;


e1_4.. -(sqr(x3) - x3 - sqr(x2) - x2) + x1_4 =E= 0;

e2.. 0 =L=    x2 + x3;

e3.. 0 =G=    x2 - x3;

e1_5.. -(sqr(x3) - x3 - sqr(x2) - x2) + x1_5 =E= 0;

Model m / e1_4,e2,e3,e1_5 /;

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


File empinfo / '%emp.info%' /;

Put empinfo 'equilibrium' /;
Put 'min',x1_4,x3,e1_4 /;
Put 'max',x1_5,x2,e2,e3,e1_5 /;
Putclose empinfo;

m.optfile = 0;

Solve m using EMP;
$offecho

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

$onecho > implRepMM_dict
* written by GAMS/JAMS at 12/05/16 14:39:30

********************************************************************************
 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 obj defobj
     3:min obj p
     4:max obj x defxlo defxup


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


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

                e1_4  defobj
                  e2  defxlo
                  e3  defxup
                e1_5  defobj


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

                x1_4  obj
                  x2  x
                  x3  p
                x1_5  obj
$offecho

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