From b3c8ea7f26d6b092f467b765925745b8603914d9 Mon Sep 17 00:00:00 2001 From: Michael Bynum Date: Thu, 25 Feb 2021 11:10:32 -0700 Subject: [PATCH] inequality function should work with floats --- pyomo/core/base/constraint.py | 18 ++++++++++-------- pyomo/core/tests/unit/test_con.py | 21 +++++++++++++++++++++ 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/pyomo/core/base/constraint.py b/pyomo/core/base/constraint.py index 60f0dace41b..3c872080088 100644 --- a/pyomo/core/base/constraint.py +++ b/pyomo/core/base/constraint.py @@ -601,18 +601,20 @@ def set_value(self, expr): "using '<=', '>=', or '=='." % (self.name)) - if not expr.arg(1).is_potentially_variable(): + arg0 = as_numeric(expr.arg(0)) + arg1 = as_numeric(expr.arg(1)) + if not arg1.is_potentially_variable(): self._lower = None - self._body = expr.arg(0) - self._upper = as_numeric(expr.arg(1)) - elif not expr.arg(0).is_potentially_variable(): - self._lower = as_numeric(expr.arg(0)) - self._body = expr.arg(1) + self._body = arg0 + self._upper = arg1 + elif not arg0.is_potentially_variable(): + self._lower = arg0 + self._body = arg1 self._upper = None else: self._lower = None - self._body = expr.arg(0) - self._body -= expr.arg(1) + self._body = arg0 + self._body -= arg1 self._upper = ZeroConstant diff --git a/pyomo/core/tests/unit/test_con.py b/pyomo/core/tests/unit/test_con.py index f7e919a13de..9f0855ecbec 100644 --- a/pyomo/core/tests/unit/test_con.py +++ b/pyomo/core/tests/unit/test_con.py @@ -568,6 +568,27 @@ def test_mutable_novalue_param_equality(self): self.assertEqual(model.c.equality, True) model.del_component(model.c) + def test_inequality(self): + m = ConcreteModel() + m.x = Var() + m.c = Constraint(expr=inequality(lower=-1, body=m.x)) + self.assertEqual(m.c.lower.value, -1) + self.assertIs(m.c.body, m.x) + self.assertIs(m.c.upper, None) + + del m.c + m.c = Constraint(expr=inequality(body=m.x, upper=1)) + self.assertIs(m.c.lower, None) + self.assertIs(m.c.body, m.x) + self.assertEqual(m.c.upper.value, 1) + + del m.c + m.c = Constraint(expr=inequality(lower=-1, body=m.x, upper=1)) + self.assertEqual(m.c.lower.value, -1) + self.assertIs(m.c.body, m.x) + self.assertEqual(m.c.upper.value, 1) + + class TestSimpleCon(unittest.TestCase): def test_set_expr_explicit_multivariate(self):