{ "cells": [ { "cell_type": "markdown", "id": "4dae19b6", "metadata": {}, "source": [ "\n", "*This notebook contains material from [CBE60499](https://ndcbe.github.io/CBE60499);\n", "content is available [on Github](git@github.com:ndcbe/CBE60499.git).*\n" ] }, { "cell_type": "markdown", "id": "4ad3d2d1", "metadata": {}, "source": [ "\n", "| [Contents](toc.html) | [Tag Index](tag_index.html) | [1.1 60 Minutes to Pyomo: An Energy Storage Model Predictive Control Example](https://ndcbe.github.io/CBE60499/01.01-Pyomo-Nuts-and-Bolts.html) >
"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 1,
"link": "[1.0 Getting Started with Pyomo](https://ndcbe.github.io/CBE60499/01.00-Pyomo-Introduction.html#1.0-Getting-Started-with-Pyomo)",
"section": "1.0 Getting Started with Pyomo"
}
},
"source": [
"# 1.0 Getting Started with Pyomo"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 2,
"link": "[1.0.1 Local Installation](https://ndcbe.github.io/CBE60499/01.00-Pyomo-Introduction.html#1.0.1-Local-Installation)",
"section": "1.0.1 Local Installation"
}
},
"source": [
"## 1.0.1 Local Installation"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 3,
"link": "[1.0.1.1 Install anaconda](https://ndcbe.github.io/CBE60499/01.00-Pyomo-Introduction.html#1.0.1.1-Install-anaconda)",
"section": "1.0.1.1 Install anaconda"
}
},
"source": [
"### 1.0.1.1 Install anaconda"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 3,
"link": "[1.0.1.1 Install anaconda](https://ndcbe.github.io/CBE60499/01.00-Pyomo-Introduction.html#1.0.1.1-Install-anaconda)",
"section": "1.0.1.1 Install anaconda"
}
},
"source": [
"Anaconda is a Python distribution that supports the `conda` package manager and environments. Follow these steps if you do not have anaconda or miniconda already installed:\n",
"* https://docs.anaconda.com/anaconda/install/\n",
"* https://docs.conda.io/en/latest/miniconda.html"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 3,
"link": "[1.0.1.2 Create a new conda environment](https://ndcbe.github.io/CBE60499/01.00-Pyomo-Introduction.html#1.0.1.2-Create-a-new-conda-environment)",
"section": "1.0.1.2 Create a new conda environment"
}
},
"source": [
"### 1.0.1.2 Create a new conda environment"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 3,
"link": "[1.0.1.2 Create a new conda environment](https://ndcbe.github.io/CBE60499/01.00-Pyomo-Introduction.html#1.0.1.2-Create-a-new-conda-environment)",
"section": "1.0.1.2 Create a new conda environment"
}
},
"source": [
"`conda` envirorments allow us to maintain several difference Python installations on our computer. This allows for easy switching between Python versions or different versions of Python packages. Using an environment will ensure anything we install for this class does not disrupt your other Python workflows.\n",
"\n",
"After installing `conda`, run this command in the terminal:\n",
"\n",
"```\n",
"conda create --name spring2021 python=3.8\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 3,
"link": "[1.0.1.2 Create a new conda environment](https://ndcbe.github.io/CBE60499/01.00-Pyomo-Introduction.html#1.0.1.2-Create-a-new-conda-environment)",
"section": "1.0.1.2 Create a new conda environment"
}
},
"source": [
"This creates an environment called `spring2021`. Next we need to activate it:\n",
"\n",
"```\n",
"conda activate spring2021\n",
"```\n",
"\n",
"Whenever you open a new terminal, you will likely need to activate our new environment."
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 3,
"link": "[1.0.1.3 Install IDAES-PSE](https://ndcbe.github.io/CBE60499/01.00-Pyomo-Introduction.html#1.0.1.3-Install-IDAES-PSE)",
"section": "1.0.1.3 Install IDAES-PSE"
}
},
"source": [
"### 1.0.1.3 Install IDAES-PSE"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 3,
"link": "[1.0.1.3 Install IDAES-PSE](https://ndcbe.github.io/CBE60499/01.00-Pyomo-Introduction.html#1.0.1.3-Install-IDAES-PSE)",
"section": "1.0.1.3 Install IDAES-PSE"
}
},
"source": [
"The IDAES-PSE toolset (Institute for the Design of Advanced Energy Systems - Process Systems Engineering) is an open-source framework for multiscale process modeling, optimization, and advanced analytics. The IDAES toolset is built on top of Pyomo. We'll mainly use Pyomo in this class, although we'll highlight a few IDAES capabilities.\n",
"\n",
"To install `idaes`, run:\n",
"\n",
"```\n",
" conda install -c idaes-pse -c conda-forge idaes-pse\n",
"```\n",
"\n",
"This command will install `idaes` as well as all of its dependencies, including `pyomo`.\n",
"\n",
"After `conda` finishes, run:\n",
"\n",
"```\n",
"idaes --version\n",
"```\n",
"\n",
"To verify `idaes` is installed in your active environment."
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 3,
"link": "[1.0.1.4 Install Ipopt](https://ndcbe.github.io/CBE60499/01.00-Pyomo-Introduction.html#1.0.1.4-Install-Ipopt)",
"section": "1.0.1.4 Install Ipopt"
}
},
"source": [
"### 1.0.1.4 Install Ipopt"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 3,
"link": "[1.0.1.4 Install Ipopt](https://ndcbe.github.io/CBE60499/01.00-Pyomo-Introduction.html#1.0.1.4-Install-Ipopt)",
"section": "1.0.1.4 Install Ipopt"
}
},
"source": [
"**Windows and Linux users** - you are in luck. IDAES comes with a nonlinear optimization solver Ipopt compiled with the HSL linear algebra routines; I may refer to this as \"good Ipopt\". Later in the semester, we'll talk about why the linear alegbra library is important. To install the \"good Ipopt\", run:\n",
"\n",
"```\n",
"idaes get-extensions\n",
"```\n",
"\n",
"Linux users: you may need to install some dependencies too: https://idaes-pse.readthedocs.io/en/stable/getting_started/index.html#linux"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 3,
"link": "[1.0.1.4 Install Ipopt](https://ndcbe.github.io/CBE60499/01.00-Pyomo-Introduction.html#1.0.1.4-Install-Ipopt)",
"section": "1.0.1.4 Install Ipopt"
}
},
"source": [
"This will install Ipopt in a hidden user folder. If you get the error `WARNING: Could not locate the 'ipopt' executable` add the line `import idaes` to the top of your notebook then restart the Python kernel. Our library `helper` takes care of adding Ipopt to the system path on Colab. If you use the instructions below, \"okay\" Ipopt will be installed."
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 3,
"link": "[1.0.1.4 Install Ipopt](https://ndcbe.github.io/CBE60499/01.00-Pyomo-Introduction.html#1.0.1.4-Install-Ipopt)",
"section": "1.0.1.4 Install Ipopt"
}
},
"source": [
"**macOS users** - unfortunately, IDAES does not include binaries for \"good Ipopt\". You can either compile the solver yourself after obtaining an academic license for HSL or you can install the \"okay Ipopt\" using conda:\n",
"\n",
"```\n",
"conda install -c conda-forge ipopt\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 3,
"link": "[1.0.1.5 Install Additional Solvers](https://ndcbe.github.io/CBE60499/01.00-Pyomo-Introduction.html#1.0.1.5-Install-Additional-Solvers)",
"section": "1.0.1.5 Install Additional Solvers"
}
},
"source": [
"### 1.0.1.5 Install Additional Solvers"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 3,
"link": "[1.0.1.5 Install Additional Solvers](https://ndcbe.github.io/CBE60499/01.00-Pyomo-Introduction.html#1.0.1.5-Install-Additional-Solvers)",
"section": "1.0.1.5 Install Additional Solvers"
}
},
"source": [
"In additional to `ipopt`, we will also use `glpk`. To install it, run:\n",
"\n",
"```\n",
"conda install -c conda-forge glpk\n",
"```\n",
"\n",
"You may also wish to install other commercial solvers, especially if you want to consider large mixed integer optimization problems for research. These solvers offer free academic licenses:\n",
"* CPLEX: https://www.ibm.com/products/ilog-cplex-optimization-studio\n",
"* Gurobi: https://www.gurobi.com/downloads/end-user-license-agreement-academic/"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 3,
"link": "[1.0.1.6 Install Notebook Spellcheker](https://ndcbe.github.io/CBE60499/01.00-Pyomo-Introduction.html#1.0.1.6-Install-Notebook-Spellcheker)",
"section": "1.0.1.6 Install Notebook Spellcheker"
}
},
"source": [
"### 1.0.1.6 Install Notebook Spellcheker\n",
"\n",
"Here is a great Jupyter extension that adds a [spellchecker](https://jupyter-contrib-nbextensions.readthedocs.io/en/latest/nbextensions/spellchecker/README.html) to your notebooks. To install, run the following commands in the terminal (in your new conda environment):\n",
"\n",
"```\n",
"conda install -c conda-forge jupyter_contrib_nbextensions\n",
"jupyter contrib nbextension install --user\n",
"jupyter nbextension enable spellchecker/main\n",
"```\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 2,
"link": "[1.0.2 Cloud Computing with Google Colab](https://ndcbe.github.io/CBE60499/01.00-Pyomo-Introduction.html#1.0.2-Cloud-Computing-with-Google-Colab)",
"section": "1.0.2 Cloud Computing with Google Colab"
}
},
"source": [
"## 1.0.2 Cloud Computing with Google Colab"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"nbpages": {
"level": 2,
"link": "[1.0.2 Cloud Computing with Google Colab](https://ndcbe.github.io/CBE60499/01.00-Pyomo-Introduction.html#1.0.2-Cloud-Computing-with-Google-Colab)",
"section": "1.0.2 Cloud Computing with Google Colab"
}
},
"outputs": [],
"source": [
"## Tip: Please put code like this at the top of your notebook.\n",
"# We want all of the module/package installations to start up front\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()"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 2,
"link": "[1.0.3 Your First Optimization Problem](https://ndcbe.github.io/CBE60499/01.00-Pyomo-Introduction.html#1.0.3-Your-First-Optimization-Problem)",
"section": "1.0.3 Your First Optimization Problem"
}
},
"source": [
"## 1.0.3 Your First Optimization Problem"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 2,
"link": "[1.0.3 Your First Optimization Problem](https://ndcbe.github.io/CBE60499/01.00-Pyomo-Introduction.html#1.0.3-Your-First-Optimization-Problem)",
"section": "1.0.3 Your First Optimization Problem"
}
},
"source": [
"We are now ready to solve your first optimization problem in Pyomo."
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 3,
"link": "[1.0.3.1 Mathematical Model](https://ndcbe.github.io/CBE60499/01.00-Pyomo-Introduction.html#1.0.3.1-Mathematical-Model)",
"section": "1.0.3.1 Mathematical Model"
}
},
"source": [
"### 1.0.3.1 Mathematical Model"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 3,
"link": "[1.0.3.1 Mathematical Model](https://ndcbe.github.io/CBE60499/01.00-Pyomo-Introduction.html#1.0.3.1-Mathematical-Model)",
"section": "1.0.3.1 Mathematical Model"
}
},
"source": [
"Let's start with a purely mathematical example:\n",
"\n",
"$$\\begin{align*} \\min_{x} \\quad & x_1^2 + 2 x_2^2 - x_3 \\\\\n",
"\\mathrm{s.t.} \\quad & x_1 + x_2 = 1 \\\\\n",
"& x_1 + 2 x_2 - x_3 = 5 \\\\\n",
"& -10 \\leq x_1, x_2, x_3 \\leq 10 \n",
"\\end{align*} $$\n",
"\n",
"We want to solve the constrained optimization problem numerically."
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 3,
"link": "[1.0.3.2 Define the Model in Pyomo](https://ndcbe.github.io/CBE60499/01.00-Pyomo-Introduction.html#1.0.3.2-Define-the-Model-in-Pyomo)",
"section": "1.0.3.2 Define the Model in Pyomo"
}
},
"source": [
"### 1.0.3.2 Define the Model in Pyomo"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 3,
"link": "[1.0.3.2 Define the Model in Pyomo](https://ndcbe.github.io/CBE60499/01.00-Pyomo-Introduction.html#1.0.3.2-Define-the-Model-in-Pyomo)",
"section": "1.0.3.2 Define the Model in Pyomo"
}
},
"source": [
"
"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.8.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}