Pathway Planning
In this example, we demonstrate how PyPSA can deal with optimisation problems spanning multiple investment periods, also known as pathway planning.
For models with multiple investment periods, the total set of snapshots is divided into investment periods, which translates into multi-indexed snapshots with investment periods as the first level time steps as the second level. In each investment period, new components may be added to the system. Additionally, any component may only operate as long as allowed by their lifetime.
In contrast to the models with a single investment period (overnight scenarios), the following concepts have to be taken into account.:
- The network attribute
n.investment_periods: This is the set of periods which specify when new components may be built. These have to be the same as the first level values in then.snapshotsindex. - The network attribute
n.investment_period_weightings: These specify the weighting of each period in the objective function and the global constraints. - The component attribute
build_year: Any one component may only be built when the build year is equal to the current investment period, or larger than the previous investment period. That means, components withbuild_year=2029are considered in the investment period2030, but not in the period2025. - The component attribute
lifetime: Any one component is only considered for dispatch in an investment period if it is still active at the beginning of an investment period. That means, components withbuild_year=2029andlifetime=30are considered in the investment period2055, but not in the period2060.
In the following, we set up a three bus network with generators, lines and storage units and run a optimisation for the investment periods 2020, 2030, 2040 and 2050.
In [2]:
Copied!
import numpy as np
import pandas as pd
import pypsa
rng = np.random.default_rng() # Create a random number generatorimport numpy as np import pandas as pd import pypsa rng = np.random.default_rng() # Create a random number generator
We set up the network with investment periods and snapshots.
In [3]:
Copied!
n = pypsa.Network()
years = [2020, 2030, 2040, 2050]
freq = 24
snapshots = pd.DatetimeIndex([])
for year in years:
period = pd.date_range(
start=f"{year}-01-01 00:00",
freq=f"{freq}h",
periods=8760 // freq,
)
snapshots = snapshots.append(period)
# convert to multiindex and assign to network
n.snapshots = pd.MultiIndex.from_arrays([snapshots.year, snapshots])
n.investment_periods = years
n.snapshot_weightingsn = pypsa.Network() years = [2020, 2030, 2040, 2050] freq = 24 snapshots = pd.DatetimeIndex([]) for year in years: period = pd.date_range( start=f"{year}-01-01 00:00", freq=f"{freq}h", periods=8760 // freq, ) snapshots = snapshots.append(period)
convert to multiindex and assign to network
n.snapshots = pd.MultiIndex.from_arrays([snapshots.year, snapshots]) n.investment_periods = years n.snapshot_weightings
/tmp/ipykernel_4316/349236114.py:1: PyPSA2Warning: pandas reads strings as `str` dtype since 3.0. PyPSA converts them back to object dtype on import, but keeps `str` from PyPSA 2.0 on. Set `pypsa.options.future.string_dtype` to True to opt in now or to False to keep the current behaviour.
n = pypsa.Network()Out[3]:
| objective | stores | generators | ||
|---|---|---|---|---|
| period | timestep | |||
| 2020 | 2020-01-01 | 1.0 | 1.0 | 1.0 |
| 2020-01-02 | 1.0 | 1.0 | 1.0 | |
| 2020-01-03 | 1.0 | 1.0 | 1.0 | |
| 2020-01-04 | 1.0 | 1.0 | 1.0 | |
| 2020-01-05 | 1.0 | 1.0 | 1.0 | |
| ... | ... | ... | ... | ... |
| 2050 | 2050-12-27 | 1.0 | 1.0 | 1.0 |
| 2050-12-28 | 1.0 | 1.0 | 1.0 | |
| 2050-12-29 | 1.0 | 1.0 | 1.0 | |
| 2050-12-30 | 1.0 | 1.0 | 1.0 | |
| 2050-12-31 | 1.0 | 1.0 | 1.0 |
1460 rows × 3 columns
In [4]:
Copied!
n.investment_periodsn.investment_periods
Out[4]:
Index([2020, 2030, 2040, 2050], dtype='int32', name='period')Set the years and objective weighting per investment period. For the objective weighting, we consider a discount rate defined by
where is the discount rate. For each period we sum up all discounts rates of the corresponding years which gives us the effective objective weighting.
In [5]:
Copied!
n.investment_period_weightings["years"] = list(np.diff(years)) + [10]
r = 0.01
T = 0
for period, nyears in n.investment_period_weightings.years.items():
discounts = [(1 / (1 + r) ** t) for t in range(T, T + nyears)]
n.investment_period_weightings.at[period, "objective"] = sum(discounts)
T += nyears
n.investment_period_weightingsn.investment_period_weightings["years"] = list(np.diff(years)) + [10] r = 0.01 T = 0 for period, nyears in n.investment_period_weightings.years.items(): discounts = [(1 / (1 + r) ** t) for t in range(T, T + nyears)] n.investment_period_weightings.at[period, "objective"] = sum(discounts) T += nyears n.investment_period_weightings
Out[5]:
| objective | years | |
|---|---|---|
| period | ||
| 2020 | 9.566018 | 10 |
| 2030 | 8.659991 | 10 |
| 2040 | 7.839777 | 10 |
| 2050 | 7.097248 | 10 |
Add the components
In [6]:
Copied!
for i in range(3):
n.add("Bus", f"bus {i}")
# add three lines in a ring
n.add(
"Line",
"line 0->1",
bus0="bus 0",
bus1="bus 1",
)
n.add(
"Line",
"line 1->2",
bus0="bus 1",
bus1="bus 2",
capital_cost=10,
build_year=2030,
)
n.add(
"Line",
"line 2->0",
bus0="bus 2",
bus1="bus 0",
)
n.lines["x"] = 0.0001
n.lines["s_nom_extendable"] = True
n.linesfor i in range(3): n.add("Bus", f"bus {i}")
add three lines in a ring
n.add( "Line", "line 0->1", bus0="bus 0", bus1="bus 1", ) n.add( "Line", "line 1->2", bus0="bus 1", bus1="bus 2", capital_cost=10, build_year=2030, ) n.add( "Line", "line 2->0", bus0="bus 2", bus1="bus 0", ) n.lines["x"] = 0.0001 n.lines["s_nom_extendable"] = True n.lines
Out[6]:
| bus0 | bus1 | type | x | r | g | b | s_nom | s_nom_mod | s_nom_extendable | ... | v_ang_max | sub_network | x_pu | r_pu | g_pu | b_pu | x_pu_eff | r_pu_eff | s_nom_opt | capital_cost_piecewise_opt | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| name | |||||||||||||||||||||
| line 0->1 | bus 0 | bus 1 | 0.0001 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | True | ... | inf | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | ||
| line 1->2 | bus 1 | bus 2 | 0.0001 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | True | ... | inf | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | ||
| line 2->0 | bus 2 | bus 0 | 0.0001 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | True | ... | inf | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 |
3 rows × 36 columns
In [7]:
Copied!
# add some generators
p_nom_max = pd.Series(
(rng.uniform() for sn in range(len(n.snapshots))),
index=n.snapshots,
name="generator ext 2020",
)
# renewable (can operate 2020, 2030)
n.add(
"Generator",
"generator ext 0 2020",
bus="bus 0",
p_nom=50,
build_year=2020,
lifetime=20,
marginal_cost=2,
capital_cost=1,
p_max_pu=p_nom_max,
carrier="solar",
p_nom_extendable=True,
)
# can operate 2040, 2050
n.add(
"Generator",
"generator ext 0 2040",
bus="bus 0",
p_nom=50,
build_year=2040,
lifetime=11,
marginal_cost=25,
capital_cost=10,
carrier="OCGT",
p_nom_extendable=True,
)
# can operate in 2040
n.add(
"Generator",
"generator fix 1 2040",
bus="bus 1",
p_nom=50,
build_year=2040,
lifetime=10,
carrier="CCGT",
marginal_cost=20,
capital_cost=1,
)
n.generatorsadd some generators
p_nom_max = pd.Series( (rng.uniform() for sn in range(len(n.snapshots))), index=n.snapshots, name="generator ext 2020", )
renewable (can operate 2020, 2030)
n.add( "Generator", "generator ext 0 2020", bus="bus 0", p_nom=50, build_year=2020, lifetime=20, marginal_cost=2, capital_cost=1, p_max_pu=p_nom_max, carrier="solar", p_nom_extendable=True, )
can operate 2040, 2050
n.add( "Generator", "generator ext 0 2040", bus="bus 0", p_nom=50, build_year=2040, lifetime=11, marginal_cost=25, capital_cost=10, carrier="OCGT", p_nom_extendable=True, )
can operate in 2040
n.add( "Generator", "generator fix 1 2040", bus="bus 1", p_nom=50, build_year=2040, lifetime=10, carrier="CCGT", marginal_cost=20, capital_cost=1, ) n.generators
Out[7]:
| bus | control | type | p_nom | p_nom_mod | p_nom_extendable | p_nom_min | p_nom_max | p_nom_set | p_min_pu | ... | down_time_before | ramp_limit_up | ramp_limit_down | ramp_limit_start_up | ramp_limit_shut_down | ramp_up_cost | ramp_down_cost | weight | p_nom_opt | capital_cost_piecewise_opt | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| name | |||||||||||||||||||||
| generator ext 0 2020 | bus 0 | PQ | 50.0 | 0.0 | True | 0.0 | inf | NaN | 0.0 | ... | 0 | NaN | NaN | NaN | NaN | 0.0 | 0.0 | 1.0 | 0.0 | 0.0 | |
| generator ext 0 2040 | bus 0 | PQ | 50.0 | 0.0 | True | 0.0 | inf | NaN | 0.0 | ... | 0 | NaN | NaN | NaN | NaN | 0.0 | 0.0 | 1.0 | 0.0 | 0.0 | |
| generator fix 1 2040 | bus 1 | PQ | 50.0 | 0.0 | False | 0.0 | inf | NaN | 0.0 | ... | 0 | NaN | NaN | NaN | NaN | 0.0 | 0.0 | 1.0 | 0.0 | 0.0 |
3 rows × 49 columns
In [8]:
Copied!
n.add(
"Store",
"store non-cyclic 2030",
bus="bus 2",
e_nom=0,
capital_cost=2,
build_year=2030,
lifetime=21,
e_cyclic=False,
e_nom_extendable=False,
)
n.add(
"Store",
"store periodic 2020",
bus="bus 2",
e_nom=0,
capital_cost=1,
build_year=2020,
lifetime=21,
e_cyclic=True,
e_cyclic_per_period=True,
e_nom_extendable=True,
)
n.storesn.add( "Store", "store non-cyclic 2030", bus="bus 2", e_nom=0, capital_cost=2, build_year=2030, lifetime=21, e_cyclic=False, e_nom_extendable=False, ) n.add( "Store", "store periodic 2020", bus="bus 2", e_nom=0, capital_cost=1, build_year=2020, lifetime=21, e_cyclic=True, e_cyclic_per_period=True, e_nom_extendable=True, ) n.stores
Out[8]:
| bus | control | type | carrier | e_nom | e_nom_mod | e_nom_extendable | e_nom_min | e_nom_max | e_nom_set | ... | standing_loss | efficiency_store | efficiency_dispatch | inflow | spill_cost | active | build_year | lifetime | e_nom_opt | capital_cost_piecewise_opt | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| name | |||||||||||||||||||||
| store non-cyclic 2030 | bus 2 | PQ | 0.0 | 0.0 | False | 0.0 | inf | NaN | ... | 0.0 | 1.0 | 1.0 | 0.0 | 0.0 | True | 2030 | 21.0 | 0.0 | 0.0 | ||
| store periodic 2020 | bus 2 | PQ | 0.0 | 0.0 | True | 0.0 | inf | NaN | ... | 0.0 | 1.0 | 1.0 | 0.0 | 0.0 | True | 2020 | 21.0 | 0.0 | 0.0 |
2 rows × 45 columns
Add the load
In [9]:
Copied!
load_var = pd.Series(
100 * rng.random(size=len(n.snapshots)), index=n.snapshots, name="load"
)
n.add("Load", "load 2", bus="bus 2", p_set=load_var)
load_fix = pd.Series(75, index=n.snapshots, name="load")
n.add("Load", "load 1", bus="bus 1", p_set=load_fix)
n.loads_t.p_set.head()load_var = pd.Series( 100 * rng.random(size=len(n.snapshots)), index=n.snapshots, name="load" ) n.add("Load", "load 2", bus="bus 2", p_set=load_var) load_fix = pd.Series(75, index=n.snapshots, name="load") n.add("Load", "load 1", bus="bus 1", p_set=load_fix) n.loads_t.p_set.head()
Out[9]:
| name | load 2 | load 1 | |
|---|---|---|---|
| period | timestep | ||
| 2020 | 2020-01-01 | 50.058343 | 75.0 |
| 2020-01-02 | 27.994693 | 75.0 | |
| 2020-01-03 | 7.448936 | 75.0 | |
| 2020-01-04 | 86.773219 | 75.0 | |
| 2020-01-05 | 44.859446 | 75.0 |
Run the optimization
In [10]:
Copied!
n.optimize(multi_investment_periods=True)n.optimize(multi_investment_periods=True)
/tmp/ipykernel_4316/264390566.py:1: PyPSA2Warning: From PyPSA 2.0 on, the objective leaves out the capital costs of existing capacity on extendable assets, which improves LP conditioning. Pass `include_objective_constant` to `n.optimize` to choose per call. Set `pypsa.options.future.exclude_objective_constant` to True to opt in now or to False to keep the current behaviour.
n.optimize(multi_investment_periods=True)
WARNING:pypsa.consistency:The following buses have carriers which are not defined. Run n.sanitize() to add them. Components with undefined carriers:
Index(['bus 0', 'bus 1', 'bus 2'], dtype='object', name='name')WARNING:pypsa.consistency:The following generators have carriers which are not defined. Run n.sanitize() to add them. Components with undefined carriers:
Index(['generator ext 0 2020', 'generator ext 0 2040', 'generator fix 1 2040'], dtype='object', name='name')WARNING:pypsa.consistency:The following stores have carriers which are not defined. Run n.sanitize() to add them. Components with undefined carriers:
Index(['store non-cyclic 2030', 'store periodic 2020'], dtype='object', name='name')WARNING:pypsa.consistency:The following lines have carriers which are not defined. Run n.sanitize() to add them. Components with undefined carriers:
Index(['line 0->1', 'line 1->2', 'line 2->0'], dtype='object', name='name')WARNING:pypsa.consistency:The following lines have zero r, which could break the linear load flow:
Index(['line 0->1', 'line 1->2', 'line 2->0'], dtype='object', name='name')INFO:pypsa.optimization.window:Building the multi-period model over a MultiIndex `snapshot`. Opt into the flat representation via `pypsa.options.optimization.model_snapshot_index = 'flat'`.WARNING:pypsa.optimization.constraints:Stores ['store periodic 2020']: Per-period cyclic (e_cyclic_per_period=True) overrides global cyclic (e_cyclic=True). Storage will cycle within each investment period, not across the entire horizon.INFO:linopy.model: Solve problem using Gurobi solverINFO:linopy.model:Solver options:
- log_to_console: FalseINFO:linopy.io:Writing objective.Writing constraints.: 0%| | 0/24 [00:00<?, ?it/s]Writing constraints.: 92%|█████████▏| 22/24 [00:00<00:00, 218.25it/s]Writing constraints.: 100%|██████████| 24/24 [00:00<00:00, 212.45it/s]Writing continuous variables.: 0%| | 0/9 [00:00<?, ?it/s]Writing continuous variables.: 100%|██████████| 9/9 [00:00<00:00, 349.62it/s]INFO:linopy.io: Writing time: 0.16sSet parameter WLSAccessIDSet parameter WLSSecretSet parameter LicenseID to value 2537914Academic license 2537914 - for non-commercial use only - registered to l.___@tu-berlin.deRead LP format model from file /tmp/linopy-problem-ol1brca1.lpReading time = 0.03 secondsobj: 28111 rows, 12417 columns, 52930 nonzerosSet parameter LogToConsole to value 0INFO:linopy.constants: Optimization successful:
Status: ok
Termination condition: optimal
Solution: 17527 primals, 43441 duals
Objective: 1.80e+07
Solver: gurobi
Runtime: 0.06s
Dual bound: 1.80e+07
Solver model: available
Solver message: 2INFO:pypsa.optimization.optimize:The shadow-prices of the constraints Generator-fix-p-lower, Generator-fix-p-upper, Generator-ext-p-lower, Generator-ext-p-upper, Line-ext-s-lower, Line-ext-s-upper, Store-fix-e-lower, Store-fix-e-upper, Store-ext-e-lower, Store-ext-e-upper, Store-fix-p-lower, Store-fix-p-upper, Store-ext-p-lower, Store-ext-p-upper, Store-fix-p_store-lower, Store-fix-p_store-upper, Store-ext-p_store-lower, Store-ext-p_store-upper, Kirchhoff-Voltage-Law, Store-energy_balance were not assigned to the network.Out[10]:
('ok', 'optimal')In [11]:
Copied!
c = "Generator"
df = pd.concat(
{
period: n.components[c].get_active_assets(period)
* n.components[c].static.p_nom_opt
for period in n.investment_periods
},
axis=1,
)
df.T.plot.bar(
stacked=True,
edgecolor="white",
width=1,
ylabel="Capacity (MW)",
xlabel="Investment Period",
rot=0,
)c = "Generator" df = pd.concat( { period: n.components[c].get_active_assets(period) * n.components[c].static.p_nom_opt for period in n.investment_periods }, axis=1, ) df.T.plot.bar( stacked=True, edgecolor="white", width=1, ylabel="Capacity (MW)", xlabel="Investment Period", rot=0, )
Out[11]:
<Axes: xlabel='Investment Period', ylabel='Capacity (MW)'>
In [12]:
Copied!
df = n.generators_t.p.sum(axis=0).T.div(1e3)
df.T.plot.bar(
stacked=True,
edgecolor="white",
width=1,
ylabel="Generation (GWh)",
xlabel="Investment Period",
rot=0,
)df = n.generators_t.p.sum(axis=0).T.div(1e3) df.T.plot.bar( stacked=True, edgecolor="white", width=1, ylabel="Generation (GWh)", xlabel="Investment Period", rot=0, )
Out[12]:
<Axes: xlabel='Investment Period', ylabel='Generation (GWh)'>
2026-11-02
2021-07-23