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

Ensure templatize_constraint returns an expression #2983

Merged
merged 3 commits into from
Aug 31, 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
7 changes: 7 additions & 0 deletions pyomo/core/expr/relational_expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,3 +458,10 @@ def _generate_relational_expression(etype, lhs, rhs):
)
else:
return InequalityExpression((lhs, rhs), _relational_op[etype][2])


def tuple_to_relational_expr(args):
if len(args) == 2:
return EqualityExpression(args)
else:
return inequality(*args)
7 changes: 6 additions & 1 deletion pyomo/core/expr/template_expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
Numeric_NPV_Mixin,
register_arg_type,
ARG_TYPE,
_balanced_parens,
)
from pyomo.core.expr.numvalue import (
NumericValue,
Expand All @@ -34,6 +35,7 @@
value,
is_constant,
)
from pyomo.core.expr.relational_expr import tuple_to_relational_expr
from pyomo.core.expr.visitor import (
ExpressionReplacementVisitor,
StreamBasedExpressionVisitor,
Expand Down Expand Up @@ -1173,4 +1175,7 @@ def templatize_rule(block, rule, index_set):


def templatize_constraint(con):
return templatize_rule(con.parent_block(), con.rule, con.index_set())
expr, indices = templatize_rule(con.parent_block(), con.rule, con.index_set())
if expr.__class__ is tuple:
expr = tuple_to_relational_expr(expr)
return expr, indices
47 changes: 47 additions & 0 deletions pyomo/core/tests/unit/test_template_expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,53 @@ def c(m, i):
indices[0].set_value(2)
self.assertEqual(str(resolve_template(template)), 'x[2] <= 0')

def test_tuple_rules(self):
m = ConcreteModel()
m.I = RangeSet(3)
m.x = Var(m.I)

@m.Constraint(m.I)
def c(m, i):
return (None, m.x[i], 0)

template, indices = templatize_constraint(m.c)
self.assertEqual(len(indices), 1)
self.assertIs(indices[0]._set, m.I)
self.assertEqual(str(template), "x[_1] <= 0")
# Test that the RangeSet iterator was put back
self.assertEqual(list(m.I), list(range(1, 4)))
# Evaluate the template
indices[0].set_value(2)
self.assertEqual(str(resolve_template(template)), 'x[2] <= 0')

@m.Constraint(m.I)
def d(m, i):
return (0, m.x[i], 10)

template, indices = templatize_constraint(m.d)
self.assertEqual(len(indices), 1)
self.assertIs(indices[0]._set, m.I)
self.assertEqual(str(template), "0 <= x[_1] <= 10")
# Test that the RangeSet iterator was put back
self.assertEqual(list(m.I), list(range(1, 4)))
# Evaluate the template
indices[0].set_value(2)
self.assertEqual(str(resolve_template(template)), '0 <= x[2] <= 10')

@m.Constraint(m.I)
def e(m, i):
return (m.x[i], 0)

template, indices = templatize_constraint(m.e)
self.assertEqual(len(indices), 1)
self.assertIs(indices[0]._set, m.I)
self.assertEqual(str(template), "x[_1] == 0")
# Test that the RangeSet iterator was put back
self.assertEqual(list(m.I), list(range(1, 4)))
# Evaluate the template
indices[0].set_value(2)
self.assertEqual(str(resolve_template(template)), 'x[2] == 0')

def test_simple_rule_nonfinite_set(self):
m = ConcreteModel()
m.x = Var(Integers, dense=False)
Expand Down