Note
You can download this example as a Jupyter notebook or start it in interactive mode.
Optimization with Linopy#
In PyPSA v0.22
, an additional optimization module was introduced to the package. It is built on Linopy and aims at
performance as we know it from the native PyPSA optimization (
lopf
withpyomo=False
)flexibility as we know from the Pyomo implementation
usability as we know from pandas/xarray
Linopy is a stand-alone package and works similar to Pyomo, but without the memory overhead and much faster. In the long-term, we are planning to slowly migrate towards the Linopy-based optimization only. In order to facilitate the transmission from the native PyPSA optimization (lopf
with pyomo=False
), the module pypsa.optimization.compat
provides functions similar to pypsa.linopt
. Have a look at our migration guide (next notebook).
If you don’t have any code to migrate, we recommend to directly use the Linopy functions instead.
For additional information on the Linopy package, have a look at the documentation.
Let’s get started#
Now, we demonstrate the behaviour of the optimization with linopy. The core functions for the optimization can be called via the Network.optimize
accessor. The accessor is used for creating, solving, modifying the optimization problem. Further, it supports to run different optimization formulations and provides helper functions.
At first, we run the ordinary linearized optimal power flow (LOPF). We then extend the formulation by some additional constraints.
[1]:
import pypsa
import pandas as pd
[2]:
n = pypsa.examples.ac_dc_meshed(from_master=True)
WARNING:pypsa.io:Importing network from PyPSA version v0.17.1 while current version is v0.23.0. Read the release notes at https://pypsa.readthedocs.io/en/latest/release_notes.html to prepare your network for import.
INFO:pypsa.io:Imported network ac-dc-meshed.nc has buses, carriers, generators, global_constraints, lines, links, loads
In order to make the network a bit more interesting, we modify its data: We set gas generators to non-extendable,
[3]:
n.generators.loc[n.generators.carrier == "gas", "p_nom_extendable"] = False
… add ramp limits,
[4]:
n.generators.loc[n.generators.carrier == "gas", "ramp_limit_down"] = 0.2
n.generators.loc[n.generators.carrier == "gas", "ramp_limit_up"] = 0.2
… add additional storage units (cyclic and non-cyclic) and fix one state_of_charge,
[5]:
n.add(
"StorageUnit",
"su",
bus="Manchester",
marginal_cost=10,
inflow=50,
p_nom_extendable=True,
capital_cost=10,
p_nom=2000,
efficiency_dispatch=0.5,
cyclic_state_of_charge=True,
state_of_charge_initial=1000,
)
n.add(
"StorageUnit",
"su2",
bus="Manchester",
marginal_cost=10,
p_nom_extendable=True,
capital_cost=50,
p_nom=2000,
efficiency_dispatch=0.5,
carrier="gas",
cyclic_state_of_charge=False,
state_of_charge_initial=1000,
)
n.storage_units_t.state_of_charge_set.loc[n.snapshots[7], "su"] = 100
…and add an additional store.
[6]:
n.add("Bus", "storebus", carrier="hydro", x=-5, y=55)
n.madd(
"Link",
["battery_power", "battery_discharge"],
"",
bus0=["Manchester", "storebus"],
bus1=["storebus", "Manchester"],
p_nom=100,
efficiency=0.9,
p_nom_extendable=True,
p_nom_max=1000,
)
n.madd(
"Store",
["store"],
bus="storebus",
e_nom=2000,
e_nom_extendable=True,
marginal_cost=10,
capital_cost=10,
e_nom_max=5000,
e_initial=100,
e_cyclic=True,
);
Ordinary Optimization#
Per default, the optimization based on linopy mimics the well-known n.lopf
optimization. We run it by calling the optimize
accessor.
[7]:
n.optimize()
WARNING:pypsa.components:The following lines have zero x, which could break the linear load flow:
Index(['2', '3', '4'], dtype='object', name='Line')
WARNING:pypsa.components:The following lines have zero r, which could break the linear load flow:
Index(['0', '1', '5', '6'], dtype='object', name='Line')
WARNING:pypsa.components:The following lines have zero x, which could break the linear load flow:
Index(['2', '3', '4'], dtype='object', name='Line')
WARNING:pypsa.components:The following lines have zero r, which could break the linear load flow:
Index(['0', '1', '5', '6'], dtype='object', name='Line')
/home/docs/checkouts/readthedocs.org/user_builds/pypsa/conda/latest/lib/python3.11/site-packages/linopy/model.py:607: UserWarning: Reindexing constraint `Generator-fix-p-ramp_limit_up` to match existing coordinates.
warnings.warn(
/home/docs/checkouts/readthedocs.org/user_builds/pypsa/conda/latest/lib/python3.11/site-packages/linopy/model.py:607: UserWarning: Reindexing constraint `Generator-fix-p-ramp_limit_down` to match existing coordinates.
warnings.warn(
<__array_function__ internals>:200: RuntimeWarning: invalid value encountered in cast
<__array_function__ internals>:200: RuntimeWarning: invalid value encountered in cast
<__array_function__ internals>:200: RuntimeWarning: invalid value encountered in cast
<__array_function__ internals>:200: RuntimeWarning: invalid value encountered in cast
<__array_function__ internals>:200: RuntimeWarning: invalid value encountered in cast
<__array_function__ internals>:200: RuntimeWarning: invalid value encountered in cast
<__array_function__ internals>:200: RuntimeWarning: invalid value encountered in cast
INFO:linopy.model: Solve linear problem using Glpk solver
INFO:linopy.io: Writing time: 0.43s
INFO:linopy.constants: Optimization successful:
Status: ok
Termination condition: optimal
Solution: 300 primals, 748 duals
Objective: 1.41e+07
Solver model: not available
Solver message: optimal
GLPSOL--GLPK LP/MIP Solver 5.0
Parameter(s) specified in the command line:
--lp /tmp/linopy-problem-awyv6sem.lp --output /tmp/linopy-solve-ldk07xw8.sol
Reading problem data from '/tmp/linopy-problem-awyv6sem.lp'...
748 rows, 300 columns, 1571 non-zeros
4993 lines were read
GLPK Simplex Optimizer 5.0
748 rows, 300 columns, 1571 non-zeros
Preprocessing...
544 rows, 288 columns, 1354 non-zeros
Scaling...
A: min|aij| = 9.693e-03 max|aij| = 2.000e+00 ratio = 2.063e+02
GM: min|aij| = 5.541e-01 max|aij| = 1.805e+00 ratio = 3.257e+00
EQ: min|aij| = 3.118e-01 max|aij| = 1.000e+00 ratio = 3.207e+00
Constructing initial basis...
Size of triangular part is 543
0: obj = 3.234549052e+03 inf = 7.824e+04 (101)
137: obj = 4.262375488e+07 inf = 3.126e-12 (0) 1
* 259: obj = 1.412123769e+07 inf = 7.958e-13 (0) 1
OPTIMAL LP SOLUTION FOUND
Time used: 0.0 secs
Memory used: 0.9 Mb (962113 bytes)
Writing basic solution to '/tmp/linopy-solve-ldk07xw8.sol'...
[7]:
('ok', 'optimal')
Compared to the native optimization, we now have a model instance attached to our network. It is a container of all variables, constraints and the objective function. You can modify this as much as you please, by directly adding or deleting variables or constraints etc.
[8]:
n.model
[8]:
Linopy model
============
Variables:
----------
Dimensions: (Generator-ext: 3, Line-ext: 7, Link-ext: 6,
Store-ext: 1, StorageUnit-ext: 2,
snapshot: 10, Generator: 6, Line: 7, Link: 6,
Store: 1, StorageUnit: 2)
Coordinates:
* Generator-ext (Generator-ext) object 'Manchester Wind' ......
* Line-ext (Line-ext) object '0' '1' '2' '3' '4' '5' '6'
* Link-ext (Link-ext) object 'Norwich Converter' ... 'b...
* Store-ext (Store-ext) object 'store'
* StorageUnit-ext (StorageUnit-ext) object 'su' 'su2'
* snapshot (snapshot) datetime64[ns] 2015-01-01 ... 201...
* Generator (Generator) object 'Manchester Wind' ... 'Fr...
* Line (Line) object '0' '1' '2' '3' '4' '5' '6'
* Link (Link) object 'Norwich Converter' ... 'batte...
* Store (Store) object 'store'
* StorageUnit (StorageUnit) object 'su' 'su2'
Data variables: (12/15)
Generator-p_nom (Generator-ext) int64 0 1 2
Line-s_nom (Line-ext) int64 3 4 5 6 7 8 9
Link-p_nom (Link-ext) int64 10 11 12 13 14 15
Store-e_nom (Store-ext) int64 16
StorageUnit-p_nom (StorageUnit-ext) int64 17 18
Generator-p (snapshot, Generator) int64 19 20 21 ... 77 78
... ...
StorageUnit-p_dispatch (snapshot, StorageUnit) int64 219 220 ... 238
StorageUnit-p_store (snapshot, StorageUnit) int64 239 240 ... 258
StorageUnit-state_of_charge (snapshot, StorageUnit) int64 259 260 ... 278
StorageUnit-spill (snapshot, StorageUnit) int64 279 -1 ... 297 -1
Store-p (snapshot, Store) int64 299 300 301 ... 307 308
objective_constant int64 309
Constraints:
------------
Dimensions: (Generator-ext: 3, Line-ext: 7,
Link-ext: 6, Store-ext: 1,
StorageUnit-ext: 2, snapshot: 10,
Generator-fix: 3,
StorageUnit-state_of_charge_set_i: 1,
Bus: 10, cycles: 2, StorageUnit: 2,
Store: 1)
Coordinates:
* Generator-ext (Generator-ext) object 'Manchester...
* Line-ext (Line-ext) object '0' '1' ... '5' '6'
* Link-ext (Link-ext) object 'Norwich Convert...
* Store-ext (Store-ext) object 'store'
* StorageUnit-ext (StorageUnit-ext) object 'su' 'su2'
* snapshot (snapshot) datetime64[ns] 2015-01-...
* Generator-fix (Generator-fix) object 'Manchester...
* StorageUnit-state_of_charge_set_i (StorageUnit-state_of_charge_set_i) object ...
* Bus (Bus) object 'London' ... 'storebus'
* cycles (cycles) int64 0 1
* StorageUnit (StorageUnit) object 'su' 'su2'
* Store (Store) object 'store'
Data variables: (12/34)
Generator-ext-p_nom-lower (Generator-ext) int64 0 1 2
Generator-ext-p_nom-upper (Generator-ext) int64 -1 -1 -1
Line-ext-s_nom-lower (Line-ext) int64 6 7 8 9 10 11 12
Line-ext-s_nom-upper (Line-ext) int64 -1 -1 -1 -1 -1 -1 -1
Link-ext-p_nom-lower (Link-ext) int64 20 21 22 23 24 25
Link-ext-p_nom-upper (Link-ext) int64 -1 -1 -1 -1 30 31
... ...
StorageUnit-state_of_charge_set (snapshot, StorageUnit-state_of_charge_set_i) int64 ...
Bus-nodal_balance (Bus, snapshot) int64 628 629 ... 727
Kirchhoff-Voltage-Law (cycles, snapshot) int64 728 ... 747
StorageUnit-energy-balance (snapshot, StorageUnit) int64 748 ...
Store-energy-balance (snapshot, Store) int64 768 ... 777
GlobalConstraint-co2_limit int64 778
Status:
-------
ok
Modify model, optimize and feed back to network#
When you have a fresh network and you just want to create the model instance, run
[9]:
n.optimize.create_model();
WARNING:pypsa.components:The following lines have zero x, which could break the linear load flow:
Index(['2', '3', '4'], dtype='object', name='Line')
WARNING:pypsa.components:The following lines have zero r, which could break the linear load flow:
Index(['0', '1', '5', '6'], dtype='object', name='Line')
/home/docs/checkouts/readthedocs.org/user_builds/pypsa/conda/latest/lib/python3.11/site-packages/linopy/model.py:607: UserWarning: Reindexing constraint `Generator-fix-p-ramp_limit_up` to match existing coordinates.
warnings.warn(
/home/docs/checkouts/readthedocs.org/user_builds/pypsa/conda/latest/lib/python3.11/site-packages/linopy/model.py:607: UserWarning: Reindexing constraint `Generator-fix-p-ramp_limit_down` to match existing coordinates.
warnings.warn(
<__array_function__ internals>:200: RuntimeWarning: invalid value encountered in cast
<__array_function__ internals>:200: RuntimeWarning: invalid value encountered in cast
<__array_function__ internals>:200: RuntimeWarning: invalid value encountered in cast
<__array_function__ internals>:200: RuntimeWarning: invalid value encountered in cast
<__array_function__ internals>:200: RuntimeWarning: invalid value encountered in cast
<__array_function__ internals>:200: RuntimeWarning: invalid value encountered in cast
<__array_function__ internals>:200: RuntimeWarning: invalid value encountered in cast
Through the model instance we gain a lot of flexibility. Let’s say for example we want to remove the Kirchhoff Voltage Law constraint, thus convert the model to a transport model. This can be done via
[10]:
n.model.constraints.remove("Kirchhoff-Voltage-Law")
Now, we want to optimize the altered model and feed to solution back to the network. Here again, we use the optimize
accessor.
[11]:
n.optimize.solve_model()
INFO:linopy.model: Solve linear problem using Glpk solver
INFO:linopy.io: Writing time: 0.43s
INFO:linopy.constants: Optimization successful:
Status: ok
Termination condition: optimal
Solution: 300 primals, 728 duals
Objective: 1.41e+07
Solver model: not available
Solver message: optimal
GLPSOL--GLPK LP/MIP Solver 5.0
Parameter(s) specified in the command line:
--lp /tmp/linopy-problem-v6tj_7ke.lp --output /tmp/linopy-solve-1gkcvxla.sol
Reading problem data from '/tmp/linopy-problem-v6tj_7ke.lp'...
728 rows, 300 columns, 1511 non-zeros
4853 lines were read
GLPK Simplex Optimizer 5.0
728 rows, 300 columns, 1511 non-zeros
Preprocessing...
524 rows, 288 columns, 1294 non-zeros
Scaling...
A: min|aij| = 9.693e-03 max|aij| = 2.000e+00 ratio = 2.063e+02
GM: min|aij| = 5.546e-01 max|aij| = 1.803e+00 ratio = 3.251e+00
EQ: min|aij| = 3.122e-01 max|aij| = 1.000e+00 ratio = 3.203e+00
Constructing initial basis...
Size of triangular part is 523
0: obj = -1.290279700e+04 inf = 7.479e+04 (61)
132: obj = 4.039431164e+07 inf = 4.547e-13 (0) 1
* 306: obj = 1.412107457e+07 inf = 1.510e-11 (0) 1
OPTIMAL LP SOLUTION FOUND
Time used: 0.0 secs
Memory used: 0.9 Mb (939361 bytes)
Writing basic solution to '/tmp/linopy-solve-1gkcvxla.sol'...
[11]:
('ok', 'optimal')
Here, we followed the recommended way to run altered models:
Create the model instance -
n.optimize.create_model()
Modify the model to your needs
Solve and feed back -
n.optimize.solve_model()
For compatibility reasons the optimize
function, also allows passing a extra_funcionality
argument, as we know it from the lopf
function. The above behaviour with use of the extra functionality is obtained through
[12]:
def remove_kvl(n, sns):
print("KVL removed!")
n.model.constraints.remove("Kirchhoff-Voltage-Law")
n.optimize(extra_functionality=remove_kvl)
WARNING:pypsa.components:The following lines have zero x, which could break the linear load flow:
Index(['2', '3', '4'], dtype='object', name='Line')
WARNING:pypsa.components:The following lines have zero r, which could break the linear load flow:
Index(['0', '1', '5', '6'], dtype='object', name='Line')
WARNING:pypsa.components:The following lines have zero x, which could break the linear load flow:
Index(['2', '3', '4'], dtype='object', name='Line')
WARNING:pypsa.components:The following lines have zero r, which could break the linear load flow:
Index(['0', '1', '5', '6'], dtype='object', name='Line')
/home/docs/checkouts/readthedocs.org/user_builds/pypsa/conda/latest/lib/python3.11/site-packages/linopy/model.py:607: UserWarning: Reindexing constraint `Generator-fix-p-ramp_limit_up` to match existing coordinates.
warnings.warn(
/home/docs/checkouts/readthedocs.org/user_builds/pypsa/conda/latest/lib/python3.11/site-packages/linopy/model.py:607: UserWarning: Reindexing constraint `Generator-fix-p-ramp_limit_down` to match existing coordinates.
warnings.warn(
<__array_function__ internals>:200: RuntimeWarning: invalid value encountered in cast
<__array_function__ internals>:200: RuntimeWarning: invalid value encountered in cast
<__array_function__ internals>:200: RuntimeWarning: invalid value encountered in cast
<__array_function__ internals>:200: RuntimeWarning: invalid value encountered in cast
<__array_function__ internals>:200: RuntimeWarning: invalid value encountered in cast
<__array_function__ internals>:200: RuntimeWarning: invalid value encountered in cast
<__array_function__ internals>:200: RuntimeWarning: invalid value encountered in cast
INFO:linopy.model: Solve linear problem using Glpk solver
KVL removed!
INFO:linopy.io: Writing time: 0.43s
INFO:linopy.constants: Optimization successful:
Status: ok
Termination condition: optimal
Solution: 300 primals, 728 duals
Objective: 1.41e+07
Solver model: not available
Solver message: optimal
GLPSOL--GLPK LP/MIP Solver 5.0
Parameter(s) specified in the command line:
--lp /tmp/linopy-problem-r9mpkl0a.lp --output /tmp/linopy-solve-rs7sezor.sol
Reading problem data from '/tmp/linopy-problem-r9mpkl0a.lp'...
728 rows, 300 columns, 1511 non-zeros
4853 lines were read
GLPK Simplex Optimizer 5.0
728 rows, 300 columns, 1511 non-zeros
Preprocessing...
524 rows, 288 columns, 1294 non-zeros
Scaling...
A: min|aij| = 9.693e-03 max|aij| = 2.000e+00 ratio = 2.063e+02
GM: min|aij| = 5.546e-01 max|aij| = 1.803e+00 ratio = 3.251e+00
EQ: min|aij| = 3.122e-01 max|aij| = 1.000e+00 ratio = 3.203e+00
Constructing initial basis...
Size of triangular part is 523
0: obj = -1.290279700e+04 inf = 7.479e+04 (61)
132: obj = 4.039431164e+07 inf = 4.547e-13 (0) 1
* 306: obj = 1.412107457e+07 inf = 1.510e-11 (0) 1
OPTIMAL LP SOLUTION FOUND
Time used: 0.0 secs
Memory used: 0.9 Mb (939361 bytes)
Writing basic solution to '/tmp/linopy-solve-rs7sezor.sol'...
[12]:
('ok', 'optimal')
Additional constraints#
In the following, we exemplarily present a set of additional constraints. Note, the dual values of the additional constraints won’t be stored in default data fields in the PyPSA
network. But in any case, they are stored in the linopy.Model
.
Again, we first build the optimization model, add our constraints and finally solve the network. For the first step, we use again our accessor optimize
to access the function create_model
. This returns the linopy
model that we can modify.
[13]:
m = n.optimize.create_model() # the return value is the model, let's use it directly!
WARNING:pypsa.components:The following lines have zero x, which could break the linear load flow:
Index(['2', '3', '4'], dtype='object', name='Line')
WARNING:pypsa.components:The following lines have zero r, which could break the linear load flow:
Index(['0', '1', '5', '6'], dtype='object', name='Line')
/home/docs/checkouts/readthedocs.org/user_builds/pypsa/conda/latest/lib/python3.11/site-packages/linopy/model.py:607: UserWarning: Reindexing constraint `Generator-fix-p-ramp_limit_up` to match existing coordinates.
warnings.warn(
/home/docs/checkouts/readthedocs.org/user_builds/pypsa/conda/latest/lib/python3.11/site-packages/linopy/model.py:607: UserWarning: Reindexing constraint `Generator-fix-p-ramp_limit_down` to match existing coordinates.
warnings.warn(
<__array_function__ internals>:200: RuntimeWarning: invalid value encountered in cast
<__array_function__ internals>:200: RuntimeWarning: invalid value encountered in cast
<__array_function__ internals>:200: RuntimeWarning: invalid value encountered in cast
<__array_function__ internals>:200: RuntimeWarning: invalid value encountered in cast
<__array_function__ internals>:200: RuntimeWarning: invalid value encountered in cast
<__array_function__ internals>:200: RuntimeWarning: invalid value encountered in cast
<__array_function__ internals>:200: RuntimeWarning: invalid value encountered in cast
Minimum for state of charge
Assume we want to set a minimum state of charge of 50 MWh in our storage unit. This is done by:
[14]:
sus = m.variables["StorageUnit-state_of_charge"]
m.add_constraints(sus >= 50, name="StorageUnit-minimum_soc")
[14]:
Constraint `StorageUnit-minimum_soc` (snapshot: 10, StorageUnit: 2)
-------------------------------------------------------------------
[2015-01-01 00:00:00, su]: 1.0 StorageUnit-state_of_charge[2015-01-01 00:00:00, su] >= 50
[2015-01-01 00:00:00, su2]: 1.0 StorageUnit-state_of_charge[2015-01-01 00:00:00, su2] >= 50
[2015-01-01 01:00:00, su]: 1.0 StorageUnit-state_of_charge[2015-01-01 01:00:00, su] >= 50
[2015-01-01 01:00:00, su2]: 1.0 StorageUnit-state_of_charge[2015-01-01 01:00:00, su2] >= 50
[2015-01-01 02:00:00, su]: 1.0 StorageUnit-state_of_charge[2015-01-01 02:00:00, su] >= 50
[2015-01-01 02:00:00, su2]: 1.0 StorageUnit-state_of_charge[2015-01-01 02:00:00, su2] >= 50
[2015-01-01 03:00:00, su]: 1.0 StorageUnit-state_of_charge[2015-01-01 03:00:00, su] >= 50
...
[2015-01-01 06:00:00, su2]: 1.0 StorageUnit-state_of_charge[2015-01-01 06:00:00, su2] >= 50
[2015-01-01 07:00:00, su]: 1.0 StorageUnit-state_of_charge[2015-01-01 07:00:00, su] >= 50
[2015-01-01 07:00:00, su2]: 1.0 StorageUnit-state_of_charge[2015-01-01 07:00:00, su2] >= 50
[2015-01-01 08:00:00, su]: 1.0 StorageUnit-state_of_charge[2015-01-01 08:00:00, su] >= 50
[2015-01-01 08:00:00, su2]: 1.0 StorageUnit-state_of_charge[2015-01-01 08:00:00, su2] >= 50
[2015-01-01 09:00:00, su]: 1.0 StorageUnit-state_of_charge[2015-01-01 09:00:00, su] >= 50
[2015-01-01 09:00:00, su2]: 1.0 StorageUnit-state_of_charge[2015-01-01 09:00:00, su2] >= 50
The return value of the add_constraints
function is a array with the labels of the constraints. You can access the constraint now through:
[15]:
m.constraints["StorageUnit-minimum_soc"]
[15]:
Constraint `StorageUnit-minimum_soc` (snapshot: 10, StorageUnit: 2)
-------------------------------------------------------------------
[2015-01-01 00:00:00, su]: 1.0 StorageUnit-state_of_charge[2015-01-01 00:00:00, su] >= 50
[2015-01-01 00:00:00, su2]: 1.0 StorageUnit-state_of_charge[2015-01-01 00:00:00, su2] >= 50
[2015-01-01 01:00:00, su]: 1.0 StorageUnit-state_of_charge[2015-01-01 01:00:00, su] >= 50
[2015-01-01 01:00:00, su2]: 1.0 StorageUnit-state_of_charge[2015-01-01 01:00:00, su2] >= 50
[2015-01-01 02:00:00, su]: 1.0 StorageUnit-state_of_charge[2015-01-01 02:00:00, su] >= 50
[2015-01-01 02:00:00, su2]: 1.0 StorageUnit-state_of_charge[2015-01-01 02:00:00, su2] >= 50
[2015-01-01 03:00:00, su]: 1.0 StorageUnit-state_of_charge[2015-01-01 03:00:00, su] >= 50
...
[2015-01-01 06:00:00, su2]: 1.0 StorageUnit-state_of_charge[2015-01-01 06:00:00, su2] >= 50
[2015-01-01 07:00:00, su]: 1.0 StorageUnit-state_of_charge[2015-01-01 07:00:00, su] >= 50
[2015-01-01 07:00:00, su2]: 1.0 StorageUnit-state_of_charge[2015-01-01 07:00:00, su2] >= 50
[2015-01-01 08:00:00, su]: 1.0 StorageUnit-state_of_charge[2015-01-01 08:00:00, su] >= 50
[2015-01-01 08:00:00, su2]: 1.0 StorageUnit-state_of_charge[2015-01-01 08:00:00, su2] >= 50
[2015-01-01 09:00:00, su]: 1.0 StorageUnit-state_of_charge[2015-01-01 09:00:00, su] >= 50
[2015-01-01 09:00:00, su2]: 1.0 StorageUnit-state_of_charge[2015-01-01 09:00:00, su2] >= 50
and inspects its attributes like lhs
, sign
and rhs
, e.g.
[16]:
m.constraints["StorageUnit-minimum_soc"].rhs
[16]:
<xarray.DataArray 'StorageUnit-minimum_soc' ()> array(50)
Fix the ratio between ingoing and outgoing capacity of the Store
The battery in our system is modelled with two links and a store. We should make sure that its charging and discharging capacities, meaning their links, are somehow coupled.
[17]:
capacity = m.variables["Link-p_nom"]
eff = n.links.at["battery_power", "efficiency"]
lhs = capacity.loc["battery_power"] - eff * capacity.loc["battery_discharge"]
m.add_constraints(lhs == 0, name="Link-battery_fix_ratio")
[17]:
Constraint `Link-battery_fix_ratio`
-----------------------------------
1.0 Link-p_nom[battery_power] - 0.9 Link-p_nom[battery_discharge] = 0
Every bus must in total produce the 20% of the total demand
For this, we use the linopy function groupby_sum
which follows the pattern from pandas
/xarray
’s groupby
function.
[18]:
total_demand = n.loads_t.p_set.sum().sum()
buses = n.generators.bus.to_xarray()
prod_per_bus = m.variables["Generator-p"].groupby(buses).sum().sum("snapshot")
m.add_constraints(prod_per_bus >= total_demand / 5, name="Bus-minimum_production_share")
[18]:
Constraint `Bus-minimum_production_share` (bus: 3)
--------------------------------------------------
[Frankfurt]: 1.0 Generator-p[2015-01-01 00:00:00, Frankfurt Wind] + 1.0 Generator-p[2015-01-01 01:00:00, Frankfurt Wind] + 1.0 Generator-p[2015-01-01 02:00:00, Frankfurt Wind] ... 1.0 Generator-p[2015-01-01 07:00:00, Frankfurt Gas] + 1.0 Generator-p[2015-01-01 08:00:00, Frankfurt Gas] + 1.0 Generator-p[2015-01-01 09:00:00, Frankfurt Gas] >= 6509.525616820881
[Manchester]: 1.0 Generator-p[2015-01-01 00:00:00, Manchester Wind] + 1.0 Generator-p[2015-01-01 01:00:00, Manchester Wind] + 1.0 Generator-p[2015-01-01 02:00:00, Manchester Wind] ... 1.0 Generator-p[2015-01-01 07:00:00, Manchester Gas] + 1.0 Generator-p[2015-01-01 08:00:00, Manchester Gas] + 1.0 Generator-p[2015-01-01 09:00:00, Manchester Gas] >= 6509.525616820881
[Norway]: 1.0 Generator-p[2015-01-01 00:00:00, Norway Wind] + 1.0 Generator-p[2015-01-01 01:00:00, Norway Wind] + 1.0 Generator-p[2015-01-01 02:00:00, Norway Wind] ... 1.0 Generator-p[2015-01-01 07:00:00, Norway Gas] + 1.0 Generator-p[2015-01-01 08:00:00, Norway Gas] + 1.0 Generator-p[2015-01-01 09:00:00, Norway Gas] >= 6509.525616820881
[19]:
con = prod_per_bus >= total_demand / 5
[20]:
con
[20]:
AnomymousConstraint (bus: 3)
-----------------------------
[Frankfurt]: 1.0 Generator-p[2015-01-01 00:00:00, Frankfurt Wind] + 1.0 Generator-p[2015-01-01 01:00:00, Frankfurt Wind] + 1.0 Generator-p[2015-01-01 02:00:00, Frankfurt Wind] ... 1.0 Generator-p[2015-01-01 07:00:00, Frankfurt Gas] + 1.0 Generator-p[2015-01-01 08:00:00, Frankfurt Gas] + 1.0 Generator-p[2015-01-01 09:00:00, Frankfurt Gas] >= 6509.525616820881
[Manchester]: 1.0 Generator-p[2015-01-01 00:00:00, Manchester Wind] + 1.0 Generator-p[2015-01-01 01:00:00, Manchester Wind] + 1.0 Generator-p[2015-01-01 02:00:00, Manchester Wind] ... 1.0 Generator-p[2015-01-01 07:00:00, Manchester Gas] + 1.0 Generator-p[2015-01-01 08:00:00, Manchester Gas] + 1.0 Generator-p[2015-01-01 09:00:00, Manchester Gas] >= 6509.525616820881
[Norway]: 1.0 Generator-p[2015-01-01 00:00:00, Norway Wind] + 1.0 Generator-p[2015-01-01 01:00:00, Norway Wind] + 1.0 Generator-p[2015-01-01 02:00:00, Norway Wind] ... 1.0 Generator-p[2015-01-01 07:00:00, Norway Gas] + 1.0 Generator-p[2015-01-01 08:00:00, Norway Gas] + 1.0 Generator-p[2015-01-01 09:00:00, Norway Gas] >= 6509.525616820881
… and now let’s solve the network again.
[21]:
n.optimize.solve_model()
INFO:linopy.model: Solve linear problem using Glpk solver
INFO:linopy.io: Writing time: 0.47s
INFO:linopy.constants: Optimization successful:
Status: ok
Termination condition: optimal
Solution: 300 primals, 772 duals
Objective: 1.43e+07
Solver model: not available
Solver message: optimal
GLPSOL--GLPK LP/MIP Solver 5.0
Parameter(s) specified in the command line:
--lp /tmp/linopy-problem-as0rvkts.lp --output /tmp/linopy-solve-l275qni9.sol
Reading problem data from '/tmp/linopy-problem-as0rvkts.lp'...
772 rows, 300 columns, 1653 non-zeros
5171 lines were read
GLPK Simplex Optimizer 5.0
772 rows, 300 columns, 1653 non-zeros
Preprocessing...
548 rows, 288 columns, 1416 non-zeros
Scaling...
A: min|aij| = 9.693e-03 max|aij| = 2.000e+00 ratio = 2.063e+02
GM: min|aij| = 4.602e-01 max|aij| = 2.173e+00 ratio = 4.721e+00
EQ: min|aij| = 2.125e-01 max|aij| = 1.000e+00 ratio = 4.706e+00
Constructing initial basis...
Size of triangular part is 547
0: obj = 3.234549052e+03 inf = 8.754e+04 (102)
166: obj = 2.678169432e+07 inf = 3.411e-13 (0) 1
* 297: obj = 1.433711256e+07 inf = 3.582e-14 (0) 1
OPTIMAL LP SOLUTION FOUND
Time used: 0.0 secs
Memory used: 0.9 Mb (982675 bytes)
Writing basic solution to '/tmp/linopy-solve-l275qni9.sol'...
[21]:
('ok', 'optimal')
Analysing the constraints#
Let’s see if the system got our own constraints. We look at n.constraints
which combines summarises constraints going into the linear problem
[22]:
n.model.constraints
[22]:
linopy.model.Constraints
------------------------
Dimensions: (Generator-ext: 3, Line-ext: 7,
Link-ext: 6, Store-ext: 1,
StorageUnit-ext: 2, snapshot: 10,
Generator-fix: 3,
StorageUnit-state_of_charge_set_i: 1,
Bus: 10, cycles: 2, StorageUnit: 2,
Store: 1, bus: 3)
Coordinates: (12/13)
* Generator-ext (Generator-ext) object 'Manchester...
* Line-ext (Line-ext) object '0' '1' ... '5' '6'
* Link-ext (Link-ext) object 'Norwich Convert...
* Store-ext (Store-ext) object 'store'
* StorageUnit-ext (StorageUnit-ext) object 'su' 'su2'
* snapshot (snapshot) datetime64[ns] 2015-01-...
... ...
* StorageUnit-state_of_charge_set_i (StorageUnit-state_of_charge_set_i) object ...
* Bus (Bus) object 'London' ... 'storebus'
* cycles (cycles) int64 0 1
* StorageUnit (StorageUnit) object 'su' 'su2'
* Store (Store) object 'store'
* bus (bus) object 'Frankfurt' ... 'Norway'
Constraints:
* Generator-ext-p_nom-lower (Generator-ext)
* Generator-ext-p_nom-upper (Generator-ext)
* Line-ext-s_nom-lower (Line-ext)
* Line-ext-s_nom-upper (Line-ext)
* Link-ext-p_nom-lower (Link-ext)
* Link-ext-p_nom-upper (Link-ext)
* Store-ext-e_nom-lower (Store-ext)
* Store-ext-e_nom-upper (Store-ext)
* StorageUnit-ext-p_nom-lower (StorageUnit-ext)
* StorageUnit-ext-p_nom-upper (StorageUnit-ext)
* Generator-fix-p-lower (snapshot, Generator-fix)
* Generator-fix-p-upper (snapshot, Generator-fix)
* Generator-ext-p-lower (Generator-ext, snapshot)
* Generator-ext-p-upper (Generator-ext, snapshot)
* Generator-fix-p-ramp_limit_up (snapshot, Generator-fix)
* Generator-fix-p-ramp_limit_down (snapshot, Generator-fix)
* Line-ext-s-lower (Line-ext, snapshot)
* Line-ext-s-upper (Line-ext, snapshot)
* Link-ext-p-lower (Link-ext, snapshot)
* Link-ext-p-upper (Link-ext, snapshot)
* Store-ext-e-lower (Store-ext, snapshot)
* Store-ext-e-upper (Store-ext, snapshot)
* StorageUnit-ext-p_dispatch-lower (StorageUnit-ext, snapshot)
* StorageUnit-ext-p_dispatch-upper (StorageUnit-ext, snapshot)
* StorageUnit-ext-p_store-lower (StorageUnit-ext, snapshot)
* StorageUnit-ext-p_store-upper (StorageUnit-ext, snapshot)
* StorageUnit-ext-state_of_charge-lower (StorageUnit-ext, snapshot)
* StorageUnit-ext-state_of_charge-upper (StorageUnit-ext, snapshot)
* StorageUnit-state_of_charge_set (snapshot, StorageUnit-state_of_charge_set_i)
* Bus-nodal_balance (snapshot, Bus)
* Kirchhoff-Voltage-Law (snapshot, cycles)
* StorageUnit-energy-balance (snapshot, StorageUnit)
* Store-energy-balance (snapshot, Store)
* GlobalConstraint-co2_limit ()
* StorageUnit-minimum_soc (snapshot, StorageUnit)
* Link-battery_fix_ratio ()
* Bus-minimum_production_share (bus)
The last three entries show our constraints. Let’s check whether out two custom constraint are fulfilled:
[23]:
n.links.loc[["battery_power", "battery_discharge"], ["p_nom_opt"]]
[23]:
p_nom_opt | |
---|---|
Link | |
battery_power | 900.0 |
battery_discharge | 1000.0 |
[24]:
n.storage_units_t.state_of_charge
[24]:
StorageUnit | su | su2 |
---|---|---|
snapshot | ||
2015-01-01 00:00:00 | 1835.75 | 1000.000 |
2015-01-01 01:00:00 | 1836.06 | 490.091 |
2015-01-01 02:00:00 | 1886.06 | 490.091 |
2015-01-01 03:00:00 | 1936.06 | 490.091 |
2015-01-01 04:00:00 | 1986.06 | 1000.000 |
2015-01-01 05:00:00 | 50.00 | 1000.000 |
2015-01-01 06:00:00 | 50.00 | 50.000 |
2015-01-01 07:00:00 | 100.00 | 156.725 |
2015-01-01 08:00:00 | 50.00 | 140.119 |
2015-01-01 09:00:00 | 50.00 | 50.000 |
[25]:
n.generators_t.p.groupby(n.generators.bus, axis=1).sum().sum() / n.loads_t.p.sum().sum()
[25]:
bus
Frankfurt 0.200000
Manchester 0.200000
Norway 0.637048
dtype: float64
Looks good! Now, let’s see which dual values were parsed. Therefore we have a look into n.model.dual
[26]:
n.model.dual
[26]:
<xarray.Dataset> Dimensions: (Generator-ext: 3, Line-ext: 7, Link-ext: 6, Store-ext: 1, StorageUnit-ext: 2, snapshot: 10, Generator-fix: 3, StorageUnit-state_of_charge_set_i: 1, Bus: 10, cycles: 2, StorageUnit: 2, Store: 1, bus: 3) Coordinates: (12/13) * Generator-ext (Generator-ext) object 'Manchester... * Line-ext (Line-ext) object '0' '1' ... '5' '6' * Link-ext (Link-ext) object 'Norwich Convert... * Store-ext (Store-ext) object 'store' * StorageUnit-ext (StorageUnit-ext) object 'su' 'su2' * snapshot (snapshot) datetime64[ns] 2015-01-... ... ... * StorageUnit-state_of_charge_set_i (StorageUnit-state_of_charge_set_i) object ... * Bus (Bus) object 'London' ... 'storebus' * cycles (cycles) int64 0 1 * StorageUnit (StorageUnit) object 'su' 'su2' * Store (Store) object 'store' * bus (bus) object 'Frankfurt' ... 'Norway' Data variables: (12/37) Generator-ext-p_nom-lower (Generator-ext) float64 0.0 0.0 0.0 Generator-ext-p_nom-upper (Generator-ext) float64 nan nan nan Line-ext-s_nom-lower (Line-ext) float64 0.0 0.0 ... 0.0 Line-ext-s_nom-upper (Line-ext) float64 nan nan ... nan Link-ext-p_nom-lower (Link-ext) float64 0.0 0.0 ... 0.0 Link-ext-p_nom-upper (Link-ext) float64 nan nan ... -527.1 ... ... StorageUnit-energy-balance (snapshot, StorageUnit) float64 29... Store-energy-balance (snapshot, Store) float64 521.7 ..... GlobalConstraint-co2_limit float64 -950.5 StorageUnit-minimum_soc (snapshot, StorageUnit) float64 0.... Link-battery_fix_ratio float64 -567.9 Bus-minimum_production_share (bus) float64 35.2 45.42 0.0
[27]:
n.model.dual["StorageUnit-minimum_soc"]
[27]:
<xarray.DataArray 'StorageUnit-minimum_soc' (snapshot: 10, StorageUnit: 2)> array([[ 0. , 0. ], [ 0. , 0. ], [ 0. , 0. ], [ 0. , 0. ], [ 0. , 0. ], [ 0. , 0. ], [157.423 , 4.44444], [ 0. , 0. ], [ 0. , 0. ], [ 5.55556, 67.8484 ]]) Coordinates: * snapshot (snapshot) datetime64[ns] 2015-01-01 ... 2015-01-01T09:00:00 * StorageUnit (StorageUnit) object 'su' 'su2'
[28]:
n.model.dual["Link-battery_fix_ratio"]
[28]:
<xarray.DataArray 'Link-battery_fix_ratio' ()> array(-567.89)
[29]:
n.model.dual["Bus-minimum_production_share"]
[29]:
<xarray.DataArray 'Bus-minimum_production_share' (bus: 3)> array([35.2006, 45.4186, 0. ]) Coordinates: * bus (bus) object 'Frankfurt' 'Manchester' 'Norway'
These are the basic functionalities of the optimize
accessor. There are many more functions like abstract optimziation formulations (security constraint optimization, iterative transmission expansion optimization, etc.) or helper functions (fixing optimized capacities, adding load shedding). Try them out if you want!
[30]:
print("\n".join([func for func in n.optimize.__dir__() if not func.startswith("_")]))
create_model
solve_model
assign_solution
assign_duals
post_processing
optimize_transmission_expansion_iteratively
optimize_security_constrained
fix_optimal_capacities
fix_optimal_dispatch
add_load_shedding