dumpopt2.gms : Test dumpOpt with dumpOptGDX

Description

This test ensures correct behavior of the command line parameter dumpOptGDX
in combination with dumpOpt.

Contributor: Lutz Westermann, November 2021


Small Model of Type : GAMS


Category : GAMS Test library


Main file : dumpopt2.gms

$title 'Test dumpOpt with dumpOptGDX' (DUMPOPT2,SEQ=884)

$onText
This test ensures correct behavior of the command line parameter dumpOptGDX
in combination with dumpOpt.

Contributor: Lutz Westermann, November 2021
$offText

$onCheckErrorLevel
option CheckErrorLevel = 1;

Set duVals               / 1*4, 11, 21 /
    restartRecov(duVals) / 1*4         /
    reWriteInput(duVals) /      11, 21 /;

$onEcho > tran1.gms
$title A Transportation Problem (TRNSPORT,SEQ=1)

$onText
This problem finds a least cost shipping schedule that meets
requirements at markets and supplies at factories.


Dantzig, G B, Chapter 3.3. In Linear Programming and Extensions.
Princeton University Press, Princeton, New Jersey, 1963.

This formulation is described in detail in:
Rosenthal, R E, Chapter 2: A GAMS Tutorial. In GAMS: A User's Guide.
The Scientific Press, Redwood City, California, 1988.

The line numbers will not match those in the book because of these
comments.

Keywords: linear programming, transportation problem, scheduling
$offText

* Add some complexity

display 'hello';
$onEmbeddedCode Python:
pass
$offEmbeddedCode

embeddedCode Python:
pass
endEmbeddedCode

file fx;
put fx;
$onPut
xxx
$offPut

Set
   i 'canning plants' / seattle,  san-diego /
   j 'markets'        / new-york, chicago, topeka /;

Parameter
   a(i) 'capacity of plant i in cases'
        / seattle    350
          san-diego  600 /

   b(j) 'demand at market j in cases'
        / new-york   325
          chicago    300
          topeka     275 /;

Table d(i,j) 'distance in thousands of miles'
              new-york  chicago  topeka
   seattle         2.5      1.7     1.8
   san-diego       2.5      1.8     1.4;

Scalar f 'freight in dollars per case per thousand miles' / 90 /;

Parameter c(i,j) 'transport cost in thousands of dollars per case';
c(i,j) = f*d(i,j)/1000;

Variable
   x(i,j) 'shipment quantities in cases'
   z      'total transportation costs in thousands of dollars';

Positive Variable x;

Equation
   cost      'define objective function'
   supply(i) 'observe supply limit at plant i'
   demand(j) 'satisfy demand at market j';

cost..      z =e= sum((i,j), c(i,j)*x(i,j));

supply(i).. sum(j, x(i,j)) =l= a(i);

demand(j).. sum(i, x(i,j)) =g= b(j);

Model transport / all /;
$offEcho

* Generate the base case: This creates a restart and GDX file to be used and compared to with different runs
$call gams tran1.gms lo=%GAMS.lo% s=base GDX=trans1base

* W/o dumpOptGDX
loop(duVals$reWriteInput(duVals),
  put_utility 'shell' / 'gams tran1.gms lo=%GAMS.lo% dumpOpt=' duVals.tl:0;
  put_utility 'shell' / 'gams tran1.dmp lo=%GAMS.lo% GDX=trans1' duVals.tl:0;
  put_utility 'shell' / 'gdxdiff trans1base.gdx trans1' duVals.tl:0 '.gdx > %system.nullfile%';
);

* W/ dumpOptGDX
loop(duVals$reWriteInput(duVals),
  put_utility 'shell' / 'gams tran1.gms lo=%GAMS.lo% dumpOptGDX=trans1 dumpOpt=' duVals.tl:0;
  put_utility 'shell' / 'gams tran1.dmp lo=%GAMS.lo% GDX=trans1' duVals.tl:0;
  put_utility 'shell' / 'gdxdiff trans1base.gdx trans1' duVals.tl:0 '.gdx > %system.nullfile%';
);

$onEcho > tran2.gms
solve transport using lp minimizing z ;
$offEcho

* Restart recovery w/ and w/o dumpOptGDX 
loop(duVals$restartRecov(duVals),
  put_utility 'shell' / 'gams tran2.gms lo=%GAMS.lo% r=base dumpOpt=' duVals.tl:0;
  put_utility 'shell' / 'gams tran2.dmp lo=%GAMS.lo% GDX=trans2' duVals.tl:0;
  put_utility 'shell' / 'gams tran2.gms lo=%GAMS.lo% r=base dumpOptGDX=trans2 dumpOpt=' duVals.tl:0;
  put_utility 'shell' / 'gams tran2.dmp lo=%GAMS.lo% GDX=trans2' duVals.tl:0 'GDX';
  put_utility 'shell' / 'gdxdiff trans2' duVals.tl:0 '.gdx trans2' duVals.tl:0 'GDX.gdx > %system.nullfile%';
);

* Check some constructed cases
$onEcho > data.gms
Set       i    / i1*i3/;
Parameter a(i) / i1  1
                 i2  2/;
$onMulti
Parameter a(i) / i1 0, i3  3/;
Scalar    b    /  7 /; Scalar    b    / 77 /;
Set       i    / i2*i5/;

Scalar y /1/, x;
$onVerbatim
Set       i    / i3*i6/;
$eval Y y


$offVerbatim
Set xx /x1*x%y%/;
$onVerbatim
$ifThenE %Y%=1
  x=1;
$else
  x=2;
$endIf
$offVerbatim
$offEcho

* Generate new base case
$call gams data.gms lo=%GAMS.lo% GDX=database

* W/o dumpOptGDX
loop(duVals$reWriteInput(duVals),
  put_utility 'shell' / 'gams data.gms lo=%GAMS.lo% dumpOpt=' duVals.tl:0;
  put_utility 'shell' / 'gams data.dmp lo=%GAMS.lo% GDX=data' duVals.tl:0;
  put_utility 'shell' / 'gdxdiff database.gdx data' duVals.tl:0 '.gdx > %system.nullfile%';
);

* W/ dumpOptGDX
loop(duVals$reWriteInput(duVals),
  put_utility 'shell' / 'gams data.gms lo=%GAMS.lo% dumpOptGDX=data dumpOpt=' duVals.tl:0;
  put_utility 'shell' / 'gams data.dmp lo=%GAMS.lo% GDX=data' duVals.tl:0;
  put_utility 'shell' / 'gdxdiff database.gdx data' duVals.tl:0 '.gdx > %system.nullfile%';
);


* Deal with empty data statements
$onEcho > data.gms
Set empty(*);
$gdxOut test.gdx
$unLoad empty
$gdxOut

$gdxIn test.gdx
  $$load empty
$gdxIn

Set test(*) / /;

Set i
    emptyI(i) / /;
$offEcho

* Generate new base case
$call gams data.gms lo=%GAMS.lo% GDX=database

* W/o dumpOptGDX
loop(duVals$reWriteInput(duVals),
  put_utility 'shell' / 'gams data.gms lo=%GAMS.lo% dumpOpt=' duVals.tl:0;
  put_utility 'shell' / 'gams data.dmp lo=%GAMS.lo% GDX=data' duVals.tl:0;
  put_utility 'shell' / 'gdxdiff database.gdx data' duVals.tl:0 '.gdx > %system.nullfile%';
);

* W/ dumpOptGDX
loop(duVals$reWriteInput(duVals),
  put_utility 'shell' / 'gams data.gms lo=%GAMS.lo% dumpOptGDX=data dumpOpt=' duVals.tl:0;
  put_utility 'shell' / 'gams data.dmp lo=%GAMS.lo% GDX=data' duVals.tl:0;
  put_utility 'shell' / 'gdxdiff database.gdx data' duVals.tl:0 '.gdx > %system.nullfile%';
);

* Deal with subsets
$onEcho > data.gms
Set i(i) / i1 /;

Set jj
    j(jj);
Set jj / j1 /;
$offEcho

* Generate new base case
$call gams data.gms lo=%GAMS.lo% GDX=database

* W/o dumpOptGDX
loop(duVals$reWriteInput(duVals),
  put_utility 'shell' / 'gams data.gms lo=%GAMS.lo% dumpOpt=' duVals.tl:0;
  put_utility 'shell' / 'gams data.dmp lo=%GAMS.lo% GDX=data' duVals.tl:0;
  put_utility 'shell' / 'gdxdiff database.gdx data' duVals.tl:0 '.gdx > %system.nullfile%';
);

* W/ dumpOptGDX
loop(duVals$reWriteInput(duVals),
  put_utility 'shell' / 'gams data.gms lo=%GAMS.lo% dumpOptGDX=data dumpOpt=' duVals.tl:0;
  put_utility 'shell' / 'gams data.dmp lo=%GAMS.lo% GDX=data' duVals.tl:0;
  put_utility 'shell' / 'gdxdiff database.gdx data' duVals.tl:0 '.gdx > %system.nullfile%';
);

* Deal with merged sets
$onEcho > data.gms
Set i;
Parameter p(i);
$gdxIn trans1base.gdx
$load i<c.Dim1
$loadM i<c.Dim2
display i;
$offEcho

* Generate new base case
$call gams data.gms lo=%GAMS.lo% GDX=database

* W/o dumpOptGDX
loop(duVals$reWriteInput(duVals),
  put_utility 'shell' / 'gams data.gms lo=%GAMS.lo% dumpOpt=' duVals.tl:0;
  put_utility 'shell' / 'gams data.dmp lo=%GAMS.lo% GDX=data' duVals.tl:0;
  put_utility 'shell' / 'gdxdiff database.gdx data' duVals.tl:0 '.gdx > %system.nullfile%';
);

* W/ dumpOptGDX
loop(duVals$reWriteInput(duVals),
  put_utility 'shell' / 'gams data.gms lo=%GAMS.lo% dumpOptGDX=data dumpOpt=' duVals.tl:0;
  put_utility 'shell' / 'gams data.dmp lo=%GAMS.lo% GDX=data' duVals.tl:0;
  put_utility 'shell' / 'gdxdiff database.gdx data' duVals.tl:0 '.gdx > %system.nullfile%';
);


* Some more tables
Table randomEmptyTable(*,*),randomDelimTable(*,*)
$onDelim
bli,bla,blubb
t1,123
t2,456
$offDelim
;

* Generate new base case
$call gams data.gms lo=%GAMS.lo% GDX=database

* W/o dumpOptGDX
loop(duVals$reWriteInput(duVals),
  put_utility 'shell' / 'gams data.gms lo=%GAMS.lo% dumpOpt=' duVals.tl:0;
  put_utility 'shell' / 'gams data.dmp lo=%GAMS.lo% GDX=data' duVals.tl:0;
  put_utility 'shell' / 'gdxdiff database.gdx data' duVals.tl:0 '.gdx > %system.nullfile%';
);

* W/ dumpOptGDX
loop(duVals$reWriteInput(duVals),
  put_utility 'shell' / 'gams data.gms lo=%GAMS.lo% dumpOptGDX=data dumpOpt=' duVals.tl:0;
  put_utility 'shell' / 'gams data.dmp lo=%GAMS.lo% GDX=data' duVals.tl:0;
  put_utility 'shell' / 'gdxdiff database.gdx data' duVals.tl:0 '.gdx > %system.nullfile%';
);