Skip to content

Commit

Permalink
Merge pull request #2051 from michaelbynum/appsi
Browse files Browse the repository at this point in the history
appsi: fix bug in update_variables
  • Loading branch information
jsiirola authored Jul 20, 2021
2 parents 19af433 + e8b628c commit cd1e4ba
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 4 deletions.
7 changes: 6 additions & 1 deletion pyomo/contrib/appsi/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -839,9 +839,14 @@ def remove_block(self, block):
self.remove_params([p for p in block.component_data_objects(ctype=Param, descend_into=True, sort=False)])

@abc.abstractmethod
def update_variables(self, variables: List[_GeneralVarData]):
def _update_variables(self, variables: List[_GeneralVarData]):
pass

def update_variables(self, variables: List[_GeneralVarData]):
for v in variables:
self._vars[id(v)] = (v, v.lb, v.ub, v.is_fixed(), v.domain, v.value)
self._update_variables(variables)

@abc.abstractmethod
def update_params(self):
pass
Expand Down
2 changes: 1 addition & 1 deletion pyomo/contrib/appsi/solvers/gurobi.py
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ def _remove_variables(self, variables: List[_GeneralVarData]):
def _remove_params(self, params: List[_ParamData]):
pass

def update_variables(self, variables: List[_GeneralVarData]):
def _update_variables(self, variables: List[_GeneralVarData]):
for var in variables:
var_id = id(var)
if var_id not in self._pyomo_var_to_solver_var_map:
Expand Down
12 changes: 12 additions & 0 deletions pyomo/contrib/appsi/solvers/tests/test_persistent_solvers.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,18 @@ def test_fixed_vars(self, namee: str, opt_class: Type[PersistentSolver]):
res = opt.solve(m)
self.assertAlmostEqual(m.x.value, (b2 - b1) / (a1 - a2))
self.assertAlmostEqual(m.y.value, a1 * (b2 - b1) / (a1 - a2) + b1)
m.x.fix(0)
res = opt.solve(m)
self.assertAlmostEqual(m.x.value, 0)
self.assertAlmostEqual(m.y.value, 2)
m.x.value = 2
res = opt.solve(m)
self.assertAlmostEqual(m.x.value, 2)
self.assertAlmostEqual(m.y.value, 3)
m.x.value = 0
res = opt.solve(m)
self.assertAlmostEqual(m.x.value, 0)
self.assertAlmostEqual(m.y.value, 2)

@parameterized.expand(input=all_solvers)
def test_mutable_param_with_range(self, name: str, opt_class: Type[PersistentSolver]):
Expand Down
2 changes: 1 addition & 1 deletion pyomo/contrib/appsi/writers/lp_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def _remove_params(self, params: List[_ParamData]):
self._symbol_map.removeSymbol(p)
self._param_labeler.remove_obj(p)

def update_variables(self, variables: List[_GeneralVarData]):
def _update_variables(self, variables: List[_GeneralVarData]):
for v in variables:
cv = self._pyomo_var_to_solver_var_map[id(v)]
if v.is_binary():
Expand Down
2 changes: 1 addition & 1 deletion pyomo/contrib/appsi/writers/nl_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def _remove_params(self, params: List[_ParamData]):
self._symbol_map.removeSymbol(p)
self._param_labeler.remove_obj(p)

def update_variables(self, variables: List[_GeneralVarData]):
def _update_variables(self, variables: List[_GeneralVarData]):
for v in variables:
cv = self._pyomo_var_to_solver_var_map[id(v)]
if not v.is_continuous():
Expand Down

0 comments on commit cd1e4ba

Please sign in to comment.