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

NLv2: fix 0 operands in pow expressions #2726

Merged
merged 3 commits into from
Feb 10, 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
11 changes: 8 additions & 3 deletions pyomo/repn/plugins/nl_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1649,7 +1649,7 @@ def compile_repn(self, visitor, prefix='', args=None, named_exprs=None):
elif nterms == 1:
return prefix + nl_sum, args, named_exprs
else: # nterms == 0
return prefix + (template.const % 0), [], named_exprs
return prefix + (template.const % 0), args, named_exprs

def compile_nonlinear_fragment(self, visitor):
if not self.nonlinear:
Expand Down Expand Up @@ -1924,8 +1924,13 @@ def handle_division_node(visitor, node, arg1, arg2):
return (_GENERAL, AMPLRepn(0, None, nonlin))

def handle_pow_node(visitor, node, arg1, arg2):
if arg1[0] is _CONSTANT and arg2[0] is _CONSTANT:
return _apply_node_operation(node, (arg1[1], arg2[1]))
if arg2[0] is _CONSTANT:
if arg1[0] is _CONSTANT:
return _apply_node_operation(node, (arg1[1], arg2[1]))
elif not arg2[1]:
return _CONSTANT, 1
elif arg2[1] == 1:
return arg1
nonlin = node_result_to_amplrepn(arg1).compile_repn(
visitor, visitor.template.pow)
nonlin = node_result_to_amplrepn(arg2).compile_repn(visitor, *nonlin)
Expand Down
62 changes: 62 additions & 0 deletions pyomo/repn/tests/ampl/test_nlv2.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,43 @@ def test_errors_divide_by_0(self):
self.assertEqual(repn.linear, {})
self.assertEqual(repn.nonlinear, None)

def test_pow(self):
m = ConcreteModel()
m.p = Param(mutable=True, initialize=2)
m.x = Var()

info = INFO()
with LoggingIntercept() as LOG:
repn = info.visitor.walk_expression((m.x**m.p, None, None))
self.assertEqual(LOG.getvalue(), "")
self.assertEqual(repn.nl, None)
self.assertEqual(repn.mult, 1)
self.assertEqual(repn.const, 0)
self.assertEqual(repn.linear, {})
self.assertEqual(repn.nonlinear, ('o5\nv%s\nn2\n', [id(m.x)]))

m.p = 1
info = INFO()
with LoggingIntercept() as LOG:
repn = info.visitor.walk_expression((m.x**m.p, None, None))
self.assertEqual(LOG.getvalue(), "")
self.assertEqual(repn.nl, None)
self.assertEqual(repn.mult, 1)
self.assertEqual(repn.const, 0)
self.assertEqual(repn.linear, {id(m.x): 1})
self.assertEqual(repn.nonlinear, None)

m.p = 0
info = INFO()
with LoggingIntercept() as LOG:
repn = info.visitor.walk_expression((m.x**m.p, None, None))
self.assertEqual(LOG.getvalue(), "")
self.assertEqual(repn.nl, None)
self.assertEqual(repn.mult, 1)
self.assertEqual(repn.const, 1)
self.assertEqual(repn.linear, {})
self.assertEqual(repn.nonlinear, None)

def test_errors_divide_by_0_mult_by_0(self):
# Note: we may elect to deprecate this functionality in the future
#
Expand Down Expand Up @@ -662,6 +699,31 @@ class CustomExpression(ScalarExpression):
self.assertEqual(repn.nonlinear, None)
self.assertEqual(info, [None, None, False])

def test_nested_operator_zero_arg(self):
# This tests an error encountered when developing the nlv2
# writer where var ids were being dropped then the second
# argument in a binary operator was 0. The original case was
# for expr**p where p was a variable fixed to 0. However, since
# then, _handle_pow_operator contains special handling for **0
# and **1.
m = ConcreteModel()
m.x = Var()
m.p = Param(initialize=0, mutable=True)
expr = (1/m.x) == m.p

info = INFO()
with LoggingIntercept() as LOG:
repn = info.visitor.walk_expression((expr, None, None))
self.assertEqual(LOG.getvalue(), "")
self.assertEqual(repn.nl, None)
self.assertEqual(repn.mult, 1)
self.assertEqual(repn.const, 0)
self.assertEqual(repn.linear, {})
self.assertEqual(
repn.nonlinear,
('o24\no3\nn1\nv%s\nn0\n', [id(m.x)])
)

class Test_NLWriter(unittest.TestCase):
def test_external_function_str_args(self):
m = ConcreteModel()
Expand Down