Multiple runs¶
Perform Multiple Optimization Runs with EnergyScope¶
In this tutorial, we will demonstrate how to perform multiple optimization runs using the EnergyScope model. This is useful for sensitivity analysis, scenario exploration, and understanding how changes in parameters affect the energy system configuration.
Import Necessary Libraries¶
We begin by importing the required libraries and modules:
import pandas as pd
import pickle
from energyscope.energyscope import Energyscope
from energyscope.models import infrastructure_ch_2050
from energyscope.result import postprocessing
from energyscope.plots import plot_sankey, plot_parametrisation
pandas: For data manipulation and handling data frames.pickle: For saving and loading Python objects to and from files.Energyscope: The main class for initializing and running the EnergyScope model.infrastructure_ch_2050: A predefined model configuration focusing on energy infrastructure In Switzerland in 2050.postprocessing: Functions for processing and analyzing results after optimization.plot_sankey,plot_parametrisation: Functions for visualizing results.
Define Solver Options¶
We specify the solver options to control the optimization process:
solver_options = {
'solver': 'gurobi',
'solver_msg': 0,
}
'solver': 'gurobi': Specifies that the Gurobi solver should be used.'solver_msg': 0: Suppresses solver messages during execution.
Initialize and Run the Base Model¶
We initialize the EnergyScope model with the chosen dataset and solver options:
# Load the model with the chosen dataset and solver options
es_infra_ch = Energyscope(model=infrastructure_ch_2050, solver_options=solver_options)
Then, we perform an initial calculation to ensure the model is set up correctly:
# Solve the model
results_ch = es_infra_ch.calc()
Gurobi 12.0.2:
Note: This initial run is optional but recommended to verify that the model and solver are functioning properly before proceeding to multiple runs.
Load Parameter Sequence Data¶
We load a sequence of parameters from an Excel file, which will be used to perform multiple optimization runs:
# Load the parameter sequence DataFrame
seq_data = pd.read_excel("tutorial_input/param_run_es_n_infrastructure_ch_2050.xlsx")
display(seq_data)
| param | index0 | index1 | index2 | index3 | value1 | value2 | value3 | value4 | value5 | value6 | value7 | value8 | value9 | value10 | value11 | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | f_min | PV | NaN | NaN | NaN | 2 | 2.60 | 5.20 | 7.80 | 10.40 | 13.00 | 15.60 | 18.20 | 20.80 | 23.40 | 26.00 |
| 1 | f_max | PV | NaN | NaN | NaN | 2 | 2.60 | 5.20 | 7.80 | 10.40 | 13.00 | 15.60 | 18.20 | 20.80 | 23.40 | 26.00 |
| 2 | end_uses_demand_year | MOBILITY_FREIGHT | TRANSPORTATION | NaN | NaN | 45000 | 33226.71 | 33226.71 | 33226.71 | 33226.71 | 33226.71 | 33226.71 | 33226.71 | 33226.71 | 33226.71 | 33226.71 |
| 3 | c_inv | WIND | NaN | NaN | NaN | 800 | 850.00 | 900.00 | 950.00 | 1000.00 | 1050.00 | 1100.00 | 1150.00 | 1200.00 | 1250.00 | 1300.00 |
seq_data: A DataFrame containing different sets of parameters for each run.display(seq_data): Displays the DataFrame to inspect the parameters being varied.
Perform Multiple Optimization Runs¶
We use the calc_sequence method to run the model multiple times based on the parameter changes specified in seq_data:
# Run multiple optimizations based on parameters changed in seq_data
results_ch_n = es_infra_ch.calc_sequence(seq_data)
Gurobi 12.0.2:Run 1 complete. Gurobi 12.0.2:Run 2 complete. Gurobi 12.0.2:Run 3 complete. Gurobi 12.0.2:Run 4 complete. Gurobi 12.0.2:Run 5 complete. Gurobi 12.0.2:Run 6 complete. Gurobi 12.0.2:Run 7 complete. Gurobi 12.0.2:Run 8 complete. Gurobi 12.0.2:Run 9 complete. Gurobi 12.0.2:Run 10 complete. Gurobi 12.0.2:Run 11 complete.
results_ch_n: AResultobject that contains the outputs of all runs.
Post-Process the Results¶
After obtaining the results from multiple runs, we apply post-processing to compute Key Performance Indicators (KPIs) and prepare the data for visualization:
# Postcompute KPIs
results_ch_n = postprocessing(results_ch_n)
# Generate the Sankey diagram for run 1
fig = plot_sankey(results_ch_n, run_id=1)
fig.show(renderer="notebook")