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.Energyscope
fromenergyscope.energyscope
: The main class for initializing and running the EnergyScope model.infrastructure_ch_2050
fromenergyscope.models
: The specific model configuration we will use, which focuses on energy infrastructure.postprocessing
fromenergyscope.result
: Functions for processing and analyzing the results after optimization.plot_sankey
fromenergyscope.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
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/infrastructure_ch_2050.mod',dat_filename='tutorial_output/infrastructure_ch_2050.dat')
# Export model to GLPK
es_infra_ch.export_glpk(mod_filename='tutorial_output/infrastructure_ch_2050.mod',dat_filename='tutorial_output/infrastructure_ch_2050.dat')
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 11.0.0:Gurobi 11.0.0: optimal solution; objective 8151.373244 6079 simplex iteration(s) 1 branching node(s) absmipgap=0.102405, relmipgap=1.25629e-05
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)
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()
def save_result_to_pickle(data, filename):
"""
Save the Result object to a pickle file.
Parameters:
data: The Result object to save.
filename (str): The file path to save the object to.
"""
with open(filename, 'wb') as fp:
pickle.dump(data, fp, protocol=pickle.HIGHEST_PROTOCOL)
Define Load Function¶
Create a function to load the Result
object from a pickle file.
Note: These utility functions could be integrated into the EnergyScope library for convenience.
def load_result_from_pickle(filename):
"""
Load the Result object from a pickle file.
Parameters:
filename (str): The file path to load the object from.
Returns:
The loaded Result object.
"""
with open(filename, 'rb') as handle:
result = pickle.load(handle)
return result
Save the Results¶
Use the save_result_to_pickle
function to save the results to a file.
save_result_to_pickle(results_ch, "tutorial_input/infrastructure_ch_2050.pickle")
Clear the Results Variable¶
Empty the results_ch
variable to simulate a fresh environment.
results_ch = None
Load the Results¶
Load the previously saved results using the load_result_from_pickle
function.
results_ch = load_result_from_pickle("tutorial_input/infrastructure_ch_2050.pickle")
Display Total Cost¶
Access and display the total cost from the loaded results to verify that the data was correctly saved and loaded.
results_ch.variables['TotalCost']
TotalCost | Run | |
---|---|---|
0 | 8151.373244 | 0 |
This should output a table showing the total cost of the optimized energy system configuration.
By following these steps, you can perform a basic run of the EnergyScope model, visualize the results, and save/load the data for future use. This tutorial serves as a foundation for more complex analyses and customizations.