Skip to content

Commit

Permalink
Merge pull request Pyomo#3182 from bknueven/issue-2574
Browse files Browse the repository at this point in the history
Allow APPSI's cmodel to handle non-mutable parameters in variable and constraint bounds
  • Loading branch information
michaelbynum authored Mar 8, 2024
2 parents 56a8380 + b09b307 commit 00adb80
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
5 changes: 4 additions & 1 deletion pyomo/contrib/appsi/cmodel/src/expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1548,7 +1548,10 @@ appsi_operator_from_pyomo_expr(py::handle expr, py::handle var_map,
break;
}
case param: {
res = param_map[expr_types.id(expr)].cast<std::shared_ptr<Node>>();
if (expr.attr("parent_component")().attr("mutable").cast<bool>())
res = param_map[expr_types.id(expr)].cast<std::shared_ptr<Node>>();
else
res = std::make_shared<Constant>(expr.attr("value").cast<double>());
break;
}
case product: {
Expand Down
21 changes: 21 additions & 0 deletions pyomo/contrib/appsi/solvers/tests/test_persistent_solvers.py
Original file line number Diff line number Diff line change
Expand Up @@ -918,6 +918,27 @@ def test_bounds_with_params(
res = opt.solve(m)
self.assertAlmostEqual(m.y.value, 3)

@parameterized.expand(input=_load_tests(all_solvers, only_child_vars_options))
def test_bounds_with_immutable_params(
self, name: str, opt_class: Type[PersistentSolver], only_child_vars
):
# this test is for issue #2574
opt: PersistentSolver = opt_class(only_child_vars=only_child_vars)
if not opt.available():
raise unittest.SkipTest
m = pe.ConcreteModel()
m.p = pe.Param(mutable=False, initialize=1)
m.q = pe.Param([1, 2], mutable=False, initialize=10)
m.y = pe.Var()
m.y.setlb(m.p)
m.y.setub(m.q[1])
m.obj = pe.Objective(expr=m.y)
res = opt.solve(m)
self.assertAlmostEqual(m.y.value, 1)
m.y.setlb(m.q[2])
res = opt.solve(m)
self.assertAlmostEqual(m.y.value, 10)

@parameterized.expand(input=_load_tests(all_solvers, only_child_vars_options))
def test_solution_loader(
self, name: str, opt_class: Type[PersistentSolver], only_child_vars
Expand Down

0 comments on commit 00adb80

Please sign in to comment.