linopy.constraints.CSRConstraint

linopy.constraints.CSRConstraint#

class linopy.constraints.CSRConstraint(csr, con_labels, rhs, sign, coords, model, name='', cindex=None, dual=None)#

Frozen constraint backed by a CSR sparse matrix.

Parameters:
  • csr (scipy.sparse.csr_array) – Shape (n_flat, model._xCounter). Each row is a flat position in the constraint grid (including masked/empty rows).

  • rhs (np.ndarray) – Shape (n_flat,). Right-hand-side values.

  • sign (str or np.ndarray) – Constraint sign. Either a single str (‘=’, ‘<=’, ‘>=’) for uniform signs, or a per-row np.ndarray of sign strings for mixed signs.

  • coords (list of pd.Index) – One index per coordinate dimension defining the constraint grid.

  • model (Model) – The linopy model this constraint belongs to.

  • name (str) – Name of the constraint.

  • cindex (int or None) – Starting label assigned by the model. None if not yet assigned.

  • dual (np.ndarray or None) – Shape (n_flat,). Dual values after solving, or None.

__init__(csr, con_labels, rhs, sign, coords, model, name='', cindex=None, dual=None)#

Methods

__init__(csr, con_labels, rhs, sign, coords, ...)

active_labels()

Active constraint labels in build order, without building the CSR.

freeze()

Return self (already immutable).

from_mutable(con[, cindex])

Create a CSRConstraint from a Constraint.

from_netcdf_ds(ds, model, name)

Reconstruct a Constraint from a netcdf Dataset (CSR format).

has_variable(variable)

Check if the constraint references any of the given variable labels.

iterate_slices([slice_size, slice_dims])

Yield row-batched sub-Constraints without Dataset reconstruction.

mutable()

Convert to a Constraint.

print([display_max_rows, display_max_terms])

Print the linear expression.

sanitize_infinities()

Mask out rows with invalid infinite RHS values (mutates in-place).

sanitize_missings()

No-op: missing rows are already excluded during freezing.

sanitize_zeros()

Remove terms with zero or near-zero coefficients (mutates in-place).

to_matrix([label_index])

Return the stored CSR matrix and con_labels.

to_matrix_with_rhs(label_index)

Return (csr, con_labels, b, sense) — all pre-stored, no recomputation.

to_netcdf_ds()

Return a Dataset with raw CSR components for netcdf serialization.

to_polars()

Convert frozen constraint to polars DataFrame directly from CSR.

Attributes

active_positions

Flat positions of active (non-masked) rows in the original coord shape.

attrs

Get the attributes of the constraint.

coeffs

Get coefficients DataArray, shape (*coord_dims, _term).

coord_dims

coord_names

Get the names of the coordinates.

coord_sizes

coords

Get the coordinates of the constraint.

data

Reconstruct the xarray Dataset from the CSR representation.

dims

Get the dimensions of the constraint.

dual

Get dual values DataArray, shape (*coord_dims).

flat

Convert the constraint to a pandas DataFrame.

full_size

indexes

Get the indexes of the constraint.

is_assigned

Whether the constraint has been assigned labels by the model.

labels

Get labels DataArray, shape (*coord_dims).

lhs

Get LHS as LinearExpression (triggers Dataset reconstruction).

mask

Get the mask of the constraint.

model

Get the model reference.

name

Get the constraint name.

ncons

Get the number of active constraints (non-masked, with at least one valid variable).

ndim

Get the number of dimensions of the constraint.

nterm

Get the number of terms in the constraint.

range

Return the (start, end) label range of the constraint.

rhs

Get RHS DataArray, shape (*coord_dims).

shape

Get the shape of the constraint.

sign

Get sign DataArray.

size

Get the size of the constraint.

sizes

Get the sizes of the constraint.

term_dim

Return the term dimension of the constraint.

type

Get the type string of the constraint.

vars

Get variable labels DataArray, shape (*coord_dims, _term).