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

Allow APPSI's cmodel to handle non-mutable parameters in variable and constraint bounds #3182

Merged
merged 2 commits into from
Mar 8, 2024
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
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