Skip to content

Commit 3f00134

Browse files
authored
Merge pull request #2191 from jsiirola/gams-writer-lb-cache
GAMS writer performance improvement (domain/bounds access)
2 parents 849896c + 40d5fcc commit 3f00134

File tree

1 file changed

+26
-29
lines changed

1 file changed

+26
-29
lines changed

pyomo/repn/plugins/gams_writer.py

+26-29
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
}
4141
_arc_functions = {'acos','asin','atan'}
4242
_dnlp_functions = {'ceil','floor','abs'}
43-
43+
_zero_one = {0, 1}
4444
#
4545
# A visitor pattern that creates a string for an expression
4646
# that is compatible with the GAMS syntax.
@@ -209,18 +209,20 @@ def __init__(self, var_list, symbol_map):
209209
v = symbol_map.getObject(var)
210210
if v.is_fixed():
211211
self.fixed.append(var)
212-
elif v.is_binary():
213-
self.binary.append(var)
212+
elif v.is_continuous():
213+
if v.lb == 0:
214+
self.positive.append(var)
215+
else:
216+
self.reals.append(var)
214217
elif v.is_integer():
215-
if (v.has_lb() and (value(v.lb) >= 0)) and \
216-
(v.has_ub() and (value(v.ub) <= 1)):
218+
if all(bnd in _zero_one for bnd in v.bounds):
217219
self.binary.append(var)
218220
else:
219221
self.ints.append(var)
220-
elif value(v.lb) == 0:
221-
self.positive.append(var)
222222
else:
223-
self.reals.append(var)
223+
raise RuntimeError(
224+
"Cannot output variable to GAMS: effective variable "
225+
"domain is not in {Reals, Integers, Binary}")
224226

225227
def __iter__(self):
226228
"""Iterate over all variables.
@@ -681,21 +683,21 @@ def _write_model(self,
681683
for category, var_name in categorized_vars:
682684
var = symbolMap.getObject(var_name)
683685
tc(var)
686+
lb, ub = var.bounds
684687
if category == 'positive':
685-
if var.has_ub():
688+
if ub is not None:
686689
output_file.write("%s.up = %s;\n" %
687-
(var_name, ftoa(var.ub)))
690+
(var_name, ftoa(ub)))
688691
elif category == 'ints':
689-
if not var.has_lb():
692+
if lb is None:
690693
warn_int_bounds = True
691694
# GAMS doesn't allow -INF lower bound for ints
692695
logger.warning("Lower bound for integer variable %s set "
693696
"to -1.0E+100." % var.name)
694697
output_file.write("%s.lo = -1.0E+100;\n" % (var_name))
695-
elif value(var.lb) != 0:
696-
output_file.write("%s.lo = %s;\n" %
697-
(var_name, ftoa(var.lb)))
698-
if not var.has_ub():
698+
elif lb != 0:
699+
output_file.write("%s.lo = %s;\n" % (var_name, ftoa(lb)))
700+
if ub is None:
699701
warn_int_bounds = True
700702
# GAMS has an option value called IntVarUp that is the
701703
# default upper integer bound, which it applies if the
@@ -705,22 +707,17 @@ def _write_model(self,
705707
"to +1.0E+100." % var.name)
706708
output_file.write("%s.up = +1.0E+100;\n" % (var_name))
707709
else:
708-
output_file.write("%s.up = %s;\n" %
709-
(var_name, ftoa(var.ub)))
710+
output_file.write("%s.up = %s;\n" % (var_name, ftoa(ub)))
710711
elif category == 'binary':
711-
if var.has_lb() and value(var.lb) != 0:
712-
output_file.write("%s.lo = %s;\n" %
713-
(var_name, ftoa(var.lb)))
714-
if var.has_ub() and value(var.ub) != 1:
715-
output_file.write("%s.up = %s;\n" %
716-
(var_name, ftoa(var.ub)))
712+
if lb != 0:
713+
output_file.write("%s.lo = %s;\n" % (var_name, ftoa(lb)))
714+
if ub != 1:
715+
output_file.write("%s.up = %s;\n" % (var_name, ftoa(ub)))
717716
elif category == 'reals':
718-
if var.has_lb():
719-
output_file.write("%s.lo = %s;\n" %
720-
(var_name, ftoa(var.lb)))
721-
if var.has_ub():
722-
output_file.write("%s.up = %s;\n" %
723-
(var_name, ftoa(var.ub)))
717+
if lb is not None:
718+
output_file.write("%s.lo = %s;\n" % (var_name, ftoa(lb)))
719+
if ub is not None:
720+
output_file.write("%s.up = %s;\n" % (var_name, ftoa(ub)))
724721
else:
725722
raise KeyError('Category %s not supported' % category)
726723
if warmstart and var.value is not None:

0 commit comments

Comments
 (0)