Skip to content

Commit

Permalink
Fix backwards compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
mrmundt committed Jan 31, 2024
1 parent 1dd764f commit 462457b
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 37 deletions.
39 changes: 5 additions & 34 deletions pyomo/contrib/solver/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,21 +349,24 @@ def _map_config(
load_solutions,
symbolic_solver_labels,
timelimit,
# Report timing is no longer a valid option. We now always return a
# timer object that can be inspected.
report_timing,
raise_exception_on_nonoptimal_result,
solver_io,
suffixes,
logfile,
keepfiles,
solnfile,
options,
):
"""Map between legacy and new interface configuration options"""
self.config = self.config()
self.config.tee = tee
self.config.load_solutions = load_solutions
self.config.symbolic_solver_labels = symbolic_solver_labels
self.config.time_limit = timelimit
self.config.report_timing = report_timing
self.config.solver_options.set_value(options)
# This is a new flag in the interface. To preserve backwards compatibility,
# its default is set to "False"
self.config.raise_exception_on_nonoptimal_result = (
Expand Down Expand Up @@ -483,12 +486,9 @@ def solve(
logfile,
keepfiles,
solnfile,
options,
)

original_options = self.options
if options is not None:
self.options = options

results: Results = super().solve(model)
legacy_results, legacy_soln = self._map_results(model, results)

Expand All @@ -497,7 +497,6 @@ def solve(
)

self.config = original_config
self.options = original_options

return legacy_results

Expand Down Expand Up @@ -526,31 +525,3 @@ def license_is_valid(self) -> bool:
"""
return bool(self.available())

@property
def options(self):
"""
Read the options for the dictated solver.
NOTE: Only the set of solvers for which the LegacySolverWrapper is compatible
are accounted for within this property.
Not all solvers are currently covered by this backwards compatibility
class.
"""
for solver_name in ['gurobi', 'ipopt', 'cplex', 'cbc', 'highs']:
if hasattr(self, 'solver_options'):
return getattr(self, 'solver_options')
raise NotImplementedError('Could not find the correct options')

@options.setter
def options(self, val):
"""
Set the options for the dictated solver.
"""
found = False
for solver_name in ['gurobi', 'ipopt', 'cplex', 'cbc', 'highs']:
if hasattr(self, 'solver_options'):
setattr(self, 'solver_options', val)
found = True
if not found:
raise NotImplementedError('Could not find the correct options')
4 changes: 2 additions & 2 deletions pyomo/contrib/solver/ipopt.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from typing import Mapping, Optional, Sequence

from pyomo.common import Executable
from pyomo.common.config import ConfigValue, NonNegativeInt, NonNegativeFloat
from pyomo.common.config import ConfigValue, NonNegativeFloat
from pyomo.common.errors import PyomoException
from pyomo.common.tempfiles import TempfileManager
from pyomo.common.timing import HierarchicalTimer
Expand Down Expand Up @@ -285,7 +285,7 @@ def solve(self, model, **kwds):
f'Solver {self.__class__} is not available ({avail}).'
)
# Update configuration options, based on keywords passed to solve
config: ipoptConfig = self.config(value=kwds)
config: ipoptConfig = self.config(value=kwds, preserve_implicit=True)
if config.threads:
logger.log(
logging.WARNING,
Expand Down
2 changes: 1 addition & 1 deletion pyomo/contrib/solver/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def check_optimal_termination(results):
# Look at the original version of this function to make that happen.
"""
This function returns True if the termination condition for the solver
is 'optimal', 'locallyOptimal', or 'globallyOptimal', and the status is 'ok'
is 'optimal'.
Parameters
----------
Expand Down

0 comments on commit 462457b

Please sign in to comment.