Repository navigation
fix: refuse a row emptied of all terms that cannot hold - #1175
Closed
FabianHofmann wants to merge 2 commits into
Closed
FabianHofmann wants to merge 2 commits into
FabianHofmann wants to merge 2 commits into
Conversation
A constraint row without a nonzero term reads 0 <sign> rhs. Matrices and LP files leave such rows out, so 0 * x >= 5 solved optimal. to_file and the direct-API build now raise when such a row cannot hold. The error names the constraint, the row count and the first row. A row that holds is still left out silently. The sign test is shared with Model.restrict.
Merging this PR will not alter performance
Comparing Footnotes
|
Model.restrict windows read the parent's matrices, where rows without terms are already gone. The window now feeds those rows to its own dropped-row check. It respects the selection, drop and tol. A persistent solver's in-place update, mock_solve and remote solve check too. The check reads a live-term flag per row and copies no terms. Tests cover to_file, the three senses, the tolerance edge and sparse rows.
4 of 5 tasks
FabianHofmann
marked this pull request as ready for review
November 18, 2026 11:03
4 of 5 tasks
Collaborator
Author
|
likely won't make as we want to resolve the root cause of it and drop sanitize zeros and skipping zero rows etc... |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Closes #1170.
Changes proposed in this Pull Request
Note
The following content was generated by AI.
A constraint row without a nonzero term reads
0 <sign> rhs. Matrices and LP files leave such rows out, soc * x >= 5withc = [0, 1]solved "optimal". Linopy now fails fast:Constraints.check_empty_rows()raises aValueErrorwhen such a row cannot hold. The message names the constraint, the number of rows and the first row, e.g.Constraint `con` has 1 row(s) without a nonzero term that cannot hold, first: con[0]: +0 ≥ 5.0 >= -5) is still left out silently, as before.io.to_file(lp, lp-polars, mps,Model.to_file),Solver._build_direct(direct API,to_highspy,to_gurobipy, ...), in-place updates of a persistent solver,solve(mock_solve=True)andsolve(remote=...). It runs aftersanitize_zeros, so it also catches rows that sanitizing empties.Model.restrictwindows check a selected row without terms with their own sign test, like a row that loses all terms by folding. Rows outside the window or removed bydropdo not raise.Model.restrict:unsatisfied()moves tolinopy/common.py, andDROPPED_ROW_TOL(1e-9) moves tolinopy/constants.py.LinearExpression(12, m) == 13now raises at solve, with the row named. It still builds without error, see the reviewer notes.Revision
Model.restrictwindows read the parent's matrices, where rows without terms are already gone. The window now feeds those rows to its own dropped-row check (RestrictionError, with the first row named). It respects the selection,dropandtol.update(rhs=5)on a dropped0 >= -5row raises.solve(mock_solve=True)andsolve(remote=...)check before they return or ship the model.Store.has_live_terms. 2M==rows plus 2M>=rows: dense 0.22 s / 106 MB to 0.11 s / 32 MB, sparse 0.09 s / 98 MB to 0.05 s / 32 MB.to_filewithout a solve (lp, lp-polars, mps; no file is written), the>=,<=and==senses, the tolerance edge (0 == 1e-10holds,0 == 1e-8raises), dense and sparse, with and withoutsanitize_zeros, mock and remote, restrict and persistent updates.Validation and reviewer notes
Tests
test_optimization.py: the io_api xsanitize_zerosgrid for[0, 1] * x >= 5and0 * x >= 5; a sense x tolerance x dense/sparse xsanitize_zerosgrid; mock and remote (a mockedRemoteHandleris never called); the constant rowLinearExpression(12, m) == 13; holding rows solve as before.test_io.py::test_to_file_raises_for_empty_row_that_cannot_hold: lp, lp-polars, mps.test_restrict_matrix.py::test_selected_row_without_terms_is_checked_against_its_sign: selected raises; outside the window and dropped solve "optimal".test_persistent_apply_update.py::test_update_raises_for_dropped_row_that_cannot_hold: rhs and sign updates, gurobi, highs, xpress.pyteston test_optimization, test_io, test_model, test_constraint*, test_restrict_matrix, test_windowing_integration, test_persistent*, test_spec*, indicator, SOS, scalar-constraint, matrices, csr, expressions and assign_coords: 3986 passed, 34 skipped. Store, repr, expression and variable tests: 1000 passed.ruff check .andruff format --check .(0.16.10) pass.mypy linopyreports the same 28 errors as master, all in the Xpress code oflinopy/solvers.py.Open points
sanitize_zerosempties (e.g.1e-11 * x >= 5) is caught only if the remote host runs a linopy with this fix.b = 1 -> 0 >= 5holds withb = 0, so the model is not infeasible. Separate, pre-existing issue: Gurobi's direct API drops that row and letsb = 1, while the LP path keeps it and forcesb = 0. It needs its own issue.x.where(mask).fillna(0) >= 5, where a dead term leaves a labelled row without a variable. This is the intended fail-fast. Downstream users such as PyPSA may still hit it.sanitize_zerosstill changes the user's model in place. Withsanitize_zeros=Falsethe LP writers drop the row anyway, so this is not the root cause.LinearExpression(12, m) == 13raise at build time, as add linear expression from constant #518 does for two constant expressions. Moving the check ahead of the dense shortcut inLinearExpression.to_constraintalso raised for a fully absent expression, e.g.x.loc[masked] == 0. An absent row should add nothing (see fix(spec): a constraint of masked variables alone adds no rows #1154). A clean build-time check must tell absent cells from present constant cells.DROPPED_ROW_TOLas an absolute tolerance, because an empty row has no folded terms whose magnitude could scale it.Checklist
AGENTS.md).doc.doc/release-notes.mdof the upcoming release is included.Generated by Claude Code