Skip to content

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

Merged
FabianHofmann merged 9 commits into
masterfrom
fix/1170-keep-empty-rows
Nov 19, 2026
Merged

FabianHofmann merged 9 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 drops sanitize_zeros. linopy skips terms with a zero coefficient everywhere, and the solver handles near-zero ones (HiGHS ignores |c| ≤ 1e-9 and logs it). solve no longer prunes the user's model in place. Passing the keyword warns (FutureWarning) and has no effect. Constraint.sanitize_zeros and Constraints.sanitize_zeros are removed too; densify_terms() compacts expression terms.
  • The restrict error no longer says "folding the dangling terms" for a row that had no term in the model.
  • No warning for rows that cannot hold: the solver reports the model infeasible.
  • Not changed: 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, a breaking-change line for sums over all-absent terms, a removed-API line for sanitize_zeros).

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
  • After dropping sanitize_zeros and the warning: pytest --doctest-modules linopy test with HiGHS and Gurobi 14 (restricted license): 5888 passed, 139 skipped, 2 failed. Both failures are test_quadratic_model_wo_constraint[gurobi-mps-*] (Gurobi 14 MPS read error) and fail the same way on master. ruff check, ruff format --check, mypy linopy: clean.
  • Before that change: 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), except the expected passes: restrict "outside"/"dropped", indicator LP, IIS except Xpress LP, 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); solve(sanitize_zeros=...) warns and keeps a 1e-11 term; a persistent update of a row with a stored zero term and with no term; restrict, persistent, IIS, dualize and indicator cases.
  • Solver matrix (6 solvers; lp, mps, direct): 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).
  • 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 (26047a3) with master (912a8c9)

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.
linopy skips terms with a zero coefficient everywhere, and the solver
handles near-zero ones. Model.solve no longer prunes |c| <= 1e-10 in
place; passing sanitize_zeros warns (FutureWarning) and has no effect.
Constraint(s).sanitize_zeros() stays as an explicit step.

Remove the 'cannot hold' warning with its Model.to_file wrapper: the
solver reports such a model infeasible.
The method only pruned |c| <= 1e-10 in place: exact zeros are skipped
everywhere, densify_terms() compacts expression terms, and the solver
handles near-zero coefficients. Tests that built rows without a term
through it now build them directly.
@FabianHofmann
FabianHofmann marked this pull request as ready for review November 19, 2026 13:15
FabianHofmann pushed a commit that referenced this pull request Nov 19, 2026
Follow #1183: spec no longer refuses, warns about or drops a present row
without a variable term. It builds the row, and the solver judges it, so
0 == 5 makes the model infeasible. Absent rows stay absent: a where: mask,
absence spread outside a sum, a masked variable or a vacated shift.

This removes the drop and warn machinery of this PR: EmptiedRowWarning,
the DROPPED_ROW_TOL check, the pruning of terms the data zeroes and the
objective re-run it needed. It also removes master's _check_live refusal.
to_polars takes an optional keyword empty_var. Without it the frame is
as before. The LP writer passes it, so a row without a term keeps one
zero term.
@FabianHofmann
FabianHofmann merged commit 99e123d into master Nov 19, 2026
21 checks passed
@FabianHofmann
FabianHofmann deleted the fix/1170-keep-empty-rows branch November 19, 2026 21:42
FabianHofmann pushed a commit that referenced this pull request Nov 21, 2026
Constraints.sanitize_zeros no longer exists (#1183).
FabianHofmann added a commit that referenced this pull request Nov 22, 2026
…t hold (#1198)

* feat(spec)!: Model.from_spec always builds a sparse model

from_spec forwards Model keyword arguments and now sets sparse=True
itself; sparse=False raises a ValueError. The builder tries the term path
for every declaration outside terms.eager_only(), so terms.active no
longer reads the model. Tests that ran a dense spec build compare the term
path with the eager path instead.

* fix(spec)!: keep a constraint row without a variable term where it holds

A row the data leaves without a term of nonzero coefficient is the
constant row 'lhs sense rhs'. linopy keeps such a row since #1183, so
from_spec now keeps it where it holds, also in a constraint all of whose
rows are constant, and raises a SpecDataError naming the row only where
it does not; the warning for a row whose sides are equal goes. The term
path and the eager path judge a row alike, a zero coefficient counting
as no term, so the eager fallback for such a row goes too.

* docs: release note PR number for #1198

* refactor(spec): drop the dense branch of constant_term

Model.from_spec builds a sparse model only, and a term without a variable
is made only while it builds, so constant_term never meets a dense model.

* test(spec): run the eager path where dense builds covered it

The dense spec builds that went with sparse-only from_spec were the only
ones to reach a few eager-path branches: a sum over a dimension without
members, cases that leave only data, and a dimension dim_order spreads
onto a term. Those tests now also build with terms.eager_only(). A new
test frames a quadratic expression, which the term path leaves to the
eager path, in the name order of its dims.

---------

Co-authored-by: Claude <noreply@anthropic.com>
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