Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
b57ab07
updating solution loader
michaelbynum Aug 9, 2025
710807b
Merge remote-tracking branch 'origin/main' into solver_api
michaelbynum Aug 12, 2025
438b9b5
Merge remote-tracking branch 'michaelbynum/observer_gurobi_refactor' …
michaelbynum Aug 12, 2025
ac42345
updating solution loader
michaelbynum Aug 12, 2025
70ca6e7
updating solution loader
michaelbynum Aug 13, 2025
5ec0421
Merge branch 'observer_gurobi_refactor' into solver_api
michaelbynum Aug 13, 2025
2885f42
update solution loader
michaelbynum Aug 13, 2025
d16bee5
adding tests for trivial constraints and fixing bugs
michaelbynum Aug 14, 2025
c62a7b3
Merge branch 'observer_gurobi_refactor' into solver_api
michaelbynum Aug 14, 2025
a4e2b81
run black
michaelbynum Aug 14, 2025
2001d15
Merge branch 'solver_api' into trivial_constraints
michaelbynum Aug 14, 2025
d25e721
run black
michaelbynum Aug 14, 2025
1750fc5
merge in observer_gurobi_refactor
michaelbynum Aug 16, 2025
02f383d
Merge branch 'solver_api' into trivial_constraints
michaelbynum Aug 16, 2025
413d63d
Merge branch 'observer_gurobi_refactor' into solver_api
michaelbynum Aug 16, 2025
3d302a1
Merge branch 'solver_api' into trivial_constraints
michaelbynum Aug 16, 2025
0bbdd70
Merge branch 'observer_gurobi_refactor' into solver_api
michaelbynum Aug 18, 2025
ecd602d
Merge branch 'solver_api' into trivial_constraints
michaelbynum Aug 18, 2025
a96b518
merge observer_gurobi_refactor into solver_api
michaelbynum Oct 5, 2025
92e77ba
merge solver_api into trivial_constraints
michaelbynum Oct 5, 2025
ce4e77c
merge main
michaelbynum Dec 12, 2025
c3f2d48
solution loader updates
michaelbynum Dec 12, 2025
ba4b29c
run black
michaelbynum Dec 12, 2025
045f537
merge main
michaelbynum Dec 12, 2025
0792800
fix typo
michaelbynum Dec 18, 2025
253bdd9
Merge branch 'solver_api' into trivial_constraints
michaelbynum Dec 18, 2025
f3370f3
Merge branch 'observer_gurobi_refactor' into solver_api
michaelbynum Jan 29, 2026
c3ad9b5
Merge branch 'solver_api' into trivial_constraints
michaelbynum Jan 29, 2026
3a44486
merge main
michaelbynum Feb 11, 2026
a125456
run black
michaelbynum Feb 11, 2026
b5d16d9
fix tests
michaelbynum Feb 11, 2026
9473f29
run black
michaelbynum Feb 12, 2026
7557036
Merge branch 'main' into solver_api
jsiirola Feb 13, 2026
ef6dcf6
Merge branch 'solver_api' into trivial_constraints
michaelbynum Feb 13, 2026
fb38368
Merge branch 'solver_api' into trivial_constraints
michaelbynum Feb 13, 2026
a710be3
Merge remote-tracking branch 'origin/main' into solver_api
michaelbynum Mar 17, 2026
60d4160
better handling of suffixes for the sol file solution loader
michaelbynum Mar 17, 2026
0911727
run black
michaelbynum Mar 17, 2026
0338bcc
update gams solution loader
michaelbynum Mar 17, 2026
5ed9722
Merge remote-tracking branch 'origin/main' into solver_api
michaelbynum Mar 19, 2026
c9b042b
typo
michaelbynum Mar 19, 2026
2874fe9
rename method
michaelbynum Mar 19, 2026
26ef10d
update gams solution loader
michaelbynum Mar 19, 2026
845e586
update tests
michaelbynum Mar 19, 2026
3c4716c
update tests
michaelbynum Mar 23, 2026
12cfd2f
run black
michaelbynum Mar 23, 2026
c5af43d
update highs solution loader
michaelbynum Mar 23, 2026
4f44d3b
run black
michaelbynum Mar 23, 2026
8f1e2b0
Merge remote-tracking branch 'origin/main' into solver_api
michaelbynum Mar 23, 2026
0365ae5
merge solver_api into trivial_constraints
michaelbynum Mar 23, 2026
1a26eae
update knitro solution loader
michaelbynum Mar 23, 2026
7293c29
Merge branch 'solver_api' into trivial_constraints
michaelbynum Mar 23, 2026
9819a04
Merge branch 'main' into solver_api
michaelbynum Mar 26, 2026
75fee85
Merge branch 'solver_api' into trivial_constraints
michaelbynum Mar 26, 2026
91f24a0
Merge branch 'main' into solver_api
blnicho Apr 2, 2026
29a72f7
Merge branch 'solver_api' into trivial_constraints
michaelbynum Apr 9, 2026
5a71a82
Merge remote-tracking branch 'origin/main' into solver_api
michaelbynum Apr 15, 2026
c216e19
Merge branch 'solver_api' into trivial_constraints
michaelbynum Apr 15, 2026
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
9 changes: 2 additions & 7 deletions pyomo/contrib/solver/common/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -570,15 +570,10 @@ def _solution_handler(
legacy_results._smap_id = id(symbol_map)
delete_legacy_soln = True
if load_solutions:
if hasattr(model, 'dual') and model.dual.import_enabled():
for con, val in results.solution_loader.get_duals().items():
model.dual[con] = val
if hasattr(model, 'rc') and model.rc.import_enabled():
for var, val in results.solution_loader.get_reduced_costs().items():
model.rc[var] = val
results.solution_loader.load_import_suffixes()
elif results.incumbent_objective is not None:
delete_legacy_soln = False
for var, val in results.solution_loader.get_primals().items():
for var, val in results.solution_loader.get_vars().items():
legacy_soln.variable[symbol_map.getSymbol(var)] = {'Value': val}
if hasattr(model, 'dual') and model.dual.import_enabled():
for con, val in results.solution_loader.get_duals().items():
Expand Down
182 changes: 160 additions & 22 deletions pyomo/contrib/solver/common/solution_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,35 @@
# software. This software is distributed under the 3-clause BSD License.
# ____________________________________________________________________________________

from typing import Sequence, Dict, Optional, Mapping
from __future__ import annotations

from typing import Sequence, Dict, Optional, Mapping, List, Any

from pyomo.core.base.constraint import ConstraintData
from pyomo.core.base.var import VarData
from pyomo.core.staleflag import StaleFlagManager
from pyomo.core.base.suffix import Suffix
from .util import NoSolutionError


def load_import_suffixes(
pyomo_model, solution_loader: SolutionLoaderBase, solution_id=None
):
dual_suffix = None
rc_suffix = None
for suffix in pyomo_model.component_objects(Suffix, descend_into=True, active=True):
if not suffix.import_enabled():
continue
if suffix.local_name == 'dual':
dual_suffix = suffix
elif suffix.local_name == 'rc':
rc_suffix = suffix
if dual_suffix is not None:
dual_suffix.clear()
dual_suffix.update(solution_loader.get_duals(solution_id=solution_id))
if rc_suffix is not None:
rc_suffix.clear()
rc_suffix.update(solution_loader.get_reduced_costs(solution_id=solution_id))


class SolutionLoaderBase:
Expand All @@ -21,24 +45,70 @@ class SolutionLoaderBase:
Intent of this class and its children is to load the solution back into the model.
"""

def load_vars(self, vars_to_load: Optional[Sequence[VarData]] = None) -> None:
def get_solution_ids(self) -> List[Any]:
"""
If there are multiple solutions available, this will return a
list of the solution ids which can then be used with other
methods like `load_solution`. If only one solution is
available, this will return [None]. If no solutions
are available, this will return None

Returns
-------
solutions_ids: List[Any]
The identifiers for multiple solutions
"""
return NotImplemented

def get_number_of_solutions(self) -> int:
"""
Returns
-------
num_solutions: int
Indicates the number of solutions found
"""
return NotImplemented

def load_solution(self, solution_id=None):
"""
Load the solution (everything that can be) back into the model

Parameters
----------
solution_id: Any
If there are multiple solutions, this specifies which solution
should be loaded. If None, the default solution will be used.
"""
# this should load everything it can
self.load_vars(solution_id=solution_id)
self.load_import_suffixes(solution_id=solution_id)

def load_vars(
self, vars_to_load: Optional[Sequence[VarData]] = None, solution_id=None
) -> None:
"""
Load the solution of the primal variables into the value attribute of the variables.
Load the solution of the primal variables into the value attribute
of the variables.

Parameters
----------
vars_to_load: list
The minimum set of variables whose solution should be loaded. If vars_to_load
is None, then the solution to all primal variables will be loaded. Even if
vars_to_load is specified, the values of other variables may also be
loaded depending on the interface.
The minimum set of variables whose solution should be loaded. If
vars_to_load is None, then the solution to all primal variables
will be loaded. Even if vars_to_load is specified, the values of
other variables may also be loaded depending on the interface.
solution_id: Optional[Any]
If there are multiple solutions, this specifies which solution
should be loaded. If None, the default solution will be used.
"""
for var, val in self.get_primals(vars_to_load=vars_to_load).items():
for var, val in self.get_vars(
vars_to_load=vars_to_load, solution_id=solution_id
).items():
var.set_value(val, skip_validation=True)
StaleFlagManager.mark_all_as_stale(delayed=True)

def get_primals(
self, vars_to_load: Optional[Sequence[VarData]] = None
def get_vars(
self, vars_to_load: Optional[Sequence[VarData]] = None, solution_id=None
) -> Mapping[VarData, float]:
"""
Returns a ComponentMap mapping variable to var value.
Expand All @@ -48,18 +118,21 @@ def get_primals(
vars_to_load: list
A list of the variables whose solution value should be retrieved. If vars_to_load
is None, then the values for all variables will be retrieved.
solution_id: Optional[Any]
If there are multiple solutions, this specifies which solution
should be retrieved. If None, the default solution will be used.

Returns
-------
primals: ComponentMap
Maps variables to solution values
"""
raise NotImplementedError(
f"Derived class {self.__class__.__name__} failed to implement required method 'get_primals'."
f"Derived class {self.__class__.__name__} failed to implement required method 'get_vars'."
)

def get_duals(
self, cons_to_load: Optional[Sequence[ConstraintData]] = None
self, cons_to_load: Optional[Sequence[ConstraintData]] = None, solution_id=None
) -> Dict[ConstraintData, float]:
"""
Returns a dictionary mapping constraint to dual value.
Expand All @@ -69,16 +142,19 @@ def get_duals(
cons_to_load: list
A list of the constraints whose duals should be retrieved. If cons_to_load
is None, then the duals for all constraints will be retrieved.
solution_id: Optional[Any]
If there are multiple solutions, this specifies which solution
should be retrieved. If None, the default solution will be used.

Returns
-------
duals: dict
Maps constraints to dual values
"""
raise NotImplementedError(f'{type(self)} does not support the get_duals method')
return NotImplemented

def get_reduced_costs(
self, vars_to_load: Optional[Sequence[VarData]] = None
self, vars_to_load: Optional[Sequence[VarData]] = None, solution_id=None
) -> Mapping[VarData, float]:
"""
Returns a ComponentMap mapping variable to reduced cost.
Expand All @@ -88,45 +164,107 @@ def get_reduced_costs(
vars_to_load: list
A list of the variables whose reduced cost should be retrieved. If vars_to_load
is None, then the reduced costs for all variables will be loaded.
solution_id: Optional[Any]
If there are multiple solutions, this specifies which solution
should be retrieved. If None, the default solution will be used.

Returns
-------
reduced_costs: ComponentMap
Maps variables to reduced costs
"""
raise NotImplementedError(
f'{type(self)} does not support the get_reduced_costs method'
)
return NotImplemented

def load_import_suffixes(self, solution_id=None):
"""
Parameters
----------
solution_id: Optional[Any]
If there are multiple solutions, this specifies which solution
should be loaded. If None, the default solution will be used.
"""
return NotImplemented


class NoSolutionSolutionLoader(SolutionLoaderBase):
def __init__(self) -> None:
pass

def get_solution_ids(self) -> List[Any]:
return []

def get_number_of_solutions(self) -> int:
return 0

def load_solution(self, solution_id=None):
raise NoSolutionError()

def load_vars(
self, vars_to_load: Sequence[VarData] | None = None, solution_id=None
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Shouldn't we use Optional here?

) -> None:
raise NoSolutionError()

def get_vars(
self, vars_to_load: Sequence[VarData] | None = None, solution_id=None
) -> Mapping[VarData, float]:
raise NoSolutionError()

def get_duals(
self, cons_to_load: Sequence[ConstraintData] | None = None, solution_id=None
) -> Dict[ConstraintData, float]:
raise NoSolutionError()

def get_reduced_costs(
self, vars_to_load: Sequence[VarData] | None = None, solution_id=None
) -> Mapping[VarData, float]:
raise NoSolutionError()

def load_import_suffixes(self, solution_id=None):
raise NoSolutionError()


class PersistentSolutionLoader(SolutionLoaderBase):
"""
Loader for persistent solvers
"""

def __init__(self, solver):
def __init__(self, solver, pyomo_model):
self._solver = solver
self._valid = True
self._pyomo_model = pyomo_model

def _assert_solution_still_valid(self):
if not self._valid:
raise RuntimeError('The results in the solver are no longer valid.')

def get_primals(self, vars_to_load=None):
def get_solution_ids(self) -> List[Any]:
self._assert_solution_still_valid()
return self._solver._get_primals(vars_to_load=vars_to_load)
return super().get_solution_ids()

def get_number_of_solutions(self) -> int:
self._assert_solution_still_valid()
return super().get_number_of_solutions()

def get_vars(self, vars_to_load=None, solution_id=None):
self._assert_solution_still_valid()
return self._solver._get_primals(
vars_to_load=vars_to_load, solution_id=solution_id
)

def get_duals(
self, cons_to_load: Optional[Sequence[ConstraintData]] = None
self, cons_to_load: Optional[Sequence[ConstraintData]] = None, solution_id=None
) -> Dict[ConstraintData, float]:
self._assert_solution_still_valid()
return self._solver._get_duals(cons_to_load=cons_to_load)

def get_reduced_costs(
self, vars_to_load: Optional[Sequence[VarData]] = None
self, vars_to_load: Optional[Sequence[VarData]] = None, solution_id=None
) -> Mapping[VarData, float]:
self._assert_solution_still_valid()
return self._solver._get_reduced_costs(vars_to_load=vars_to_load)

def load_import_suffixes(self, solution_id=None):
load_import_suffixes(self._pyomo_model, self, solution_id=solution_id)

def invalidate(self):
self._valid = False
Loading
Loading