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

Classes under the hook#

Variable#

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

variables.Variable(data, model, name)

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([data, model])

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([data, model])

A variables container used for storing multiple variable arrays.

variables.Variables.add(variable)

Add a variable to the variables container.

variables.Variables.remove(name)

Remove variable name from the variables.

variables.Variables.continuous

Get all continuous variables.

variables.Variables.integers

Get all integers variables.

variables.Variables.binaries

Get all binary variables.

variables.Variables.integers

Get all integers variables.

variables.Variables.flat

Convert all variables to a single pandas Dataframe.

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.where(cond[, other])

Filter variables based on a condition.

expressions.LinearExpression.groupby(group)

Returns a LinearExpressionGroupBy object for performing grouped operations.

expressions.LinearExpression.rolling([dim, ...])

Rolling window object.

expressions.LinearExpression.from_tuples(*tuples)

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

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

Merge multiple 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(data, model[, name])

Projection to a single constraint in a model.

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.

constraints.Constraint.flat

Convert the constraint to a pandas DataFrame.

Constraints#

constraints.Constraints([data, model])

A constraint container used for storing multiple constraint arrays.

constraints.Constraints.add(constraint)

Add a constraint to the constraints constrainer.

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

Convert all constraint to a single pandas Dataframe.

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[, integer_label])

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.

solvers.run_mosek(model[, io_api, ...])

Solve a linear problem using the MOSEK solver.

solvers.run_mindopt(model[, io_api, ...])

Solve a linear problem using the MindOpt solver.

solvers.run_copt(model[, io_api, ...])

Solve a linear problem using the COPT 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.