{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Choose datasets" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---\n", "## Import Necessary Libraries and Modules\n", "\n", "First, we import the essential libraries and modules required for this tutorial:\n", "\n", "- **`pandas` as `pd`**: A powerful data manipulation library.\n", "- **`Energyscope` from `energyscope.energyscope`**: The main class to initialize and run the EnergyScope model.\n", "- **`infrastructure_qc_2020` from `energyscope.models`**: Predefined models for different regions or configurations.\n", "- **`postprocessing` from `energyscope.result`**: Functions and classes for handling and processing results.\n", "- **`plot_sankey` from `energyscope.plots`**: Functions to create Sankey diagrams for visualizing energy flows.\n", "\n" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "from energyscope.energyscope import Energyscope\n", "from energyscope.models import infrastructure_qc_2020\n", "from energyscope.result import postprocessing\n", "from energyscope.plots import plot_sankey" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "---\n", "## Define Solver Options\n", "\n", "The solver options determine how the optimization solver behaves during the calculation. In this section, we demonstrate two methods to specify these options:\n", "### Directly in the Code \n", "You can define the solver options directly within your script by creating a dictionary.\n" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "solver_options = {\n", " 'solver': 'cplex',\n", " 'presolve_eps': 5e-9,\n", " 'presolve_fixeps': 7e-10,\n", " 'mipgap': 1e-10,\n", " 'display_eps': 1e-10,\n", " 'omit_zero_rows': 1,\n", " 'omit_zero_cols': 1,\n", " 'show_stats': 1,\n", " 'solver_msg': 0,\n", " 'cplex_options': 'integrality=5e-7'\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", " - **Note**: These options are specific to the CPLEX solver and control aspects like presolve tolerances, MIP gap, and output display.\n", "### Loading from a CSV File \n", "Alternatively, you can load the solver options from a CSV file, which can be convenient for managing configurations externally.\n", "\n" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "solver_options = pd.read_csv('tutorial_input/solver_options_infrastructure_qc_2020.csv')\n", "solver_options = dict(zip(solver_options['Option'], solver_options['Value'])) # Reformat dict" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", " - Here, we read the CSV file into a Pandas DataFrame and then convert it into a dictionary that can be used by the model.\n", "\n", "After loading the solver options, you can inspect them to ensure they have been set correctly:" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'solver': 'cplex',\n", " 'presolve_eps': '5.00E-09',\n", " 'presolve_fixeps': '7.00E-10',\n", " 'mipgap': '1.00E-10',\n", " 'display_eps': '1.00E-10',\n", " 'omit_zero_rows': '1',\n", " 'omit_zero_cols': '1',\n", " 'show_stats': '1',\n", " 'solver_msg': '0',\n", " 'cplex_options': 'integrality=5e-7'}" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "solver_options" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---\n", "## Initialize and Run the Model with Specific Dataset and Solver Options\n", "\n", "In this section, we initialize the EnergyScope model using the **`infrastructure_qc_2020`** dataset and apply the custom solver options defined earlier.\n", "### Initialize the Model \n", "Create an instance of the `Energyscope` class with the `infrastructure_qc_2020` model and the specified solver options." ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "es_infra_qc = Energyscope(model=infrastructure_qc_2020, solver_options=solver_options)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n", " - **Model**: The `infrastructure_qc_2020` model represents the energy system of Quebec with monthly resolution.\n", " - **Solver Options**: The `solver_options` dictionary configures the solver's behavior during optimization.\n", "### Run the Optimization \n", "Execute the calculation to solve the optimization problem." ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Presolve eliminates 14467 constraints and 10653 variables.\n", "Adjusted problem:\n", "15812 variables:\n", "\t814 binary variables\n", "\t14998 linear variables\n", "21545 constraints, all linear; 108336 nonzeros\n", "\t9834 equality constraints\n", "\t11711 inequality constraints\n", "1 linear objective; 1 nonzero.\n", "\n", "Setting $presolve_fixeps < 3.39e-10 or >= 5.5e-09\n", "could change presolve results.\n", "\n", "CPLEX 22.1.1.0: integrality=5e-7\n", "\n", "\"option abs_boundtol 5.229594535194337e-12;\"\n", "will change deduced dual values.\n", "\n" ] } ], "source": [ "results_qc = es_infra_qc.calc()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n", " - This step runs the solver (e.g., CPLEX) with the specified options and computes the optimal energy system configuration.\n", " - **Output**: You may see console outputs indicating the solver's progress and any warnings or errors.\n", "### Post-Process the Results \n", "After obtaining the raw results, we apply post-processing to compute Key Performance Indicators (KPIs) and prepare the data for visualization." ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "results_qc = postprocessing(results_qc)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n", " - The `postprocessing` function enriches the results with additional analysis, making them ready for further interpretation and plotting.\n", "\n", "---\n", "## Visualize Results with a Sankey Diagram\n", "\n", "A Sankey diagram is an effective tool for visualizing energy flows within the system, showing how energy sources are converted and consumed.\n", "### Generate the Sankey Diagram \n", "Use the `plot_sankey` function with the processed results to create the diagram." ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "fig = plot_sankey(results_qc)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n", " - This function processes the results and generates a Sankey diagram object.\n", "### Display the Diagram \n", "Render the Sankey diagram within the notebook for immediate visualization." ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "