40
40
}
41
41
_arc_functions = {'acos' ,'asin' ,'atan' }
42
42
_dnlp_functions = {'ceil' ,'floor' ,'abs' }
43
-
43
+ _zero_one = { 0 , 1 }
44
44
#
45
45
# A visitor pattern that creates a string for an expression
46
46
# that is compatible with the GAMS syntax.
@@ -209,18 +209,20 @@ def __init__(self, var_list, symbol_map):
209
209
v = symbol_map .getObject (var )
210
210
if v .is_fixed ():
211
211
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 )
214
217
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 ):
217
219
self .binary .append (var )
218
220
else :
219
221
self .ints .append (var )
220
- elif value (v .lb ) == 0 :
221
- self .positive .append (var )
222
222
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}" )
224
226
225
227
def __iter__ (self ):
226
228
"""Iterate over all variables.
@@ -681,21 +683,21 @@ def _write_model(self,
681
683
for category , var_name in categorized_vars :
682
684
var = symbolMap .getObject (var_name )
683
685
tc (var )
686
+ lb , ub = var .bounds
684
687
if category == 'positive' :
685
- if var . has_ub () :
688
+ if ub is not None :
686
689
output_file .write ("%s.up = %s;\n " %
687
- (var_name , ftoa (var . ub )))
690
+ (var_name , ftoa (ub )))
688
691
elif category == 'ints' :
689
- if not var . has_lb () :
692
+ if lb is None :
690
693
warn_int_bounds = True
691
694
# GAMS doesn't allow -INF lower bound for ints
692
695
logger .warning ("Lower bound for integer variable %s set "
693
696
"to -1.0E+100." % var .name )
694
697
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 :
699
701
warn_int_bounds = True
700
702
# GAMS has an option value called IntVarUp that is the
701
703
# default upper integer bound, which it applies if the
@@ -705,22 +707,17 @@ def _write_model(self,
705
707
"to +1.0E+100." % var .name )
706
708
output_file .write ("%s.up = +1.0E+100;\n " % (var_name ))
707
709
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 )))
710
711
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 )))
717
716
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 )))
724
721
else :
725
722
raise KeyError ('Category %s not supported' % category )
726
723
if warmstart and var .value is not None :
0 commit comments