Skip to content

fix(spec): check a row the data empties against its constants - #1164

Open
FabianHofmann wants to merge 8 commits into
masterfrom
ccr-1868f888-l1tlxg-1147
Open

FabianHofmann wants to merge 8 commits into
masterfrom
ccr-1868f888-l1tlxg-1147

Conversation

@FabianHofmann

@FabianHofmann FabianHofmann commented Nov 16, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #1147.

Changes proposed in this Pull Request

Note

The following content was generated by AI.

The rule is now: a sum of absent terms counts as 0, and a row the data leaves without a variable term is checked against its constants. It reads constant sense constant. Where it holds, the row is dropped with the new linopy.EmptiedRowWarning. Where it cannot, from_spec raises and quotes the row, e.g. constraint 'r': 1 row(s) ... cannot hold, the first at t=0: 0 == 5. This replaces the earlier _stated walk and answers the issue's open question: a shift without edge= behaves like one with edge=0 inside a sum.

  • Ported from 1ea671d (spec/prune-unbuilt): _emptied/_check_emptied/_quoted in builder.py, Context.zeroes, _without_zeroed_terms in evaluator.py (a term the data scales by 0 is pruned, so c * x >= 5 with c = 0 is refused instead of built as 0 x >= 5), EmptiedRowWarning, and its tests.
  • From 7ea5b20: a product of two terms converts only its constant side to data, so the product stays a term.
  • Left out on purpose:
    • 1ea671d's _variable hunk. It turns a fully masked variable into a constant, so an objective over it cannot be solved (No objective has been set). Without it, every test passes.
    • unfused(), _sides and the fused-sum machinery (da89882, eb922bb, 7238eb2). They do not exist on master. A small _forget_terms helper takes unfused()'s place: before the objective is evaluated again without pruning, it drops the terms the memo keeps. Without it, an objective that reads a named expression the data zeroes cannot be solved.
    • c83fcd5 and the rest of 7ea5b20. Master does not need them.
  • Tolerance: the constants are compared within DROPPED_ROW_TOL (1e-9, absolute), the value Model.restrict uses for rows it drops. So ... + 0.1 + 0.2 == 0.3 holds. Imported from linopy.restrict.matrix, where master has it now. fix: refuse a row emptied of all terms that cannot hold #1175 moves it to linopy/constants.py, so the import must follow.
  • Refusal hint: "Supply the rows of the variables" became "Fix the data that empties the row". Integral constants are quoted without a fraction (0 == 5, not 0.0 == 5.0).
  • History: merged master, reverted the PR's two commits (ba8d7a6, 0d1744e), then added the port, a tolerance commit and a docs commit. Nothing was force-pushed.

Behaviour changes

  • Rows that used to be refused now warn and drop where they hold: edge=0 and no-edge= shifts inside a sum, a sum over an empty dimension (<= 0), a missing: 0 row such as 0 <= relmax, and a row whose left side is emptied while the right side keeps variables.
  • Rows that used to build silently are now refused when they cannot hold, e.g. sum(c * x, over=k) >= 5 with c = 0. On master this solved "optimal".
  • Rows that hold in this case, e.g. c * x <= 5 with c = 0, now emit EmptiedRowWarning. Models with many zero coefficients can see more warnings.
  • A bare shift(x) outside a sum still leaves its row absent, with no warning, as before.

#1174 (a sum_back window over absent terms is 0) works with this PR: with both merged, an all-absent window row warns and is dropped, or is refused (0 >= 5). They merge with a single conflict, two adjacent release-note lines (keep both). 585 spec tests pass on the merge. #1174's note that such rows are refused by _check_live is stale once this lands.

Validation and reviewer notes
  • pytest test/test_spec*.py test/test_objective.py: 608 passed, 6 skipped. Full pytest test/: 6337 passed. The only failures are test_quadratic_model_wo_constraint[gurobi-mps-*], which also fail on master: highspy 1.16.0 writes an MPS file that Gurobi cannot read.
  • ruff check ., ruff format --check . (ruff 0.16.10), mypy linopy: clean.
  • Tests. All new or changed tests fail on master, except the guard test_an_objective_the_data_zeroes_still_solves, which also passes on master. That test protects the objective re-run: its named case fails without _forget_terms.
    • test_a_row_the_data_empties_is_checked_against_its_constants covers the issue's two cases (edge=0, load[0]=0 warns and drops; load[0]=5 is refused with 0 == 5), the same without edge=, zero coefficients, the written order, both sides emptied, and a new rounding case for the tolerance.
    • test_a_row_is_emptied_only_where_neither_side_holds_a_variable[left-against-a-sum] is the earlier PR's left-side-only gap, sum(shift(y, edge=0), over=k) == sum(y, over=k) - load. It is fixed here, so it needs no separate issue.
    • test_a_dimension_with_no_members_empties_a_summed_row: <= 0 now warns and drops, and >= 5 is refused with 0 >= 5.
    • The earlier PR's four tests are removed. Two of them expected refusals that this rule reverses.
  • Docs: overview.md and spec-data.md now describe the check instead of the old refusal. EmptiedRowWarning is listed in doc/api/warnings.md, and there is one release-note line. The release notes' "Limits" line now says a row raises only where it cannot hold.
  • Unrelated, pre-existing on master: if the objective has no live variable term, a solve through an LP file fails while it parses the solution (invalid literal for int() ... 'x'). Direct-API solves work. The guard test avoids this case.

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 row that the data empties of every variable term was dropped with a
"trivially true" warning only when every variable declared a missing:
number. A row emptied by a shift's stated edge, e.g.
sum(shift(x, along=t, offset=1, edge=0), over=k) == load at the first t,
was refused even when it read 0 == 0.

Count such a row as valued when the spec states every value it can be
left with: variables without where: (or with a missing: number), shifts
with an edge= number, and data that is never absent. A valued row whose
sides differ is now refused as one that cannot hold, naming the first
comparison (e.g. 0 == 5). A row emptied by an absent term, such as a
shift without edge=, a window, a lookup or a sum over no member, is still
refused as before.

Fixes #1147

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01F4nx8L63c3o8u6QQvr6TGN
@codspeed

codspeed Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 181 untouched benchmarks
⏩ 181 skipped benchmarks1


Comparing ccr-1868f888-l1tlxg-1147 (68fdc0c) 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. ↩

A shift whose offset parameter can be absent, and a window, keep an
emptied row refused even under edge=0.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01F4nx8L63c3o8u6QQvr6TGN
Port of 1ea671d from spec/prune-unbuilt onto master.
A sum of absent terms counts as 0.
A row the data leaves without a variable term reads constant sense constant.
It is dropped with linopy.EmptiedRowWarning where it holds and refused, quoting the row, where it cannot.
A term the data scales by zero is pruned, so 0 * x >= 5 is refused instead of built.
The objective is evaluated again without that pruning when it would lose every variable.
1ea671d's _variable hunk is left out: it made an objective over a fully masked variable unsolvable.
The product of two terms converts one constant side only, as in 7ea5b20.
A row emptied of variable terms compared its constants exactly, so 0.1 + 0.2 == 0.3 was refused.
It now holds within DROPPED_ROW_TOL, the tolerance Model.restrict uses for rows it drops.
An integral constant is quoted without a fraction, e.g. 0 == 5.
The refusal hint points to the data that empties the row.
Tests cover a left side emptied against a right side that keeps variables, and an objective the data zeroes, also through a named expression.
A row the data leaves without a variable term is dropped with EmptiedRowWarning where it holds and refused where it cannot.
The overview and the spec data guide described the old refusal.
EmptiedRowWarning is listed with the other warnings.
@FabianHofmann FabianHofmann changed the title fix(spec): drop a row emptied by a stated edge value when it holds fix(spec): check a row the data empties against its constants Nov 18, 2026
@FabianHofmann
FabianHofmann marked this pull request as ready for review November 18, 2026 11:12

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.

spec: a row emptied by the data is refused even when it holds

2 participants