Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
119 commits
Select commit Hold shift + click to select a range
e552439
Add first draft of SCIP persistent solving
Opt-Mucca Mar 18, 2024
6a14f10
Add SCIPPersistent to docs
Opt-Mucca Mar 18, 2024
c107909
Add SCIp to Github action scripts
Opt-Mucca Mar 19, 2024
e00ded8
Remove 5.0.0 specific version. Add conda to workflow
Opt-Mucca Mar 19, 2024
a0b6250
Standardise string formatting to fstring
Opt-Mucca Mar 19, 2024
d0816eb
Add parameter link to docstring
Opt-Mucca Mar 19, 2024
0e11f11
Remove redundant second objective sense check
Opt-Mucca Mar 19, 2024
068ec99
Clean up _post_solve with a helper function for status handling
Opt-Mucca Mar 19, 2024
63af6d8
Remove individual skip_test option
Opt-Mucca Mar 19, 2024
4075d9a
Update pyomo/solvers/plugins/solvers/scip_persistent.py
Opt-Mucca Mar 19, 2024
f55fcc5
Update from the black command
Opt-Mucca Mar 19, 2024
e2b49b2
Merge remote-tracking branch 'origin/main'
Opt-Mucca Mar 19, 2024
91eae7b
Fix typos
Opt-Mucca Mar 19, 2024
5ee2007
Replace trySol via more safe checkSol
Opt-Mucca Mar 19, 2024
f6ff092
Adds support for partial solution loading
Opt-Mucca Mar 20, 2024
e7ac980
Add error handling for setting non-linear objective
Opt-Mucca Mar 21, 2024
c90bb23
Merge branch 'main' into main
Opt-Mucca Apr 10, 2024
2540f65
Remove dual and rc loading for SCIP. Fix bug of ranged rows
Opt-Mucca Apr 19, 2024
9e5d944
Add safe con.body.constant check
Opt-Mucca Apr 19, 2024
f90dfad
Remove slack loading for SCIP
Opt-Mucca Apr 29, 2024
f703d1f
Remove dual loading test for SCIP
Opt-Mucca Apr 29, 2024
5c02d32
Remove slack for suffix in tests
Opt-Mucca Apr 29, 2024
8ebcf88
Remove TODO for nonlinear handling
Opt-Mucca Apr 29, 2024
236427f
Merge branch 'main' into main
mrmundt Apr 30, 2024
30d8cc6
Skip LP_trivial_constraints for SCIP persistent
Opt-Mucca Jun 13, 2024
8d787ae
Merge branch 'main' into main
Opt-Mucca Jun 13, 2024
30e5e65
Add transformation for add_cons with non float/int rhs e.g. np.int
Opt-Mucca Jun 13, 2024
9104a92
Add warning if type is converted. Tidy up logic
Opt-Mucca Jun 13, 2024
a0324af
Merge branch 'main' into main
blnicho Aug 6, 2024
cbcb96d
Merge branch 'main' into main
blnicho Oct 15, 2024
fecd201
Merge branch 'main' into main
mrmundt Oct 29, 2024
1a4663d
Merge branch 'main' into pr/3200
blnicho Nov 13, 2024
cadfe5c
Merge branch 'main' into main
blnicho Nov 19, 2024
f3f2d7c
Fix num. vars and cons from transformed. Silent warm start fail
Opt-Mucca Nov 20, 2024
e4cdc6e
Merge branch 'main' into main
blnicho Dec 10, 2024
f326493
Merge branch 'main' into main
blnicho Jan 27, 2025
d0df2fc
Merge branch 'main' into main
blnicho Feb 13, 2025
7b18354
Add minor changes
Opt-Mucca Feb 19, 2025
859a1b2
Merge branch 'main' into main
mrmundt Feb 19, 2025
9a6c3eb
Merge branch 'main' into main
mrmundt Mar 20, 2025
27e3d10
Change copyright 2024 to 2025
Opt-Mucca Mar 21, 2025
e916067
Merge branch 'main' into main
mrmundt Mar 24, 2025
ed2cf25
Merge branch 'main' into main
mrmundt Apr 16, 2025
5ea53cd
Merge branch 'main' into main
mrmundt Apr 22, 2025
5aba506
Merge branch 'main' into main
mrmundt Jun 4, 2025
8caea1c
Merge branch 'main' into main
blnicho Jul 3, 2025
dbe63c0
Merge branch 'main' into main
mrmundt Jul 8, 2025
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
16ecce0
Merge remote-tracking branch 'opt_mucca/main' into scip
michaelbynum Aug 14, 2025
9e7cd0d
moving scip to contrib solvers
michaelbynum Aug 14, 2025
bf204bb
moving scip to contrib solvers
michaelbynum Aug 14, 2025
dabf031
porting scip interface
michaelbynum Aug 15, 2025
9601768
porting scip interface
michaelbynum Aug 15, 2025
9d2f22a
porting scip interface
michaelbynum Aug 15, 2025
b483729
bugs and tests
michaelbynum Aug 15, 2025
37a31a7
scip direct
michaelbynum Aug 15, 2025
1750fc5
merge in observer_gurobi_refactor
michaelbynum Aug 16, 2025
02f383d
Merge branch 'solver_api' into trivial_constraints
michaelbynum Aug 16, 2025
3e3509c
Merge branch 'trivial_constraints' into scip_port
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
6603db6
Merge branch 'trivial_constraints' into scip_port
michaelbynum Aug 16, 2025
0b84dcc
more expression types for scip
michaelbynum Aug 18, 2025
865b622
Merge remote-tracking branch 'michaelbynum/scip_port' into scip_port
michaelbynum Aug 18, 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
efdcd69
Merge branch 'trivial_constraints' into scip_port
michaelbynum Aug 18, 2025
3180462
capture_fd for scip
michaelbynum Aug 20, 2025
b522edd
Merge remote-tracking branch 'michaelbynum/scip_port' into scip_port
michaelbynum Aug 20, 2025
72912e0
working on persistent interface to scip
michaelbynum Aug 22, 2025
cfa1e91
minor fixes
michaelbynum Sep 1, 2025
a96b518
merge observer_gurobi_refactor into solver_api
michaelbynum Oct 5, 2025
92e77ba
merge solver_api into trivial_constraints
michaelbynum Oct 5, 2025
281ddc3
merge trivial_constraints into scip_port
michaelbynum Oct 5, 2025
e6331df
persistent interface to scip
michaelbynum Oct 6, 2025
98e2c9a
update docs
michaelbynum Oct 6, 2025
f0be4ff
update docs
michaelbynum Oct 6, 2025
7ec95a8
persistent interface to scip
michaelbynum Oct 6, 2025
75903d6
persistent interface to scip
michaelbynum Oct 6, 2025
0051024
updating tests
michaelbynum Oct 6, 2025
b037b9c
forgot to delete/revert some files
michaelbynum Oct 6, 2025
c200e2e
run black
michaelbynum Oct 6, 2025
960c531
typos
michaelbynum Oct 6, 2025
2f99306
merge observer into scip_port
michaelbynum Oct 16, 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
4487a69
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
3d6f8bb
Merge branch 'trivial_constraints' into scip_port
michaelbynum Dec 18, 2025
879ed3a
update scip interface to use observer
michaelbynum Dec 18, 2025
705ec75
Merge remote-tracking branch 'origin/main' into scip_port
michaelbynum Jan 8, 2026
3bfa5cb
run black
michaelbynum Jan 8, 2026
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
3898591
Merge branch 'trivial_constraints' into scip_port
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
33d490a
Merge branch 'solver_api' into scip_port
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
7709a98
Merge branch 'trivial_constraints' into scip_port
michaelbynum Feb 13, 2026
fb38368
Merge branch 'solver_api' into trivial_constraints
michaelbynum Feb 13, 2026
9a581ad
Merge branch 'trivial_constraints' into scip_port
michaelbynum Feb 13, 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
8 changes: 7 additions & 1 deletion .github/workflows/test_branches.yml
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,12 @@ jobs:
|| echo "WARNING: Xpress Community Edition is not available"
python -m pip install --cache-dir cache/pip maingopy \
|| echo "WARNING: MAiNGO is not available"
if [[ ${{matrix.python}} == pypy* ]]; then
echo "skipping SCIP for pypy"
else
python -m pip install --cache-dir cache/pip pyscipopt \
|| echo "WARNING: SCIP is not available"
fi
if [[ ${{matrix.python}} == pypy* ]]; then
echo "skipping wntr for pypy"
else
Expand Down Expand Up @@ -380,7 +386,7 @@ jobs:
TIMEOUT_MSG="TIMEOUT: killing conda install process"
PYVER=$(echo "py${{matrix.python}}" | sed 's/\.//g')
echo "Installing for $PYVER"
for PKG in 'cplex>=12.10' docplex gurobi "$XPRESS" cyipopt pymumps scip; do
for PKG in 'cplex>=12.10' docplex gurobi "$XPRESS" cyipopt pymumps scip pyscipopt; do
echo ""
echo "*** Install $PKG ***"
echo ""
Expand Down
8 changes: 7 additions & 1 deletion .github/workflows/test_pr_and_main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,12 @@ jobs:
|| echo "WARNING: Xpress Community Edition is not available"
python -m pip install --cache-dir cache/pip maingopy \
|| echo "WARNING: MAiNGO is not available"
if [[ ${{matrix.python}} == pypy* ]]; then
echo "skipping SCIP for pypy"
else
python -m pip install --cache-dir cache/pip pyscipopt \
|| echo "WARNING: SCIP is not available"
fi
if [[ ${{matrix.python}} == pypy* ]]; then
echo "skipping wntr for pypy"
else
Expand Down Expand Up @@ -432,7 +438,7 @@ jobs:
TIMEOUT_MSG="TIMEOUT: killing conda install process"
PYVER=$(echo "py${{matrix.python}}" | sed 's/\.//g')
echo "Installing for $PYVER"
for PKG in 'cplex>=12.10' docplex gurobi "$XPRESS" cyipopt pymumps scip; do
for PKG in 'cplex>=12.10' docplex gurobi "$XPRESS" cyipopt pymumps scip pyscipopt; do
echo ""
echo "*** Install $PKG ***"
echo ""
Expand Down
1 change: 1 addition & 0 deletions pyomo/contrib/observer/model_observer.py
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,7 @@ def remove_objectives(self, objs: Collection[ObjectiveData]):
def _check_for_unknown_active_components(self):
for ctype in self._model.collect_ctypes(active=True, descend_into=True):
if not issubclass(ctype, ActiveComponent):
# strangly, this is needed to skip things like Param
continue
if ctype in self._known_active_ctypes:
continue
Expand Down
9 changes: 2 additions & 7 deletions pyomo/contrib/solver/common/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -572,15 +572,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
194 changes: 172 additions & 22 deletions pyomo/contrib/solver/common/solution_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,47 @@
# 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
import logging


logger = logging.getLogger(__name__)


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:
duals = solution_loader.get_duals(solution_id=solution_id)
if duals is NotImplemented:
logger.warning(f'Cannot load duals into suffix')
else:
for k, v in duals.items():
dual_suffix[k] = v
if rc_suffix is not None:
rc = solution_loader.get_reduced_costs(solution_id=solution_id)
if rc is NotImplemented:
logger.warning(f'cannot load duals into suffix')
else:
for k, v in rc.items():
rc_suffix[k] = v


class SolutionLoaderBase:
Expand All @@ -23,24 +59,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]:
"""
Load the solution of the primal variables into the value attribute of the variables.
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: Optional[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.

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 @@ -50,18 +132,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 @@ -71,16 +156,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 @@ -90,45 +178,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
) -> 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
11 changes: 11 additions & 0 deletions pyomo/contrib/solver/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from .solvers.gurobi.gurobi_persistent import GurobiPersistent
from .solvers.gurobi.gurobi_direct_minlp import GurobiDirectMINLP
from .solvers.highs import Highs
from .solvers.scip.scip_direct import ScipDirect, ScipPersistent
from .solvers.knitro.direct import KnitroDirectSolver


Expand All @@ -41,6 +42,16 @@ def load():
SolverFactory.register(
name="highs", legacy_name="highs", doc="Persistent interface to HiGHS"
)(Highs)
SolverFactory.register(
name='scip_direct',
legacy_name='scip_direct_v2',
doc='Direct interface pyscipopt',
)(ScipDirect)
SolverFactory.register(
name='scip_persistent',
legacy_name='scip_persistent_v2',
doc='Persistent interface pyscipopt',
)(ScipPersistent)
SolverFactory.register(
name="knitro_direct",
legacy_name="knitro_direct",
Expand Down
Loading
Loading