Redispatch Example
In this example, we compare a 2-stage market with an initial market clearing in two bidding zones with flow-based market coupling and a subsequent redispatch market (incl. curtailment) to an idealised nodal pricing scheme.
In [2]:
Copied!
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
import pypsaimport cartopy.crs as ccrs import matplotlib.pyplot as plt import pypsa
Load example network
In [3]:
Copied!
o = pypsa.examples.scigrid_de()
o.lines.s_max_pu = 0.7
o.lines.loc[["316", "527", "602"], "s_nom"] = 1715
o.set_snapshots([o.snapshots[12]])o = pypsa.examples.scigrid_de() o.lines.s_max_pu = 0.7 o.lines.loc[["316", "527", "602"], "s_nom"] = 1715 o.set_snapshots([o.snapshots[12]])
/tmp/ipykernel_8631/1441250980.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.
o = pypsa.examples.scigrid_de()INFO:pypsa.network.io:Imported network 'SciGrid-DE' has buses, carriers, generators, lines, loads, storage_units, transformersIn [4]:
Copied!
n = o.copy() # for redispatch model
m = o.copy() # for market modeln = o.copy() # for redispatch model m = o.copy() # for market model
In [5]:
Copied!
o.plot();o.plot();
INFO:shapefile:File length is exactly the declared header and records size, but misses the EOF byte. Last byte is 0x1a, but that value could also be from the last record's data. Note: In previous versions of PyShp, Writers did not output a .dbf EOF byte so no warning will be given. Set strict=True to raise an Exception for this instead. 
Solve original nodal market model o
First, let us solve a nodal market using the original model o:
In [6]:
Copied!
o.optimize()o.optimize()
/tmp/ipykernel_8631/3726322918.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.
o.optimize()
WARNING:pypsa.consistency:The following transformers have zero r, which could break the linear load flow:
Index(['2', '5', '10', '12', '13', '15', '18', '20', '22', '24', '26', '30',
'32', '37', '42', '46', '52', '56', '61', '68', '69', '74', '78', '86',
'87', '94', '95', '96', '99', '100', '104', '105', '106', '107', '117',
'120', '123', '124', '125', '128', '129', '138', '143', '156', '157',
'159', '160', '165', '184', '191', '195', '201', '220', '231', '232',
'233', '236', '247', '248', '250', '251', '252', '261', '263', '264',
'267', '272', '279', '281', '282', '292', '303', '307', '308', '312',
'315', '317', '322', '332', '334', '336', '338', '351', '353', '360',
'362', '382', '384', '385', '391', '403', '404', '413', '421', '450',
'458'],
dtype='object', name='name')INFO:linopy.model: Solve problem using Gurobi solverINFO:linopy.model:Solver options:
- log_to_console: FalseINFO:linopy.io: Writing time: 0.08sSet 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-jzz57nk1.lpReading time = 0.01 secondsobj: 5957 rows, 2485 columns, 9939 nonzerosSet parameter LogToConsole to value 0INFO:linopy.constants: Optimization successful:
Status: ok
Termination condition: optimal
Solution: 2485 primals, 5957 duals
Objective: 3.01e+05
Solver: gurobi
Runtime: 0.03s
Dual bound: 3.01e+05
Solver model: available
Solver message: 2INFO:pypsa.optimization.optimize:The shadow-prices of the constraints Generator-fix-p-lower, Generator-fix-p-upper, Line-fix-s-lower, Line-fix-s-upper, Transformer-fix-s-lower, Transformer-fix-s-upper, StorageUnit-fix-p_dispatch-lower, StorageUnit-fix-p_dispatch-upper, StorageUnit-fix-p_store-lower, StorageUnit-fix-p_store-upper, StorageUnit-fix-state_of_charge-lower, StorageUnit-fix-state_of_charge-upper, Kirchhoff-Voltage-Law, StorageUnit-energy_balance were not assigned to the network.Out[6]:
('ok', 'optimal')Costs are 301 k€.
Build market model m with two bidding zones
For this example, we split the German transmission network into two market zones at latitude 51 degrees.
You can build any other market zones by providing an alternative mapping from bus to zone.
In [7]:
Copied!
zones = (n.buses.y > 51).map(lambda x: "North" if x else "South")zones = (n.buses.y > 51).map(lambda x: "North" if x else "South")
Next, we assign this mapping to the market model m.
We re-assign the buses of all generators and loads, and remove all transmission lines within each bidding zone.
Here, we assume that the bidding zones are coupled through the transmission lines that connect them.
In [8]:
Copied!
for c in m.components:
if c.name not in m.one_port_components:
continue
c.static.bus = c.static.bus.map(zones)
for c in m.components:
if c.name not in m.branch_components:
continue
c.static.bus0 = c.static.bus0.map(zones)
c.static.bus1 = c.static.bus1.map(zones)
internal = c.static.bus0 == c.static.bus1
m.remove(c.name, c.static.loc[internal].index)
m.remove("Bus", m.buses.index)
m.add("Bus", ["North", "South"]);for c in m.components: if c.name not in m.one_port_components: continue c.static.bus = c.static.bus.map(zones) for c in m.components: if c.name not in m.branch_components: continue c.static.bus0 = c.static.bus0.map(zones) c.static.bus1 = c.static.bus1.map(zones) internal = c.static.bus0 == c.static.bus1 m.remove(c.name, c.static.loc[internal].index) m.remove("Bus", m.buses.index) m.add("Bus", ["North", "South"]);
Now, we can solve the coupled market with two bidding zones.
In [9]:
Copied!
m.optimize()m.optimize()
INFO:linopy.model: Solve problem using Gurobi solverINFO:linopy.model:Solver options:
- log_to_console: FalseINFO:linopy.io: Writing time: 0.06sSet 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-9rt3jb2p.lpReading time = 0.00 secondsobj: 3185 rows, 1561 columns, 4829 nonzerosSet parameter LogToConsole to value 0INFO:linopy.constants: Optimization successful:
Status: ok
Termination condition: optimal
Solution: 1561 primals, 3185 duals
Objective: 2.14e+05
Solver: gurobi
Runtime: 0.00s
Dual bound: 2.14e+05
Solver model: available
Solver message: 2INFO:pypsa.optimization.optimize:The shadow-prices of the constraints Generator-fix-p-lower, Generator-fix-p-upper, Line-fix-s-lower, Line-fix-s-upper, StorageUnit-fix-p_dispatch-lower, StorageUnit-fix-p_dispatch-upper, StorageUnit-fix-p_store-lower, StorageUnit-fix-p_store-upper, StorageUnit-fix-state_of_charge-lower, StorageUnit-fix-state_of_charge-upper, Kirchhoff-Voltage-Law, StorageUnit-energy_balance were not assigned to the network.Out[9]:
('ok', 'optimal')Costs are 214 k€, which is much lower than the 301 k€ of the nodal market.
This is because network restrictions apart from the North/South division are not taken into account yet.
We can look at the market clearing prices of each zone:
In [10]:
Copied!
m.buses_t.marginal_pricem.buses_t.marginal_price
Out[10]:
| name | North | South |
|---|---|---|
| snapshot | ||
| 2011-01-01 12:00:00 | 8.0 | 25.0 |
Build redispatch model n
Next, based on the market outcome with two bidding zones m, we build a secondary redispatch market n that rectifies transmission constraints through curtailment and ramping up/down thermal generators.
First, we fix the dispatch of generators to the results from the market simulation. (For simplicity, this example disregards storage units.)
In [11]:
Copied!
p = m.generators_t.p / m.generators.p_nom
n.generators_t.p_min_pu = p
n.generators_t.p_max_pu = pp = m.generators_t.p / m.generators.p_nom n.generators_t.p_min_pu = p n.generators_t.p_max_pu = p
Then, we add generators bidding into redispatch market using the following assumptions:
- All generators can reduce their dispatch to zero. This includes also curtailment of renewables.
- All generators can increase their dispatch to their available/nominal capacity.
- No changes to the marginal costs, i.e. reducing dispatch lowers costs.
With these settings, the 2-stage market should result in the same cost as the nodal market.
In [12]:
Copied!
g_up = n.generators.copy()
g_down = n.generators.copy()
g_up.index = g_up.index.map(lambda x: x + " ramp up")
g_down.index = g_down.index.map(lambda x: x + " ramp down")
up = (
m.get_switchable_as_dense("Generator", "p_max_pu") * m.generators.p_nom
- m.generators_t.p
).clip(0) / m.generators.p_nom
down = -m.generators_t.p / m.generators.p_nom
up.columns = up.columns.map(lambda x: x + " ramp up")
down.columns = down.columns.map(lambda x: x + " ramp down")
n.add("Generator", g_up.index, p_max_pu=up, **g_up.drop("p_max_pu", axis=1))
n.add(
"Generator",
g_down.index,
p_min_pu=down,
p_max_pu=0,
**g_down.drop(["p_max_pu", "p_min_pu"], axis=1),
);g_up = n.generators.copy() g_down = n.generators.copy() g_up.index = g_up.index.map(lambda x: x + " ramp up") g_down.index = g_down.index.map(lambda x: x + " ramp down") up = ( m.get_switchable_as_dense("Generator", "p_max_pu") * m.generators.p_nom
- m.generators_t.p ).clip(0) / m.generators.p_nom down = -m.generators_t.p / m.generators.p_nom up.columns = up.columns.map(lambda x: x + " ramp up") down.columns = down.columns.map(lambda x: x + " ramp down") n.add("Generator", g_up.index, p_max_pu=up, **g_up.drop("p_max_pu", axis=1)) n.add( "Generator", g_down.index, p_min_pu=down, p_max_pu=0, **g_down.drop(["p_max_pu", "p_min_pu"], axis=1), );
Now, let's solve the redispatch market:
In [13]:
Copied!
n.optimize()n.optimize()
WARNING:pypsa.consistency:The following transformers have zero r, which could break the linear load flow:
Index(['2', '5', '10', '12', '13', '15', '18', '20', '22', '24', '26', '30',
'32', '37', '42', '46', '52', '56', '61', '68', '69', '74', '78', '86',
'87', '94', '95', '96', '99', '100', '104', '105', '106', '107', '117',
'120', '123', '124', '125', '128', '129', '138', '143', '156', '157',
'159', '160', '165', '184', '191', '195', '201', '220', '231', '232',
'233', '236', '247', '248', '250', '251', '252', '261', '263', '264',
'267', '272', '279', '281', '282', '292', '303', '307', '308', '312',
'315', '317', '322', '332', '334', '336', '338', '351', '353', '360',
'362', '382', '384', '385', '391', '403', '404', '413', '421', '450',
'458'],
dtype='object', name='name')INFO:linopy.model: Solve problem using Gurobi solverINFO:linopy.model:Solver options:
- log_to_console: FalseINFO:linopy.io: Writing time: 0.07sSet 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-2wrm4lam.lpReading time = 0.01 secondsobj: 11649 rows, 5331 columns, 18477 nonzerosSet parameter LogToConsole to value 0INFO:linopy.constants: Optimization successful:
Status: ok
Termination condition: optimal
Solution: 5331 primals, 11649 duals
Objective: 3.01e+05
Solver: gurobi
Runtime: 0.03s
Dual bound: 3.01e+05
Solver model: available
Solver message: 2INFO:pypsa.optimization.optimize:The shadow-prices of the constraints Generator-fix-p-lower, Generator-fix-p-upper, Line-fix-s-lower, Line-fix-s-upper, Transformer-fix-s-lower, Transformer-fix-s-upper, StorageUnit-fix-p_dispatch-lower, StorageUnit-fix-p_dispatch-upper, StorageUnit-fix-p_store-lower, StorageUnit-fix-p_store-upper, StorageUnit-fix-state_of_charge-lower, StorageUnit-fix-state_of_charge-upper, Kirchhoff-Voltage-Law, StorageUnit-energy_balance were not assigned to the network.Out[13]:
('ok', 'optimal')And, as expected, the costs are the same as for the nodal market: 301 k€.
Now, we can plot both the market results of the 2 bidding zone market and the redispatch results:
In [14]:
Copied!
fig, axs = plt.subplots(
1, 3, figsize=(20, 10), subplot_kw={"projection": ccrs.AlbersEqualArea()}
)
market = (
n.generators_t.p[m.generators.index]
.T.squeeze()
.groupby(n.generators.bus)
.sum()
.div(2e4)
)
n.plot(ax=axs[0], bus_size=market, title="2 bidding zones market simulation")
redispatch_up = (
n.generators_t.p.filter(like="ramp up")
.T.squeeze()
.groupby(n.generators.bus)
.sum()
.div(2e4)
)
n.plot(ax=axs[1], bus_size=redispatch_up, bus_color="blue", title="Redispatch: ramp up")
redispatch_down = (
n.generators_t.p.filter(like="ramp down")
.T.squeeze()
.groupby(n.generators.bus)
.sum()
.div(-2e4)
)
n.plot(
ax=axs[2],
bus_size=redispatch_down,
bus_color="red",
title="Redispatch: ramp down / curtail",
);fig, axs = plt.subplots( 1, 3, figsize=(20, 10), subplot_kw={"projection": ccrs.AlbersEqualArea()} ) market = ( n.generators_t.p[m.generators.index] .T.squeeze() .groupby(n.generators.bus) .sum() .div(2e4) ) n.plot(ax=axs[0], bus_size=market, title="2 bidding zones market simulation") redispatch_up = ( n.generators_t.p.filter(like="ramp up") .T.squeeze() .groupby(n.generators.bus) .sum() .div(2e4) ) n.plot(ax=axs[1], bus_size=redispatch_up, bus_color="blue", title="Redispatch: ramp up") redispatch_down = ( n.generators_t.p.filter(like="ramp down") .T.squeeze() .groupby(n.generators.bus) .sum() .div(-2e4) ) n.plot( ax=axs[2], bus_size=redispatch_down, bus_color="red", title="Redispatch: ramp down / curtail", );

We can also read out the final dispatch of each generator:
In [15]:
Copied!
grouper = n.generators.index.str.split(" ramp", expand=True).get_level_values(0)
n.generators_t.p.T.groupby(grouper).sum().squeeze()grouper = n.generators.index.str.split(" ramp", expand=True).get_level_values(0) n.generators_t.p.T.groupby(grouper).sum().squeeze()
Out[15]:
1 Gas 0.000000
1 Hard Coal 0.000000
1 Solar 0.000000
1 Wind Onshore 22.277855
100_220kV Solar 14.913326
...
98 Wind Onshore 71.451646
99_220kV Gas 0.000000
99_220kV Hard Coal 0.000000
99_220kV Solar 8.246606
99_220kV Wind Onshore 3.432939
Name: 2011-01-01 12:00:00, Length: 1423, dtype: float64Changing bidding strategies in redispatch market
We can also formulate other bidding strategies or compensation mechanisms for the redispatch market.
For example, that ramping up a generator is twice as expensive.
In [16]:
Copied!
n.generators.loc[n.generators.index.str.contains("ramp up"), "marginal_cost"] *= 2n.generators.loc[n.generators.index.str.contains("ramp up"), "marginal_cost"] *= 2
Or that generators need to be compensated for curtailing them or ramping them down at 50% of their marginal cost.
In [17]:
Copied!
n.generators.loc[n.generators.index.str.contains("ramp down"), "marginal_cost"] *= -0.5n.generators.loc[n.generators.index.str.contains("ramp down"), "marginal_cost"] *= -0.5
In this way, the outcome should be more expensive than the ideal nodal market:
In [18]:
Copied!
n.optimize()n.optimize()
WARNING:pypsa.consistency:The following transformers have zero r, which could break the linear load flow:
Index(['2', '5', '10', '12', '13', '15', '18', '20', '22', '24', '26', '30',
'32', '37', '42', '46', '52', '56', '61', '68', '69', '74', '78', '86',
'87', '94', '95', '96', '99', '100', '104', '105', '106', '107', '117',
'120', '123', '124', '125', '128', '129', '138', '143', '156', '157',
'159', '160', '165', '184', '191', '195', '201', '220', '231', '232',
'233', '236', '247', '248', '250', '251', '252', '261', '263', '264',
'267', '272', '279', '281', '282', '292', '303', '307', '308', '312',
'315', '317', '322', '332', '334', '336', '338', '351', '353', '360',
'362', '382', '384', '385', '391', '403', '404', '413', '421', '450',
'458'],
dtype='object', name='name')Warning: environment still referenced so free is deferred (Continue to use WLS)INFO:linopy.model: Solve problem using Gurobi solverINFO:linopy.model:Solver options:
- log_to_console: FalseINFO:linopy.io: Writing time: 0.07sSet 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-ofctwbe4.lpReading time = 0.01 secondsobj: 11649 rows, 5331 columns, 18477 nonzerosSet parameter LogToConsole to value 0INFO:linopy.constants: Optimization successful:
Status: ok
Termination condition: optimal
Solution: 5331 primals, 11649 duals
Objective: 5.02e+05
Solver: gurobi
Runtime: 0.03s
Dual bound: 5.02e+05
Solver model: available
Solver message: 2INFO:pypsa.optimization.optimize:The shadow-prices of the constraints Generator-fix-p-lower, Generator-fix-p-upper, Line-fix-s-lower, Line-fix-s-upper, Transformer-fix-s-lower, Transformer-fix-s-upper, StorageUnit-fix-p_dispatch-lower, StorageUnit-fix-p_dispatch-upper, StorageUnit-fix-p_store-lower, StorageUnit-fix-p_store-upper, StorageUnit-fix-state_of_charge-lower, StorageUnit-fix-state_of_charge-upper, Kirchhoff-Voltage-Law, StorageUnit-energy_balance were not assigned to the network.Out[18]:
('ok', 'optimal')Costs are now 502 k€ compared to 301 k€.
2026-08-05
2022-09-11