API reference

This page provides an auto-generated summary of linopy’s API.

Creating a model

model.Model([solver_dir, chunk, force_dim_names])

Linear optimization model.

model.Model.add_variables([lower, upper, ...])

Assign a new, possibly multi-dimensional array of variables to the model.

model.Model.add_constraints(lhs[, sign, ...])

Assign a new, possibly multi-dimensional array of constraints to the model.

model.Model.add_objective(expr[, overwrite])

Add a linear objective function to the model.

model.Model.linexpr(*tuples)

Create a linopy.LinearExpression by using variable names.

model.Model.remove_constraints(name)

Remove all constraints stored under reference name name from the model.

model.Model.remove_constraints(name)

Remove all constraints stored under reference name name from the model.

Creating a model with eval strings (experimental)

model.Model.vareval(expr[, eval_kw])

Define a variable based a string expression (experimental).

model.Model.lineval(expr[, eval_kw])

Evaluate linear expressions given as a string (experimental).

model.Model.coneval(expr[, eval_kw])

Define a constraint determined by a string expression (experimental).

Classes under the hook

Variable

Variable is a subclass of xarray.DataArray and contains all labels referring to a multi-dimensional variable.

variables.Variable(*args, **kwargs)

Variable container for storing variable labels.

variables.Variable.lower

Get the lower bounds of the variables.

variables.Variable.upper

Get the upper bounds of the variables.

variables.Variable.sum([dims])

Sum the variables over all or a subset of dimensions.

variables.Variable.where(cond[, other])

Filter variables based on a condition.

variables.Variable.sanitize()

Sanitize variable by ensuring int dtype with fill value of -1.

variables.Variables([labels, lower, upper, ...])

A variables container used for storing multiple variable arrays.

Variables

Variables is a container for multiple N-D labeled variables. It is automatically added to a Model instance when initialized.

variables.Variables([labels, lower, upper, ...])

A variables container used for storing multiple variable arrays.

variables.Variables.add(name, labels, lower, ...)

Add variable name.

variables.Variables.remove(name)

Remove variable name from the variables.

variables.Variables.binaries

Get all binary variables.

variables.Variables.non_binaries

Get all non-binary variables.

variables.Variables.iter_ravel(key[, ...])

Create an generator which iterates over all arrays in key and flattens them.

variables.Variables.ravel(key[, ...])

Ravel and concate all arrays in key while aligning to broadcast_like.

variables.Variables.get_blocks(blocks)

Get a dataset of same shape as variables.labels indicating the blocks.

LinearExpressions

expressions.LinearExpression([dataset])

A linear expression consisting of terms of coefficients and variables.

expressions.LinearExpression.sum([dims, ...])

Sum the expression over all or a subset of dimensions.

expressions.LinearExpression.group_terms(group)

expressions.LinearExpression.from_tuples(*)

Create a linear expression by using tuples of coefficients and variables.

expressions.merge(*exprs[, dim])

Merge multiple linear expression together.

Constraint

Constraint is a subclass of xarray.DataArray and contains all labels referring to a multi-dimensional constraint.

constraints.Constraint(*args, **kwargs)

Constraint container for storing constraint labels.

constraints.Constraint.to_array()

Convert the variable array to a xarray.DataArray.

constraints.Constraint.coeffs

Get the left-hand-side coefficients of the constraint.

constraints.Constraint.vars

Get the left-hand-side variables of the constraint.

constraints.Constraint.lhs

Get the left-hand-side linear expression of the constraint.

constraints.Constraint.sign

Get the signs of the constraint.

constraints.Constraint.rhs

Get the right hand side constants of the constraint.

AnonymousConstraint

AnonynousConstraint contains all information about lower bounds, upper bounds, sign and right-hand-side of a constraint that is not yet added to a Model instance.

expressions.AnonymousConstraint(lhs, sign, rhs)

A constraint container used for storing multiple constraint arrays.

Constraints

constraints.Constraints([labels, coeffs, ...])

A constraint container used for storing multiple constraint arrays.

constraints.Constraints.add(name, labels, ...)

Add constraint name.

constraints.Constraints.remove(name)

Remove constraint name from the constraints.

constraints.Constraints.coefficientrange

Coefficient range of the constraint.

constraints.Constraints.inequalities

Get the subset of constraints which are purely inequalities.

constraints.Constraints.equalities

Get the subset of constraints which are purely equalities.

constraints.Constraints.sanitize_missings()

Set constraints labels to -1 where all variables in the lhs are missing.

constraints.Constraints.get_blocks(block_map)

Get a dataset of same shape as constraints.labels with block values.

constraints.Constraints.iter_ravel(key[, ...])

Create an generator which iterates over all arrays in key and flattens them.

constraints.Constraints.ravel(key[, ...])

Ravel and concate all arrays in key while aligning to broadcast_like.

constraints.Constraints.to_matrix([...])

Construct a constraint matrix in sparse format.

IO functions

model.Model.get_problem_file([problem_fn])

Get a fresh created problem file if problem file is None.

model.Model.get_solution_file([solution_fn])

Get a fresh created solution file if solution file is None.

model.Model.to_file(fn)

Write out a model to a lp file.

model.Model.to_netcdf(*args, **kwargs)

Write out the model to a netcdf file.

io.read_netcdf(path, **kwargs)

Read in a model from a netcdf file.

Solvers

solvers.run_cbc(Model[, io_api, problem_fn, ...])

Solve a linear problem using the cbc solver.

solvers.run_glpk(Model[, io_api, ...])

Solve a linear problem using the glpk solver.

solvers.run_highs(Model[, io_api, ...])

Highs solver function.

solvers.run_cplex(Model[, io_api, ...])

Solve a linear problem using the cplex solver.

solvers.run_gurobi(Model[, io_api, ...])

Solve a linear problem using the gurobi solver.

solvers.run_xpress(Model[, io_api, ...])

Solve a linear problem using the xpress solver.