From 3ff15b95b67d6d3758ba546ce39cb144a322d834 Mon Sep 17 00:00:00 2001 From: John Siirola Date: Tue, 30 May 2023 13:17:01 -0600 Subject: [PATCH] LP/QP repn: ensure that {}**0 is compiled to the constant 1 --- pyomo/repn/linear.py | 2 ++ pyomo/repn/tests/test_linear.py | 12 ++++++++++++ 2 files changed, 14 insertions(+) diff --git a/pyomo/repn/linear.py b/pyomo/repn/linear.py index 8945199b268..72c183f5a00 100644 --- a/pyomo/repn/linear.py +++ b/pyomo/repn/linear.py @@ -344,6 +344,8 @@ def _handle_pow_ANY_constant(visitor, node, arg1, arg2): visitor, None, ans, (_type, _arg.duplicate()) ) return ans + elif exp == 0: + return _CONSTANT, 1 else: return _handle_pow_nonlinear(visitor, node, arg1, arg2) diff --git a/pyomo/repn/tests/test_linear.py b/pyomo/repn/tests/test_linear.py index 0b0e2e8b68e..acc6d315251 100644 --- a/pyomo/repn/tests/test_linear.py +++ b/pyomo/repn/tests/test_linear.py @@ -852,6 +852,18 @@ def test_pow_expr(self): self.assertEqual(repn.linear, {id(m.x): 1}) self.assertEqual(repn.nonlinear, None) + m.p = 0 + + cfg = VisitorConfig() + repn = LinearRepnVisitor(*cfg).walk_expression(e) + self.assertEqual(cfg.subexpr, {}) + self.assertEqual(cfg.var_map, {id(m.x): m.x}) + self.assertEqual(cfg.var_order, {id(m.x): 0}) + self.assertEqual(repn.multiplier, 1) + self.assertEqual(repn.constant, 1) + self.assertEqual(repn.linear, {}) + self.assertEqual(repn.nonlinear, None) + m.p = 2 cfg = VisitorConfig()