Skip to content

Commit

Permalink
Merge pull request #1841 from michaelbynum/ineqs
Browse files Browse the repository at this point in the history
Inequality function should work with floats
  • Loading branch information
michaelbynum authored Feb 26, 2021
2 parents e39da20 + b3c8ea7 commit e40e41f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
18 changes: 10 additions & 8 deletions pyomo/core/base/constraint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
21 changes: 21 additions & 0 deletions pyomo/core/tests/unit/test_con.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit e40e41f

Please sign in to comment.