Basic run¶
First, we import the essential libraries and modules that will be used throughout this tutorial:
pickle: Used for saving and loading Python objects to and from files.Energyscopefromenergyscope.energyscope: The main class for initializing and running the EnergyScope model.infrastructure_ch_2050,Modelfromenergyscope.models: The specific model configuration we will use, which focuses on energy infrastructure. And the object Model to create your own model from external files.postprocessingfromenergyscope.result: Functions for processing and analyzing the results after optimization.plot_sankeyfromenergyscope.plots: A function to generate Sankey diagrams for visualizing energy flows.
import pickle
from energyscope.energyscope import Energyscope
from energyscope.models import infrastructure_ch_2050, Model
from energyscope.result import postprocessing
from energyscope.plots import plot_sankey
Initialize and Run the Model¶
In this section, we initialize the EnergyScope model using the infrastructure dataset and perform a single optimization run.
Initialize the Model¶
Create an instance of the Energyscope class with the infrastructure model. This sets up the model with predefined parameters and datasets.
es_infra_ch = Energyscope(model=infrastructure_ch_2050)
Export Model to AMPL and GLPK¶
For compatibility and further analysis, we export the model files in both AMPL and GLPK formats.
# Export model to AMPL
es_infra_ch.export_ampl(mod_filename='tutorial_output/AMPL_infrastructure_ch_2050.mod',dat_filename='tutorial_output/AMPL_infrastructure_ch_2050.dat')
# Export model to GLPK
es_infra_ch.export_glpk(mod_filename='tutorial_output/GLPK_infrastructure_ch_2050.mod',dat_filename='tutorial_output/GLPK_infrastructure_ch_2050.dat')
Load External AMPL files¶
To load external files from AMPL you need to create a new Model as follow.
# Create you own Model object from imported AMPL files
Model_es_infra_ch = Model([
('mod', "tutorial_output/AMPL_infrastructure_ch_2050.mod"),
('dat', "tutorial_output/AMPL_infrastructure_ch_2050.dat")
])
# Create an instance of the Energyscope class with your own model.
es_infra_ch = Energyscope(model=Model_es_infra_ch)
Solve the Model¶
Perform the optimization calculation. This step runs the solver and computes the optimal configuration based on the model.
results_ch = es_infra_ch.calc()
Gurobi 12.0.2:Gurobi 12.0.2: optimal solution; objective 9229.741872 6367 simplex iterations 1 branching node
Post-Process Results¶
After obtaining the raw results, we apply post-processing to compute Key Performance Indicators (KPIs) and prepare the data for visualization.
results_ch = postprocessing(results_ch)
# Example of how to extract post-processed data
results_ch.postprocessing['df_annual'].loc['WIND',:]
| C_inv | C_maint | Annual_Prod | F_Mult | tau | C_op | C_inv_an | Annual_Use | Category | Category_2 | Sector | |
|---|---|---|---|---|---|---|---|---|---|---|---|
| Run | |||||||||||
| 0 | 29312.4 | 458.0 | 40299.36 | 20.0 | 0.062433 | 0.0 | 1830.059062 | 40299.36 | ELECTRICITY_MV | Wind | Electricity |
fig = plot_sankey(results_ch)
Display the Diagram¶
Render the Sankey diagram within the notebook for immediate visualization.
Optional: You can save the diagram as an HTML file or an image for external use by uncommenting the following lines:
# fig.write_html("tutorial_output/sankey_infrastructure_ch_2050.html")
# fig.write_image('tutorial_output/sankey_infrastructure_ch_2050.png')
fig.show(renderer="notebook")