Skip to content

Expressions container - #882

Open
brynpickering wants to merge 8 commits into
masterfrom
feat/expression-container
Open

Expressions container#882
brynpickering wants to merge 8 commits into
masterfrom
feat/expression-container

Conversation

@brynpickering

Copy link
Copy Markdown
Contributor

Partially closes #881

Note

Initial implementation in #871 was human-generated. I've cherry-picked that and then iterated on it with Claude to close some implementation gaps and to add tests/docs.

Note

This does not address the concept of lazy expressions from #881 as I wanted to keep this PR lightweight and less controversial.

Changes proposed in this Pull Request

Model.add_expressions registers a LinearExpression or QuadraticExpression under a name (auto-generated as expr0, expr1, ... if omitted), accessible afterwards via Model.expressions (an Expressions container mirroring Model.variables/Model.constraints) and removable via Model.remove_expressions.
Named expressions are persisted by Model.to_netcdf/linopy.read_netcdf and preserved by Model.copy, copy.copy, copy.deepcopy, and pickling.

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.rst of the upcoming release is included.
  • I consent to the release of this PR's code under the MIT license.

@FabianHofmann FabianHofmann left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is extremely non-controversial. let's pull this in. Just to test potential future regressions let's also catch the case that returned and stored expression are the same (as suggested below)

Comment thread test/test_expressions.py
Comment thread test/test_expressions.py
@codspeed-hq

codspeed-hq Bot commented Aug 10, 2026

Copy link
Copy Markdown

Merging this PR will improve performance by 21.24%

⚡ 4 improved benchmarks
✅ 171 untouched benchmarks
⏩ 175 skipped benchmarks1

Performance Changes

Mode Benchmark BASE HEAD Efficiency
Memory test_to_lp[masked-n=100] 2.7 MB 2.1 MB +29.05%
Memory test_to_lp[milp-n=50] 2.6 MB 2.1 MB +28.96%
Memory test_to_lp[storage-n=250] 35.8 MB 30.9 MB +15.79%
Memory test_to_lp[expression_arithmetic-n=250] 45.8 MB 40.8 MB +12.14%

Tip

Curious why this is faster? Comment @codspeedbot explain why this is faster on this PR, or directly use the CodSpeed MCP with your agent.


Comparing feat/expression-container (007ca79) with master (09e11ad)

Open in CodSpeed

Footnotes

  1. 175 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.

brynpickering and others added 2 commits August 11, 2026 17:21
Co-authored-by: Fabian Hofmann <fab.hof@gmx.de>
@brynpickering

Copy link
Copy Markdown
Contributor Author

@FabianHofmann @FBumann I can't really understand the memory benchmarking results. It's showing a regression in one and massive improvements in others. I can understand regressions if the expressions container is being used but I can't understand performance improvements: there should be none.

@FabianHofmann

FabianHofmann commented Aug 11, 2026

Copy link
Copy Markdown
Collaborator

@brynpickering I think the benchmarks are in a dirty state where the reference run is from master before several package version updates and not the latest master. At least this is how I understood @FBumann. So let's ignore it and just look at the other tests

@FBumann

FBumann commented Aug 11, 2026

Copy link
Copy Markdown
Collaborator

Merging master into here should resolve this
But I'm having some flakiness issues with codspeed in lpspec too at the moment, so might be a problem on their side

@FabianHofmann
FabianHofmann self-requested a review August 12, 2026 05:20
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.

Attach expressions (incl. lazy ones) to linopy.Model

3 participants