diff --git a/pyomo/repn/plugins/baron_writer.py b/pyomo/repn/plugins/baron_writer.py index 3b7d9bee2f8..72802fbc6bc 100644 --- a/pyomo/repn/plugins/baron_writer.py +++ b/pyomo/repn/plugins/baron_writer.py @@ -61,7 +61,7 @@ def _handle_PowExpression(visitor, node, values): if type(arg) in native_types: pass elif arg.is_fixed(): - values[i] = ftoa(value(arg)) + values[i] = ftoa(value(arg), True) else: unfixed_count += 1 diff --git a/pyomo/repn/plugins/gams_writer.py b/pyomo/repn/plugins/gams_writer.py index c0138bdf920..d16d65d6c54 100644 --- a/pyomo/repn/plugins/gams_writer.py +++ b/pyomo/repn/plugins/gams_writer.py @@ -181,7 +181,7 @@ def _linear_to_string(self, node): values = [ self._monomial_to_string(arg) if arg.__class__ is EXPR.MonomialTermExpression - else ftoa(arg) + else ftoa(arg, True) for arg in node.args ] return node._to_string(values, False, self.smap) @@ -623,20 +623,20 @@ def _write_model( constraint_names.append('%s' % cName) ConstraintIO.write( '%s.. %s =e= %s ;\n' - % (constraint_names[-1], con_body_str, ftoa(con.upper)) + % (constraint_names[-1], con_body_str, ftoa(con.upper, False)) ) else: if con.has_lb(): constraint_names.append('%s_lo' % cName) ConstraintIO.write( '%s.. %s =l= %s ;\n' - % (constraint_names[-1], ftoa(con.lower), con_body_str) + % (constraint_names[-1], ftoa(con.lower, False), con_body_str) ) if con.has_ub(): constraint_names.append('%s_hi' % cName) ConstraintIO.write( '%s.. %s =l= %s ;\n' - % (constraint_names[-1], con_body_str, ftoa(con.upper)) + % (constraint_names[-1], con_body_str, ftoa(con.upper, False)) ) obj = list(model.component_data_objects(Objective, active=True, sort=sort)) diff --git a/pyomo/repn/tests/baron/test_baron.py b/pyomo/repn/tests/baron/test_baron.py index b919020d9e8..b425474cb20 100644 --- a/pyomo/repn/tests/baron/test_baron.py +++ b/pyomo/repn/tests/baron/test_baron.py @@ -260,6 +260,19 @@ def test_pow(self): test = expression_to_string(e, variables, smap) self.assertEqual(test, "3 ^ x") + def test_issue_2819(self): + m = ConcreteModel() + m.x = Var() + m.z = Var() + t = 0.55 + m.x.fix(3.5) + e = (m.x - 4) ** 2 + (m.z - 1) ** 2 - t + + variables = OrderedSet() + smap = SymbolMap() + test = expression_to_string(e, variables, smap) + self.assertEqual(test, '(-0.5) ^ 2 + (z - 1) ^ 2 + (-0.55)') + # class TestBaron_writer(unittest.TestCase): class XTestBaron_writer(object): diff --git a/pyomo/repn/tests/gams/test_gams.py b/pyomo/repn/tests/gams/test_gams.py index fb4c281d900..e6b729e5dfc 100644 --- a/pyomo/repn/tests/gams/test_gams.py +++ b/pyomo/repn/tests/gams/test_gams.py @@ -457,6 +457,21 @@ def test_negative_float_double_operator(self): expression_to_string(m.c2.body, tc, smap=smap), ("x1 ** (-1.5)", False) ) + def test_issue_2819(self): + m = ConcreteModel() + m.x = Var() + m.z = Var() + t = 0.55 + m.x.fix(3.5) + e = (m.x - 4) ** 2 + (m.z - 1) ** 2 - t + + tc = StorageTreeChecker(m) + smap = SymbolMap() + test = expression_to_string(e, tc, smap=smap) + self.assertEqual( + test, ('power((3.5 + (-4)), 2) + power((z + (-1)), 2) + (-0.55)', False) + ) + class TestGams_writer(unittest.TestCase): def _cleanup(self, fname):