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.
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"
)
/home/runner/work/PyPSA/PyPSA/pypsa/network/io.py:2085: FutureWarning: pandas infers the `str` dtype for string data since its version 3.0. PyPSA still converts it back to numpy object dtype on import, but will keep it from PyPSA 2.0 on. Set `pypsa.options.api.legacy_string_dtype` explicitly to suppress this warning. new_static = _coerce_string_dtypes(new_static) 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, stores
Since 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:
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.
n_hourly = template_n.copy()
_, s_hourly = time_it(n_hourly.optimize, solver_name="gurobi")
s_hourly
/tmp/ipykernel_8514/4243218267.py:4: FutureWarning: The default value of `include_objective_constant` will change from True to False in version 2.0. Set `include_objective_constant` explicitly to suppress this warning. Using False improves LP numerical conditioning by not including the objective constant as a variable. result = func(*args, **kwargs)
Writing constraints.: 0%| | 0/25 [00:00<?, ?it/s]
Writing constraints.: 68%|██████▊ | 17/25 [00:00<00:00, 167.24it/s]
Writing constraints.: 100%|██████████| 25/25 [00:00<00:00, 138.99it/s]
Writing continuous variables.: 0%| | 0/12 [00:00<?, ?it/s]
Writing continuous variables.: 100%|██████████| 12/12 [00:00<00:00, 322.74it/s]
Set parameter WLSAccessID
Set parameter WLSSecret
Set parameter LicenseID to value 2537914
Academic license 2537914 - for non-commercial use only - registered to l.___@tu-berlin.de
Read LP format model from file /tmp/linopy-problem-a4hfm4jc.lp
Reading time = 0.25 seconds
obj: 210246 rows, 96366 columns, 389916 nonzeros
Set parameter LogToConsole to value 0
13.855286836624146
Sampling¶
We will start with the sampling approach, which is the simplest one. We simply select every \(N\)-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 \(N\) hours. We iterate over N from 2 to 11, i.e. from 2-hourly to 11-hourly resolved models.
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] = s
/tmp/ipykernel_8514/4243218267.py:4: FutureWarning: The default value of `include_objective_constant` will change from True to False in version 2.0. Set `include_objective_constant` explicitly to suppress this warning. Using False improves LP numerical conditioning by not including the objective constant as a variable. result = func(*args, **kwargs)
Writing constraints.: 0%| | 0/25 [00:00<?, ?it/s]
Writing constraints.: 88%|████████▊ | 22/25 [00:00<00:00, 212.68it/s]
Writing constraints.: 100%|██████████| 25/25 [00:00<00:00, 191.10it/s]
Writing continuous variables.: 0%| | 0/12 [00:00<?, ?it/s]
Writing continuous variables.: 100%|██████████| 12/12 [00:00<00:00, 370.90it/s]
Set parameter WLSAccessID
Set parameter WLSSecret
Set parameter LicenseID to value 2537914
Academic license 2537914 - for non-commercial use only - registered to l.___@tu-berlin.de
Read LP format model from file /tmp/linopy-problem-5rc4pfsb.lp
Reading time = 0.11 seconds
obj: 105126 rows, 48186 columns, 194958 nonzeros
Set parameter LogToConsole to value 0
/tmp/ipykernel_8514/4243218267.py:4: FutureWarning: The default value of `include_objective_constant` will change from True to False in version 2.0. Set `include_objective_constant` explicitly to suppress this warning. Using False improves LP numerical conditioning by not including the objective constant as a variable. result = func(*args, **kwargs)
Writing constraints.: 0%| | 0/25 [00:00<?, ?it/s]
Writing constraints.: 92%|█████████▏| 23/25 [00:00<00:00, 224.72it/s]
Writing constraints.: 100%|██████████| 25/25 [00:00<00:00, 216.88it/s]
Writing continuous variables.: 0%| | 0/12 [00:00<?, ?it/s]
Writing continuous variables.: 100%|██████████| 12/12 [00:00<00:00, 392.06it/s]
Set parameter WLSAccessID
Set parameter WLSSecret
Set parameter LicenseID to value 2537914
Academic license 2537914 - for non-commercial use only - registered to l.___@tu-berlin.de
Read LP format model from file /tmp/linopy-problem-j7llcu0r.lp
Reading time = 0.08 seconds
obj: 70086 rows, 32126 columns, 129984 nonzeros
Set parameter LogToConsole to value 0
/tmp/ipykernel_8514/4243218267.py:4: FutureWarning: The default value of `include_objective_constant` will change from True to False in version 2.0. Set `include_objective_constant` explicitly to suppress this warning. Using False improves LP numerical conditioning by not including the objective constant as a variable. result = func(*args, **kwargs)
Writing constraints.: 0%| | 0/25 [00:00<?, ?it/s]
Writing constraints.: 96%|█████████▌| 24/25 [00:00<00:00, 236.22it/s]
Writing constraints.: 100%|██████████| 25/25 [00:00<00:00, 230.78it/s]
Writing continuous variables.: 0%| | 0/12 [00:00<?, ?it/s]
Writing continuous variables.: 100%|██████████| 12/12 [00:00<00:00, 409.35it/s]
Set parameter WLSAccessID
Set parameter WLSSecret
Set parameter LicenseID to value 2537914
Academic license 2537914 - for non-commercial use only - registered to l.___@tu-berlin.de
Read LP format model from file /tmp/linopy-problem-k_r8w6su.lp
Reading time = 0.06 seconds
obj: 52566 rows, 24096 columns, 97490 nonzeros
Set parameter LogToConsole to value 0
/tmp/ipykernel_8514/4243218267.py:4: FutureWarning: The default value of `include_objective_constant` will change from True to False in version 2.0. Set `include_objective_constant` explicitly to suppress this warning. Using False improves LP numerical conditioning by not including the objective constant as a variable. result = func(*args, **kwargs)
Writing constraints.: 0%| | 0/25 [00:00<?, ?it/s]
Writing constraints.: 100%|██████████| 25/25 [00:00<00:00, 243.47it/s]
Writing constraints.: 100%|██████████| 25/25 [00:00<00:00, 241.59it/s]
Writing continuous variables.: 0%| | 0/12 [00:00<?, ?it/s]
Writing continuous variables.: 100%|██████████| 12/12 [00:00<00:00, 407.91it/s]
Set parameter WLSAccessID
Set parameter WLSSecret
Set parameter LicenseID to value 2537914
Academic license 2537914 - for non-commercial use only - registered to l.___@tu-berlin.de
Read LP format model from file /tmp/linopy-problem-8y61nzw_.lp
Reading time = 0.05 seconds
obj: 42054 rows, 19278 columns, 77983 nonzeros
Set parameter LogToConsole to value 0
/tmp/ipykernel_8514/4243218267.py:4: FutureWarning: The default value of `include_objective_constant` will change from True to False in version 2.0. Set `include_objective_constant` explicitly to suppress this warning. Using False improves LP numerical conditioning by not including the objective constant as a variable. result = func(*args, **kwargs)
Writing constraints.: 0%| | 0/25 [00:00<?, ?it/s]
Writing constraints.: 100%|██████████| 25/25 [00:00<00:00, 249.60it/s]
Writing constraints.: 100%|██████████| 25/25 [00:00<00:00, 247.74it/s]
Writing continuous variables.: 0%| | 0/12 [00:00<?, ?it/s]
Writing continuous variables.: 100%|██████████| 12/12 [00:00<00:00, 406.80it/s]
Set parameter WLSAccessID
Set parameter WLSSecret
Set parameter LicenseID to value 2537914
Academic license 2537914 - for non-commercial use only - registered to l.___@tu-berlin.de
Read LP format model from file /tmp/linopy-problem-lp6s8w_v.lp
Reading time = 0.04 seconds
obj: 35046 rows, 16066 columns, 64988 nonzeros
Set parameter LogToConsole to value 0
/tmp/ipykernel_8514/4243218267.py:4: FutureWarning: The default value of `include_objective_constant` will change from True to False in version 2.0. Set `include_objective_constant` explicitly to suppress this warning. Using False improves LP numerical conditioning by not including the objective constant as a variable. result = func(*args, **kwargs)
Writing constraints.: 0%| | 0/25 [00:00<?, ?it/s]
Writing constraints.: 100%|██████████| 25/25 [00:00<00:00, 263.34it/s]
Writing continuous variables.: 0%| | 0/12 [00:00<?, ?it/s]
Writing continuous variables.: 100%|██████████| 12/12 [00:00<00:00, 419.43it/s]
Set parameter WLSAccessID
Set parameter WLSSecret
Set parameter LicenseID to value 2537914
Academic license 2537914 - for non-commercial use only - registered to l.___@tu-berlin.de
Read LP format model from file /tmp/linopy-problem-c0vsbuei.lp
Reading time = 0.03 seconds
obj: 30054 rows, 13778 columns, 55729 nonzeros
Set parameter LogToConsole to value 0
/tmp/ipykernel_8514/4243218267.py:4: FutureWarning: The default value of `include_objective_constant` will change from True to False in version 2.0. Set `include_objective_constant` explicitly to suppress this warning. Using False improves LP numerical conditioning by not including the objective constant as a variable. result = func(*args, **kwargs)
Writing constraints.: 0%| | 0/25 [00:00<?, ?it/s]
Writing constraints.: 100%|██████████| 25/25 [00:00<00:00, 267.94it/s]
Writing continuous variables.: 0%| | 0/12 [00:00<?, ?it/s]
Writing continuous variables.: 100%|██████████| 12/12 [00:00<00:00, 418.58it/s]
Set parameter WLSAccessID
Set parameter WLSSecret
Set parameter LicenseID to value 2537914
Academic license 2537914 - for non-commercial use only - registered to l.___@tu-berlin.de
Read LP format model from file /tmp/linopy-problem-pcm1h89w.lp
Reading time = 0.03 seconds
obj: 26286 rows, 12051 columns, 48778 nonzeros
Set parameter LogToConsole to value 0
/tmp/ipykernel_8514/4243218267.py:4: FutureWarning: The default value of `include_objective_constant` will change from True to False in version 2.0. Set `include_objective_constant` explicitly to suppress this warning. Using False improves LP numerical conditioning by not including the objective constant as a variable. result = func(*args, **kwargs)
Writing constraints.: 0%| | 0/25 [00:00<?, ?it/s]
Writing constraints.: 100%|██████████| 25/25 [00:00<00:00, 266.11it/s]
Writing continuous variables.: 0%| | 0/12 [00:00<?, ?it/s]
Writing continuous variables.: 100%|██████████| 12/12 [00:00<00:00, 435.59it/s]
Set parameter WLSAccessID
Set parameter WLSSecret
Set parameter LicenseID to value 2537914
Academic license 2537914 - for non-commercial use only - registered to l.___@tu-berlin.de
Read LP format model from file /tmp/linopy-problem-1rqhdq7r.lp
Reading time = 0.03 seconds
obj: 23382 rows, 10720 columns, 43368 nonzeros
Set parameter LogToConsole to value 0
/tmp/ipykernel_8514/4243218267.py:4: FutureWarning: The default value of `include_objective_constant` will change from True to False in version 2.0. Set `include_objective_constant` explicitly to suppress this warning. Using False improves LP numerical conditioning by not including the objective constant as a variable. result = func(*args, **kwargs)
Set parameter WLSAccessID
Set parameter WLSSecret
Set parameter LicenseID to value 2537914
Academic license 2537914 - for non-commercial use only - registered to l.___@tu-berlin.de
Read LP format model from file /tmp/linopy-problem-c55ao13u.lp
Reading time = 0.02 seconds
obj: 21030 rows, 9642 columns, 38994 nonzeros
Set parameter LogToConsole to value 0
/tmp/ipykernel_8514/4243218267.py:4: FutureWarning: The default value of `include_objective_constant` will change from True to False in version 2.0. Set `include_objective_constant` explicitly to suppress this warning. Using False improves LP numerical conditioning by not including the objective constant as a variable. result = func(*args, **kwargs)
Set parameter WLSAccessID
Set parameter WLSSecret
Set parameter LicenseID to value 2537914
Academic license 2537914 - for non-commercial use only - registered to l.___@tu-berlin.de
Read LP format model from file /tmp/linopy-problem-4yro9cs4.lp
Reading time = 0.02 seconds
obj: 19134 rows, 8773 columns, 35482 nonzeros
Set parameter LogToConsole to value 0
Averaging¶
The averaging approach also has equal snapshot durations, but instead of selecting data from every \(N\)-th snapshots, we average time series data for every \(N\) snapshots. This means that the resulting model has \(N\) times fewer snapshots, but each snapshot represents the average of \(N\) original snapshots. Again, we need to adjust the snapshot weightings accordingly.
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] = s
/tmp/ipykernel_8514/4243218267.py:4: FutureWarning: The default value of `include_objective_constant` will change from True to False in version 2.0. Set `include_objective_constant` explicitly to suppress this warning. Using False improves LP numerical conditioning by not including the objective constant as a variable. result = func(*args, **kwargs)
Writing constraints.: 0%| | 0/25 [00:00<?, ?it/s]
Writing constraints.: 88%|████████▊ | 22/25 [00:00<00:00, 219.84it/s]
Writing constraints.: 100%|██████████| 25/25 [00:00<00:00, 197.71it/s]
Writing continuous variables.: 0%| | 0/12 [00:00<?, ?it/s]
Writing continuous variables.: 100%|██████████| 12/12 [00:00<00:00, 376.71it/s]
Set parameter WLSAccessID
Set parameter WLSSecret
Set parameter LicenseID to value 2537914
Academic license 2537914 - for non-commercial use only - registered to l.___@tu-berlin.de
Read LP format model from file /tmp/linopy-problem-yiqu924t.lp
Reading time = 0.12 seconds
obj: 105126 rows, 48186 columns, 195175 nonzeros
Set parameter LogToConsole to value 0
/tmp/ipykernel_8514/4243218267.py:4: FutureWarning: The default value of `include_objective_constant` will change from True to False in version 2.0. Set `include_objective_constant` explicitly to suppress this warning. Using False improves LP numerical conditioning by not including the objective constant as a variable. result = func(*args, **kwargs)
Writing constraints.: 0%| | 0/25 [00:00<?, ?it/s]
Writing constraints.: 92%|█████████▏| 23/25 [00:00<00:00, 227.60it/s]
Writing constraints.: 100%|██████████| 25/25 [00:00<00:00, 220.48it/s]
Writing continuous variables.: 0%| | 0/12 [00:00<?, ?it/s]
Writing continuous variables.: 100%|██████████| 12/12 [00:00<00:00, 398.20it/s]
Set parameter WLSAccessID
Set parameter WLSSecret
Set parameter LicenseID to value 2537914
Academic license 2537914 - for non-commercial use only - registered to l.___@tu-berlin.de
Read LP format model from file /tmp/linopy-problem-_twshyhj.lp
Reading time = 0.08 seconds
obj: 70086 rows, 32126 columns, 130192 nonzeros
Set parameter LogToConsole to value 0
/tmp/ipykernel_8514/4243218267.py:4: FutureWarning: The default value of `include_objective_constant` will change from True to False in version 2.0. Set `include_objective_constant` explicitly to suppress this warning. Using False improves LP numerical conditioning by not including the objective constant as a variable. result = func(*args, **kwargs)
Writing constraints.: 0%| | 0/25 [00:00<?, ?it/s]
Writing constraints.: 96%|█████████▌| 24/25 [00:00<00:00, 236.38it/s]
Writing constraints.: 100%|██████████| 25/25 [00:00<00:00, 232.65it/s]
Writing continuous variables.: 0%| | 0/12 [00:00<?, ?it/s]
Writing continuous variables.: 100%|██████████| 12/12 [00:00<00:00, 408.49it/s]
Set parameter WLSAccessID
Set parameter WLSSecret
Set parameter LicenseID to value 2537914
Academic license 2537914 - for non-commercial use only - registered to l.___@tu-berlin.de
Read LP format model from file /tmp/linopy-problem-bkb0d_zo.lp
Reading time = 0.06 seconds
obj: 52566 rows, 24096 columns, 97777 nonzeros
Set parameter LogToConsole to value 0
/tmp/ipykernel_8514/4243218267.py:4: FutureWarning: The default value of `include_objective_constant` will change from True to False in version 2.0. Set `include_objective_constant` explicitly to suppress this warning. Using False improves LP numerical conditioning by not including the objective constant as a variable. result = func(*args, **kwargs)
Writing constraints.: 0%| | 0/25 [00:00<?, ?it/s]
Writing constraints.: 100%|██████████| 25/25 [00:00<00:00, 253.14it/s]
Writing continuous variables.: 0%| | 0/12 [00:00<?, ?it/s]
Writing continuous variables.: 100%|██████████| 12/12 [00:00<00:00, 403.82it/s]
Set parameter WLSAccessID
Set parameter WLSSecret
Set parameter LicenseID to value 2537914
Academic license 2537914 - for non-commercial use only - registered to l.___@tu-berlin.de
Read LP format model from file /tmp/linopy-problem-j926ad_i.lp
Reading time = 0.05 seconds
obj: 42054 rows, 19278 columns, 78275 nonzeros
Set parameter LogToConsole to value 0
/tmp/ipykernel_8514/4243218267.py:4: FutureWarning: The default value of `include_objective_constant` will change from True to False in version 2.0. Set `include_objective_constant` explicitly to suppress this warning. Using False improves LP numerical conditioning by not including the objective constant as a variable. result = func(*args, **kwargs)
Writing constraints.: 0%| | 0/25 [00:00<?, ?it/s]
Writing constraints.: 100%|██████████| 25/25 [00:00<00:00, 252.82it/s]
Writing continuous variables.: 0%| | 0/12 [00:00<?, ?it/s]
Writing continuous variables.: 100%|██████████| 12/12 [00:00<00:00, 427.26it/s]
Set parameter WLSAccessID
Set parameter WLSSecret
Set parameter LicenseID to value 2537914
Academic license 2537914 - for non-commercial use only - registered to l.___@tu-berlin.de
Read LP format model from file /tmp/linopy-problem-teq4gigv.lp
Reading time = 0.04 seconds
obj: 35046 rows, 16066 columns, 65284 nonzeros
Set parameter LogToConsole to value 0
/tmp/ipykernel_8514/4243218267.py:4: FutureWarning: The default value of `include_objective_constant` will change from True to False in version 2.0. Set `include_objective_constant` explicitly to suppress this warning. Using False improves LP numerical conditioning by not including the objective constant as a variable. result = func(*args, **kwargs)
Writing constraints.: 0%| | 0/25 [00:00<?, ?it/s]
Writing constraints.: 100%|██████████| 25/25 [00:00<00:00, 260.27it/s]
Writing continuous variables.: 0%| | 0/12 [00:00<?, ?it/s]
Writing continuous variables.: 100%|██████████| 12/12 [00:00<00:00, 418.56it/s]
Set parameter WLSAccessID
Set parameter WLSSecret
Set parameter LicenseID to value 2537914
Academic license 2537914 - for non-commercial use only - registered to l.___@tu-berlin.de
Read LP format model from file /tmp/linopy-problem-xg_6f5x2.lp
Reading time = 0.03 seconds
obj: 30054 rows, 13778 columns, 56042 nonzeros
Set parameter LogToConsole to value 0
/tmp/ipykernel_8514/4243218267.py:4: FutureWarning: The default value of `include_objective_constant` will change from True to False in version 2.0. Set `include_objective_constant` explicitly to suppress this warning. Using False improves LP numerical conditioning by not including the objective constant as a variable. result = func(*args, **kwargs)
Writing constraints.: 0%| | 0/25 [00:00<?, ?it/s]
Writing constraints.: 100%|██████████| 25/25 [00:00<00:00, 265.18it/s]
Writing continuous variables.: 0%| | 0/12 [00:00<?, ?it/s]
Writing continuous variables.: 100%|██████████| 12/12 [00:00<00:00, 417.89it/s]
Set parameter WLSAccessID
Set parameter WLSSecret
Set parameter LicenseID to value 2537914
Academic license 2537914 - for non-commercial use only - registered to l.___@tu-berlin.de
Read LP format model from file /tmp/linopy-problem-2v5594s8.lp
Reading time = 0.03 seconds
obj: 26286 rows, 12051 columns, 49143 nonzeros
Set parameter LogToConsole to value 0
/tmp/ipykernel_8514/4243218267.py:4: FutureWarning: The default value of `include_objective_constant` will change from True to False in version 2.0. Set `include_objective_constant` explicitly to suppress this warning. Using False improves LP numerical conditioning by not including the objective constant as a variable. result = func(*args, **kwargs)
Writing constraints.: 0%| | 0/25 [00:00<?, ?it/s]
Writing constraints.: 100%|██████████| 25/25 [00:00<00:00, 267.06it/s]
Writing continuous variables.: 0%| | 0/12 [00:00<?, ?it/s]
Writing continuous variables.: 100%|██████████| 12/12 [00:00<00:00, 432.18it/s]
Set parameter WLSAccessID
Set parameter WLSSecret
Set parameter LicenseID to value 2537914
Academic license 2537914 - for non-commercial use only - registered to l.___@tu-berlin.de
Read LP format model from file /tmp/linopy-problem-5jsls6qx.lp
Reading time = 0.03 seconds
obj: 23382 rows, 10720 columns, 43678 nonzeros
Set parameter LogToConsole to value 0
/tmp/ipykernel_8514/4243218267.py:4: FutureWarning: The default value of `include_objective_constant` will change from True to False in version 2.0. Set `include_objective_constant` explicitly to suppress this warning. Using False improves LP numerical conditioning by not including the objective constant as a variable. result = func(*args, **kwargs)
Set parameter WLSAccessID
Set parameter WLSSecret
Set parameter LicenseID to value 2537914
Academic license 2537914 - for non-commercial use only - registered to l.___@tu-berlin.de
Read LP format model from file /tmp/linopy-problem-q6b7gn3n.lp
Reading time = 0.02 seconds
obj: 21030 rows, 9642 columns, 39315 nonzeros
Set parameter LogToConsole to value 0
/tmp/ipykernel_8514/4243218267.py:4: FutureWarning: The default value of `include_objective_constant` will change from True to False in version 2.0. Set `include_objective_constant` explicitly to suppress this warning. Using False improves LP numerical conditioning by not including the objective constant as a variable. result = func(*args, **kwargs)
Set parameter WLSAccessID
Set parameter WLSSecret
Set parameter LicenseID to value 2537914
Academic license 2537914 - for non-commercial use only - registered to l.___@tu-berlin.de
Read LP format model from file /tmp/linopy-problem-behfeohw.lp
Reading time = 0.02 seconds
obj: 19134 rows, 8773 columns, 35799 nonzeros
Set parameter LogToConsole to value 0
Segmentation¶
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.
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] = s
/tmp/ipykernel_8514/4243218267.py:4: FutureWarning: The default value of `include_objective_constant` will change from True to False in version 2.0. Set `include_objective_constant` explicitly to suppress this warning. Using False improves LP numerical conditioning by not including the objective constant as a variable. result = func(*args, **kwargs)
Writing constraints.: 0%| | 0/25 [00:00<?, ?it/s]
Writing constraints.: 88%|████████▊ | 22/25 [00:00<00:00, 217.64it/s]
Writing constraints.: 100%|██████████| 25/25 [00:00<00:00, 197.76it/s]
Writing continuous variables.: 0%| | 0/12 [00:00<?, ?it/s]
Writing continuous variables.: 100%|██████████| 12/12 [00:00<00:00, 379.40it/s]
Set parameter WLSAccessID
Set parameter WLSSecret
Set parameter LicenseID to value 2537914
Academic license 2537914 - for non-commercial use only - registered to l.___@tu-berlin.de
Read LP format model from file /tmp/linopy-problem-f3zwwtr2.lp
Reading time = 0.12 seconds
obj: 105126 rows, 48186 columns, 196032 nonzeros
Set parameter LogToConsole to value 0
/tmp/ipykernel_8514/4243218267.py:4: FutureWarning: The default value of `include_objective_constant` will change from True to False in version 2.0. Set `include_objective_constant` explicitly to suppress this warning. Using False improves LP numerical conditioning by not including the objective constant as a variable. result = func(*args, **kwargs)
Writing constraints.: 0%| | 0/25 [00:00<?, ?it/s]
Writing constraints.: 92%|█████████▏| 23/25 [00:00<00:00, 220.49it/s]
Writing constraints.: 100%|██████████| 25/25 [00:00<00:00, 210.49it/s]
Writing continuous variables.: 0%| | 0/12 [00:00<?, ?it/s]
Writing continuous variables.: 100%|██████████| 12/12 [00:00<00:00, 397.75it/s]
Set parameter WLSAccessID
Set parameter WLSSecret
Set parameter LicenseID to value 2537914
Academic license 2537914 - for non-commercial use only - registered to l.___@tu-berlin.de
Read LP format model from file /tmp/linopy-problem-rf_foirw.lp
Reading time = 0.08 seconds
obj: 70086 rows, 32126 columns, 130934 nonzeros
Set parameter LogToConsole to value 0
/tmp/ipykernel_8514/4243218267.py:4: FutureWarning: The default value of `include_objective_constant` will change from True to False in version 2.0. Set `include_objective_constant` explicitly to suppress this warning. Using False improves LP numerical conditioning by not including the objective constant as a variable. result = func(*args, **kwargs)
Writing constraints.: 0%| | 0/25 [00:00<?, ?it/s]
Writing constraints.: 96%|█████████▌| 24/25 [00:00<00:00, 237.44it/s]
Writing constraints.: 100%|██████████| 25/25 [00:00<00:00, 232.12it/s]
Writing continuous variables.: 0%| | 0/12 [00:00<?, ?it/s]
Writing continuous variables.: 100%|██████████| 12/12 [00:00<00:00, 398.27it/s]
Set parameter WLSAccessID
Set parameter WLSSecret
Set parameter LicenseID to value 2537914
Academic license 2537914 - for non-commercial use only - registered to l.___@tu-berlin.de
Read LP format model from file /tmp/linopy-problem-1xm37_qp.lp
Reading time = 0.06 seconds
obj: 52566 rows, 24096 columns, 98300 nonzeros
Set parameter LogToConsole to value 0
/tmp/ipykernel_8514/4243218267.py:4: FutureWarning: The default value of `include_objective_constant` will change from True to False in version 2.0. Set `include_objective_constant` explicitly to suppress this warning. Using False improves LP numerical conditioning by not including the objective constant as a variable. result = func(*args, **kwargs)
Writing constraints.: 0%| | 0/25 [00:00<?, ?it/s]
Writing constraints.: 100%|██████████| 25/25 [00:00<00:00, 245.61it/s]
Writing constraints.: 100%|██████████| 25/25 [00:00<00:00, 243.80it/s]
Writing continuous variables.: 0%| | 0/12 [00:00<?, ?it/s]
Writing continuous variables.: 100%|██████████| 12/12 [00:00<00:00, 403.92it/s]
Set parameter WLSAccessID
Set parameter WLSSecret
Set parameter LicenseID to value 2537914
Academic license 2537914 - for non-commercial use only - registered to l.___@tu-berlin.de
Read LP format model from file /tmp/linopy-problem-qfjezb1m.lp
Reading time = 0.05 seconds
obj: 42054 rows, 19278 columns, 78697 nonzeros
Set parameter LogToConsole to value 0
/tmp/ipykernel_8514/4243218267.py:4: FutureWarning: The default value of `include_objective_constant` will change from True to False in version 2.0. Set `include_objective_constant` explicitly to suppress this warning. Using False improves LP numerical conditioning by not including the objective constant as a variable. result = func(*args, **kwargs)
Writing constraints.: 0%| | 0/25 [00:00<?, ?it/s]
Writing constraints.: 100%|██████████| 25/25 [00:00<00:00, 250.84it/s]
Writing continuous variables.: 0%| | 0/12 [00:00<?, ?it/s]
Writing continuous variables.: 100%|██████████| 12/12 [00:00<00:00, 418.60it/s]
Set parameter WLSAccessID
Set parameter WLSSecret
Set parameter LicenseID to value 2537914
Academic license 2537914 - for non-commercial use only - registered to l.___@tu-berlin.de
Read LP format model from file /tmp/linopy-problem-g42dohs2.lp
Reading time = 0.04 seconds
obj: 35046 rows, 16066 columns, 65592 nonzeros
Set parameter LogToConsole to value 0
/tmp/ipykernel_8514/4243218267.py:4: FutureWarning: The default value of `include_objective_constant` will change from True to False in version 2.0. Set `include_objective_constant` explicitly to suppress this warning. Using False improves LP numerical conditioning by not including the objective constant as a variable. result = func(*args, **kwargs)
Writing constraints.: 0%| | 0/25 [00:00<?, ?it/s]
Writing constraints.: 100%|██████████| 25/25 [00:00<00:00, 256.47it/s]
Writing continuous variables.: 0%| | 0/12 [00:00<?, ?it/s]
Writing continuous variables.: 100%|██████████| 12/12 [00:00<00:00, 421.38it/s]
Set parameter WLSAccessID
Set parameter WLSSecret
Set parameter LicenseID to value 2537914
Academic license 2537914 - for non-commercial use only - registered to l.___@tu-berlin.de
Read LP format model from file /tmp/linopy-problem-e9hbj1dj.lp
Reading time = 0.03 seconds
obj: 30030 rows, 13767 columns, 56209 nonzeros
Set parameter LogToConsole to value 0
/tmp/ipykernel_8514/4243218267.py:4: FutureWarning: The default value of `include_objective_constant` will change from True to False in version 2.0. Set `include_objective_constant` explicitly to suppress this warning. Using False improves LP numerical conditioning by not including the objective constant as a variable. result = func(*args, **kwargs)
Writing constraints.: 0%| | 0/25 [00:00<?, ?it/s]
Writing constraints.: 100%|██████████| 25/25 [00:00<00:00, 263.35it/s]
Writing continuous variables.: 0%| | 0/12 [00:00<?, ?it/s]
Writing continuous variables.: 100%|██████████| 12/12 [00:00<00:00, 421.36it/s]
Set parameter WLSAccessID
Set parameter WLSSecret
Set parameter LicenseID to value 2537914
Academic license 2537914 - for non-commercial use only - registered to l.___@tu-berlin.de
Read LP format model from file /tmp/linopy-problem-fkuu88p5.lp
Reading time = 0.03 seconds
obj: 26286 rows, 12051 columns, 49207 nonzeros
Set parameter LogToConsole to value 0
/tmp/ipykernel_8514/4243218267.py:4: FutureWarning: The default value of `include_objective_constant` will change from True to False in version 2.0. Set `include_objective_constant` explicitly to suppress this warning. Using False improves LP numerical conditioning by not including the objective constant as a variable. result = func(*args, **kwargs)
Writing constraints.: 0%| | 0/25 [00:00<?, ?it/s]
Writing constraints.: 100%|██████████| 25/25 [00:00<00:00, 250.54it/s]
Writing continuous variables.: 0%| | 0/12 [00:00<?, ?it/s]
Writing continuous variables.: 100%|██████████| 12/12 [00:00<00:00, 407.71it/s]
Set parameter WLSAccessID
Set parameter WLSSecret
Set parameter LicenseID to value 2537914
Academic license 2537914 - for non-commercial use only - registered to l.___@tu-berlin.de
Read LP format model from file /tmp/linopy-problem-0hv1swz8.lp
Reading time = 0.03 seconds
obj: 23358 rows, 10709 columns, 43728 nonzeros
Set parameter LogToConsole to value 0
/tmp/ipykernel_8514/4243218267.py:4: FutureWarning: The default value of `include_objective_constant` will change from True to False in version 2.0. Set `include_objective_constant` explicitly to suppress this warning. Using False improves LP numerical conditioning by not including the objective constant as a variable. result = func(*args, **kwargs)
Set parameter WLSAccessID
Set parameter WLSSecret
Set parameter LicenseID to value 2537914
Academic license 2537914 - for non-commercial use only - registered to l.___@tu-berlin.de
Read LP format model from file /tmp/linopy-problem-p96l56m3.lp
Reading time = 0.02 seconds
obj: 21030 rows, 9642 columns, 39377 nonzeros
Set parameter LogToConsole to value 0
/tmp/ipykernel_8514/4243218267.py:4: FutureWarning: The default value of `include_objective_constant` will change from True to False in version 2.0. Set `include_objective_constant` explicitly to suppress this warning. Using False improves LP numerical conditioning by not including the objective constant as a variable. result = func(*args, **kwargs)
Set parameter WLSAccessID
Set parameter WLSSecret
Set parameter LicenseID to value 2537914
Academic license 2537914 - for non-commercial use only - registered to l.___@tu-berlin.de
Read LP format model from file /tmp/linopy-problem-fo_7x7nu.lp
Reading time = 0.02 seconds
obj: 19110 rows, 8762 columns, 35786 nonzeros
Set parameter LogToConsole to value 0
Now 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.
segmentation_n[3].snapshot_weightings.generators.value_counts().sort_index(
ascending=True
).plot.bar(ylabel="snapshots [number]", xlabel="snapshot duration [h]")
<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.
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
)
<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.
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
)
<Axes: xlabel='resolution [h]', ylabel='relative objective error [%]'>
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
)
<Axes: xlabel='resolution [h]', ylabel='relative solar capacity error [%]'>
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
)
<Axes: xlabel='resolution [h]', ylabel='relative battery capacity error [%]'>