API reference#
Reference for linopy’s public API. Most workflows start at
Model — Variable,
Constraint, and
Objective are all built through
Model.add_variables,
Model.add_constraints,
Model.add_objective,
and accessed through the matching
Model.variables,
Model.constraints, and
Model.objective accessors.
The supporting classes below cover those types in detail.
Model#
Central container for an optimization problem. Most of linopy’s surface lives here.
|
Linear optimization model. |
Building a model#
|
Assign a new, possibly multi-dimensional array of variables to the model. |
|
Assign a new, possibly multi-dimensional array of constraints to the model. |
|
Add an objective function to the model. |
|
Add an sos1 or sos2 constraint for one dimension of a variable |
Add piecewise linear constraints. |
Inspecting a model#
Variables assigned to the model. |
|
Constraints assigned to the model. |
|
Objective assigned to the model. |
|
Sense of the objective function. |
|
Short string identifying the problem type. |
|
Whether the objective is linear. |
|
Whether the objective is quadratic. |
Modifying a model#
Remove all variables stored under reference name name from the model. |
|
Remove all constraints stored under reference name 'name' from the model. |
|
Remove the objective's linear expression from the model. |
|
|
Remove all sos constraints from a given variable. |
|
Return a copy of this model. |
Reformulate SOS constraints into binary + linear form, in place. |
|
Revert a previously applied SOS reformulation. |
|
|
Reformulate SOS constraints as binary + linear constraints. |
Solving#
|
Solve the model with possibly different solvers. |
Post-solve access#
Solution calculated by the optimization. |
|
Dual values calculated by the optimization. |
|
Status of the model. |
|
Termination condition of the model. |
Diagnostics#
Compute a set of infeasible constraints. |
|
Return a string representation of infeasible constraints. |
IO#
|
Write out a model to a lp or mps file. |
|
Write out the model to a netcdf file. |
|
Read in a model from a netcdf file. |
Variable#
Subclass of xarray.DataArray carrying labels for a multi-dimensional
decision variable.
|
Variable container for storing variable labels. |
Attributes#
Get the lower bounds of the variables. |
|
Get the upper bounds of the variables. |
|
Type of the variable. |
|
Get the optimal values of the variable. |
Modification#
|
Fix the variable to a given value by adding an equality constraint. |
Remove the fix constraint for this variable. |
|
Relax the integrality of this variable. |
|
Restore the original integrality type of a relaxed variable. |
Operations#
|
Sum the variables over all or a subset of dimensions. |
|
Filter variables based on a condition. |
Conversion#
|
Create a linear expression from the variables. |
Convert all variables to a single polars DataFrame. |
Variables#
Container for the collection of variables on a model. Accessed via
model.variables.
|
A variables container used for storing multiple variable arrays. |
Attributes#
Get the lower bounds of all variables. |
|
Get the upper bounds of all variables. |
|
Get the solution of variables. |
Modification#
|
Fix all variables in this container to their solution or a scalar value. |
Unfix all variables in this container. |
|
Relax integrality of all integer/binary variables in this container. |
|
Restore integrality of all previously relaxed variables in this container. |
Inventory#
Get all continuous variables. |
|
Get all binary variables. |
|
Get all integers variables. |
|
Get all semi-continuous variables. |
|
Get all variables involved in an sos constraint. |
LinearExpression#
Linear combination of variables. Arithmetic on Variable /
LinearExpression returns a LinearExpression.
|
A linear expression consisting of terms of coefficients and variables. |
Post-solve access#
Get the optimal values of the expression. |
Operations#
|
Sum the expression over all or a subset of dimensions. |
|
Filter variables based on a condition. |
Returns a LinearExpressionGroupBy object for performing grouped operations. |
|
|
Rolling window object. |
Structure#
Variable labels referenced by each term of the expression. |
|
Coefficient applied to each term of the expression. |
|
Constant offset added to the expression. |
|
Get the number of terms in the linear expression. |
Conversion#
Convert the expression to a polars DataFrame. |
Construction#
Create a linear expression by using tuples of coefficients and variables. |
|
|
Merge multiple expression together. |
QuadraticExpression#
Quadratic combination of variables, returned when squared
Variable / LinearExpression arithmetic is performed.
|
A quadratic expression consisting of terms of coefficients and variables. |
Structure#
Variable labels referenced by each term of the expression. |
|
Coefficient applied to each term of the expression. |
|
Constant offset added to the expression. |
|
Get the number of terms in the linear expression. |
Conversion#
Return a sparse matrix representation of the expression only including quadratic terms. |
|
Convert the expression to a polars DataFrame. |
Post-solve access#
Get the optimal values of the expression. |
Constraint#
Subclass of xarray.DataArray carrying labels for a multi-dimensional
constraint.
|
Constraint backed by an xarray Dataset. |
Structure#
Get the left-hand-side linear expression of the constraint. |
|
Get the constraint sign DataArray. |
|
Get the RHS DataArray. |
|
Get the LHS coefficients DataArray. |
|
Get the LHS variable labels DataArray. |
Post-solve access#
Get the dual values DataArray. |
Conversion#
Convert the constraint to a polars DataFrame. |
CSRConstraint#
Memory-efficient, immutable constraint representation backed by a scipy
CSR sparse matrix. Opt in via Model(freeze_constraints=True) or
Model.add_constraints(..., freeze=True). See the
Creating Constraints guide for usage.
|
Frozen constraint backed by a CSR sparse matrix. |
Structure#
Get coefficients DataArray, shape (*coord_dims, _term). |
|
Get variable labels DataArray, shape (*coord_dims, _term). |
|
Get sign DataArray. |
|
Get RHS DataArray, shape (*coord_dims). |
|
Get the number of active constraints (non-masked, with at least one valid variable). |
|
Get the number of terms in the constraint. |
Post-solve access#
Get dual values DataArray, shape (*coord_dims). |
Conversion#
Convert frozen constraint to polars DataFrame directly from CSR. |
Constraints#
Container for the collection of constraints on a model. Accessed via
model.constraints.
|
A constraint container used for storing multiple constraint arrays. |
Inventory#
Get the subset of constraints which are purely inequalities. |
|
Get the subset of constraints which are purely equalities. |
Aggregate access#
Get the coefficients of all constraints. |
|
Get the variables of all constraints. |
|
Get the signs of all constraints. |
|
Get the right-hand-side constants of all constraints. |
|
Get the dual values of all constraints. |
Conversion#
Construct a constraint matrix in sparse format by stacking per-constraint CSR matrices. |
Objective#
Wraps the objective expression on a model. Accessed via
model.objective.
|
An objective expression containing all relevant information. |
Returns the expression of the objective. |
|
Returns the sense of the objective. |
|
Returns the value of the objective. |
|
Whether the objective expression is linear. |
|
Whether the objective expression is quadratic. |
Piecewise#
Construction helpers#
|
Create a breakpoint DataArray for piecewise linear constraints. |
|
Create a segmented breakpoint DataArray for disjunctive piecewise constraints. |
|
Per-piece slopes + initial y-value, deferred until an x grid is known. |
PiecewiseFormulation#
Returned by Model.add_piecewise_formulation().
|
Result of |
Resolved formulation method (see |
|
Shape of the piecewise curve when well-defined (see |
|
View of the auxiliary variables in this formulation. |
|
View of the auxiliary constraints in this formulation. |
Low-level helper#
|
Compute tangent-line (chord) expressions for a piecewise linear function. |
Type aliases#
Allowed values for the |
|
Set of valid |
|
Possible values for |
|
Set of valid |
Solvers#
The stateful Solver instance owns the solver-side
model and exposes a two-step from_name() /
solve() workflow. Model.solve() is a
thin wrapper around it.
|
Abstract base class for solving a given linear problem. |
Construction#
|
Construct and build the solver subclass registered as |
|
Instantiate and build the solver against |
Solving#
|
Run the prepared solver and return a |
|
|
Post-solve state#
Capabilities#
Return True if this solver's package/binary is importable. |
|
Probe license/runtime availability. |
|
|
Check if this solver supports a given feature. |
All features supported by this solver, static plus runtime. |
|
Features whose availability depends on the installed solver version or runtime environment. |
Discovery#
Lazy sequence of installed solver names. |
|
Installed solvers whose |
|
|
Enumeration of all solver capabilities tracked by linopy. |
|
Result of |
Implementations#
|
Solver subclass for the CBC solver. |
|
Solver subclass for the COPT solver. |
|
Solver subclass for the Cplex solver. |
|
Solver subclass for the GLPK solver. |
|
Solver subclass for the gurobi solver. |
|
Solver subclass for the HiGHS solver. |
|
Solver subclass for the Knitro solver. |
|
Solver subclass for the MindOpt solver. |
|
Solver subclass for the Mosek solver. |
|
Solver subclass for the SCIP solver. |
|
Solver subclass for the xpress solver. |
|
Solver subclass for the cuPDLPx solver. |
Remote solving#
|
Handler class for solving models on a remote machine via an SSH connection. |
Solver status and result types#
Types returned by or compared against Model.status,
Model.termination_condition, and Model.solution, plus
SolverReport surfaced on
Solver.report and
Result.report.
|
Solver status. |
|
Termination condition of the solver. |
|
Status and termination condition of the solver. |
|
Solution returned by the solver. |
|
Solver-reported performance metrics. |
|
Result of the optimization. |
Utilities#
Warnings#
These warning classes can be silenced or filtered via
warnings.filterwarnings().
Signals a newly-added API whose details may evolve in minor releases. |
|
Warning raised when an operation triggers expensive Dataset reconstruction. |