Skip to content

fix: refuse a row emptied of all terms that cannot hold - #1175

Closed
FabianHofmann wants to merge 2 commits into
masterfrom
fix/1170-emptied-row-cannot-hold
Closed

FabianHofmann wants to merge 2 commits into
masterfrom
fix/1170-emptied-row-cannot-hold

Conversation

@FabianHofmann

@FabianHofmann FabianHofmann commented Nov 18, 2026 •

Copy link
Copy Markdown
Collaborator

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, so c * x >= 5 with c = [0, 1] solved "optimal". Linopy now fails fast:

  • Constraints.check_empty_rows() raises a ValueError when 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.
  • A row that holds (0 >= -5) is still left out silently, as before.
  • The check runs where a build passes through: 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) and solve(remote=...). It runs after sanitize_zeros, so it also catches rows that sanitizing empties.
  • Model.restrict windows 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 by drop do not raise.
  • The sign test is shared with Model.restrict: unsatisfied() moves to linopy/common.py, and DROPPED_ROW_TOL (1e-9) moves to linopy/constants.py.
  • LinearExpression(12, m) == 13 now raises at solve, with the row named. It still builds without error, see the reviewer notes.

Revision

  • 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 (RestrictionError, with the first row named). It respects the selection, drop and tol.
  • A persistent solver's in-place update checks before it applies, so update(rhs=5) on a dropped 0 >= -5 row raises.
  • solve(mock_solve=True) and solve(remote=...) check before they return or ship the model.
  • The check counts no terms and copies none: a new 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.
  • New tests: to_file without a solve (lp, lp-polars, mps; no file is written), the >=, <= and == senses, the tolerance edge (0 == 1e-10 holds, 0 == 1e-8 raises), dense and sparse, with and without sanitize_zeros, mock and remote, restrict and persistent updates.
  • Release note: one line under Solvers, linking this PR.
Validation and reviewer notes

Tests

  • test_optimization.py: the io_api x sanitize_zeros grid for [0, 1] * x >= 5 and 0 * x >= 5; a sense x tolerance x dense/sparse x sanitize_zeros grid; mock and remote (a mocked RemoteHandler is never called); the constant row LinearExpression(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.
  • All new "cannot hold" cases fail on master. On the first revision of this PR, the restrict, persistent (except HiGHS sign, which rebuilds) and mock/remote cases fail.
  • pytest on 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 . and ruff format --check . (0.16.10) pass. mypy linopy reports the same 28 errors as master, all in the Xpress code of linopy/solvers.py.

Open points

  • Remote is not run here (no SSH host). The local check catches every row without a nonzero term before the model is shipped. A row that only the remote sanitize_zeros empties (e.g. 1e-11 * x >= 5) is caught only if the remote host runs a linopy with this fix.
  • Indicator constraints are not checked on purpose: b = 1 -> 0 >= 5 holds with b = 0, so the model is not infeasible. Separate, pre-existing issue: Gurobi's direct API drops that row and lets b = 1, while the LP path keeps it and forces b = 0. It needs its own issue.
  • Behaviour change: a model that solved before can now raise, e.g. 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_zeros still changes the user's model in place. With sanitize_zeros=False the LP writers drop the row anyway, so this is not the root cause.
  • Not done: making LinearExpression(12, m) == 13 raise at build time, as add linear expression from constant #518 does for two constant expressions. Moving the check ahead of the dense shortcut in LinearExpression.to_constraint also 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.
  • The check uses DROPPED_ROW_TOL as an absolute tolerance, because an empty row has no folded terms whose magnitude could scale it.

Checklist

  • AI-generated content is marked (see AGENTS.md).
  • Code changes are sufficiently documented; i.e. new functions contain docstrings and further explanations may be given in doc.
  • Unit tests for new features were added (if applicable).
  • A note for the release notes doc/release-notes.md of the upcoming release is included.
  • I consent to the release of this PR's code under the MIT license.

Generated by Claude Code

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.
@codspeed

codspeed Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 181 untouched benchmarks
⏩ 181 skipped benchmarks1


Comparing fix/1170-emptied-row-cannot-hold (8223ba6) with master (cfedd1e)

Open in CodSpeed

Footnotes

  1. 181 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

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.
@FabianHofmann

Copy link
Copy Markdown
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...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A row whose coefficients are all zero is dropped at solve time, even when it cannot hold (0 >= 5 solves "optimal")

2 participants