Solve a Basic Model

Contents

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:

$x + 2y$

subject to:

$x \ge 0$
$y \ge 0$
$3x + 7y \ge 10$
$5x + 2y \ge 3$
[ ]:




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.

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


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.

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)


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

Restricted license - for non-production use only - expires 2025-11-24

Read LP format model from file /tmp/linopy-problem-1g54dnn8.lp

Reading time = 0.00 seconds

obj: 2 rows, 2 columns, 4 nonzeros

Gurobi Optimizer version 11.0.1 build v11.0.1rc0 (linux64 - "Ubuntu 22.04.3 LTS")



CPU model: Intel(R) Xeon(R) Platinum 8259CL CPU @ 2.50GHz, instruction set [SSE2|AVX|AVX2|AVX512]

Thread count: 1 physical cores, 2 logical processors, using up to 2 threads



Optimize a model with 2 rows, 2 columns and 4 nonzeros

Model fingerprint: 0x4c5ee539

Coefficient statistics:

  Matrix range     [2e+00, 7e+00]

  Objective range  [1e+00, 2e+00]

  Bounds range     [0e+00, 0e+00]

  RHS range        [3e+00, 1e+01]

Presolve time: 0.01s

Presolved: 2 rows, 2 columns, 4 nonzeros



Iteration    Objective       Primal Inf.    Dual Inf.      Time

       0    0.0000000e+00   1.625000e+00   0.000000e+00      0s

       2    2.8620690e+00   0.000000e+00   0.000000e+00      0s



Solved in 2 iterations and 0.01 seconds (0.00 work units)

Optimal objective  2.862068966e+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!