Repository navigation
fix: a constraint row without a nonzero term reaches the solver - #1183
Merged
Merged
Conversation
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.
Merging this PR will not alter performance
Comparing Footnotes
|
…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 was referenced Nov 18, 2026
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
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.
4 of 5 tasks
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.
4 of 5 tasks
FabianHofmann
pushed a commit
that referenced
this pull request
Nov 21, 2026
Constraints.sanitize_zeros no longer exists (#1183).
4 of 5 tasks
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>
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.
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, so0 * x >= 5is infeasible instead of "optimal".Constraint.active_row_maskis "label set". The matrices,ncons,clabels, duals, persistent snapshots and restrict windows follow from it.+0.0 <first variable>. It raises a clearValueErrorif the model has no variable. The publicConstraint.to_polars()is unchanged: it returns no term for such a row, as on master.Model.solvedropssanitize_zeros. linopy skips terms with a zero coefficient everywhere, and the solver handles near-zero ones (HiGHS ignores |c| ≤ 1e-9 and logs it).solveno longer prunes the user's model in place. Passing the keyword warns (FutureWarning) and has no effect.Constraint.sanitize_zerosandConstraints.sanitize_zerosare removed too;densify_terms()compacts expression terms.toldrop,linopy/spec.sanitize_missings,sanitize_infinities, restricttol) 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 forsanitize_zeros).The fix also corrects five more master bugs, each with a test:
ok. Its status stringsINTEGER EMPTYandINFEASIBLE (FINAL)were not mapped, so they becameunknownand the parsed solution flipped them took. A model whose rows are all empty skips GLPK's presolve and hit this.IndexError(highs, gurobi, xpress).dualizekept such rows while the primal dropped them: primal "optimal", dual "unbounded".Validation and reviewer notes
sanitize_zerosand the warning:pytest --doctest-modules linopy testwith HiGHS and Gurobi 14 (restricted license): 5888 passed, 139 skipped, 2 failed. Both failures aretest_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.--doctest-modules linopy test, GLPK 5.0 installed (aptglpk-utils, as CI): 8095 passed, 94 skipped.ruff check .,ruff format --check .(0.16.10): clean.mypy .as CI ([dev,spec]): 6 errors intest/test_spec_api.py, identical on master (from feat(spec): dim_order sets the axis order of a spec build #1179).'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 theto_polarstest that pins master's output. The infeasible-MILP test fails on master for GLPK only.0*x>=5,[0,1]*x>=5,LinearExpression(12)==13andx.where(mask).sum('j')>=5over 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 keepsncons == 2and 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 a1e-11term; a persistent update of a row with a stored zero term and with no term; restrict, persistent, IIS, dualize and indicator cases.0*x>=5,[0,1]*x>=5,12==13infeasible in 30/30 runs each; the holding row optimal in 30/30.sanitize_missings(),sanitize_infinities(default insolve), and a restrict window, which drops a row with no term in the window that holds withintol.Checklist
AGENTS.md).doc.doc/release-notes.mdof the upcoming release is included.Generated by Claude Code