feat: add piecewise linear constraint API#559
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 |
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.