linopy.constraints.Constraint.update#
- Constraint.update(constraint=None, *, lhs=None, rhs=None, sign=None, coeffs=None, variables=None)#
Update the constraint in place.
The only mutation API; setters forward here. Two call shapes:
c.update(x + 5 <= 3)— pass a complete constraint expression (mirroringadd_constraints). Replaces lhs, sign, and rhs at once.c.update(lhs=, rhs=, sign=, coeffs=, variables=)— pass only what you want to change.
Use the keyword form for targeted changes — it skips the unchanged attributes entirely. The positional form always rewrites lhs / sign / rhs (and flips
_coef_dirty), so it is the wrong shape for hot loops that only touch one part:# Hot loop, rhs is the only thing changing per iteration: for k in scenarios: c.update(rhs=rhs_k) # ← targeted, cheap # Same loop written positionally rebuilds lhs every # iteration even though it never changes: for k in scenarios: c.update(big_lhs_expr <= rhs_k) # ← avoid
- Parameters:
constraint (
ConstraintLike, optional) – A complete constraint expression (e.g.x + 5 <= 3). Mutually exclusive with the keyword arguments below.lhs (
ExpressionLike / VariableLike / ConstantLike, optional) – Replace the LHS expression. Any constant part is moved torhssoc.lhsstays pure-variable. Cannot be combined withcoeffs/variables. Sets the internal_coef_dirtyflag.rhs (
ExpressionLike / VariableLike / ConstantLike, optional) – New right-hand side.Constant rhs (scalar, array, DataArray) → assigned directly to
c.rhs;c.lhsis untouched.Variable / Expression rhs → rearranged onto the lhs to preserve the invariant that
c.rhsis constant-only, matchingadd_constraints. This rewrites ``c.lhs``.
Example — the two calls below produce the same final state:
# Form A: explicit, only changes rhs c.update(rhs=5) # Form B: rhs carries a variable, so lhs is rewritten too. # Starting from `2*x <= 3`, this gives `2*x - y <= 5`: c.update(rhs=y + 5)
If you want the rewrite to be loud, use the positional form (
c.update(2*x - y <= 5)) which makes both sides explicit.sign (
SignLike, optional) – New sign. One of"<=" / "==" / ">="(or their< > =aliases).coeffs (
ConstantLike, optional) – Replace coefficient values (same sparsity / term structure). Lower-level thanlhs=; sets_coef_dirty.variables (
Variable, optional) – Replace variable label array (same sparsity / term structure). Lower-level thanlhs=; sets_coef_dirty.A raw
DataArrayof integer labels is still accepted for back-compat but emits aFutureWarning— pass aVariableinstead. The DataArray path will be removed in a future release.
- Returns:
Constraint–selffor chaining.