Skip to content

feat(spec)!: build spec models sparse only and keep constant rows that hold - #1198

Merged
FabianHofmann merged 5 commits into
perf/spec-term-tablefrom
feat/spec-sparse-only
Nov 22, 2026
Merged

FabianHofmann merged 5 commits into
perf/spec-term-tablefrom
feat/spec-sparse-only

Conversation

@FabianHofmann

Copy link
Copy Markdown
Collaborator

Changes proposed in this Pull Request

Note

The following content was generated by AI.

Stacked on #1197. Two breaking changes to the unreleased Model.from_spec, a step towards dropping the eager build path:

  • Sparse only. from_spec always builds Model(sparse=True); sparse=False raises a ValueError. Every declaration now tries the term path, so the dense-model fallback to the eager path is gone.
  • Rows without a variable term. A row the data leaves without a term of nonzero coefficient is the constant row lhs sense rhs. It is kept where it holds (0 <= 5), as linopy keeps such rows since fix: a constraint row without a nonzero term reaches the solver #1183, and refused with a SpecDataError naming the row where it does not (0 >= 5). Both build paths judge a row alike, so the dead-row fallback to the eager path is gone too.

Eager fallbacks over PyPSA's test suite (PyPSA#2181): 902 → 414 declarations, now only window sums (409) and products of two variables (5). The built models on 4 PyPSA networks are identical to #1197's (strict compare); build speed is unchanged within noise.

Notes
  • Behaviour change: a violated constant row such as 0*x >= 1 used to reach the solver (infeasible on both paths); now both paths raise at build time.
  • The violation check is exact, with no tolerance.
  • PyPSA needs to drop its dense spec builds (--linopy-dense, two dense test parameters, the dense CI jobs); a follow-up PR on top of PyPSA#2181 does that.
  • Next steps towards removing the eager build path: window sums, quadratic tables, the remaining small gaps.

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

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.
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.
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.
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.
@FabianHofmann
FabianHofmann marked this pull request as ready for review November 22, 2026 17:43
@FabianHofmann
FabianHofmann merged commit d04e922 into perf/spec-term-table Nov 22, 2026
18 checks passed
@FabianHofmann
FabianHofmann deleted the feat/spec-sparse-only branch November 22, 2026 17:43
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.

2 participants