linopy.model.Model.add_piecewise_formulation#
- Model.add_piecewise_formulation(*pairs, sign='==', method='auto', active=None, name=None)#
Add piecewise linear constraints.
Each positional argument is a
(expression, breakpoints)tuple. All expressions are linked through shared interpolation weights so that every operating point lies on the same segment of the piecewise curve.Example — 2 variables:
m.add_piecewise_formulation( (power, [0, 30, 60, 100]), (fuel, [0, 36, 84, 170]), )
Example — 3 variables (CHP plant):
m.add_piecewise_formulation( (power, [0, 30, 60, 100]), (fuel, [0, 40, 85, 160]), (heat, [0, 25, 55, 95]), )
Sign — inequality bounds:
The
signparameter follows the first-tuple convention:sign="=="(default): all expressions must lie exactly on the piecewise curve (joint equality).sign="<=": the first tuple’s expression is bounded above by its interpolated value; all other tuples are forced to equality (inputs on the curve). Reads as “first expression ≤ f(the rest)”.sign=">=": same but the first is bounded below.
For 2-variable inequality on convex/concave curves,
method="auto"automatically selects a pure-LP tangent-line formulation (no auxiliary variables). Non-convex curves fall back to SOS2/incremental with the sign applied to the first tuple’s link constraint.Example —
fuel ≤ f(power)on a concave curve:m.add_piecewise_formulation( (fuel, y_pts), # bounded output, listed first (power, x_pts), # input, always equality sign="<=", )
- Parameters:
*pairs (
tupleof(expression,breakpoints)) – Each pair links an expression (Variable or LinearExpression) to its breakpoint values. At least two pairs are required. Withsign != EQUALthe first pair is the bounded output; all later pairs are treated as inputs forced to equality.sign (
{"==", "<=", ">="}, default"==") – Constraint sign applied to the first tuple’s link constraint. Later tuples always use equality. See description above.method (
{"auto", "sos2", "incremental", "lp"}, default"auto") – Formulation method."lp"uses tangent lines (pure LP, no variables) and requiressign != EQUALplus a matching-convexity curve with exactly two tuples."auto"picks"lp"when applicable, otherwise"incremental"(monotonic breakpoints) or"sos2".active (
VariableorLinearExpression, optional) – Binary variable that gates the piecewise function. Whenactive=0, all auxiliary variables are forced to zero. Not supported withmethod="lp".With
sign="=="(the default), the output is then pinned to0. Withsign="<="/">=", deactivation only pushes the signed bound to0(the output is ≤ 0 or ≥ 0 respectively) — the complementary bound still comes from the output variable’s own lower/upper. In the common case where the output is naturally non-negative (fuel, cost, heat, …), just setlower=0on that variable: combined with they ≤ 0constraint from deactivation, this forcesy = 0automatically. For outputs that genuinely need both signs you must add the complementary bound yourself (e.g., a big-M couplingywithactive).name (
str, optional) – Base name for generated variables/constraints.
- Returns:
PiecewiseFormulation- Warns:
EvolvingAPIWarning –
add_piecewise_formulationis a newly-added API; details such as thesign/first-tuple convention andactive+ non-equality sign semantics may be refined based on user feedback. Silence withwarnings.filterwarnings("ignore", category=linopy.EvolvingAPIWarning).