Note
You can download this example as a Jupyter notebook or start it in interactive mode.
Solve a Basic Model#
In this example, we explain the basic functions of the linopy Model class. First, we are setting up a very simple linear optimization model, given by
Minimize:
subject to:
[ ]:
Initializing a Model#
The Model class in Linopy is a fundamental part of the library. It serves as a container for all the relevant data associated with a linear optimization problem. This includes variables, constraints, and the objective function.
[1]:
from linopy import Model
m = Model()
This creates a new Model object, which you can then use to define your optimization problem.
Adding variables#
Variables in a linear optimization problem represent the decision variables. A variable can always be assigned with a lower and an upper bound. In our case, both x and y have a lower bound of zero (default is unbouded). In Linopy, you can add variables to a Model using the add_variables method:
[2]:
x = m.add_variables(lower=0, name="x")
y = m.add_variables(lower=0, name="y");
x and y are linopy variables of the class linopy.Variable. Each of them contain all relevant information that define it. The name parameter is optional but can be useful for referencing the variables later.
[3]:
x
[3]:
Variable
--------
x ∈ [0, inf]
Since both x and y are scalar variables (meaning they don’t have any dimensions), their underlying data contain only one optimization variable each.
Adding Constraints#
Constraints define the feasible region of the optimization problem. They consist of the left hand side (lhs) and the right hand side (rhs). The first constraint that we want to write down is \(3x + 7y >= 10\), which we write out exactly in the mathematical way
[4]:
3 * x + 7 * y >= 10
[4]:
Constraint (unassigned)
-----------------------
+3 x + 7 y ≥ 10.0
Note, we can also mix the constant and the variable expression, like this
[5]:
3 * x + 7 * y - 10 >= 0
[5]:
Constraint (unassigned)
-----------------------
+3 x + 7 y ≥ 10.0
… and linopy will automatically take over the separation of variables expression on the lhs, and constant values on the rhs.
The constraint is currently not assigned to the model. We assign it by calling the function m.add_constraints.
[6]:
m.add_constraints(3 * x + 7 * y >= 10)
m.add_constraints(5 * x + 2 * y >= 3);
Adding the Objective#
The objective function defines what you want to optimize. You can set the objective function of a Model in Linopy using the add_objective method. For our example that would be
[7]:
m.add_objective(x + 2 * y)
Solving the Model#
Once you’ve defined your Model with variables, constraints, and an objective function, you can solve it using the solve method:
[8]:
m.solve(solver_name="highs")
Running HiGHS 1.14.0 (git hash: 7df0786): Copyright (c) 2026 under MIT licence terms
LP linopy-problem-lqaezvyw has 2 rows; 2 cols; 4 nonzeros
Coefficient ranges:
Matrix [2e+00, 7e+00]
Cost [1e+00, 2e+00]
Bound [0e+00, 0e+00]
RHS [3e+00, 1e+01]
Presolving model
2 rows, 2 cols, 4 nonzeros 0s
2 rows, 2 cols, 4 nonzeros 0s
Presolve reductions: rows 2(-0); columns 2(-0); nonzeros 4(-0) - Not reduced
Problem not reduced by presolve: solving the LP
Using dual simplex solver
Iteration Objective Infeasibilities num(sum)
0 0.0000000000e+00 Pr: 2(13) 0.0s
2 2.8620689655e+00 Pr: 0(0) 0.0s
Model name : linopy-problem-lqaezvyw
Model status : Optimal
Simplex iterations: 2
Objective value : 2.8620689655e+00
P-D objective error : 0.0000000000e+00
HiGHS run time : 0.00
[8]:
('ok', 'optimal')
The solution of the linear problem assigned to the variables under solution in form of a xarray.Dataset.
[9]:
x.solution
[9]:
<xarray.DataArray 'solution' ()> Size: 8B array(0.03448276)
[10]:
y.solution
[10]:
<xarray.DataArray 'solution' ()> Size: 8B array(1.4137931)
Well done! You solved your first linopy model!