{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Basic run"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"First, we import the essential libraries and modules that will be used throughout this tutorial:\n",
"\n",
"- `pickle`: Used for saving and loading Python objects to and from files.\n",
"- `Energyscope` from `energyscope.energyscope`: The main class for initializing and running the EnergyScope model.\n",
"- `infrastructure_ch_2050`, `Model` from `energyscope.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.\n",
"- `postprocessing` from `energyscope.result`: Functions for processing and analyzing the results after optimization.\n",
"- `plot_sankey` from `energyscope.plots`: A function to generate Sankey diagrams for visualizing energy flows."
]
},
{
"cell_type": "code",
"execution_count": 67,
"metadata": {
"ExecuteTime": {
"end_time": "2025-03-17T14:51:17.569162Z",
"start_time": "2025-03-17T14:51:17.350512Z"
}
},
"outputs": [],
"source": [
"import pickle\n",
"from energyscope.energyscope import Energyscope\n",
"from energyscope.models import infrastructure_ch_2050, Model\n",
"from energyscope.result import postprocessing\n",
"from energyscope.plots import plot_sankey"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---\n",
"## Initialize and Run the Model\n",
"\n",
"In this section, we initialize the EnergyScope model using the infrastructure dataset and perform a single optimization run.\n",
"\n",
"### Initialize the Model\n",
"\n",
"Create an instance of the `Energyscope` class with the `infrastructure` model. This sets up the model with predefined parameters and datasets."
]
},
{
"cell_type": "code",
"execution_count": 68,
"metadata": {
"ExecuteTime": {
"end_time": "2025-03-17T14:51:17.571675Z",
"start_time": "2025-03-17T14:51:17.569991Z"
}
},
"outputs": [],
"source": [
"es_infra_ch = Energyscope(model=infrastructure_ch_2050)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Export Model to AMPL and GLPK\n",
"\n",
"For compatibility and further analysis, we export the model files in both AMPL and GLPK formats."
]
},
{
"cell_type": "code",
"execution_count": 69,
"metadata": {
"ExecuteTime": {
"end_time": "2025-03-17T14:51:18.385735Z",
"start_time": "2025-03-17T14:51:17.572310Z"
}
},
"outputs": [],
"source": [
"# Export model to AMPL\n",
"es_infra_ch.export_ampl(mod_filename='tutorial_output/AMPL_infrastructure_ch_2050.mod',dat_filename='tutorial_output/AMPL_infrastructure_ch_2050.dat')\n",
"\n",
"# Export model to GLPK\n",
"es_infra_ch.export_glpk(mod_filename='tutorial_output/GLPK_infrastructure_ch_2050.mod',dat_filename='tutorial_output/GLPK_infrastructure_ch_2050.dat')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Load External AMPL files\n",
"\n",
"To load external files from AMPL you need to create a new Model as follow."
]
},
{
"cell_type": "code",
"execution_count": 70,
"metadata": {
"ExecuteTime": {
"end_time": "2025-03-17T14:51:18.389413Z",
"start_time": "2025-03-17T14:51:18.386760Z"
}
},
"outputs": [],
"source": [
"# Create you own Model object from imported AMPL files\n",
"Model_es_infra_ch = Model([\n",
" ('mod', \"tutorial_output/AMPL_infrastructure_ch_2050.mod\"),\n",
" ('dat', \"tutorial_output/AMPL_infrastructure_ch_2050.dat\")\n",
"])\n",
"\n",
"# Create an instance of the Energyscope class with your own model.\n",
"es_infra_ch = Energyscope(model=Model_es_infra_ch)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Solve the Model\n",
"Perform the optimization calculation. This step runs the solver and computes the optimal configuration based on the model."
]
},
{
"cell_type": "code",
"execution_count": 71,
"metadata": {
"ExecuteTime": {
"end_time": "2025-03-17T14:51:19.366227Z",
"start_time": "2025-03-17T14:51:18.390355Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Gurobi 12.0.2:Gurobi 12.0.2: optimal solution; objective 9229.741872\n",
"6367 simplex iterations\n",
"1 branching node\n"
]
}
],
"source": [
"results_ch = es_infra_ch.calc()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"### Post-Process 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": 72,
"metadata": {
"ExecuteTime": {
"end_time": "2025-03-17T14:51:19.391788Z",
"start_time": "2025-03-17T14:51:19.366970Z"
}
},
"outputs": [
{
"data": {
"application/vnd.microsoft.datawrangler.viewer.v0+json": {
"columns": [
{
"name": "Run",
"rawType": "int64",
"type": "integer"
},
{
"name": "C_inv",
"rawType": "float64",
"type": "float"
},
{
"name": "C_maint",
"rawType": "float64",
"type": "float"
},
{
"name": "Annual_Prod",
"rawType": "float64",
"type": "float"
},
{
"name": "F_Mult",
"rawType": "float64",
"type": "float"
},
{
"name": "tau",
"rawType": "float64",
"type": "float"
},
{
"name": "C_op",
"rawType": "float64",
"type": "float"
},
{
"name": "C_inv_an",
"rawType": "float64",
"type": "float"
},
{
"name": "Annual_Use",
"rawType": "float64",
"type": "float"
},
{
"name": "Category",
"rawType": "object",
"type": "string"
},
{
"name": "Category_2",
"rawType": "object",
"type": "string"
},
{
"name": "Sector",
"rawType": "object",
"type": "string"
}
],
"ref": "b64cf115-f1cf-4bc0-95a9-ac518ffc5509",
"rows": [
[
"0",
"29312.4",
"458.0",
"40299.36",
"20.0",
"0.062432931537380894",
"0.0",
"1830.0590623963237",
"40299.36",
"ELECTRICITY_MV",
"Wind",
"Electricity"
]
],
"shape": {
"columns": 11,
"rows": 1
}
},
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" C_inv | \n",
" C_maint | \n",
" Annual_Prod | \n",
" F_Mult | \n",
" tau | \n",
" C_op | \n",
" C_inv_an | \n",
" Annual_Use | \n",
" Category | \n",
" Category_2 | \n",
" Sector | \n",
"
\n",
" \n",
" Run | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 29312.4 | \n",
" 458.0 | \n",
" 40299.36 | \n",
" 20.0 | \n",
" 0.062433 | \n",
" 0.0 | \n",
" 1830.059062 | \n",
" 40299.36 | \n",
" ELECTRICITY_MV | \n",
" Wind | \n",
" Electricity | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" C_inv C_maint Annual_Prod F_Mult tau C_op C_inv_an \\\n",
"Run \n",
"0 29312.4 458.0 40299.36 20.0 0.062433 0.0 1830.059062 \n",
"\n",
" Annual_Use Category Category_2 Sector \n",
"Run \n",
"0 40299.36 ELECTRICITY_MV Wind Electricity "
]
},
"execution_count": 72,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"results_ch = postprocessing(results_ch)\n",
"\n",
"# Example of how to extract post-processed data\n",
"results_ch.postprocessing['df_annual'].loc['WIND',:]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---\n",
"\n",
"## Visualize Results with a Sankey Diagram\n",
"\n",
"A Sankey diagram is an effective way to visualize energy flows within the system.\n",
"\n",
"### Generate the Sankey Diagram\n",
"Use the `plot_sankey` function with the processed results to create the diagram.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 73,
"metadata": {
"ExecuteTime": {
"end_time": "2025-03-17T14:51:19.605759Z",
"start_time": "2025-03-17T14:51:19.392483Z"
}
},
"outputs": [],
"source": [
"fig = plot_sankey(results_ch)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Display the Diagram\n",
"Render the Sankey diagram within the notebook for immediate visualization.\n",
" > **Optional**: You can save the diagram as an HTML file or an image for external use by uncommenting the following lines:"
]
},
{
"cell_type": "code",
"execution_count": 74,
"metadata": {
"ExecuteTime": {
"end_time": "2025-03-17T14:51:19.608048Z",
"start_time": "2025-03-17T14:51:19.606569Z"
}
},
"outputs": [],
"source": [
"# fig.write_html(\"tutorial_output/sankey_infrastructure_ch_2050.html\")\n",
"# fig.write_image('tutorial_output/sankey_infrastructure_ch_2050.png')"
]
},
{
"cell_type": "code",
"execution_count": 75,
"metadata": {
"ExecuteTime": {
"end_time": "2025-03-17T14:51:19.735478Z",
"start_time": "2025-03-17T14:51:19.608780Z"
}
},
"outputs": [
{
"data": {
"text/html": [
" \n",
" "
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"fig.show(renderer=\"notebook\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---\n",
"\n",
"## Save and Load Results\n",
"\n",
"To preserve the results and avoid re-running the optimization every time, we can save the results to a file and load them later.\n",
"\n",
"### Define Save Function\n",
"Create a function to save the `Result` object using `pickle`.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 76,
"metadata": {},
"outputs": [],
"source": [
"def save_result_to_pickle(data, filename):\n",
" \"\"\"\n",
" Save the Result object to a pickle file.\n",
"\n",
" Parameters:\n",
" data: The Result object to save.\n",
" filename (str): The file path to save the object to.\n",
" \"\"\"\n",
" with open(filename, 'wb') as fp:\n",
" pickle.dump(data, fp, protocol=pickle.HIGHEST_PROTOCOL)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Define Load Function\n",
"Create a function to load the `Result` object from a pickle file.\n",
"\n",
"\n",
" > **Note**: These utility functions could be integrated into the EnergyScope library for convenience.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 77,
"metadata": {},
"outputs": [],
"source": [
"def load_result_from_pickle(filename):\n",
" \"\"\"\n",
" Load the Result object from a pickle file.\n",
"\n",
" Parameters:\n",
" filename (str): The file path to load the object from.\n",
"\n",
" Returns:\n",
" The loaded Result object.\n",
" \"\"\"\n",
" with open(filename, 'rb') as handle:\n",
" result = pickle.load(handle)\n",
" return result"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Save the Results\n",
"Use the `save_result_to_pickle` function to save the results to a file.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 78,
"metadata": {
"ExecuteTime": {
"end_time": "2025-01-29T11:08:48.613214Z",
"start_time": "2025-01-29T11:08:48.601302Z"
}
},
"outputs": [],
"source": [
"save_result_to_pickle(results_ch, \"tutorial_input/infrastructure_ch_2050.pickle\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Clear the Results Variable\n",
"Empty the `results_ch` variable to simulate a fresh environment.\n"
]
},
{
"cell_type": "code",
"execution_count": 79,
"metadata": {
"ExecuteTime": {
"end_time": "2025-01-29T11:08:48.618185Z",
"start_time": "2025-01-29T11:08:48.614947Z"
}
},
"outputs": [],
"source": [
"results_ch = None"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Load the Results\n",
"Load the previously saved results using the `load_result_from_pickle` function.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 80,
"metadata": {
"ExecuteTime": {
"end_time": "2025-01-29T11:08:48.632705Z",
"start_time": "2025-01-29T11:08:48.620113Z"
}
},
"outputs": [],
"source": [
"results_ch = load_result_from_pickle(\"tutorial_input/infrastructure_ch_2050.pickle\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Display Total Cost\n",
"Access and display the total cost from the loaded results to verify that the data was correctly saved and loaded.\n",
"\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 81,
"metadata": {
"ExecuteTime": {
"end_time": "2025-01-29T11:08:48.639321Z",
"start_time": "2025-01-29T11:08:48.633955Z"
}
},
"outputs": [
{
"data": {
"application/vnd.microsoft.datawrangler.viewer.v0+json": {
"columns": [
{
"name": "index",
"rawType": "int64",
"type": "integer"
},
{
"name": "TotalCost",
"rawType": "float64",
"type": "float"
},
{
"name": "Run",
"rawType": "int64",
"type": "integer"
}
],
"ref": "cf9be385-ff0d-40bc-92bb-25884c772a5c",
"rows": [
[
"0",
"9229.74187234957",
"0"
]
],
"shape": {
"columns": 2,
"rows": 1
}
},
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" TotalCost | \n",
" Run | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 9229.741872 | \n",
" 0 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" TotalCost Run\n",
"0 9229.741872 0"
]
},
"execution_count": 81,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"results_ch.variables['TotalCost']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" This should output a table showing the total cost of the optimized energy system configuration.\n",
"\n",
"---\n",
"\n",
"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."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}