linopy.constraints.CSRConstraint

linopy.constraints.CSRConstraint#

class linopy.constraints.CSRConstraint(csr, active_positions, rhs, sign, grid, model, name='', cindex=None, dual=None, binvar_labels=None, binval=None, scaling=None)#

Frozen constraint backed by a CSR sparse matrix.

Parameters:
  • csr (scipy.sparse.csr_array) – Shape (n_active_cons, model._xCounter). Rows are the active rows of the constraint grid, column indices are raw variable labels. They are mapped to dense positions in to_matrix/to_matrix_with_rhs, so adding or removing variables after freezing stays consistent.

  • active_positions (np.ndarray) – Shape (n_active_cons,). Flat positions of the active rows in the constraint grid; labels are cindex + active_positions.

  • rhs (np.ndarray) – Shape (n_active_cons,). 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.

  • grid (Grid) – The coordinate grid the constraint rows live on.

  • 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, active_positions, rhs, sign, grid, model, name='', cindex=None, dual=None, binvar_labels=None, binval=None, scaling=None)#

Methods

__init__(csr, active_positions, rhs, sign, ...)

active_labels()

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

active_row_mask()

Boolean mask over raveled rows selecting active constraint rows.

assign_labels(cindex, name[, scaling])

Return a copy labelled from cindex and named name.

freeze()

Return self (already immutable).

from_csr(expr, sign, rhs)

Staple sign and rhs onto a CSR-backed lhs to form an unassigned CSRConstraint.

from_dense(con[, cindex])

Create a CSRConstraint from a Constraint.

from_netcdf_ds(ds, model, name)

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

has_labels(labels)

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

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.

to_dense()

Convert to a Constraint.

to_matrix(label_index)

Return the CSR matrix with dense variable positions and con_labels.

to_matrix_with_rhs(label_index)

Return (csr, con_labels, b, sense); only the columns are recomputed.

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.

binary_val

Get the indicator triggering value(s), or None.

binary_var

Get the indicator binary variable labels, or None.

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.

data_attrs

Data variables that define this constraint's persistent state.

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.

is_indicator

Whether the constraint is an indicator constraint.

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).

scaling

Get row scaling 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).