Note

You can download this example as a Jupyter notebook or start it in interactive mode.

Modifying Models

Given a model that is already built and possibly optimized, the user might want to modify single constraint or variable bounds by means of correction or exploration of the feasible space.

In the following we show how single elements can be tweaked or rewritten. Let’s start with the simple model of the Getting Started section.

[1]:
import linopy
import pandas as pd
import xarray as xr
[2]:
m = linopy.Model()
time = pd.Index(range(10), name='time')

x = m.add_variables(lower=0, coords=[time], name='x', )
y = m.add_variables(lower=0, coords=[time], name='y')

factor = pd.Series(time, index=time)

con1 = m.add_constraints(3*x + 7*y >= 10*factor, name='con1')
con2 = m.add_constraints(5*x + 2*y >= 3*factor, name='con2')

m.add_objective(x + 2*y)
m.solve()

m.solution.to_dataframe().plot(grid=True, ylabel='Optimal Value')
Restricted license - for non-production use only - expires 2023-10-25
Read LP format model from file /tmp/linopy-problem-egwqixwe.lp
Reading time = 0.00 seconds
obj: 20 rows, 20 columns, 40 nonzeros
Gurobi Optimizer version 9.5.2 build v9.5.2rc0 (linux64)
Thread count: 1 physical cores, 2 logical processors, using up to 2 threads
Optimize a model with 20 rows, 20 columns and 40 nonzeros
Model fingerprint: 0x89bb3e20
Coefficient statistics:
  Matrix range     [2e+00, 7e+00]
  Objective range  [1e+00, 2e+00]
  Bounds range     [0e+00, 0e+00]
  RHS range        [3e+00, 9e+01]
Presolve removed 2 rows and 2 columns
Presolve time: 0.01s
Presolved: 18 rows, 18 columns, 36 nonzeros

Iteration    Objective       Primal Inf.    Dual Inf.      Time
       0    0.0000000e+00   7.312500e+01   0.000000e+00      0s
      18    1.2879310e+02   0.000000e+00   0.000000e+00      0s

Solved in 18 iterations and 0.01 seconds (0.00 work units)
Optimal objective  1.287931034e+02
[2]:
<AxesSubplot:xlabel='time', ylabel='Optimal Value'>
_images/manipulating-models_2_2.png

The figure above shows the optimal values of x(t) and y(t).

Varying lower and upper bounds

Now, let’s say we want to set the lower bound of y(t) to 1. This would translate to:

[3]:
x.lower = 1

Note: The same could have been achieved by calling

m.variables['x'].lower = 1

Let’s solve it again!

[4]:
m.solve();
m.solution.to_dataframe().plot(grid=True, ylabel='Optimal Value')
Read LP format model from file /tmp/linopy-problem-a4x5_3hc.lp
Reading time = 0.00 seconds
obj: 20 rows, 20 columns, 40 nonzeros
Gurobi Optimizer version 9.5.2 build v9.5.2rc0 (linux64)
Thread count: 1 physical cores, 2 logical processors, using up to 2 threads
Optimize a model with 20 rows, 20 columns and 40 nonzeros
Model fingerprint: 0xd3985941
Coefficient statistics:
  Matrix range     [2e+00, 7e+00]
  Objective range  [1e+00, 2e+00]
  Bounds range     [1e+00, 1e+00]
  RHS range        [3e+00, 9e+01]
Presolve removed 4 rows and 4 columns
Presolve time: 0.01s
Presolved: 16 rows, 16 columns, 32 nonzeros

Iteration    Objective       Primal Inf.    Dual Inf.      Time
       0    1.3085714e+02   0.000000e+00   0.000000e+00      0s
       0    1.3085714e+02   0.000000e+00   0.000000e+00      0s

Solved in 0 iterations and 0.01 seconds (0.00 work units)
Optimal objective  1.308571429e+02
[4]:
<AxesSubplot:xlabel='time', ylabel='Optimal Value'>
_images/manipulating-models_6_2.png

We see that the new lower bound of x is binding across all time steps.

Of course the implementation is flexible over the dimensions, so we can pass non-scalar values:

[5]:
x.lower = xr.DataArray(range(10, 0, -1), coords=(time,))
[6]:
m.solve();
m.solution.to_dataframe().plot(grid=True, ylabel='Optimal Value')
Read LP format model from file /tmp/linopy-problem-eorn9f51.lp
Reading time = 0.00 seconds
obj: 20 rows, 20 columns, 40 nonzeros
Gurobi Optimizer version 9.5.2 build v9.5.2rc0 (linux64)
Thread count: 1 physical cores, 2 logical processors, using up to 2 threads
Optimize a model with 20 rows, 20 columns and 40 nonzeros
Model fingerprint: 0x3cc0a80d
Coefficient statistics:
  Matrix range     [2e+00, 7e+00]
  Objective range  [1e+00, 2e+00]
  Bounds range     [1e+00, 1e+01]
  RHS range        [3e+00, 9e+01]
Presolve removed 14 rows and 14 columns
Presolve time: 0.01s
Presolved: 6 rows, 6 columns, 12 nonzeros

Iteration    Objective       Primal Inf.    Dual Inf.      Time
       0    1.5100000e+02   0.000000e+00   0.000000e+00      0s
       0    1.5100000e+02   0.000000e+00   0.000000e+00      0s

Solved in 0 iterations and 0.01 seconds (0.00 work units)
Optimal objective  1.510000000e+02
[6]:
<AxesSubplot:xlabel='time', ylabel='Optimal Value'>
_images/manipulating-models_9_2.png

You can manipulate the upper bound of a variable in the same way.

Varying Constraints

A similar functionality is implemented for constraints. Here we can modify the left-hand-side, the sign and the right-hand-side.

Assume we want to relax the right-hand-side of the first constraint con1 to 8 * factor. This would translate to:

[7]:
con1.rhs = 8 * factor

Note: The same could have been achieved by calling

m.variables['con1'].rhs = 8 * factor

Let’s solve it again!

[8]:
m.solve();
m.solution.to_dataframe().plot(grid=True, ylabel='Optimal Value')
Read LP format model from file /tmp/linopy-problem-1aq7zuv2.lp
Reading time = 0.00 seconds
obj: 20 rows, 20 columns, 40 nonzeros
Gurobi Optimizer version 9.5.2 build v9.5.2rc0 (linux64)
Thread count: 1 physical cores, 2 logical processors, using up to 2 threads
Optimize a model with 20 rows, 20 columns and 40 nonzeros
Model fingerprint: 0x60818c1c
Coefficient statistics:
  Matrix range     [2e+00, 7e+00]
  Objective range  [1e+00, 2e+00]
  Bounds range     [1e+00, 1e+01]
  RHS range        [3e+00, 7e+01]
Presolve removed 14 rows and 14 columns
Presolve time: 0.01s
Presolved: 6 rows, 6 columns, 12 nonzeros

Iteration    Objective       Primal Inf.    Dual Inf.      Time
       0    1.2700000e+02   2.857143e-01   0.000000e+00      0s
       1    1.2707882e+02   0.000000e+00   0.000000e+00      0s

Solved in 1 iterations and 0.01 seconds (0.00 work units)
Optimal objective  1.270788177e+02
[8]:
<AxesSubplot:xlabel='time', ylabel='Optimal Value'>
_images/manipulating-models_14_2.png

In contrast to previous figure, we now see that the optimal value of y does not reach values above 10 in the end.

In the same way, we can modify the left-hand-side. Assume we want to weight y with a coefficient of 8 in the constraints, this gives

[9]:
con1.lhs = 3 * x + 8 * y

Note: The same could have been achieved by calling

m.constraints['con1'].lhs = 3 * x + 8 * y

which leads to

[10]:
m.solve();
m.solution.to_dataframe().plot(grid=True, ylabel='Optimal Value')
Read LP format model from file /tmp/linopy-problem-gwnfm745.lp
Reading time = 0.00 seconds
obj: 20 rows, 20 columns, 40 nonzeros
Gurobi Optimizer version 9.5.2 build v9.5.2rc0 (linux64)
Thread count: 1 physical cores, 2 logical processors, using up to 2 threads
Optimize a model with 20 rows, 20 columns and 40 nonzeros
Model fingerprint: 0x13ecf09d
Coefficient statistics:
  Matrix range     [2e+00, 8e+00]
  Objective range  [1e+00, 2e+00]
  Bounds range     [1e+00, 1e+01]
  RHS range        [3e+00, 7e+01]
Presolve removed 14 rows and 14 columns
Presolve time: 0.01s
Presolved: 6 rows, 6 columns, 12 nonzeros

Iteration    Objective       Primal Inf.    Dual Inf.      Time
       0    1.1800000e+02   5.937500e-01   0.000000e+00      0s
       1    1.1827941e+02   0.000000e+00   0.000000e+00      0s

Solved in 1 iterations and 0.01 seconds (0.00 work units)
Optimal objective  1.182794118e+02
[10]:
<AxesSubplot:xlabel='time', ylabel='Optimal Value'>
_images/manipulating-models_19_2.png

Varying the objective

Varying the objective happens in the same way as for the left-hand-side of the constraint as it is a linear expression too. Note, when passing an unstacked linear expression, i.e. an expression with more than the _term dimension, linopy will automatically stack it.

So assume, we would like to modify the weight of y in the objective function, this translates to:

[11]:
m.objective = x + 3*y
[12]:
m.solve();
m.solution.to_dataframe().plot(grid=True, ylabel='Optimal Value')
Read LP format model from file /tmp/linopy-problem-wsjvz3gq.lp
Reading time = 0.00 seconds
obj: 20 rows, 20 columns, 40 nonzeros
Gurobi Optimizer version 9.5.2 build v9.5.2rc0 (linux64)
Thread count: 1 physical cores, 2 logical processors, using up to 2 threads
Optimize a model with 20 rows, 20 columns and 40 nonzeros
Model fingerprint: 0x49195df6
Coefficient statistics:
  Matrix range     [2e+00, 8e+00]
  Objective range  [1e+00, 3e+00]
  Bounds range     [1e+00, 1e+01]
  RHS range        [3e+00, 7e+01]
Presolve removed 14 rows and 14 columns
Presolve time: 0.01s
Presolved: 6 rows, 6 columns, 12 nonzeros

Iteration    Objective       Primal Inf.    Dual Inf.      Time
       0    1.3900000e+02   0.000000e+00   0.000000e+00      0s
       0    1.3900000e+02   0.000000e+00   0.000000e+00      0s

Solved in 0 iterations and 0.01 seconds (0.00 work units)
Optimal objective  1.390000000e+02
[12]:
<AxesSubplot:xlabel='time', ylabel='Optimal Value'>
_images/manipulating-models_22_2.png

As a consequence, y stays at zero for all time steps.