Choose datasets¶
Import Necessary Libraries and Modules¶
First, we import the essential libraries and modules required for this tutorial:
pandas
aspd
: A powerful data manipulation library.Energyscope
fromenergyscope.energyscope
: The main class to initialize and run the EnergyScope model.infrastructure_qc_2020
fromenergyscope.models
: Predefined models for different regions or configurations.postprocessing
fromenergyscope.result
: Functions and classes for handling and processing results.plot_sankey
fromenergyscope.plots
: Functions to create Sankey diagrams for visualizing energy flows.
import pandas as pd
from energyscope.energyscope import Energyscope
from energyscope.models import infrastructure_qc_2020
from energyscope.result import postprocessing
from energyscope.plots import plot_sankey
--------------------------------------------------------------------------- ModuleNotFoundError Traceback (most recent call last) Cell In[1], line 5 3 from energyscope.models import infrastructure_qc_2020 4 from energyscope.result import postprocessing ----> 5 from energyscope.plots import plot_sankey File /builds/energyscope/energyscope/src/energyscope/plots.py:19 13 import numpy as np 14 from pandas.api.types import ( 15 is_integer_dtype, 16 is_datetime64_any_dtype, 17 is_period_dtype, 18 ) ---> 19 from ipywidgets import Dropdown, HBox, VBox, Button, Output 20 from ipywidgets import Dropdown, HBox, VBox, Button, Output 21 import matplotlib.pyplot as plt ModuleNotFoundError: No module named 'ipywidgets'
solver_options = {
'solver': 'cplex',
'presolve_eps': 5e-9,
'presolve_fixeps': 7e-10,
'mipgap': 1e-10,
'display_eps': 1e-10,
'omit_zero_rows': 1,
'omit_zero_cols': 1,
'show_stats': 1,
'solver_msg': 0,
'cplex_options': 'integrality=5e-7'
}
- Note: These options are specific to the CPLEX solver and control aspects like presolve tolerances, MIP gap, and output display.
Loading from a CSV File¶
Alternatively, you can load the solver options from a CSV file, which can be convenient for managing configurations externally.
solver_options = pd.read_csv('tutorial_input/solver_options_infrastructure_qc_2020.csv')
solver_options = dict(zip(solver_options['Option'], solver_options['Value'])) # Reformat dict
- Here, we read the CSV file into a Pandas DataFrame and then convert it into a dictionary that can be used by the model.
After loading the solver options, you can inspect them to ensure they have been set correctly:
solver_options
{'solver': 'cplex', 'presolve_eps': '5.00E-09', 'presolve_fixeps': '7.00E-10', 'mipgap': '1.00E-10', 'display_eps': '1.00E-10', 'omit_zero_rows': '1', 'omit_zero_cols': '1', 'show_stats': '1', 'solver_msg': '0', 'cplex_options': 'integrality=5e-7'}
Initialize and Run the Model with Specific Dataset and Solver Options¶
In this section, we initialize the EnergyScope model using the infrastructure_qc_2020
dataset and apply the custom solver options defined earlier.
Initialize the Model¶
Create an instance of the Energyscope
class with the infrastructure_qc_2020
model and the specified solver options.
es_infra_qc = Energyscope(model=infrastructure_qc_2020, solver_options=solver_options)
- Model: The
infrastructure_qc_2020
model represents the energy system of Quebec with monthly resolution. - Solver Options: The
solver_options
dictionary configures the solver's behavior during optimization.
Run the Optimization¶
Execute the calculation to solve the optimization problem.
results_qc = es_infra_qc.calc()
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) Cell In[6], line 1 ----> 1 results_qc = es_infra_qc.calc() File /builds/energyscope/energyscope/src/energyscope/energyscope.py:72, in Energyscope.calc(self, ds, parser) 68 def calc(self, ds: Dataset = None, parser: Callable[[AMPL], Result] = parse_result) -> Result: 69 """ 70 Calls AMPL with `df` as .dat and returns the parsed result. 71 """ ---> 72 if self.es_model.getSets().__len__() == 0: # Check if AMPL instance is empty 73 self._initial_run(ds=ds) 75 # Solve the model File /builds/energyscope/energyscope/src/energyscope/energyscope.py:30, in Energyscope.es_model(self) 28 else: 29 try: ---> 30 self.__es_model = AMPL(Environment(os.environ["AMPL_PATH"])) 31 except SystemError: 32 # Try to create the object a second time to prevent errors when starting `ampl_lic` 33 self.__es_model = AMPL() File /usr/local/lib/python3.10/os.py:680, in _Environ.__getitem__(self, key) 677 value = self._data[self.encodekey(key)] 678 except KeyError: 679 # raise KeyError with the original key value --> 680 raise KeyError(key) from None 681 return self.decodevalue(value) KeyError: 'AMPL_PATH'
- This step runs the solver (e.g., CPLEX) with the specified options and computes the optimal energy system configuration.
- Output: You may see console outputs indicating the solver's progress and any warnings or errors.
Post-Process the Results¶
After obtaining the raw results, we apply post-processing to compute Key Performance Indicators (KPIs) and prepare the data for visualization.
results_qc = postprocessing(results_qc)
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[7], line 1 ----> 1 results_qc = postprocessing(results_qc) NameError: name 'results_qc' is not defined
- The
postprocessing
function enriches the results with additional analysis, making them ready for further interpretation and plotting.
Visualize Results with a Sankey Diagram¶
A Sankey diagram is an effective tool for visualizing energy flows within the system, showing how energy sources are converted and consumed.
Generate the Sankey Diagram¶
Use the plot_sankey
function with the processed results to create the diagram.
fig = plot_sankey(results_qc)
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[8], line 1 ----> 1 fig = plot_sankey(results_qc) NameError: name 'plot_sankey' is not defined
- This function processes the results and generates a Sankey diagram object.
Display the Diagram¶
Render the Sankey diagram within the notebook for immediate visualization.
fig.show(renderer="notebook")
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[9], line 1 ----> 1 fig.show(renderer="notebook") NameError: name 'fig' is not defined
The diagram will display in the output cell, providing a visual representation of the energy flows in the optimized system.
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_qc_2020.html")
# fig.write_image('tutorial_output/sankey_infrastructure_qc_2020.png')
By following these steps, you have:
- Imported the necessary libraries and modules.
- Defined solver options either directly or by loading from a CSV file.
- Initialized and ran the EnergyScope model using the
infrastructure_qc_2020
dataset with custom solver configurations. - Post-processed the results to compute KPIs and prepare for visualization.
- Visualized the energy flows using a Sankey diagram.
This tutorial demonstrates how to customize solver settings and work with different datasets, enabling you to perform more advanced analyses tailored to specific regions or scenarios.