Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gams/Baron: ensure negative numbers are parenthesized #2833

Merged
merged 1 commit into from
May 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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