Skip to content

fix: a constraint row without a nonzero term reaches the solver - #1183

Draft
FabianHofmann wants to merge 4 commits into
masterfrom
fix/1170-keep-empty-rows
Draft

FabianHofmann wants to merge 4 commits into
masterfrom
fix/1170-keep-empty-rows

Conversation

@FabianHofmann

@FabianHofmann FabianHofmann commented Nov 18, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #1170.
Supersedes #1175.

Changes proposed in this Pull Request

Note

The following content was generated by AI.

Rule: linopy never silently drops a present (labelled) constraint row. A row without a nonzero term goes to the solver as 0 sign rhs, so 0 * x >= 5 is infeasible instead of "optimal".

  • Constraint.active_row_mask is "label set". The matrices, ncons, clabels, duals, persistent snapshots and restrict windows follow from it.
  • The LP writer gives such a row one term +0.0 <first variable>. It raises a clear ValueError if the model has no variable. The public Constraint.to_polars() is unchanged: it returns no term for such a row, as on master.
  • Model.solve and Model.to_file warn once, naming the present rows without a nonzero term whose right-hand side cannot hold (capped at display_max_rows). solve warns after sanitize_zeros, so rows that sanitizing empties are named too. It is a hint; the solver decides. Solver._build_file calls linopy.io.to_file, so a file solve warns once, not twice.
  • The restrict error no longer says "folding the dangling terms" for a row that had no term in the model.
  • Not changed: sanitize_zeros (follow-up), the restrict tol drop, linopy/spec.
  • Docs: the rule and its exceptions (sanitize_missings, sanitize_infinities, restrict tol) in convention §12, a migration-table row, restrict.md, release notes (bug fix plus a breaking-change line for sums over all-absent terms).

The fix also corrects five more master bugs, each with a test:

  • GLPK reported an infeasible MIP, or an infeasible LP solved without presolve, as ok. Its status strings INTEGER EMPTY and INFEASIBLE (FINAL) were not mapped, so they became unknown and the parsed solution flipped them to ok. A model whose rows are all empty skips GLPK's presolve and hit this.
  • Xpress IIS named the wrong row on the LP path after a row with only zero coefficients (positions shifted).
  • A persistent update of a row without a stored term raised IndexError (highs, gurobi, xpress).
  • dualize kept such rows while the primal dropped them: primal "optimal", dual "unbounded".
  • Gurobi direct dropped an indicator row with an empty lhs; the LP path kept it.
Validation and reviewer notes
  • Full suite with --doctest-modules linopy test, GLPK 5.0 installed (apt glpk-utils, as CI): 8095 passed, 94 skipped. ruff check ., ruff format --check . (0.16.10): clean.
  • mypy . as CI ([dev,spec]): 6 errors in test/test_spec_api.py, identical on master (from feat(spec): dim_order sets the axis order of a spec build #1179).
  • New test cases: 313 collected in the touched test files. Earlier review run on master: the regression tests fail there ('ok' == 'warning', ncons 1 == 2, optimal == infeasible, IndexError, missing LP rows, no warning), except the expected passes: restrict "outside"/"dropped", indicator LP, IIS except Xpress LP, the no-warning test, and the to_polars test that pins master's output. The infeasible-MILP test fails on master for GLPK only.
  • New tests: 0*x>=5, [0,1]*x>=5, LinearExpression(12)==13 and x.where(mask).sum('j')>=5 over every licensed solver × (lp, lp-polars, mps, direct) × explicit names, plus one sparse-model case on HiGHS direct; an infeasible MILP over every MIP solver; a holding row keeps ncons == 2 and dual 0; LP section with the dummy term per slice (dense and sparse); LP/MPS files keep the row (read back with HiGHS); warning names rows once for to_file (lp, mps) and solve (mock, lp, direct), also rows emptied by sanitize_zeros; restrict, persistent, IIS, dualize and indicator cases.
  • Solver matrix (6 solvers; lp, mps, direct; sanitize_zeros True/False): 0*x>=5, [0,1]*x>=5, 12==13 infeasible in 30/30 runs each; the holding row optimal in 30/30.
  • Verified solvers: HiGHS 1.15.1, Gurobi 14.0.0 (restricted license), CPLEX 22.2, Xpress 10.0 (community license), SCIP (pyscipopt 6.3), COPT 8.0.7, GLPK 5.0. CBC runs in CI only. Not verified: MOSEK, Knitro, MindOpt (no license), cuOpt, cuPDLPx (not installed).
  • Hint cost: 2M rows × 2 constraints, 0.14 s (dense) / 0.09 s (sparse) against 2.9 s for to_file.
  • to_file does not sanitize, so it names only rows that are empty as built.
  • Known exceptions to the rule: sanitize_missings(), sanitize_infinities (default in solve), and a restrict window, which drops a row with no term in the window that holds within tol.

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

linopy dropped a labelled row whose terms were all zero or absent, so
0 * x >= 5 solved "optimal". The matrices, ncons and duals now keep every
row with a label, and the LP writer gives such a row the single term
+0.0 times the first variable.

solve and to_file warn once about such rows whose right-hand side cannot
hold. The fix also corrects Xpress IIS row names on the LP path, a
persistent update of a row without a stored term, and dualize of an
empty row.
@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-keep-empty-rows (cdf9391) with master (ac3eaf7)

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. ↩

…sible

GLPK writes INTEGER EMPTY or INFEASIBLE (FINAL) for a proven infeasible
problem. linopy did not map these, so the status was unknown, the solution
was parsed anyway and the status became ok. An empty row that cannot hold
skips GLPK's presolve and exposed this.
Only the LP writer adds the 0 * first-variable term to a row without a term.
The public Constraint.to_polars returns the same frame as on master.
solve warns after sanitize_zeros, so it also names rows that become empty.
The solver-grid test runs on a dense model; one HiGHS case covers sparse.

This branch has not been deployed

No deployments
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