-
Notifications
You must be signed in to change notification settings - Fork 72
feat: add piecewise linear constraint API #559
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
Add `add_piecewise_constraint` method to Model class that creates piecewise linear constraints using SOS2 formulation. Features: - Single Variable or LinearExpression support - Dict of Variables/Expressions for linking multiple quantities - Auto-detection of link_dim from breakpoints coordinates - NaN-based masking with skip_nan_check option for performance - Counter-based name generation for efficiency The SOS2 formulation creates: 1. Lambda variables with bounds [0, 1] for each breakpoint 2. SOS2 constraint ensuring at most two adjacent lambdas are non-zero 3. Convexity constraint: sum(lambda) = 1 4. Linking constraints: expr = sum(lambda * breakpoints)
|
It could be nice to have a linear formulation as well to cover the strictly monotonic case |
|
@RobbieKiwi Yes why not? |
|
Yeah makes more sense for a future PR. I was thinking continuous with filling-order constraints, I don't like relying on the objective to make the mapping work correctly |
Ok perfect. We should bundle those under a "method" or sth. Just to have it linked here: FBumann#10 |
|
@RobbieKiwi @FabianHofmann Is there a blocker to this PR? Or is the blocker in #549 ? |
Create dedicated documentation page for the add_piecewise_constraints method covering the SOS2 (convex combination) formulation with math, usage examples, generated variables/constraints reference, and solver compatibility. Update index.rst toctree, api.rst, and add cross-ref from sos-constraints.rst. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
I added some docs. If you want to, we can also merge all 3 as a single new feature. WOuld add full generalized support for piecewise linear formulation, both for linear, sos2 and dicontinous formulations |
|
By the way there is an issue with readthedocs, nothing to do with your changes, I am fixing it here #574 |
|
|
||
| Examples | ||
| -------- | ||
| Single variable piecewise constraint: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the purpose of a single variable piecewise constraint?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought fording a variable into such bounds would be useful to someone.
Im not completely sure about the actual use case. But i don't see why we should strip it.
|
@RobbieKiwi if you prefer, we can make a bigger combined feature PR: #576 |
This PR looks good to me, but I can't approve PRs you need to check with the others |
Add Piecewise Linear Constraint API
Summary
Add
add_piecewise_constraintsmethod toModelclass for creating piecewise linear constraints using SOS2 formulation.Features
link_dimfrom breakpoints coordinatesskip_nan_checkoption for performanceDesign decision
Is this something the linopy package wants to add? And if, do you want to see this as a helper method, that adds variables and constraints itself, or store sth like a
PWL-Constraintobject?This implementation adds variables and (sos-)constraints, which i prefer over adding other classes of constraints.
This kind of relies on #549 to be widely uses, as it uses sos2 internally
As a convention, we could establish that such internally added variables/constraints get a prefix starting with an double underscore. This would simplify filtering.
Usage
SOS2 Formulation
The method creates:
Σλ = 1expr = Σ(λ × breakpoint)for each expressionKnown Limitations
model.variables[f"{name}_lambda"]Future Work
Checklist
doc.doc/release_notes.rstof the upcoming release is included.