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 into_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 arecindex + active_positions.rhs (
np.ndarray) – Shape (n_active_cons,). Right-hand-side values.sign (
strornp.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 (
intorNone) – Starting label assigned by the model. None if not yet assigned.dual (
np.ndarrayorNone) – 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
cindexand namedname.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.
Convert frozen constraint to polars DataFrame directly from CSR.
Attributes
active_positionsFlat positions of active (non-masked) rows in the original coord shape.
attrsGet the attributes of the constraint.
binary_valGet the indicator triggering value(s), or None.
binary_varGet the indicator binary variable labels, or None.
Get coefficients DataArray, shape (*coord_dims, _term).
coord_dimscoord_namesGet the names of the coordinates.
coord_sizescoordsGet the coordinates of the constraint.
dataReconstruct the xarray Dataset from the CSR representation.
data_attrsData variables that define this constraint's persistent state.
dimsGet the dimensions of the constraint.
Get dual values DataArray, shape (*coord_dims).
flatConvert the constraint to a pandas DataFrame.
full_sizeindexesGet the indexes of the constraint.
is_assignedWhether the constraint has been assigned labels by the model.
is_indicatorWhether the constraint is an indicator constraint.
labelsGet labels DataArray, shape (*coord_dims).
lhsGet LHS as LinearExpression (triggers Dataset reconstruction).
maskGet the mask of the constraint.
modelGet the model reference.
nameGet the constraint name.
Get the number of active constraints (non-masked, with at least one valid variable).
ndimGet the number of dimensions of the constraint.
Get the number of terms in the constraint.
rangeReturn the (start, end) label range of the constraint.
Get RHS DataArray, shape (*coord_dims).
Get row scaling DataArray, shape (*coord_dims).
shapeGet the shape of the constraint.
Get sign DataArray.
sizeGet the size of the constraint.
sizesGet the sizes of the constraint.
term_dimReturn the term dimension of the constraint.
typeGet the type string of the constraint.
Get variable labels DataArray, shape (*coord_dims, _term).