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

Fix appsi auto-update when unfixing a variable and changing its bound #2996

Merged
merged 4 commits into from
Sep 19, 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
10 changes: 5 additions & 5 deletions pyomo/contrib/appsi/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1406,17 +1406,17 @@ def update(self, timer: HierarchicalTimer = None):
vars_to_update = list()
for v in vars_to_check:
_v, lb, ub, fixed, domain_interval, value = self._vars[id(v)]
if lb is not v._lb:
vars_to_update.append(v)
elif ub is not v._ub:
vars_to_update.append(v)
elif (fixed is not v.fixed) or (fixed and (value != v.value)):
if (fixed != v.fixed) or (fixed and (value != v.value)):
vars_to_update.append(v)
if self.update_config.treat_fixed_vars_as_params:
for c in self._referenced_variables[id(v)][0]:
cons_to_remove_and_add[c] = None
if self._referenced_variables[id(v)][2] is not None:
need_to_set_objective = True
elif lb is not v._lb:
vars_to_update.append(v)
elif ub is not v._ub:
vars_to_update.append(v)
Comment on lines +1416 to +1419
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This catches if the user re-assigns the lower / upper bound, but what about bounds that include mutable Params? Does changing the Param value get propagated to updated bounds?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that should get caught on the next two lines.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that's the case: that only catches if the domain changes, and not if the value of the bounds change:

>>> from pyomo.environ import *
>>> m = ConcreteModel()
>>> m.x = Var()
>>> m.p = Param(mutable=True, initialize=1)
>>> m.x.bounds = -m.p, m.p
>>> m.x.bounds
(-1.0, 1.0)
>>> m.x.domain.get_interval()
(None, None, 0)
>>> m.p = 5
>>> m.x.bounds
(-5.0, 5.0)
>>> m.x.domain.get_interval()
(None, None, 0)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, my mistake. You are right. The case of bounds with mutable parameters is actually caught with the update_params method and is handled differently for each solver. Here is how it is handled with Gurobi.

elif domain_interval != v.domain.get_interval():
vars_to_update.append(v)
Comment on lines 1420 to 1421
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right here, @jsiirola

self.update_variables(vars_to_update)
Expand Down
30 changes: 29 additions & 1 deletion pyomo/contrib/appsi/solvers/tests/test_persistent_solvers.py
Original file line number Diff line number Diff line change
Expand Up @@ -563,8 +563,8 @@ def test_mutable_quadratic_objective(
def test_fixed_vars(
self, name: str, opt_class: Type[PersistentSolver], only_child_vars
):
opt: PersistentSolver = opt_class(only_child_vars=only_child_vars)
for treat_fixed_vars_as_params in [True, False]:
opt: PersistentSolver = opt_class(only_child_vars=only_child_vars)
opt.update_config.treat_fixed_vars_as_params = treat_fixed_vars_as_params
if not opt.available():
raise unittest.SkipTest
Expand Down Expand Up @@ -1281,6 +1281,34 @@ def test_bug_1(self, name: str, opt_class: Type[PersistentSolver], only_child_va
self.assertEqual(res.termination_condition, TerminationCondition.optimal)
self.assertAlmostEqual(res.best_feasible_objective, 3)

@parameterized.expand(input=_load_tests(all_solvers, only_child_vars_options))
def test_bug_2(self, name: str, opt_class: Type[PersistentSolver], only_child_vars):
"""
This test is for a bug where an objective containing a fixed variable does
not get updated properly when the variable is unfixed.
"""
for fixed_var_option in [True, False]:
opt: PersistentSolver = opt_class(only_child_vars=only_child_vars)
if not opt.available():
raise unittest.SkipTest
opt.update_config.treat_fixed_vars_as_params = fixed_var_option

m = pe.ConcreteModel()
m.x = pe.Var(bounds=(-10, 10))
m.y = pe.Var()
m.obj = pe.Objective(expr=3 * m.y - m.x)
m.c = pe.Constraint(expr=m.y >= m.x)

m.x.fix(1)
res = opt.solve(m)
self.assertAlmostEqual(res.best_feasible_objective, 2, 5)

m.x.unfix()
m.x.setlb(-9)
m.x.setub(9)
res = opt.solve(m)
self.assertAlmostEqual(res.best_feasible_objective, -18, 5)


@unittest.skipUnless(cmodel_available, 'appsi extensions are not available')
class TestLegacySolverInterface(unittest.TestCase):
Expand Down