Skip to content

Commit

Permalink
Merge pull request #1446 from renkekuhlmann/gams_gdx
Browse files Browse the repository at this point in the history
Add GDX interface for returning solution values from GAMS
  • Loading branch information
jsiirola authored May 21, 2020
2 parents 0b83aad + 6121dde commit cc7d5d8
Show file tree
Hide file tree
Showing 24 changed files with 346 additions and 67 deletions.
93 changes: 67 additions & 26 deletions pyomo/repn/plugins/gams_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,16 @@ def __call__(self,
| 2 : sort keys AND sort names (over declaration order)
- put_results=None
Filename for optionally writing solution values and
marginals to (put_results).dat, and solver statuses
to (put_results + 'stat').dat.
marginals. If put_results_format is 'gdx', then GAMS
will write solution values and marginals to
GAMS_MODEL_p.gdx and solver statuses to
{put_results}_s.gdx. If put_results_format is 'dat',
then solution values and marginals are written to
(put_results).dat, and solver statuses to (put_results +
'stat').dat.
- put_results_format='gdx'
Format used for put_results, one of 'gdx', 'dat'.
"""

# Make sure not to modify the user's dictionary,
Expand All @@ -343,6 +351,12 @@ def __call__(self,
# If None, will chose from lp, nlp, mip, and minlp.
mtype = io_options.pop("mtype", None)

# Improved GAMS calling options
solprint = io_options.pop("solprint", "off")
limrow = io_options.pop("limrow", 0)
limcol = io_options.pop("limcol", 0)
solvelink = io_options.pop("solvelink", 5)

# Lines to add before solve statement.
add_options = io_options.pop("add_options", None)

Expand All @@ -368,6 +382,8 @@ def __call__(self,
# Filename for optionally writing solution values and marginals
# Set to True by GAMSSolver
put_results = io_options.pop("put_results", None)
put_results_format = io_options.pop("put_results_format", 'gdx')
assert put_results_format in ('gdx','dat')

if len(io_options):
raise ValueError(
Expand Down Expand Up @@ -461,8 +477,13 @@ def var_label(obj):
warmstart=warmstart,
solver=solver,
mtype=mtype,
solprint=solprint,
limrow=limrow,
limcol=limcol,
solvelink=solvelink,
add_options=add_options,
put_results=put_results
put_results=put_results,
put_results_format=put_results_format,
)
finally:
if isinstance(output_filename, string_types):
Expand All @@ -483,8 +504,14 @@ def _write_model(self,
warmstart,
solver,
mtype,
solprint,
limrow,
limcol,
solvelink,
add_options,
put_results):
put_results,
put_results_format,
):
constraint_names = []
ConstraintIO = StringIO()
linear = True
Expand Down Expand Up @@ -578,6 +605,7 @@ def _write_model(self,
categorized_vars = Categorizer(var_list, symbolMap)

# Write the GAMS model
output_file.write("$offlisting\n")
# $offdigit ignores extra precise digits instead of erroring
output_file.write("$offdigit\n\n")
output_file.write("EQUATIONS\n\t")
Expand Down Expand Up @@ -679,6 +707,14 @@ def _write_model(self,
% (solver, mtype))
output_file.write("option %s=%s;\n" % (mtype, solver))

output_file.write("option solprint=%s;\n" % solprint)
output_file.write("option limrow=%d;\n" % limrow)
output_file.write("option limcol=%d;\n" % limcol)
output_file.write("option solvelink=%d;\n" % solvelink)

if put_results is not None and put_results_format == 'gdx':
output_file.write("option savepoint=1;\n")

if add_options is not None:
output_file.write("\n* START USER ADDITIONAL OPTIONS\n")
for line in add_options:
Expand Down Expand Up @@ -720,28 +756,33 @@ def _write_model(self,
output_file.write("ETSOLVE = %s.etsolve\n\n" % model_name)

if put_results is not None:
results = put_results + '.dat'
output_file.write("\nfile results /'%s'/;" % results)
output_file.write("\nresults.nd=15;")
output_file.write("\nresults.nw=21;")
output_file.write("\nput results;")
output_file.write("\nput 'SYMBOL : LEVEL : MARGINAL' /;")
for var in var_list:
output_file.write("\nput %s %s.l %s.m /;" % (var, var, var))
for con in constraint_names:
output_file.write("\nput %s %s.l %s.m /;" % (con, con, con))
output_file.write("\nput GAMS_OBJECTIVE GAMS_OBJECTIVE.l "
"GAMS_OBJECTIVE.m;\n")

statresults = put_results + 'stat.dat'
output_file.write("\nfile statresults /'%s'/;" % statresults)
output_file.write("\nstatresults.nd=15;")
output_file.write("\nstatresults.nw=21;")
output_file.write("\nput statresults;")
output_file.write("\nput 'SYMBOL : VALUE' /;")
for stat in stat_vars:
output_file.write("\nput '%s' %s /;\n" % (stat, stat))

if put_results_format == 'gdx':
output_file.write("\nexecute_unload '%s_s.gdx'" % put_results)
for stat in stat_vars:
output_file.write(", %s" % stat)
output_file.write(";\n")
else:
results = put_results + '.dat'
output_file.write("\nfile results /'%s'/;" % results)
output_file.write("\nresults.nd=15;")
output_file.write("\nresults.nw=21;")
output_file.write("\nput results;")
output_file.write("\nput 'SYMBOL : LEVEL : MARGINAL' /;")
for var in var_list:
output_file.write("\nput %s %s.l %s.m /;" % (var, var, var))
for con in constraint_names:
output_file.write("\nput %s %s.l %s.m /;" % (con, con, con))
output_file.write("\nput GAMS_OBJECTIVE GAMS_OBJECTIVE.l "
"GAMS_OBJECTIVE.m;\n")

statresults = put_results + 'stat.dat'
output_file.write("\nfile statresults /'%s'/;" % statresults)
output_file.write("\nstatresults.nd=15;")
output_file.write("\nstatresults.nw=21;")
output_file.write("\nput statresults;")
output_file.write("\nput 'SYMBOL : VALUE' /;")
for stat in stat_vars:
output_file.write("\nput '%s' %s /;\n" % (stat, stat))

valid_solvers = {
'ALPHAECP': {'MINLP','MIQCP'},
Expand Down
5 changes: 5 additions & 0 deletions pyomo/repn/tests/gams/no_column_ordering_linear.gams.baseline
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
$offlisting
$offdigit

EQUATIONS
Expand All @@ -15,6 +16,10 @@ obj.. GAMS_OBJECTIVE =e= a + b + c ;


MODEL GAMS_MODEL /all/ ;
option solprint=off;
option limrow=0;
option limcol=0;
option solvelink=5;
SOLVE GAMS_MODEL USING lp minimizing GAMS_OBJECTIVE;

Scalars MODELSTAT 'model status', SOLVESTAT 'solve status';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
$offlisting
$offdigit

EQUATIONS
Expand All @@ -15,6 +16,10 @@ obj.. GAMS_OBJECTIVE =e= a + b + c + a*a + b*b + c*c + a*b + a*c + b*c ;


MODEL GAMS_MODEL /all/ ;
option solprint=off;
option limrow=0;
option limcol=0;
option solvelink=5;
SOLVE GAMS_MODEL USING nlp minimizing GAMS_OBJECTIVE;

Scalars MODELSTAT 'model status', SOLVESTAT 'solve status';
Expand Down
5 changes: 5 additions & 0 deletions pyomo/repn/tests/gams/no_row_ordering.gams.baseline
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
$offlisting
$offdigit

EQUATIONS
Expand All @@ -23,6 +24,10 @@ obj.. GAMS_OBJECTIVE =e= a ;


MODEL GAMS_MODEL /all/ ;
option solprint=off;
option limrow=0;
option limcol=0;
option solvelink=5;
SOLVE GAMS_MODEL USING lp minimizing GAMS_OBJECTIVE;

Scalars MODELSTAT 'model status', SOLVESTAT 'solve status';
Expand Down
5 changes: 5 additions & 0 deletions pyomo/repn/tests/gams/small1.pyomo.gms
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
$offlisting
$offdigit

EQUATIONS
Expand All @@ -16,6 +17,10 @@ x1.l = 1;
x2.l = 1;

MODEL GAMS_MODEL /all/ ;
option solprint=off;
option limrow=0;
option limcol=0;
option solvelink=5;
SOLVE GAMS_MODEL USING nlp minimizing GAMS_OBJECTIVE;

Scalars MODELSTAT 'model status', SOLVESTAT 'solve status';
Expand Down
5 changes: 5 additions & 0 deletions pyomo/repn/tests/gams/small10.pyomo.gms
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
$offlisting
$offdigit

EQUATIONS
Expand Down Expand Up @@ -39,6 +40,10 @@ c15.. GAMS_OBJECTIVE =e= x1 + 0*x1 + 0*x1 + x1*x1*0 + x1*x1*0 + 0*power(x1, 2) ;


MODEL GAMS_MODEL /all/ ;
option solprint=off;
option limrow=0;
option limcol=0;
option solvelink=5;
SOLVE GAMS_MODEL USING nlp minimizing GAMS_OBJECTIVE;

Scalars MODELSTAT 'model status', SOLVESTAT 'solve status';
Expand Down
5 changes: 5 additions & 0 deletions pyomo/repn/tests/gams/small11.pyomo.gms
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
$offlisting
$offdigit

EQUATIONS
Expand Down Expand Up @@ -25,6 +26,10 @@ c4.. GAMS_OBJECTIVE =e= x3 ;


MODEL GAMS_MODEL /all/ ;
option solprint=off;
option limrow=0;
option limcol=0;
option solvelink=5;
SOLVE GAMS_MODEL USING lp minimizing GAMS_OBJECTIVE;

Scalars MODELSTAT 'model status', SOLVESTAT 'solve status';
Expand Down
5 changes: 5 additions & 0 deletions pyomo/repn/tests/gams/small12.pyomo.gms
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
$offlisting
$offdigit

EQUATIONS
Expand Down Expand Up @@ -72,6 +73,10 @@ x6.l = -2;
x7.l = 2;

MODEL GAMS_MODEL /all/ ;
option solprint=off;
option limrow=0;
option limcol=0;
option solvelink=5;
SOLVE GAMS_MODEL USING nlp minimizing GAMS_OBJECTIVE;

Scalars MODELSTAT 'model status', SOLVESTAT 'solve status';
Expand Down
5 changes: 5 additions & 0 deletions pyomo/repn/tests/gams/small13.pyomo.gms
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
$offlisting
$offdigit

EQUATIONS
Expand All @@ -18,6 +19,10 @@ c4.. GAMS_OBJECTIVE =e= x1 ;
x1.l = 0.5;

MODEL GAMS_MODEL /all/ ;
option solprint=off;
option limrow=0;
option limcol=0;
option solvelink=5;
SOLVE GAMS_MODEL USING nlp maximizing GAMS_OBJECTIVE;

Scalars MODELSTAT 'model status', SOLVESTAT 'solve status';
Expand Down
5 changes: 5 additions & 0 deletions pyomo/repn/tests/gams/small14a.pyomo.gms
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
$offlisting
$offdigit

EQUATIONS
Expand Down Expand Up @@ -46,6 +47,10 @@ x1.l = 1;
x2.l = 0;

MODEL GAMS_MODEL /all/ ;
option solprint=off;
option limrow=0;
option limcol=0;
option solvelink=5;
SOLVE GAMS_MODEL USING dnlp minimizing GAMS_OBJECTIVE;

Scalars MODELSTAT 'model status', SOLVESTAT 'solve status';
Expand Down
5 changes: 5 additions & 0 deletions pyomo/repn/tests/gams/small15.pyomo.gms
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
$offlisting
$offdigit

EQUATIONS
Expand All @@ -16,6 +17,10 @@ x1.l = 1;
x2.l = 1;

MODEL GAMS_MODEL /all/ ;
option solprint=off;
option limrow=0;
option limcol=0;
option solvelink=5;
SOLVE GAMS_MODEL USING nlp minimizing GAMS_OBJECTIVE;

Scalars MODELSTAT 'model status', SOLVESTAT 'solve status';
Expand Down
5 changes: 5 additions & 0 deletions pyomo/repn/tests/gams/small2.pyomo.gms
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
$offlisting
$offdigit

EQUATIONS
Expand All @@ -16,6 +17,10 @@ x1.l = 1;
x2.l = 1;

MODEL GAMS_MODEL /all/ ;
option solprint=off;
option limrow=0;
option limcol=0;
option solvelink=5;
SOLVE GAMS_MODEL USING nlp minimizing GAMS_OBJECTIVE;

Scalars MODELSTAT 'model status', SOLVESTAT 'solve status';
Expand Down
5 changes: 5 additions & 0 deletions pyomo/repn/tests/gams/small3.pyomo.gms
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
$offlisting
$offdigit

EQUATIONS
Expand All @@ -16,6 +17,10 @@ x1.l = 1;
x2.l = 1;

MODEL GAMS_MODEL /all/ ;
option solprint=off;
option limrow=0;
option limcol=0;
option solvelink=5;
SOLVE GAMS_MODEL USING nlp minimizing GAMS_OBJECTIVE;

Scalars MODELSTAT 'model status', SOLVESTAT 'solve status';
Expand Down
5 changes: 5 additions & 0 deletions pyomo/repn/tests/gams/small4.pyomo.gms
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
$offlisting
$offdigit

EQUATIONS
Expand All @@ -16,6 +17,10 @@ x1.l = 1;
x2.l = 1;

MODEL GAMS_MODEL /all/ ;
option solprint=off;
option limrow=0;
option limcol=0;
option solvelink=5;
SOLVE GAMS_MODEL USING nlp minimizing GAMS_OBJECTIVE;

Scalars MODELSTAT 'model status', SOLVESTAT 'solve status';
Expand Down
5 changes: 5 additions & 0 deletions pyomo/repn/tests/gams/small5.pyomo.gms
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
$offlisting
$offdigit

EQUATIONS
Expand Down Expand Up @@ -46,6 +47,10 @@ x3.up = 1;
x3.l = 2;

MODEL GAMS_MODEL /all/ ;
option solprint=off;
option limrow=0;
option limcol=0;
option solvelink=5;
SOLVE GAMS_MODEL USING nlp minimizing GAMS_OBJECTIVE;

Scalars MODELSTAT 'model status', SOLVESTAT 'solve status';
Expand Down
5 changes: 5 additions & 0 deletions pyomo/repn/tests/gams/small6.pyomo.gms
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
$offlisting
$offdigit

EQUATIONS
Expand Down Expand Up @@ -34,6 +35,10 @@ x3.up = 1;
x3.l = 2;

MODEL GAMS_MODEL /all/ ;
option solprint=off;
option limrow=0;
option limcol=0;
option solvelink=5;
SOLVE GAMS_MODEL USING nlp minimizing GAMS_OBJECTIVE;

Scalars MODELSTAT 'model status', SOLVESTAT 'solve status';
Expand Down
Loading

0 comments on commit cc7d5d8

Please sign in to comment.