{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 60 Minutes to Pyomo: An Energy Storage Model Predictive Control Example" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# This code cell installs packages on Colab\n", "\n", "import sys\n", "if \"google.colab\" in sys.modules:\n", " !wget \"https://raw.githubusercontent.com/ndcbe/CBE60499/main/notebooks/helper.py\"\n", " import helper\n", " helper.install_idaes()\n", " helper.install_ipopt()\n", " helper.install_glpk()\n", " '''\n", " helper.download_data(['Prices_DAM_ALTA2G_7_B1.csv'])\n", " helper.download_figures(['battery.png','pyomo-table-4.1.png',\n", " 'pyomo-table-4.2.png','pyomo-table-4.3.png',\n", " 'pyomo-table-4.4.png','pyomo-table-4.6.png'])\n", " '''" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import pyomo.environ as pyo\n", "import numpy as np\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Problem Setup" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Background" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In many regions of the world, including the US, electricity generation is scheduled through wholesale electricity markets. Individual generators (resources) transmit information about their operating costs and constraints to the market via a bid. The market operator then solves an optimization problem (e.g., the unit commitment problem) to minimize the total electricity generator cost. The market operator decides which generators to dispatch during each hour to satisfy the forecasted demand while honoring limitations for each generator (e.g., maximum ramp rate, the required time for start-up/shutdown, etc.).\n", "\n", "\n", "\n", "Read more information here:\n", "* https://www.ferc.gov/industries-data/market-assessments/electric-power-markets\n", "* https://www.sciencedirect.com/science/article/pii/S0306261916318487" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Pandas and Energy Prices" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The CSV (comma separated value) file `Prices_DAM_ALTA2G_7_B1.csv` contains price data for a single location in California for an entire year. The prices are set every hour and have units $/MWh. We will use the package `pandas` to import and analyze the data." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | price | \n", "
---|---|
0 | \n", "36.757 | \n", "
1 | \n", "34.924 | \n", "
2 | \n", "33.389 | \n", "
3 | \n", "32.035 | \n", "
4 | \n", "33.694 | \n", "
\n", " | price | \n", "
---|---|
count | \n", "8760.000000 | \n", "
mean | \n", "32.516994 | \n", "
std | \n", "9.723477 | \n", "
min | \n", "-2.128700 | \n", "
25% | \n", "26.510000 | \n", "
50% | \n", "30.797500 | \n", "
75% | \n", "37.544750 | \n", "
max | \n", "116.340000 | \n", "
Activity
\n", " What are 2 or 3 interesting observations from these summary statistics?\n", "Activity
\n", " What are 1 or 2 interesting observations from these plots?\n", "Activity
\n", " Write on paper a 1-sentence description for each equation.\n", "Activity
\n", " Perform degree of freedom analysis.\n", "Activity
\n", " Uncomment the line below and look at the error message and read below. Then comment the line out again and rerun the notebook up to this cell.\n", "Activity
\n", " Confirm my_data_dict contains the correct prices for the January 2, 2015.\n", "Activity
\n", " Comment out Approach 2 above and rerun the notebook. You'll get a warning message if you do not comment out Approach 1. The answer should not change.\n", "Activity
\n", " Compare the two model equations above to the optimization formulation below. Notice the one-to-one corespondance between the equality constraints in the mathematical formulation (below) and calls to pyo.Constraint. Also notice the sets used to create the Pyomo model are listed next to each constraint in the mathematical model. Once you learn the Pyomo syntax, translated a mathematical model into code is easy! \n", "Activity
\n", " Does our Pyomo model match the optimization mathematical model (equations above)? How did we incorporate the inequality constraints into the Pyomo model?\n", "Activity
\n", " The function below uses elements of price directly. Update the function to add the price data as a parameter in the Pyomo model. Make this parameter mutable as shown above.\n", "Activity
\n", " Fix any syntax errors with the function below.\n", "Activity
\n", " Compare this output to your degree of freedom analysis.\n", "Activity
\n", " Create a new instance of SolverFactory by specifying 'glpk' as the solver name. Then solve the Pyomo model m and store the results in results2.\n", "Activity
\n", " Improve the formatting of the plots using the code from the top of the notebook (pandas section).\n", "