Time Series Aggregation
In this example, we are going to explore different ways to cluster the temporal resolution of PyPSA models, and what impact they have on the optimisation results and solving times. Using an hourly resolved variant of the single-node capacity expansion example, we will compare three different approaches to reduce the number of time steps in the model:
- Sampling: Selecting a subset of the given snapshots based on a given frequency,
- Averaging: Aggregating the snapshots by averaging them over a given frequency, and
- Segmentation: Clustering the snapshots into segments of a given frequency and using
tsamlibrary.
We start with the usual imports and loading the hourly resolved model.
In [2]:
Copied!
import logging
import time
import pandas as pd
import pypsa
logging.getLogger().setLevel(logging.WARNING)
template_n = pypsa.Network(
"https://tubcloud.tu-berlin.de/s/4ra3NKrLGzE42of/download/model-energy-hourly.nc"
)import logging import time import pandas as pd import pypsa logging.getLogger().setLevel(logging.WARNING) template_n = pypsa.Network( "https://tubcloud.tu-berlin.de/s/4ra3NKrLGzE42of/download/model-energy-hourly.nc" )
/tmp/ipykernel_8909/3104600064.py:11: 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.
template_n = pypsa.Network(
INFO:pypsa.network.io:Retrieving network data from https://tubcloud.tu-berlin.de/s/4ra3NKrLGzE42of/download/model-energy-hourly.nc.WARNING:pypsa.network.io:Importing network from PyPSA version v0.35.0 while current version is v1.4.0. Read the release notes at `https://go.pypsa.org/release-notes` to prepare your network for import.INFO:pypsa.network.io:Imported network 'Unnamed Network' has buses, carriers, generators, links, loads, storage_units, storesSince we also want to monitor the solving times, we will use a small utility function that wraps around the solving process and returns the seconds it took to solve the model:
In [3]:
Copied!
def time_it(func, *args, **kwargs):
"""Time the execution of a function and return the elapsed time in seconds."""
start_time = time.time()
result = func(*args, **kwargs)
elapsed_time = time.time() - start_time
return result, elapsed_time
logging.getLogger().setLevel(logging.WARNING)def time_it(func, *args, **kwargs): """Time the execution of a function and return the elapsed time in seconds.""" start_time = time.time() result = func(*args, **kwargs) elapsed_time = time.time() - start_time return result, elapsed_time logging.getLogger().setLevel(logging.WARNING)
Hourly Baseline
Additionally, we need a baseline model to compare aggregated models against. We will use the hourly resolved model for this.
In [4]:
Copied!
n_hourly = template_n.copy()
_, s_hourly = time_it(n_hourly.optimize, solver_name="gurobi")
s_hourlyn_hourly = template_n.copy() _, s_hourly = time_it(n_hourly.optimize, solver_name="gurobi") s_hourly
/tmp/ipykernel_8909/4243218267.py:4: 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.
result = func(*args, **kwargs)Writing constraints.: 0%| | 0/25 [00:00<?, ?it/s]Writing constraints.: 68%|██████▊ | 17/25 [00:00<00:00, 168.54it/s]Writing constraints.: 100%|██████████| 25/25 [00:00<00:00, 141.39it/s]Writing continuous variables.: 0%| | 0/12 [00:00<?, ?it/s]Writing continuous variables.: 100%|██████████| 12/12 [00:00<00:00, 354.32it/s]Set 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-hng49g3o.lpReading time = 0.26 secondsobj: 210246 rows, 96366 columns, 389916 nonzerosSet parameter LogToConsole to value 0Out[4]:
13.590047836303711Sampling
We will start with the sampling approach, which is the simplest one. We simply select every -th snapshot from the model. The important part here is that we need to adjust the snapshot weightings accordingly, as each remaining snapshot now represents hours. We iterate over N from 2 to 11, i.e. from 2-hourly to 11-hourly resolved models.
In [5]:
Copied!
sampling_n = {1: n_hourly}
sampling_s = {1: s_hourly}
for resolution in range(2, 12):
n = template_n.cluster.temporal.downsample(resolution)
_, s = time_it(n.optimize, solver_name="gurobi")
sampling_n[resolution] = n
sampling_s[resolution] = ssampling_n = {1: n_hourly} sampling_s = {1: s_hourly} for resolution in range(2, 12): n = template_n.cluster.temporal.downsample(resolution) _, s = time_it(n.optimize, solver_name="gurobi") sampling_n[resolution] = n sampling_s[resolution] = s
Writing constraints.: 0%| | 0/25 [00:00<?, ?it/s]Writing constraints.: 92%|█████████▏| 23/25 [00:00<00:00, 203.88it/s]Writing constraints.: 100%|██████████| 25/25 [00:00<00:00, 195.00it/s]Writing continuous variables.: 0%| | 0/12 [00:00<?, ?it/s]Writing continuous variables.: 100%|██████████| 12/12 [00:00<00:00, 396.67it/s]Set 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-1k4bk5ko.lpReading time = 0.12 secondsobj: 105126 rows, 48186 columns, 194958 nonzerosSet parameter LogToConsole to value 0Writing constraints.: 0%| | 0/25 [00:00<?, ?it/s]Writing constraints.: 92%|█████████▏| 23/25 [00:00<00:00, 226.24it/s]Writing constraints.: 100%|██████████| 25/25 [00:00<00:00, 218.83it/s]Writing continuous variables.: 0%| | 0/12 [00:00<?, ?it/s]Writing continuous variables.: 100%|██████████| 12/12 [00:00<00:00, 410.57it/s]Set 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-0evn0_is.lpReading time = 0.08 secondsobj: 70086 rows, 32126 columns, 129984 nonzerosSet parameter LogToConsole to value 0Writing constraints.: 0%| | 0/25 [00:00<?, ?it/s]Writing constraints.: 96%|█████████▌| 24/25 [00:00<00:00, 234.44it/s]Writing constraints.: 100%|██████████| 25/25 [00:00<00:00, 230.49it/s]Writing continuous variables.: 0%| | 0/12 [00:00<?, ?it/s]Writing continuous variables.: 100%|██████████| 12/12 [00:00<00:00, 433.03it/s]Set 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-cpdznysd.lpReading time = 0.06 secondsobj: 52566 rows, 24096 columns, 97490 nonzerosSet parameter LogToConsole to value 0Writing constraints.: 0%| | 0/25 [00:00<?, ?it/s]Writing constraints.: 96%|█████████▌| 24/25 [00:00<00:00, 238.11it/s]Writing constraints.: 100%|██████████| 25/25 [00:00<00:00, 232.47it/s]Writing continuous variables.: 0%| | 0/12 [00:00<?, ?it/s]Writing continuous variables.: 100%|██████████| 12/12 [00:00<00:00, 398.42it/s]Set 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-7ixp7ir5.lpReading time = 0.05 secondsobj: 42054 rows, 19278 columns, 77983 nonzerosSet parameter LogToConsole to value 0Writing constraints.: 0%| | 0/25 [00:00<?, ?it/s]Writing constraints.: 100%|██████████| 25/25 [00:00<00:00, 253.87it/s]Writing continuous variables.: 0%| | 0/12 [00:00<?, ?it/s]Writing continuous variables.: 100%|██████████| 12/12 [00:00<00:00, 422.66it/s]Set 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-ne1rpxew.lpReading time = 0.04 secondsobj: 35046 rows, 16066 columns, 64988 nonzerosSet parameter LogToConsole to value 0Writing constraints.: 0%| | 0/25 [00:00<?, ?it/s]Writing constraints.: 100%|██████████| 25/25 [00:00<00:00, 255.20it/s]Writing continuous variables.: 0%| | 0/12 [00:00<?, ?it/s]Writing continuous variables.: 100%|██████████| 12/12 [00:00<00:00, 438.46it/s]Set 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-h112cm82.lpReading time = 0.03 secondsobj: 30054 rows, 13778 columns, 55729 nonzerosSet parameter LogToConsole to value 0Writing constraints.: 0%| | 0/25 [00:00<?, ?it/s]Writing constraints.: 100%|██████████| 25/25 [00:00<00:00, 262.13it/s]Writing continuous variables.: 0%| | 0/12 [00:00<?, ?it/s]Writing continuous variables.: 100%|██████████| 12/12 [00:00<00:00, 438.87it/s]Set 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-zeznaw92.lpReading time = 0.03 secondsobj: 26286 rows, 12051 columns, 48778 nonzerosSet parameter LogToConsole to value 0Writing constraints.: 0%| | 0/25 [00:00<?, ?it/s]Writing constraints.: 92%|█████████▏| 23/25 [00:00<00:00, 229.55it/s]Writing constraints.: 100%|██████████| 25/25 [00:00<00:00, 222.95it/s]Writing continuous variables.: 0%| | 0/12 [00:00<?, ?it/s]Writing continuous variables.: 100%|██████████| 12/12 [00:00<00:00, 385.41it/s]Set 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-9q4fb10o.lpReading time = 0.03 secondsobj: 23382 rows, 10720 columns, 43368 nonzerosSet parameter LogToConsole to value 0Set 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-vj1koo50.lpReading time = 0.02 secondsobj: 21030 rows, 9642 columns, 38994 nonzerosSet parameter LogToConsole to value 0Set 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-4q8d2kdp.lpReading time = 0.02 secondsobj: 19134 rows, 8773 columns, 35482 nonzerosSet parameter LogToConsole to value 0Averaging
The averaging approach also has equal snapshot durations, but instead of selecting data from every -th snapshots, we average time series data for every snapshots. This means that the resulting model has times fewer snapshots, but each snapshot represents the average of original snapshots. Again, we need to adjust the snapshot weightings accordingly.
In [6]:
Copied!
averaging_n = {1: n_hourly}
averaging_s = {1: s_hourly}
for resolution in range(2, 12):
n = template_n.cluster.temporal.resample(f"{resolution}h")
_, s = time_it(n.optimize, solver_name="gurobi")
averaging_n[resolution] = n
averaging_s[resolution] = saveraging_n = {1: n_hourly} averaging_s = {1: s_hourly} for resolution in range(2, 12): n = template_n.cluster.temporal.resample(f"{resolution}h") _, s = time_it(n.optimize, solver_name="gurobi") averaging_n[resolution] = n averaging_s[resolution] = s
Writing constraints.: 0%| | 0/25 [00:00<?, ?it/s]Writing constraints.: 92%|█████████▏| 23/25 [00:00<00:00, 207.83it/s]Writing constraints.: 100%|██████████| 25/25 [00:00<00:00, 201.21it/s]Writing continuous variables.: 0%| | 0/12 [00:00<?, ?it/s]Writing continuous variables.: 100%|██████████| 12/12 [00:00<00:00, 393.86it/s]Set 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-fjn3us_0.lpReading time = 0.13 secondsobj: 105126 rows, 48186 columns, 195175 nonzerosSet parameter LogToConsole to value 0Writing constraints.: 0%| | 0/25 [00:00<?, ?it/s]Writing constraints.: 92%|█████████▏| 23/25 [00:00<00:00, 226.71it/s]Writing constraints.: 100%|██████████| 25/25 [00:00<00:00, 219.73it/s]Writing continuous variables.: 0%| | 0/12 [00:00<?, ?it/s]Writing continuous variables.: 100%|██████████| 12/12 [00:00<00:00, 416.72it/s]Set 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-qmhc3_n0.lpReading time = 0.08 secondsobj: 70086 rows, 32126 columns, 130192 nonzerosSet parameter LogToConsole to value 0Writing constraints.: 0%| | 0/25 [00:00<?, ?it/s]Writing constraints.: 100%|██████████| 25/25 [00:00<00:00, 240.30it/s]Writing constraints.: 100%|██████████| 25/25 [00:00<00:00, 238.78it/s]Writing continuous variables.: 0%| | 0/12 [00:00<?, ?it/s]Writing continuous variables.: 100%|██████████| 12/12 [00:00<00:00, 422.94it/s]Set 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-3fnyfz6k.lpReading time = 0.06 secondsobj: 52566 rows, 24096 columns, 97777 nonzerosSet parameter LogToConsole to value 0Writing constraints.: 0%| | 0/25 [00:00<?, ?it/s]Writing constraints.: 96%|█████████▌| 24/25 [00:00<00:00, 236.98it/s]Writing constraints.: 100%|██████████| 25/25 [00:00<00:00, 234.45it/s]Writing continuous variables.: 0%| | 0/12 [00:00<?, ?it/s]Writing continuous variables.: 100%|██████████| 12/12 [00:00<00:00, 426.05it/s]Set 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-5pqepnmx.lpReading time = 0.05 secondsobj: 42054 rows, 19278 columns, 78275 nonzerosSet parameter LogToConsole to value 0Writing constraints.: 0%| | 0/25 [00:00<?, ?it/s]Writing constraints.: 100%|██████████| 25/25 [00:00<00:00, 249.91it/s]Writing constraints.: 100%|██████████| 25/25 [00:00<00:00, 248.23it/s]Writing continuous variables.: 0%| | 0/12 [00:00<?, ?it/s]Writing continuous variables.: 100%|██████████| 12/12 [00:00<00:00, 431.08it/s]Set 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-eujkd2r_.lpReading time = 0.04 secondsobj: 35046 rows, 16066 columns, 65284 nonzerosSet parameter LogToConsole to value 0Writing constraints.: 0%| | 0/25 [00:00<?, ?it/s]Writing constraints.: 100%|██████████| 25/25 [00:00<00:00, 254.58it/s]Writing continuous variables.: 0%| | 0/12 [00:00<?, ?it/s]Writing continuous variables.: 100%|██████████| 12/12 [00:00<00:00, 433.03it/s]Set 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-izcwb2o8.lpReading time = 0.04 secondsobj: 30054 rows, 13778 columns, 56042 nonzerosSet parameter LogToConsole to value 0Writing constraints.: 0%| | 0/25 [00:00<?, ?it/s]Writing constraints.: 100%|██████████| 25/25 [00:00<00:00, 246.35it/s]Writing constraints.: 100%|██████████| 25/25 [00:00<00:00, 244.54it/s]Writing continuous variables.: 0%| | 0/12 [00:00<?, ?it/s]Writing continuous variables.: 100%|██████████| 12/12 [00:00<00:00, 433.78it/s]Set 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-57h55ohp.lpReading time = 0.03 secondsobj: 26286 rows, 12051 columns, 49143 nonzerosSet parameter LogToConsole to value 0Writing constraints.: 0%| | 0/25 [00:00<?, ?it/s]Writing constraints.: 100%|██████████| 25/25 [00:00<00:00, 262.95it/s]Writing continuous variables.: 0%| | 0/12 [00:00<?, ?it/s]Writing continuous variables.: 100%|██████████| 12/12 [00:00<00:00, 434.96it/s]Set 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-bjvgfo92.lpReading time = 0.03 secondsobj: 23382 rows, 10720 columns, 43678 nonzerosSet parameter LogToConsole to value 0Set 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-5boagu2n.lpReading time = 0.02 secondsobj: 21030 rows, 9642 columns, 39315 nonzerosSet parameter LogToConsole to value 0Set 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-jne6y7d2.lpReading time = 0.02 secondsobj: 19134 rows, 8773 columns, 35799 nonzerosSet parameter LogToConsole to value 0Segmentation
The segmentation approach is more complex. It uses a separate library called tsam to cluster the snapshots into segments of varying lengths. The sequence of snapshots is preserved as segments are only formed from neighbouring snapshots based on their similarity. For measuring similarity, it is advisable to normalise the time series data. This approach promises to capture the temporal patterns more effectively, as it can opt for higher resolution during periods of high variability and lower resolution during periods with low variability. The snapshot weightings are adjusted based on the number of snapshots in each segment.
In [7]:
Copied!
segmentation_n = {1: n_hourly}
segmentation_s = {1: s_hourly}
for resolution in range(2, 12):
# calculate number of segments equivalent to resolution
segments = int(8760 / resolution)
n = template_n.cluster.temporal.segment(segments)
# run optimization
_, s = time_it(n.optimize, solver_name="gurobi")
segmentation_n[resolution] = n
segmentation_s[resolution] = ssegmentation_n = {1: n_hourly} segmentation_s = {1: s_hourly} for resolution in range(2, 12):
calculate number of segments equivalent to resolution
segments = int(8760 / resolution) n = template_n.cluster.temporal.segment(segments)
run optimization
_, s = time_it(n.optimize, solver_name="gurobi") segmentation_n[resolution] = n segmentation_s[resolution] = s
Writing constraints.: 0%| | 0/25 [00:00<?, ?it/s]Writing constraints.: 92%|█████████▏| 23/25 [00:00<00:00, 207.21it/s]Writing constraints.: 100%|██████████| 25/25 [00:00<00:00, 199.22it/s]Writing continuous variables.: 0%| | 0/12 [00:00<?, ?it/s]Writing continuous variables.: 100%|██████████| 12/12 [00:00<00:00, 337.86it/s]Set 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-xf_8utyg.lpReading time = 0.13 secondsobj: 105126 rows, 48186 columns, 196032 nonzerosSet parameter LogToConsole to value 0Writing constraints.: 0%| | 0/25 [00:00<?, ?it/s]Writing constraints.: 92%|█████████▏| 23/25 [00:00<00:00, 224.13it/s]Writing constraints.: 100%|██████████| 25/25 [00:00<00:00, 217.98it/s]Writing continuous variables.: 0%| | 0/12 [00:00<?, ?it/s]Writing continuous variables.: 100%|██████████| 12/12 [00:00<00:00, 399.98it/s]Set 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-i40oqdu1.lpReading time = 0.08 secondsobj: 70086 rows, 32126 columns, 130934 nonzerosSet parameter LogToConsole to value 0Writing constraints.: 0%| | 0/25 [00:00<?, ?it/s]Writing constraints.: 96%|█████████▌| 24/25 [00:00<00:00, 239.40it/s]Writing constraints.: 100%|██████████| 25/25 [00:00<00:00, 234.35it/s]Writing continuous variables.: 0%| | 0/12 [00:00<?, ?it/s]Writing continuous variables.: 100%|██████████| 12/12 [00:00<00:00, 421.87it/s]Set 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-pn7li0hm.lpReading time = 0.06 secondsobj: 52566 rows, 24096 columns, 98300 nonzerosSet parameter LogToConsole to value 0Writing constraints.: 0%| | 0/25 [00:00<?, ?it/s]Writing constraints.: 100%|██████████| 25/25 [00:00<00:00, 240.98it/s]Writing constraints.: 100%|██████████| 25/25 [00:00<00:00, 239.16it/s]Writing continuous variables.: 0%| | 0/12 [00:00<?, ?it/s]Writing continuous variables.: 100%|██████████| 12/12 [00:00<00:00, 426.06it/s]Set 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-6ovk3qrh.lpReading time = 0.05 secondsobj: 42054 rows, 19278 columns, 78697 nonzerosSet parameter LogToConsole to value 0Writing constraints.: 0%| | 0/25 [00:00<?, ?it/s]Writing constraints.: 100%|██████████| 25/25 [00:00<00:00, 247.04it/s]Writing constraints.: 100%|██████████| 25/25 [00:00<00:00, 245.38it/s]Writing continuous variables.: 0%| | 0/12 [00:00<?, ?it/s]Writing continuous variables.: 100%|██████████| 12/12 [00:00<00:00, 421.26it/s]Set 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-fadmg8wk.lpReading time = 0.04 secondsobj: 35046 rows, 16066 columns, 65592 nonzerosSet parameter LogToConsole to value 0Writing constraints.: 0%| | 0/25 [00:00<?, ?it/s]Writing constraints.: 100%|██████████| 25/25 [00:00<00:00, 255.45it/s]Writing continuous variables.: 0%| | 0/12 [00:00<?, ?it/s]Writing continuous variables.: 100%|██████████| 12/12 [00:00<00:00, 435.38it/s]Set 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-ogsbcbj_.lpReading time = 0.04 secondsobj: 30030 rows, 13767 columns, 56209 nonzerosSet parameter LogToConsole to value 0Writing constraints.: 0%| | 0/25 [00:00<?, ?it/s]Writing constraints.: 100%|██████████| 25/25 [00:00<00:00, 251.65it/s]Writing continuous variables.: 0%| | 0/12 [00:00<?, ?it/s]Writing continuous variables.: 100%|██████████| 12/12 [00:00<00:00, 435.50it/s]Set 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-517izs6g.lpReading time = 0.03 secondsobj: 26286 rows, 12051 columns, 49207 nonzerosSet parameter LogToConsole to value 0Writing constraints.: 0%| | 0/25 [00:00<?, ?it/s]Writing constraints.: 100%|██████████| 25/25 [00:00<00:00, 259.14it/s]Writing continuous variables.: 0%| | 0/12 [00:00<?, ?it/s]Writing continuous variables.: 100%|██████████| 12/12 [00:00<00:00, 428.46it/s]Set 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-2m7gp7ba.lpReading time = 0.03 secondsobj: 23358 rows, 10709 columns, 43728 nonzerosSet parameter LogToConsole to value 0Set 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-g3wm3t19.lpReading time = 0.02 secondsobj: 21030 rows, 9642 columns, 39377 nonzerosSet parameter LogToConsole to value 0Set 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-8qo_bxcp.lpReading time = 0.02 secondsobj: 19110 rows, 8762 columns, 35786 nonzerosSet parameter LogToConsole to value 0Now before we go ahead with the evaluation of the different approaches, let's quickly glance at the distribution of snapshot durations obtained from the segmentation approach for a resolution equivalent to a 3-hourly model. We can see quite some variability in the segment length.
In [8]:
Copied!
segmentation_n[3].snapshot_weightings.generators.value_counts().sort_index(
ascending=True
).plot.bar(ylabel="snapshots [number]", xlabel="snapshot duration [h]")segmentation_n[3].snapshot_weightings.generators.value_counts().sort_index( ascending=True ).plot.bar(ylabel="snapshots [number]", xlabel="snapshot duration [h]")
Out[8]:
<Axes: xlabel='snapshot duration [h]', ylabel='snapshots [number]'>
Evaluation
Let's start our evaluation with a look at the solving times. We can see that across all approaches, the solving times quickly decay, especially as we go from hourly to 2-hourly resolved models and decrease less substantially afterwards.
In [9]:
Copied!
pd.Series(sampling_s).plot(label="sampling", legend=True)
pd.Series(averaging_s).plot(label="averaging", legend=True)
pd.Series(segmentation_s).plot(
label="segmentation", ylabel="time [s]", xlabel="resolution [h]", legend=True
)pd.Series(sampling_s).plot(label="sampling", legend=True) pd.Series(averaging_s).plot(label="averaging", legend=True) pd.Series(segmentation_s).plot( label="segmentation", ylabel="time [s]", xlabel="resolution [h]", legend=True )
Out[9]:
<Axes: xlabel='resolution [h]', ylabel='time [s]'>
Furthermore, we compare the relative error in total system costs compared to the hourly resolved model. We can see how the segmentation approach remains more stable than the other two approaches, especially for lower resolutions. Even with 11-hourly equivalent resolution, the segmentation approach only has a relative error of -1% compared to the hourly resolved model (i.e. it appears to be 1% cheaper). Similar patterns can be observed for the relative error in the total installed capacity of solar and batteries, two technologies that are particularly sensitive to the temporal resolution of the model.
In [10]:
Copied!
def tsc(n):
return (n.statistics.capex().sum() + n.statistics.opex().sum()) / 1e9
pd.concat(
[
pd.Series({("sampling", res): tsc(n) for res, n in sampling_n.items()}),
pd.Series({("averaging", res): tsc(n) for res, n in averaging_n.items()}),
pd.Series({("segmentation", res): tsc(n) for res, n in segmentation_n.items()}),
]
).unstack(0).div(tsc(n_hourly)).sub(1).mul(100).plot(
ylabel="relative objective error [%]", xlabel="resolution [h]", legend=True
)def tsc(n): return (n.statistics.capex().sum() + n.statistics.opex().sum()) / 1e9 pd.concat( [ pd.Series({("sampling", res): tsc(n) for res, n in sampling_n.items()}), pd.Series({("averaging", res): tsc(n) for res, n in averaging_n.items()}), pd.Series({("segmentation", res): tsc(n) for res, n in segmentation_n.items()}), ] ).unstack(0).div(tsc(n_hourly)).sub(1).mul(100).plot( ylabel="relative objective error [%]", xlabel="resolution [h]", legend=True )
Out[10]:
<Axes: xlabel='resolution [h]', ylabel='relative objective error [%]'>
In [11]:
Copied!
def solar(n):
return n.generators.loc["solar", "p_nom_opt"]
pd.concat(
[
pd.Series({("sampling", res): solar(n) for res, n in sampling_n.items()}),
pd.Series({("averaging", res): solar(n) for res, n in averaging_n.items()}),
pd.Series(
{("segmentation", res): solar(n) for res, n in segmentation_n.items()}
),
]
).unstack(0).div(solar(n_hourly)).sub(1).mul(100).plot(
ylabel="relative solar capacity error [%]", xlabel="resolution [h]", legend=True
)def solar(n): return n.generators.loc["solar", "p_nom_opt"] pd.concat( [ pd.Series({("sampling", res): solar(n) for res, n in sampling_n.items()}), pd.Series({("averaging", res): solar(n) for res, n in averaging_n.items()}), pd.Series( {("segmentation", res): solar(n) for res, n in segmentation_n.items()} ), ] ).unstack(0).div(solar(n_hourly)).sub(1).mul(100).plot( ylabel="relative solar capacity error [%]", xlabel="resolution [h]", legend=True )
Out[11]:
<Axes: xlabel='resolution [h]', ylabel='relative solar capacity error [%]'>
In [12]:
Copied!
def battery(n):
return n.storage_units.loc["battery storage", "p_nom_opt"]
pd.concat(
[
pd.Series({("sampling", res): battery(n) for res, n in sampling_n.items()}),
pd.Series({("averaging", res): battery(n) for res, n in averaging_n.items()}),
pd.Series(
{("segmentation", res): battery(n) for res, n in segmentation_n.items()}
),
]
).unstack(0).div(battery(n_hourly)).sub(1).mul(100).plot(
ylabel="relative battery capacity error [%]", xlabel="resolution [h]", legend=True
)def battery(n): return n.storage_units.loc["battery storage", "p_nom_opt"] pd.concat( [ pd.Series({("sampling", res): battery(n) for res, n in sampling_n.items()}), pd.Series({("averaging", res): battery(n) for res, n in averaging_n.items()}), pd.Series( {("segmentation", res): battery(n) for res, n in segmentation_n.items()} ), ] ).unstack(0).div(battery(n_hourly)).sub(1).mul(100).plot( ylabel="relative battery capacity error [%]", xlabel="resolution [h]", legend=True )
Out[12]:
<Axes: xlabel='resolution [h]', ylabel='relative battery capacity error [%]'>
2026-11-05
2025-08-04