linopy.constraints.Constraint.update

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 (mirroring add_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 to rhs so c.lhs stays pure-variable. Cannot be combined with coeffs / variables. Sets the internal _coef_dirty flag.

  • rhs (ExpressionLike / VariableLike / ConstantLike, optional) – New right-hand side.

    • Constant rhs (scalar, array, DataArray) → assigned directly to c.rhs; c.lhs is untouched.

    • Variable / Expression rhs → rearranged onto the lhs to preserve the invariant that c.rhs is constant-only, matching add_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 than lhs=; sets _coef_dirty.

  • variables (Variable, optional) – Replace variable label array (same sparsity / term structure). Lower-level than lhs=; sets _coef_dirty.

    A raw DataArray of integer labels is still accepted for back-compat but emits a FutureWarning — pass a Variable instead. The DataArray path will be removed in a future release.

Returns:

Constraintself for chaining.