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
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()
Presolve eliminates 18607 constraints and 13787 variables. Adjusted problem: 12678 variables: 814 binary variables 11864 linear variables 17405 constraints, all linear; 107580 nonzeros 7892 equality constraints 9513 inequality constraints 1 linear objective; 1 nonzero. Setting $presolve_fixeps < 4.26e-10 or >= 5.26e-09 could change presolve results. CPLEX 22.1.1.0: integrality=5e-7
- 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)
- 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)
- 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()
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.