Skip to content

Commit

Permalink
Merge pull request #2833 from jsiirola/baron-writer-negative-linear
Browse files Browse the repository at this point in the history
Gams/Baron: ensure negative numbers are parenthesized
  • Loading branch information
jsiirola authored May 20, 2023
2 parents 47deef1 + e20b685 commit dca0388
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pyomo/repn/plugins/baron_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 4 additions & 4 deletions pyomo/repn/plugins/gams_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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))
Expand Down
13 changes: 13 additions & 0 deletions pyomo/repn/tests/baron/test_baron.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
15 changes: 15 additions & 0 deletions pyomo/repn/tests/gams/test_gams.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit dca0388

Please sign in to comment.