{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "*This notebook contains course material from [CBE30338](https://jckantor.github.io/CBE30338)\n", "by Jeffrey Kantor (jeff at nd.edu); the content is available [on Github](https://github.com/jckantor/CBE30338.git).\n", "The text is released under the [CC-BY-NC-ND-4.0 license](https://creativecommons.org/licenses/by-nc-nd/4.0/legalcode),\n", "and code is released under the [MIT license](https://opensource.org/licenses/MIT).*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "< [Gasoline Blending](http://nbviewer.jupyter.org/github/jckantor/CBE30338/blob/master/notebooks/06.08-Gasoline-Blending.ipynb) | [Contents](toc.ipynb) | [Simulation and Optimal Control](http://nbviewer.jupyter.org/github/jckantor/CBE30338/blob/master/notebooks/07.00-Simulation-and-Optimal-Control.ipynb) >
"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Pyomo Examples"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example: Nonlinear Optimization of Series Reaction in a Continuous Stirred Tank Reactor"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 Var Declarations\n",
" q : Size=1, Index=None\n",
" Key : Lower : Value : Upper : Fixed : Stale : Domain\n",
" None : None : 8.944271909985442 : None : False : False : Reals\n",
"\n",
"1 Objective Declarations\n",
" objective : Size=1, Index=None, Active=True\n",
" Key : Active : Sense : Expression\n",
" None : True : maximize : 40.0*q/(q + 4.0)/(q + 20.0)\n",
"\n",
"2 Declarations: q objective\n",
"\n",
"Flowrate at maximum CB = 8.944271909985442 liters per minute.\n",
"\n",
"Maximum CB = 0.954915028125263 moles per liter.\n",
"\n",
"Productivity = 8.541019662483748 moles per minute.\n"
]
}
],
"source": [
"from pyomo.environ import *\n",
"\n",
"V = 40 # liters\n",
"kA = 0.5 # 1/min\n",
"kB = 0.1 # l/min\n",
"CAf = 2.0 # moles/liter\n",
"\n",
"# create a model instance\n",
"model = ConcreteModel()\n",
"\n",
"# create x and y variables in the model\n",
"model.q = Var()\n",
"\n",
"# add a model objective\n",
"model.objective = Objective(expr = model.q*V*kA*CAf/(model.q + V*kB)/(model.q + V*kA), sense=maximize)\n",
"\n",
"# compute a solution using ipopt for nonlinear optimization\n",
"results = SolverFactory('ipopt').solve(model)\n",
"model.pprint()\n",
"\n",
"\n",
"# print solutions\n",
"qmax = model.q()\n",
"CBmax = model.objective()\n",
"print('\\nFlowrate at maximum CB = ', qmax, 'liters per minute.')\n",
"print('\\nMaximum CB =', CBmax, 'moles per liter.')\n",
"print('\\nProductivity = ', qmax*CBmax, 'moles per minute.')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example 19.3: Linear Programming Refinery"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
" Price Max Production\n",
"Gasoline 72 24000\n",
"Kerosine 48 2000\n",
"Fuel Oil 42 6000\n",
"Residual 20 100000\n",
"\n",
" Processing Cost Feed Costs\n",
"Crude 1 1.0 48\n",
"Crude 2 2.0 30\n",
"\n",
" Crude 1 Crude 2\n",
"Gasoline 0.80 0.44\n",
"Kerosine 0.05 0.10\n",
"Fuel Oil 0.10 0.36\n",
"Residual 0.05 0.10\n"
]
}
],
"source": [
"import pandas as pd\n",
"\n",
"PRODUCTS = ['Gasoline', 'Kerosine', 'Fuel Oil', 'Residual']\n",
"FEEDS = ['Crude 1', 'Crude 2']\n",
"\n",
"products = pd.DataFrame(index=PRODUCTS)\n",
"products['Price'] = [72, 48, 42, 20]\n",
"products['Max Production'] = [24000, 2000, 6000, 100000]\n",
"\n",
"crudes = pd.DataFrame(index=FEEDS)\n",
"crudes['Processing Cost'] = [1.00, 2.00]\n",
"crudes['Feed Costs'] = [48, 30]\n",
"\n",
"yields = pd.DataFrame(index=PRODUCTS)\n",
"yields['Crude 1'] = [0.80, 0.05, 0.10, 0.05]\n",
"yields['Crude 2'] = [0.44, 0.10, 0.36, 0.10]\n",
"\n",
"print('\\n', products)\n",
"print('\\n', crudes)\n",
"print('\\n', yields)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'Gasoline': 72, 'Kerosine': 48}"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"price = {}\n",
"price['Gasoline'] = 72\n",
"price['Kerosine'] = 48\n",
"price\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"72"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"products.loc['Gasoline','Price']"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/jeff/opt/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:9: FutureWarning: \n",
".ix is deprecated. Please use\n",
".loc for label based indexing or\n",
".iloc for positional indexing\n",
"\n",
"See the documentation here:\n",
"http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#ix-indexer-is-deprecated\n",
" if __name__ == '__main__':\n",
"/Users/jeff/opt/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:10: FutureWarning: \n",
".ix is deprecated. Please use\n",
".loc for label based indexing or\n",
".iloc for positional indexing\n",
"\n",
"See the documentation here:\n",
"http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#ix-indexer-is-deprecated\n",
" # Remove the CWD from sys.path while we load stuff.\n"
]
}
],
"source": [
"# model formulation\n",
"model = ConcreteModel()\n",
"\n",
"# variables\n",
"model.x = Var(FEEDS, domain=NonNegativeReals)\n",
"model.y = Var(PRODUCTS, domain=NonNegativeReals)\n",
"\n",
"# objective\n",
"income = sum(products.ix[p, 'Price'] * model.y[p] for p in PRODUCTS)\n",
"raw_materials_cost = sum(crudes.ix[f,'Feed Costs'] * model.x[f] for f in FEEDS)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'processing_costs' is not defined",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m "
]
}
],
"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.7.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}