Repository navigation
feat(restrict): window a model's flattened matrices - #1036
olivier-lacroix wants to merge 7 commits into
Conversation
c48108a to
366d819
Compare
Merging this PR will not alter performance
Comparing Footnotes
|
408ba31 to
3da2fd1
Compare
|
I'll resolve typing issues Edit: done |
3da2fd1 to
4f7d8ee
Compare
eaa7222 to
f78d4ca
Compare
|
@olivier-lacroix thanks for the PR! I now start to have more time to look into other things in linopy. there were some major features which kept me buisy. will take a look at this soon |
|
@FabianHofmann no worries! I can see the big cleanup happening 😄 |
`Model.restrict` returns a `WindowModel` backed by a slice of the parent's `A`, `b`, `c`, `lb` and `ub` instead of a rebuild of every variable and constraint. A window is a row slice plus a column range; boundary terms are folded at their actual coefficient, and containers are materialised only when their values are read. - `WindowModel.matrices` exposes the window arrays; `solution`, `dual` and `objective` read them back with column, row and objective scaling undone. - `WindowModel.solve` builds the native model from the window and moves it in place through `ModelDiff.from_windows` and the existing per-backend apply hooks. - `advance` re-slices the parent; a shape change rebuilds the solver. - A window that cuts a coupling without `fix` raises `RestrictionError`, naming the constraints. Supported: one selected dimension, dense and CSR parents, masked rows. Not supported: indicators, quadratic objectives, SOS, piecewise, several selected dimensions, subset carriers. `linopy/restrict` is at 100% statement and branch coverage. Co-Authored-By: Claude Code <noreply@anthropic.com>
… test Master removed the autouse semantics fixture with legacy semantics (PyPSA#1041). The windowing equivalence test still requested it and errored at setup on every platform. Co-Authored-By: Claude Code <noreply@anthropic.com>
|
Rebased onto Benchmark rerun on the rebased branch, 5 runs, median:
Windowed assembly now costs 47% over one full build (for that small bench). Restrict still costs 6.5x less than 24 rebuilds. |
9a2af9d to
8048ef2
Compare
Unable to generate the performance reportYour organization has exceeded its Macro Runners budget. Because this run uses tokenless mode, CodSpeed needs to wait for the GitHub Actions workflow to complete to validate the upload. Since the walltime jobs will never run, the workflow stays pending and we are unable to process this run. To run these benchmarks again, update your plan or increase your Macro Runners budget. If you need help, please contact us at support@codspeed.io or on Discord. |
…nt, slices and sparsity checks Windows select container labels via xarray and map them through label_to_pos, replacing per-container position bookkeeping. fix values align to coords and honour column scaling; slices and several dimensions work; in-place updates require an identical sparsity pattern; dropped-row checks use stored signs.
|
@olivier-lacroix I would push here if you don't mind? |
|
still have a major refactor outstanding |
|
Yes of course @FabianHofmann, go for it. |
…dowModelView shim
…e, rebuild on rejected window update
Closes #768. Interested in having rolling horizon patterns in linopy.
Note
The following content was generated by AI.
What
Model.restrictreturns aWindowModelbacked by a slice of the parent'sflattened
A,b,c,lbandub. A window is a row slice plus a columnrange; terms whose variables fall outside the window are folded into the RHS at
their actual matrix coefficient. No container is rebuilt.
window.matricesexposes the window arrays, with the parent labels of its rows and columns.window.solutionandwindow.dualareDatasets, materialised per container on access, with column, row and objective scaling undone.window.solvebuilds the native model from the window and updates it in place throughModelDiff.from_windowsand each backend's existing_apply_*hooks. A backend without in-place updates rebuilds, and logs a warning; passdisallow_rebuild=Trueto raise instead.window.advancere-slices the parent; a shape change rebuilds the solver.Semantics
fixraisesRestrictionError, naming the constraints. ANaNat a dangling label counts as missing.Limits
Model: it cannot take new constraints.Benchmark
Build plus solver assembly, optimisation excluded. 35040 steps, a 1460-step
window, 24 chunks, three variables and three constraints.
hyperfine --warmup 1 --runs 5:Windowing costs 3.4% over one full build and avoids 24 rebuilds. In-process,
excluding import overhead: 0.170, 0.938 and 0.171 s.
Benchmark script
Solver support
Only HiGHS has been exercised by hand.
test_window_solve_across_solversrunsthe same recipe on highs, gurobi, cplex, scip, xpress and mosek, skipping
solvers that are absent, file-only, or installed without a license.
solve_windowrequiresio_api="direct". A file-based backend would have towrite the window to disk on every solve, and has no in-place update.
Tests
test_restrict_matrix.pyandtest_windowing_integration.py, including a solver-parametrized run of the window recipe.linopy/restrictis at 100% statement and branch coverage.AI assistance
Drafted with Claude Code: the implementation, tests and benchmark. Reviewed by hand before opening.