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(*args)

Create a linopy.LinearExpression from argument list.

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

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

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

Classes under the hook

Variable

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

variables.Variable(labels, model)

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.ScalarVariable(label, model)

A scalar variable container.

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

Get all integers 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(data, model)

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.groupby_sum(group)

Sum expression over groups.

expressions.LinearExpression.from_tuples(*tuples)

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

expressions.merge(*exprs[, dim, cls])

Merge multiple linear expression together.

expressions.ScalarLinearExpression(coeffs, ...)

A scalar linear expression container.

Constraint

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

constraints.Constraint(labels, model, name)

Projection to a single constraint in a model.

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.

constraints.AnonymousConstraint(_lhs, _sign, ...)

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 or mps 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.

Solving

model.Model.solve([solver_name, io_api, ...])

Solve the model with possibly different solvers.

constants.SolverStatus(value)

Solver status.

constants.TerminationCondition(value)

Termination condition of the solver.

constants.Status(status, termination_condition)

Status and termination condition of the solver.

constants.Solution([primal, dual, objective])

Solution returned by the solver.

constants.Result(status[, solution, ...])

Result of the optimization.