Skip to content

Commit

Permalink
Update constraint processing to leverage new Constraint internal storage
Browse files Browse the repository at this point in the history
  • Loading branch information
jsiirola committed Jul 29, 2024
1 parent d8900b2 commit bb17e9c
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions pyomo/contrib/sensitivity_toolbox/sens.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

from pyomo.common.sorting import sorted_robust
from pyomo.core.expr import ExpressionReplacementVisitor
from pyomo.core.expr.numvalue import is_potentially_variable

from pyomo.common.modeling import unique_component_name
from pyomo.common.dependencies import numpy as np, scipy
Expand Down Expand Up @@ -673,25 +674,29 @@ def _replace_parameters_in_constraints(self, variableSubMap):
)
last_idx = 0
for con in old_con_list:
if con.equality or con.lower is None or con.upper is None:
new_expr = param_replacer.walk_expression(con.expr)
block.constList.add(expr=new_expr)
new_expr = param_replacer.walk_expression(con.expr)
# TODO: We could only create new constraints for expressions
# where substitution actually happened, but that breaks some
# current tests:
#
# if new_expr is con.expr:
# # No params were substituted. We can ignore this constraint
# continue
if new_expr.nargs() == 3 and (
is_potentially_variable(new_expr.arg(0))
or is_potentially_variable(new_expr.arg(2))
):
# This is a potentially "invalid" range constraint: it
# may now have variables in the bounds. For safety, we
# will split it into two simple inequalities.
block.constList.add(expr=(new_expr.arg(0) <= new_expr.arg(1)))
last_idx += 1
new_old_comp_map[block.constList[last_idx]] = con
else:
# Constraint must be a ranged inequality, break into
# separate constraints
new_body = param_replacer.walk_expression(con.body)
new_lower = param_replacer.walk_expression(con.lower)
new_upper = param_replacer.walk_expression(con.upper)

# Add constraint for lower bound
block.constList.add(expr=(new_lower <= new_body))
block.constList.add(expr=(new_expr.arg(1) <= new_expr.arg(2)))
last_idx += 1
new_old_comp_map[block.constList[last_idx]] = con

# Add constraint for upper bound
block.constList.add(expr=(new_body <= new_upper))
else:
block.constList.add(expr=new_expr)
last_idx += 1
new_old_comp_map[block.constList[last_idx]] = con
con.deactivate()
Expand Down

0 comments on commit bb17e9c

Please sign in to comment.