Basic Run with Pyomo¶
This tutorial demonstrates how to run the EnergyScope model using Pyomo instead of AMPL. Pyomo is an open-source optimization modeling language that doesn't require commercial solvers.
Imports¶
First, we import the essential libraries and modules:
run_pyomo_modelfromenergyscope.energyscope: Function to run the Pyomo-based model.pyomo_chfromenergyscope.models: The Pyomo model configuration for Switzerland.postprocessingfromenergyscope.result: Functions for processing results after optimization.plot_sankeyfromenergyscope.plots: Function to generate Sankey diagrams for visualizing energy flows.
In [1]:
Copied!
from energyscope.energyscope import run_pyomo_model
from energyscope.models import pyomo_ch
from energyscope.result import postprocessing
from energyscope.plots import plot_sankey
from energyscope.energyscope import run_pyomo_model
from energyscope.models import pyomo_ch
from energyscope.result import postprocessing
from energyscope.plots import plot_sankey
Run the Model¶
Run the Pyomo model with a single function call. The model uses HiGHS as the solver (open-source, no license required).
Parameters:
model: The PyomoModel instance (e.g.,pyomo_ch)objective: Objective function to minimize ("gwp"for emissions,"cost"for total cost)
In [2]:
Copied!
result = run_pyomo_model(pyomo_ch, objective="gwp")
result = run_pyomo_model(pyomo_ch, objective="gwp")
Inspect Results¶
The result object contains variables, parameters, sets, and objectives from the solved model.
In [3]:
Copied!
# View objectives
print("Total GWP:", result.objectives['TotalGWP'].values[0][0])
print("Total Cost:", result.objectives['TotalCost'].values[0][0])
# View objectives
print("Total GWP:", result.objectives['TotalGWP'].values[0][0])
print("Total Cost:", result.objectives['TotalCost'].values[0][0])
Total GWP: 14118.02386754794 Total Cost: 44634.461179539176
In [4]:
Copied!
# View installed capacities (F_Mult)
f_mult = result.variables['F_Mult']
f_mult[f_mult['F_Mult'] > 0].head(10)
# View installed capacities (F_Mult)
f_mult = result.variables['F_Mult']
f_mult[f_mult['F_Mult'] > 0].head(10)
Out[4]:
| F_Mult | Run | |
|---|---|---|
| index | ||
| BUS_COACH_CNG_STOICH | 16.7 | 0 |
| BUS_COACH_DIESEL | 16.7 | 0 |
| BUS_COACH_FC_HYBRIDH2 | 16.7 | 0 |
| BUS_COACH_HYDIESEL | 16.7 | 0 |
| CAR_BEV | 16.7 | 0 |
| CAR_DIESEL | 16.7 | 0 |
| CAR_FUEL_CELL | 16.7 | 0 |
| CAR_GASOLINE | 16.7 | 0 |
| CAR_HEV | 16.7 | 0 |
| CAR_NG | 16.7 | 0 |
Post-Processing¶
Apply post-processing to compute Key Performance Indicators (KPIs) and prepare the data for visualization.
In [5]:
Copied!
result = postprocessing(result)
result = postprocessing(result)
In [6]:
Copied!
# View annual data for a specific technology
result.postprocessing['df_annual'].head(10)
# View annual data for a specific technology
result.postprocessing['df_annual'].head(10)
Out[6]:
| C_inv | C_maint | Annual_Prod | F_Mult | tau | C_op | C_inv_an | Annual_Use | Category | Category_2 | Sector | ||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| index | Run | |||||||||||
| BUS_COACH_CNG_STOICH | 0 | 0.0 | 0.0 | 0.000 | 16.7 | 1.03215 | 0.0 | 0.0 | 0.000 | MOB_PUBLIC | Mobility | Mobility |
| BUS_COACH_DIESEL | 0 | 0.0 | 0.0 | 0.000 | 16.7 | 1.03215 | 0.0 | 0.0 | 0.000 | MOB_PUBLIC | Mobility | Mobility |
| BUS_COACH_FC_HYBRIDH2 | 0 | 0.0 | 0.0 | 0.000 | 16.7 | 1.03215 | 0.0 | 0.0 | 0.000 | MOB_PUBLIC | Mobility | Mobility |
| BUS_COACH_HYDIESEL | 0 | 0.0 | 0.0 | 0.000 | 16.7 | 1.03215 | 0.0 | 0.0 | 0.000 | MOB_PUBLIC | Mobility | Mobility |
| CAR_BEV | 0 | 0.0 | 0.0 | 21907.395 | 16.7 | 1.03215 | 0.0 | 0.0 | 21907.395 | MOB_PRIVATE | Mobility | Mobility |
| CAR_DIESEL | 0 | 0.0 | 0.0 | 14604.930 | 16.7 | 1.03215 | 0.0 | 0.0 | 14604.930 | MOB_PRIVATE | Mobility | Mobility |
| CAR_FUEL_CELL | 0 | 0.0 | 0.0 | 14604.930 | 16.7 | 1.03215 | 0.0 | 0.0 | 14604.930 | MOB_PRIVATE | Mobility | Mobility |
| CAR_GASOLINE | 0 | 0.0 | 0.0 | 14604.930 | 16.7 | 1.03215 | 0.0 | 0.0 | 14604.930 | MOB_PRIVATE | Mobility | Mobility |
| CAR_HEV | 0 | 0.0 | 0.0 | 0.000 | 16.7 | 1.03215 | 0.0 | 0.0 | 0.000 | MOB_PRIVATE | Mobility | Mobility |
| CAR_NG | 0 | 0.0 | 0.0 | 0.000 | 16.7 | 1.03215 | 0.0 | 0.0 | 0.000 | MOB_PRIVATE | Mobility | Mobility |
In [7]:
Copied!
fig = plot_sankey(result, aggregate_grid=False) # aggregate_grid=False as the model don't have grid layers
fig.show(renderer="notebook")
fig = plot_sankey(result, aggregate_grid=False) # aggregate_grid=False as the model don't have grid layers
fig.show(renderer="notebook")
Save the Diagram¶
Optionally save the diagram as an HTML file:
In [8]:
Copied!
# fig.write_html("tutorial_output/sankey_pyomo_ch.html")
# fig.write_html("tutorial_output/sankey_pyomo_ch.html")
In [9]:
Copied!
result_cost = run_pyomo_model(pyomo_ch, objective="cost", id_run=1)
print("Cost optimization:")
print(" Total GWP:", result_cost.objectives['TotalGWP'].values[0][0])
print(" Total Cost:", result_cost.objectives['TotalCost'].values[0][0])
result_cost = run_pyomo_model(pyomo_ch, objective="cost", id_run=1)
print("Cost optimization:")
print(" Total GWP:", result_cost.objectives['TotalGWP'].values[0][0])
print(" Total Cost:", result_cost.objectives['TotalCost'].values[0][0])
Cost optimization: Total GWP: 26249.879832788352 Total Cost: 13668.185984877611