-
Notifications
You must be signed in to change notification settings - Fork 79
refactor(solver): validation, sanitize kwargs, and result wiring on Solver path #691
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
Merged
FabianHofmann
merged 9 commits into
refactor/sos-reformulation-methods
from
refactor/solver-from-model-options
May 18, 2026
Merged
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f1858de
refactor(solver): lift feature checks + sanitize/wiring to Solver path
FBumann 6994252
move empty-objective check to Solver.solve() for entry-point parity
FBumann 05a549e
test: parametrize empty-objective check across both entry points
FBumann d34e453
test: collapse parametrize to a single test with two raises blocks
FBumann eacca3d
preserve empty-objective check for remote-solve path in Model.solve()
FBumann 20e636b
move remote-path empty-objective check inside the remote branch
FBumann 691bfac
keep sanitize on Model; Solver.from_model() stays mutation-free
FBumann fb85872
Merge branch 'refactor/sos-reformulation-methods' into refactor/solve…
FBumann 3f765ca
address review: SOS hint, lp_only_solver fixture, assign_result doc
FabianHofmann File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -464,3 +464,112 @@ def test_xpress_gpu_feature_reflects_installed_version() -> None: | |
| assert solvers.Xpress.supports( | ||
| SolverFeature.GPU_ACCELERATION | ||
| ) == _installed_version_in("xpress", ">=9.8.0") | ||
|
|
||
|
|
||
| class TestValidateModelOnBuild: | ||
| """Solver._build() runs solver-feature checks regardless of entry point.""" | ||
|
|
||
| @pytest.mark.skipif( | ||
| "highs" not in solvers.available_solvers, reason="HiGHS not installed" | ||
| ) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we definitely need a helper function that makes these skips snappy |
||
| def test_quadratic_without_qp_support_raises(self) -> None: | ||
| # GLPK is LP-only; if not installed, fall back to a different LP-only path. | ||
| # CBC and GLPK both lack QUADRATIC_OBJECTIVE. | ||
| lp_only = next( | ||
| (s for s in ("glpk", "cbc") if s in solvers.available_solvers), None | ||
| ) | ||
| if lp_only is None: | ||
| pytest.skip("Need an LP-only solver (glpk or cbc) to run this test") | ||
|
|
||
| m = Model() | ||
| x = m.add_variables(name="x", lower=0, upper=10) | ||
| m.add_objective(x * x, sense="min") | ||
|
|
||
| with pytest.raises(ValueError, match="does not support quadratic"): | ||
| solvers.Solver.from_name(lp_only, m, io_api="lp") | ||
|
|
||
| def test_semi_continuous_without_support_raises(self) -> None: | ||
| lp_only = next( | ||
| (s for s in ("glpk", "cbc") if s in solvers.available_solvers), None | ||
| ) | ||
| if lp_only is None: | ||
| pytest.skip("Need an LP-only solver (glpk or cbc) to run this test") | ||
|
|
||
| m = Model() | ||
| x = m.add_variables(name="x", lower=1, upper=10, semi_continuous=True) | ||
| m.add_objective(x) | ||
|
|
||
| with pytest.raises(ValueError, match="does not support semi-continuous"): | ||
| solvers.Solver.from_name(lp_only, m, io_api="lp") | ||
|
|
||
| @pytest.mark.skipif( | ||
| "highs" not in solvers.available_solvers, reason="HiGHS not installed" | ||
| ) | ||
| def test_solve_without_objective_raises(self) -> None: | ||
| m = Model() | ||
| m.add_variables(name="x", lower=0, upper=10) | ||
| # No objective added — both entry points should raise the same error. | ||
| with pytest.raises(ValueError, match="No objective has been set"): | ||
| solvers.Solver.from_name("highs", m, io_api="lp").solve() | ||
| with pytest.raises(ValueError, match="No objective has been set"): | ||
| m.solve("highs") | ||
|
|
||
|
|
||
| class TestSolverDoesNotMutateModel: | ||
| """Solver.from_model() must not mutate model state (sanitize stays Model-level).""" | ||
|
|
||
| @pytest.mark.skipif( | ||
| "highs" not in solvers.available_solvers, reason="HiGHS not installed" | ||
| ) | ||
| def test_from_model_leaves_constraints_untouched(self) -> None: | ||
| m = Model() | ||
| x = m.add_variables(name="x", lower=0, upper=10) | ||
| # Constraint with a near-zero coefficient — would be sanitized away if | ||
| # the Solver path were sanitizing on build. | ||
| m.add_constraints(1e-12 * x + x >= 0, name="c") | ||
| m.add_objective(x) | ||
|
|
||
| before = m.constraints["c"].coeffs.values.copy() | ||
| solvers.Solver.from_name("highs", m, io_api="lp") | ||
| after = m.constraints["c"].coeffs.values | ||
|
|
||
| assert np.allclose(before, after, equal_nan=True), ( | ||
| "Solver.from_model() must not mutate model constraints. " | ||
| "Sanitization is a Model-level primitive; call " | ||
| "model.constraints.sanitize_zeros() / .sanitize_infinities() " | ||
| "explicitly before building." | ||
| ) | ||
|
|
||
|
|
||
| class TestAssignResultWiring: | ||
| """assign_result(result, solver=...) populates model.solver.""" | ||
|
|
||
| @pytest.mark.skipif( | ||
| "highs" not in solvers.available_solvers, reason="HiGHS not installed" | ||
| ) | ||
| def test_assign_result_with_solver_wires_model_solver(self) -> None: | ||
| m = Model() | ||
| x = m.add_variables(name="x", lower=0, upper=10) | ||
| m.add_objective(x, sense="min") | ||
|
|
||
| assert m.solver is None | ||
| solver = solvers.Solver.from_name("highs", m, io_api="lp") | ||
| result = solver.solve() | ||
| m.assign_result(result, solver=solver) | ||
|
|
||
| assert m.solver is solver | ||
| assert m.solver_model is solver.solver_model | ||
|
|
||
| @pytest.mark.skipif( | ||
| "highs" not in solvers.available_solvers, reason="HiGHS not installed" | ||
| ) | ||
| def test_assign_result_without_solver_kwarg_leaves_solver_unset(self) -> None: | ||
| m = Model() | ||
| x = m.add_variables(name="x", lower=0, upper=10) | ||
| m.add_objective(x, sense="min") | ||
|
|
||
| solver = solvers.Solver.from_name("highs", m, io_api="lp") | ||
| result = solver.solve() | ||
| m.assign_result(result) # no solver kwarg | ||
|
|
||
| assert m.solver is None | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
very good catch!