{"nbformat":4,"nbformat_minor":0,"metadata":{"celltoolbar":"Tags","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"},"colab":{"name":"Copy of 06.08-Gasoline-Blending.ipynb","provenance":[{"file_id":"https://github.com/jckantor/CBE30338/blob/master/notebooks/06.08-Gasoline-Blending.ipynb","timestamp":1631146520813}]}},"cells":[{"cell_type":"markdown","metadata":{"id":"f94FEwMv5LcL"},"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":{"id":"OL2Cw-KL5LcP"},"source":["\n","< [Design of a Cold Weather Fuel](http://nbviewer.jupyter.org/github/jckantor/CBE30338/blob/master/notebooks/06.07-Design-of-a-Cold-Weather-Fuel.ipynb) | [Contents](toc.ipynb) | [Pyomo Examples](http://nbviewer.jupyter.org/github/jckantor/CBE30338/blob/master/notebooks/06.99-Pyomo-Examples.ipynb) >

\"Open

\"Download\""]},{"cell_type":"markdown","metadata":{"id":"SlZow33_5LcP"},"source":["# Gasoline Blending\n","\n","The task is to determine the most profitable blend of gasoline products from given set of refinery streams."]},{"cell_type":"code","metadata":{"id":"UAT3SRgP5LcQ","executionInfo":{"status":"ok","timestamp":1631146307667,"user_tz":240,"elapsed":24629,"user":{"displayName":"","photoUrl":"","userId":""}},"outputId":"683ee93e-34e5-4b94-d70a-f48daabc5ab5","colab":{"base_uri":"https://localhost:8080/"}},"source":["%matplotlib inline\n","\n","import shutil\n","import sys\n","import os.path\n","\n","if not shutil.which(\"pyomo\"):\n"," !pip install -q pyomo\n"," assert(shutil.which(\"pyomo\"))\n","\n","if not (shutil.which(\"cbc\") or os.path.isfile(\"cbc\")):\n"," if \"google.colab\" in sys.modules:\n"," !apt-get install -y -qq coinor-cbc\n"," else:\n"," try:\n"," !conda install -c conda-forge coincbc \n"," except:\n"," pass\n","\n","assert(shutil.which(\"cbc\") or os.path.isfile(\"cbc\"))\n","\n","import pandas as pd\n","import pyomo.environ as pyomo"],"execution_count":1,"outputs":[{"output_type":"stream","name":"stdout","text":["\u001b[K |████████████████████████████████| 9.1 MB 2.6 MB/s \n","\u001b[K |████████████████████████████████| 49 kB 3.0 MB/s \n","\u001b[?25hSelecting previously unselected package coinor-libcoinutils3v5.\n","(Reading database ... 148492 files and directories currently installed.)\n","Preparing to unpack .../0-coinor-libcoinutils3v5_2.10.14+repack1-1_amd64.deb ...\n","Unpacking coinor-libcoinutils3v5 (2.10.14+repack1-1) ...\n","Selecting previously unselected package coinor-libosi1v5.\n","Preparing to unpack .../1-coinor-libosi1v5_0.107.9+repack1-1_amd64.deb ...\n","Unpacking coinor-libosi1v5 (0.107.9+repack1-1) ...\n","Selecting previously unselected package coinor-libclp1.\n","Preparing to unpack .../2-coinor-libclp1_1.16.11+repack1-1_amd64.deb ...\n","Unpacking coinor-libclp1 (1.16.11+repack1-1) ...\n","Selecting previously unselected package coinor-libcgl1.\n","Preparing to unpack .../3-coinor-libcgl1_0.59.10+repack1-1_amd64.deb ...\n","Unpacking coinor-libcgl1 (0.59.10+repack1-1) ...\n","Selecting previously unselected package coinor-libcbc3.\n","Preparing to unpack .../4-coinor-libcbc3_2.9.9+repack1-1_amd64.deb ...\n","Unpacking coinor-libcbc3 (2.9.9+repack1-1) ...\n","Selecting previously unselected package coinor-cbc.\n","Preparing to unpack .../5-coinor-cbc_2.9.9+repack1-1_amd64.deb ...\n","Unpacking coinor-cbc (2.9.9+repack1-1) ...\n","Setting up coinor-libcoinutils3v5 (2.10.14+repack1-1) ...\n","Setting up coinor-libosi1v5 (0.107.9+repack1-1) ...\n","Setting up coinor-libclp1 (1.16.11+repack1-1) ...\n","Setting up coinor-libcgl1 (0.59.10+repack1-1) ...\n","Setting up coinor-libcbc3 (2.9.9+repack1-1) ...\n","Setting up coinor-cbc (2.9.9+repack1-1) ...\n","Processing triggers for man-db (2.8.3-2ubuntu0.1) ...\n","Processing triggers for libc-bin (2.27-3ubuntu1.2) ...\n","/sbin/ldconfig.real: /usr/local/lib/python3.7/dist-packages/ideep4py/lib/libmkldnn.so.0 is not a symbolic link\n","\n"]}]},{"cell_type":"markdown","metadata":{"id":"CBTtE8RN5LcR"},"source":["## Gasoline Product Specifications\n","\n","The gasoline products include regular and premium gasoline. In addition to the current price, the specifications include\n","\n","* **octane** the minimum road octane number. Road octane is the computed as the average of the Research Octane Number (RON) and Motor Octane Number (MON).\n","* **Reid Vapor Pressure** Upper and lower limits are specified for the Reid vapor pressure. The Reid vapor pressure is the absolute pressure exerted by the liquid at 100°F.\n","* **benzene** the maximum volume percentage of benzene allowed in the final product. Benzene helps to increase octane rating, but is also a treacherous environmental contaminant.\n"]},{"cell_type":"code","metadata":{"id":"HD6Vu1Y-5LcR","executionInfo":{"status":"ok","timestamp":1631146312704,"user_tz":240,"elapsed":144,"user":{"displayName":"","photoUrl":"","userId":""}},"outputId":"74478fcd-00ef-4100-ebc7-ef651284c349","colab":{"base_uri":"https://localhost:8080/"}},"source":["products = {\n"," 'Regular' : {'price': 2.75, 'octane': 87, 'RVPmin': 0.0, 'RVPmax': 15.0, 'benzene': 1.1},\n"," 'Premium' : {'price': 2.85, 'octane': 91, 'RVPmin': 0.0, 'RVPmax': 15.0, 'benzene': 1.1},\n","}\n","\n","print(pd.DataFrame.from_dict(products).T)"],"execution_count":2,"outputs":[{"output_type":"stream","name":"stdout","text":[" price octane RVPmin RVPmax benzene\n","Regular 2.75 87.0 0.0 15.0 1.1\n","Premium 2.85 91.0 0.0 15.0 1.1\n"]}]},{"cell_type":"markdown","metadata":{"id":"h6y6T93A5LcS"},"source":["## Stream Specifications\n","\n","A typical refinery produces many intermediate streams that can be incorporated in a blended gasoline product. Here we provide data on seven streams that include:\n","\n","* **Butane** n-butane is a C4 product stream produced from the light components of the crude being processed by the refinery. Butane is a highly volatile of gasoline.\n","* **LSR** Light straight run naptha is a 90°F to 190°F cut from the crude distillation column primarily consisting of straight chain C5-C6 hydrocarbons.\n","* **Isomerate** is the result of isomerizing LSR to produce branched molecules that results in higher octane number.\n","* **Reformate** is result of catalytic reforming heavy straight run napthenes to produce a high octane blending component, as well by-product hydrogen used elsewhere in the refinery for hydro-treating.\n","* **Reformate LB** is a is a low benzene variant of reformate.\n","* **FCC Naphta** is the product of a fluidized catalytic cracking unit designed to produce gasoline blending components from long chain hydrocarbons present in the crude oil being processed by the refinery.\n","* **Alkylate** The alkylation unit reacts iso-butane with low-molecular weight alkenes to produce a high octane blending component for gasoline.\n","\n","The stream specifications include research octane and motor octane numbers for each blending component, the Reid vapor pressure, the benzene content, cost, and availability (in gallons per day). The road octane number is computed as the average of the RON and MON."]},{"cell_type":"code","metadata":{"id":"GOaiyUlp5LcT","executionInfo":{"status":"ok","timestamp":1631146359027,"user_tz":240,"elapsed":152,"user":{"displayName":"","photoUrl":"","userId":""}},"outputId":"63351443-2112-4d59-ce01-eb4db9118807","colab":{"base_uri":"https://localhost:8080/"}},"source":["streams = {\n"," 'Butane' : {'RON': 93.0, 'MON': 92.0, 'RVP': 54.0, 'benzene': 0.00, 'cost': 0.85, 'avail': 30000},\n"," 'LSR' : {'RON': 78.0, 'MON': 76.0, 'RVP': 11.2, 'benzene': 0.73, 'cost': 2.05, 'avail': 35000},\n"," 'Isomerate' : {'RON': 83.0, 'MON': 81.1, 'RVP': 13.5, 'benzene': 0.00, 'cost': 2.20, 'avail': 0},\n"," 'Reformate' : {'RON':100.0, 'MON': 88.2, 'RVP': 3.2, 'benzene': 1.85, 'cost': 2.80, 'avail': 60000},\n"," 'Reformate LB' : {'RON': 93.7, 'MON': 84.0, 'RVP': 2.8, 'benzene': 0.12, 'cost': 2.75, 'avail': 0},\n"," 'FCC Naphtha' : {'RON': 92.1, 'MON': 77.1, 'RVP': 1.4, 'benzene': 1.06, 'cost': 2.60, 'avail': 70000},\n"," 'Alkylate' : {'RON': 97.3, 'MON': 95.9, 'RVP': 4.6, 'benzene': 0.00, 'cost': 2.75, 'avail': 40000},\n","}\n","\n","# calculate road octane as (R+M)/2\n","for s in streams.keys():\n"," streams[s]['octane'] = (streams[s]['RON'] + streams[s]['MON'])/2\n"," \n","# display feed information\n","print(pd.DataFrame.from_dict(streams).T)"],"execution_count":3,"outputs":[{"output_type":"stream","name":"stdout","text":[" RON MON RVP benzene cost avail octane\n","Butane 93.0 92.0 54.0 0.00 0.85 30000.0 92.50\n","LSR 78.0 76.0 11.2 0.73 2.05 35000.0 77.00\n","Isomerate 83.0 81.1 13.5 0.00 2.20 0.0 82.05\n","Reformate 100.0 88.2 3.2 1.85 2.80 60000.0 94.10\n","Reformate LB 93.7 84.0 2.8 0.12 2.75 0.0 88.85\n","FCC Naphtha 92.1 77.1 1.4 1.06 2.60 70000.0 84.60\n","Alkylate 97.3 95.9 4.6 0.00 2.75 40000.0 96.60\n"]}]},{"cell_type":"markdown","metadata":{"id":"NPzSRsBC5LcU"},"source":["## Blending Model\n","\n","This simplified blending model assumes the product attributes can be computed as linear volume weighted averages of the component properties. Let the decision variable $x_{s,p} \\geq 0$ be the volume, in gallons, of blending component $s \\in S$ used in the final product $p \\in P$.\n","\n","The objective is maximize profit, which is the difference between product revenue and stream costs. \n","\n","\\begin{align}\n","\\mbox{profit} & = \\max_{x_{s,p}}\\left( \\sum_{p\\in P} \\mbox{Price}_p \\sum_{s\\in S} x_{s,p}\n","- \\sum_{s\\in S} \\mbox{Cost}_s \\sum_{p\\in P} x_{s,p}\\right)\n","\\end{align}\n","or\n","\\begin{align}\n","\\mbox{profit} & = \\max_{x_{s,p}} \\sum_{p\\in P} \\sum_{s\\in S} x_{s,p}\\mbox{Price}_p - \\max_{x_{s,p}} \\sum_{p\\in P} \\sum_{s\\in S} x_{s,p}\\mbox{Cost}_s\n","\\end{align}\n","\n","The blending constraint for octane can be written as \n","\n","\\begin{align}\n","\\frac{\\sum_{s \\in S} x_{s,p} \\mbox{Octane}_s}{\\sum_{s \\in S} x_{s,p}} & \\geq \\mbox{Octane}_p & \\forall p \\in P\n","\\end{align}\n","\n","where $\\mbox{Octane}_s$ refers to the octane rating of stream $s$, whereas $\\mbox{Octane}_p$ refers to the octane rating of product $p$. Multiplying through by the denominator, and consolidating terms gives\n","\n","\\begin{align}\n","\\sum_{s \\in S} x_{s,p}\\left(\\mbox{Octane}_s - \\mbox{Octane}_p\\right) & \\geq 0 & \\forall p \\in P\n","\\end{align}\n","\n","The same assumptions and development apply to the benzene constraint\n","\n","\\begin{align}\n","\\sum_{s \\in S} x_{s,p}\\left(\\mbox{Benzene}_s - \\mbox{Benzene}_p\\right) & \\leq 0 & \\forall p \\in P\n","\\end{align}\n","\n","Reid vapor pressure, however, follows a somewhat different mixing rule. For the Reid vapor pressure we have\n","\n","\\begin{align}\n","\\sum_{s \\in S} x_{s,p}\\left(\\mbox{RVP}_s^{1.25} - \\mbox{RVP}_{min,p}^{1.25}\\right) & \\geq 0 & \\forall p \\in P \\\\\n","\\sum_{s \\in S} x_{s,p}\\left(\\mbox{RVP}_s^{1.25} - \\mbox{RVP}_{max,p}^{1.25}\\right) & \\leq 0 & \\forall p \\in P\n","\\end{align}\n","\n","This model is implemented in the following cell."]},{"cell_type":"code","metadata":{"scrolled":false,"id":"NrY25kiq5LcV","executionInfo":{"status":"ok","timestamp":1631146400280,"user_tz":240,"elapsed":330,"user":{"displayName":"","photoUrl":"","userId":""}},"outputId":"bb4b0619-91fb-4dd5-962a-36ea7c91584e","colab":{"base_uri":"https://localhost:8080/"}},"source":["import pyomo.environ as pyomo\n","\n","# create model\n","m = pyomo.ConcreteModel()\n","\n","# create decision variables\n","S = streams.keys()\n","P = products.keys()\n","m.x = pyomo.Var(S,P, domain=pyomo.NonNegativeReals)\n"," \n","# objective\n","revenue = sum(sum(m.x[s,p]*products[p]['price'] for s in S) for p in P)\n","cost = sum(sum(m.x[s,p]*streams[s]['cost'] for s in S) for p in P)\n","m.profit = pyomo.Objective(expr = revenue - cost, sense=pyomo.maximize)\n","\n","# constraints\n","m.cons = pyomo.ConstraintList()\n","for s in S:\n"," m.cons.add(sum(m.x[s,p] for p in P) <= streams[s]['avail'])\n","for p in P:\n"," m.cons.add(sum(m.x[s,p]*(streams[s]['octane'] - products[p]['octane']) for s in S) >= 0)\n"," m.cons.add(sum(m.x[s,p]*(streams[s]['RVP']**1.25 - products[p]['RVPmin']**1.25) for s in S) >= 0)\n"," m.cons.add(sum(m.x[s,p]*(streams[s]['RVP']**1.25 - products[p]['RVPmax']**1.25) for s in S) <= 0)\n"," m.cons.add(sum(m.x[s,p]*(streams[s]['benzene'] - products[p]['benzene']) for s in S) <= 0)\n","\n","# solve\n","solver = pyomo.SolverFactory('cbc')\n","solver.solve(m)\n","\n","# display results\n","vol = sum(m.x[s,p]() for s in S for p in P)\n","print(\"Total Volume =\", round(vol, 1), \"gallons.\")\n","print(\"Total Profit =\", round(m.profit(), 1), \"dollars.\")\n","print(\"Profit =\", round(100*m.profit()/vol,1), \"cents per gallon.\")"],"execution_count":6,"outputs":[{"output_type":"stream","name":"stdout","text":["Total Volume = 235000.0 gallons.\n","Total Profit = 100425.0 dollars.\n","Profit = 42.7 cents per gallon.\n"]}]},{"cell_type":"markdown","metadata":{"id":"kjRg5vIf5LcW"},"source":["## Display Results"]},{"cell_type":"markdown","metadata":{"id":"4fPqFazD5LcW"},"source":["### Results for each Stream"]},{"cell_type":"code","metadata":{"id":"ttSHjaOl5LcX","executionInfo":{"status":"ok","timestamp":1631146415736,"user_tz":240,"elapsed":138,"user":{"displayName":"","photoUrl":"","userId":""}},"outputId":"e1ef2d7f-72ba-4667-d433-5f9e64cecd63","colab":{"base_uri":"https://localhost:8080/"}},"source":["stream_results = pd.DataFrame()\n","for s in S:\n"," for p in P:\n"," stream_results.loc[s,p] = round(m.x[s,p](), 1)\n"," stream_results.loc[s,'Total'] = round(sum(m.x[s,p]() for p in P), 1)\n"," stream_results.loc[s,'Available'] = streams[s]['avail']\n"," \n","stream_results['Unused (Slack)'] = stream_results['Available'] - stream_results['Total']\n","print(stream_results)"],"execution_count":7,"outputs":[{"output_type":"stream","name":"stdout","text":[" Regular Premium Total Available Unused (Slack)\n","Butane 21754.6 8245.4 30000.0 30000.0 0.0\n","LSR 9211.6 25788.4 35000.0 35000.0 0.0\n","Isomerate 0.0 0.0 0.0 0.0 0.0\n","Reformate 19783.9 40216.1 60000.0 60000.0 0.0\n","Reformate LB 0.0 0.0 0.0 0.0 0.0\n","FCC Naphtha 70000.0 0.0 70000.0 70000.0 0.0\n","Alkylate -0.0 40000.0 40000.0 40000.0 0.0\n"]}]},{"cell_type":"markdown","metadata":{"id":"KlxqC3pO5LcX"},"source":["### Results for each Product"]},{"cell_type":"code","metadata":{"id":"05EE855E5LcX","executionInfo":{"status":"ok","timestamp":1631146417334,"user_tz":240,"elapsed":133,"user":{"displayName":"","photoUrl":"","userId":""}},"outputId":"9442cef2-5791-44f2-8ca4-3e37cc53dda0","colab":{"base_uri":"https://localhost:8080/"}},"source":["product_results = pd.DataFrame()\n","for p in P:\n"," product_results.loc[p,'Volume'] = round(sum(m.x[s,p]() for s in S), 1)\n"," product_results.loc[p,'octane'] = round(sum(m.x[s,p]()*streams[s]['octane'] for s in S)\n"," /product_results.loc[p,'Volume'], 1)\n"," product_results.loc[p,'RVP'] = round((sum(m.x[s,p]()*streams[s]['RVP']**1.25 for s in S)\n"," /product_results.loc[p,'Volume'])**0.8, 1)\n"," product_results.loc[p,'benzene'] = round(sum(m.x[s,p]()*streams[s]['benzene'] for s in S)\n"," /product_results.loc[p,'Volume'], 1)\n","print(product_results)"],"execution_count":8,"outputs":[{"output_type":"stream","name":"stdout","text":[" Volume octane RVP benzene\n","Regular 120750.0 87.0 15.0 1.0\n","Premium 114250.0 91.0 10.6 0.8\n"]}]},{"cell_type":"markdown","metadata":{"tags":["exercise"],"id":"vFswmC8Z5LcY"},"source":["## Exercise 1.\n","\n","The marketing team says there is an opportunity to create a mid-grade gasoline product with a road octane number of 89 that would sell for $2.82/gallon, and with all other specifications the same. Could an additional profit be created?\n","\n","Create a new cell (or cells) below to compute a solution to this exercise."]},{"cell_type":"code","metadata":{"id":"UsGCA5Uz5LcY","executionInfo":{"status":"ok","timestamp":1631146455720,"user_tz":240,"elapsed":367,"user":{"displayName":"","photoUrl":"","userId":""}},"outputId":"b99bb7c7-4279-4048-8edd-707aa7916c5e","colab":{"base_uri":"https://localhost:8080/"}},"source":["products = {\n"," 'Regular' : {'price': 2.75, 'octane': 87, 'RVPmin': 0.0, 'RVPmax': 15.0, 'benzene': 1.1},\n"," 'Midgrade' : {'price': 2.82, 'octane': 89, 'RVPmin': 0.0, 'RVPmax': 15.0, 'benzene': 1.1},\n"," 'Premium' : {'price': 2.85, 'octane': 91, 'RVPmin': 0.0, 'RVPmax': 15.0, 'benzene': 1.1},\n","}\n","\n","print(pd.DataFrame.from_dict(products).T)\n","\n","streams = {\n"," 'Butane' : {'RON': 93.0, 'MON': 92.0, 'RVP': 54.0, 'benzene': 0.00, 'cost': 0.85, 'avail': 30000},\n"," 'LSR' : {'RON': 78.0, 'MON': 76.0, 'RVP': 11.2, 'benzene': 0.73, 'cost': 2.05, 'avail': 35000},\n"," 'Isomerate' : {'RON': 83.0, 'MON': 81.1, 'RVP': 13.5, 'benzene': 0.00, 'cost': 2.20, 'avail': 0},\n"," 'Reformate' : {'RON':100.0, 'MON': 88.2, 'RVP': 3.2, 'benzene': 1.85, 'cost': 2.80, 'avail': 60000},\n"," 'Reformate LB' : {'RON': 93.7, 'MON': 84.0, 'RVP': 2.8, 'benzene': 0.12, 'cost': 2.75, 'avail': 0},\n"," 'FCC Naphtha' : {'RON': 92.1, 'MON': 77.1, 'RVP': 1.4, 'benzene': 1.06, 'cost': 2.60, 'avail': 70000},\n"," 'Alkylate' : {'RON': 97.3, 'MON': 95.9, 'RVP': 4.6, 'benzene': 0.00, 'cost': 2.75, 'avail': 40000},\n","}\n","\n","# calculate road octane as (R+M)/2\n","for s in streams.keys():\n"," streams[s]['octane'] = (streams[s]['RON'] + streams[s]['MON'])/2\n"," \n","# display feed information\n","print(pd.DataFrame.from_dict(streams).T)\n","\n","# create model\n","m = pyomo.ConcreteModel()\n","\n","# create decision variables\n","S = streams.keys()\n","P = products.keys()\n","m.x = pyomo.Var(S,P, domain=pyomo.NonNegativeReals)\n"," \n","# objective\n","revenue = sum(sum(m.x[s,p]*products[p]['price'] for s in S) for p in P)\n","cost = sum(sum(m.x[s,p]*streams[s]['cost'] for s in S) for p in P)\n","m.profit = pyomo.Objective(expr = revenue - cost, sense=pyomo.maximize)\n","\n","# constraints\n","m.cons = pyomo.ConstraintList()\n","for s in S:\n"," m.cons.add(sum(m.x[s,p] for p in P) <= streams[s]['avail'])\n","for p in P:\n"," m.cons.add(sum(m.x[s,p]*(streams[s]['octane'] - products[p]['octane']) for s in S) >= 0)\n"," m.cons.add(sum(m.x[s,p]*(streams[s]['RVP']**1.25 - products[p]['RVPmin']**1.25) for s in S) >= 0)\n"," m.cons.add(sum(m.x[s,p]*(streams[s]['RVP']**1.25 - products[p]['RVPmax']**1.25) for s in S) <= 0)\n"," m.cons.add(sum(m.x[s,p]*(streams[s]['benzene'] - products[p]['benzene']) for s in S) <= 0)\n","\n","# solve\n","solver = pyomo.SolverFactory('cbc')\n","solver.solve(m)\n","print(\"Profit = $\", round(m.profit(),2))\n","\n","stream_results = pd.DataFrame()\n","for s in S:\n"," for p in P:\n"," stream_results.loc[s,p] = round(m.x[s,p](), 1)\n"," stream_results.loc[s,'Total'] = round(sum(m.x[s,p]() for p in P), 1)\n"," stream_results.loc[s,'Available'] = streams[s]['avail']\n"," \n","stream_results['Unused (Slack)'] = stream_results['Available'] - stream_results['Total']\n","print(stream_results)\n","\n","product_results = pd.DataFrame()\n","for p in P:\n"," product_results.loc[p,'Volume'] = round(sum(m.x[s,p]() for s in S), 1)\n"," product_results.loc[p,'octane'] = round(sum(m.x[s,p]()*streams[s]['octane'] for s in S)\n"," /product_results.loc[p,'Volume'], 1)\n"," product_results.loc[p,'RVP'] = round((sum(m.x[s,p]()*streams[s]['RVP']**1.25 for s in S)\n"," /product_results.loc[p,'Volume'])**0.8, 1)\n"," product_results.loc[p,'benzene'] = round(sum(m.x[s,p]()*streams[s]['benzene'] for s in S)\n"," /product_results.loc[p,'Volume'], 1)\n","print(product_results)\n","\n","vol = sum(m.x[s,p]() for s in S for p in P)\n","print(\"Total Profit =\", round(m.profit(), 1), \"dollars.\")\n","print(\"Total Volume =\", round(vol, 1), \"gallons.\")\n","print(\"Profit =\", round(100*m.profit()/vol,1), \"cents per gallon.\")"],"execution_count":11,"outputs":[{"output_type":"stream","name":"stdout","text":[" price octane RVPmin RVPmax benzene\n","Regular 2.75 87.0 0.0 15.0 1.1\n","Midgrade 2.82 89.0 0.0 15.0 1.1\n","Premium 2.85 91.0 0.0 15.0 1.1\n"," RON MON RVP benzene cost avail octane\n","Butane 93.0 92.0 54.0 0.00 0.85 30000.0 92.50\n","LSR 78.0 76.0 11.2 0.73 2.05 35000.0 77.00\n","Isomerate 83.0 81.1 13.5 0.00 2.20 0.0 82.05\n","Reformate 100.0 88.2 3.2 1.85 2.80 60000.0 94.10\n","Reformate LB 93.7 84.0 2.8 0.12 2.75 0.0 88.85\n","FCC Naphtha 92.1 77.1 1.4 1.06 2.60 70000.0 84.60\n","Alkylate 97.3 95.9 4.6 0.00 2.75 40000.0 96.60\n","Profit = $ 104995.0\n"," Regular Midgrade Premium Total Available Unused (Slack)\n","Butane 849.5 29150.5 0.0 30000.0 30000.0 0.0\n","LSR 2646.2 32353.8 0.0 35000.0 35000.0 0.0\n","Isomerate 0.0 0.0 0.0 0.0 0.0 0.0\n","Reformate 2820.7 57179.3 0.0 60000.0 60000.0 0.0\n","Reformate LB 0.0 0.0 0.0 0.0 0.0 0.0\n","FCC Naphtha 0.0 70000.0 0.0 70000.0 70000.0 0.0\n","Alkylate 183.6 39816.4 0.0 40000.0 40000.0 0.0\n"," Volume octane RVP benzene\n","Regular 6500.0 87.0 15.0 1.1\n","Midgrade 228500.0 89.0 12.8 0.9\n","Premium 0.0 NaN NaN NaN\n","Total Profit = 104995.0 dollars.\n","Total Volume = 235000.0 gallons.\n","Profit = 44.7 cents per gallon.\n"]},{"output_type":"stream","name":"stderr","text":["/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:68: RuntimeWarning: invalid value encountered in double_scalars\n","/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:70: RuntimeWarning: invalid value encountered in double_scalars\n","/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:72: RuntimeWarning: invalid value encountered in double_scalars\n"]}]},{"cell_type":"markdown","metadata":{"tags":["exercise"],"id":"srdVyxza5LcZ"},"source":["## Exercise 2.\n","\n","New environmental regulations have reduced the allowable benzene levels from 1.1 vol% to 0.62 vol%, and the maximum Reid vapor pressure from 15.0 to 9.0.\n","\n","Compared to the base case (i.e., without the midgrade product), how does this change profitability? "]},{"cell_type":"code","metadata":{"id":"K5aqngwn5LcZ","executionInfo":{"status":"ok","timestamp":1631146489779,"user_tz":240,"elapsed":249,"user":{"displayName":"","photoUrl":"","userId":""}},"outputId":"657b43be-eaaf-4f53-b236-88f4538c40ed","colab":{"base_uri":"https://localhost:8080/"}},"source":["products = {\n"," 'Regular' : {'price': 2.75, 'octane': 87, 'RVPmin': 0.0, 'RVPmax': 9.0, 'benzene': 0.62},\n"," 'Premium' : {'price': 2.85, 'octane': 91, 'RVPmin': 0.0, 'RVPmax': 9.0, 'benzene': 0.62},\n","}\n","\n","print(pd.DataFrame.from_dict(products).T)\n","\n","streams = {\n"," 'Butane' : {'RON': 93.0, 'MON': 92.0, 'RVP': 54.0, 'benzene': 0.00, 'cost': 0.85, 'avail': 30000},\n"," 'LSR' : {'RON': 78.0, 'MON': 76.0, 'RVP': 11.2, 'benzene': 0.73, 'cost': 2.05, 'avail': 35000},\n"," 'Isomerate' : {'RON': 83.0, 'MON': 81.1, 'RVP': 13.5, 'benzene': 0.00, 'cost': 2.20, 'avail': 0},\n"," 'Reformate' : {'RON':100.0, 'MON': 88.2, 'RVP': 3.2, 'benzene': 1.85, 'cost': 2.80, 'avail': 60000},\n"," 'Reformate LB' : {'RON': 93.7, 'MON': 84.0, 'RVP': 2.8, 'benzene': 0.12, 'cost': 2.75, 'avail': 0},\n"," 'FCC Naphtha' : {'RON': 92.1, 'MON': 77.1, 'RVP': 1.4, 'benzene': 1.06, 'cost': 2.60, 'avail': 70000},\n"," 'Alkylate' : {'RON': 97.3, 'MON': 95.9, 'RVP': 4.6, 'benzene': 0.00, 'cost': 2.75, 'avail': 40000},\n","}\n","\n","# calculate road octane as (R+M)/2\n","for s in streams.keys():\n"," streams[s]['octane'] = (streams[s]['RON'] + streams[s]['MON'])/2\n"," \n","# display feed information\n","print(pd.DataFrame.from_dict(streams).T)\n","\n","# create model\n","m = pyomo.ConcreteModel()\n","\n","# create decision variables\n","S = streams.keys()\n","P = products.keys()\n","m.x = pyomo.Var(S,P, domain=pyomo.NonNegativeReals)\n"," \n","# objective\n","revenue = sum(sum(m.x[s,p]*products[p]['price'] for s in S) for p in P)\n","cost = sum(sum(m.x[s,p]*streams[s]['cost'] for s in S) for p in P)\n","m.profit = pyomo.Objective(expr = revenue - cost, sense=pyomo.maximize)\n","\n","# constraints\n","m.cons = pyomo.ConstraintList()\n","for s in S:\n"," m.cons.add(sum(m.x[s,p] for p in P) <= streams[s]['avail'])\n","for p in P:\n"," m.cons.add(sum(m.x[s,p]*(streams[s]['octane'] - products[p]['octane']) for s in S) >= 0)\n"," m.cons.add(sum(m.x[s,p]*(streams[s]['RVP']**1.25 - products[p]['RVPmin']**1.25) for s in S) >= 0)\n"," m.cons.add(sum(m.x[s,p]*(streams[s]['RVP']**1.25 - products[p]['RVPmax']**1.25) for s in S) <= 0)\n"," m.cons.add(sum(m.x[s,p]*(streams[s]['benzene'] - products[p]['benzene']) for s in S) <= 0)\n","\n","# solve\n","solver = pyomo.SolverFactory('cbc')\n","solver.solve(m)\n","print(\"Profit = $\", round(m.profit(),2))\n","\n","stream_results = pd.DataFrame()\n","for s in S:\n"," for p in P:\n"," stream_results.loc[s,p] = round(m.x[s,p](), 1)\n"," stream_results.loc[s,'Total'] = round(sum(m.x[s,p]() for p in P), 1)\n"," stream_results.loc[s,'Available'] = streams[s]['avail']\n"," \n","stream_results['Unused (Slack)'] = stream_results['Available'] - stream_results['Total']\n","print(stream_results)\n","\n","product_results = pd.DataFrame()\n","for p in P:\n"," product_results.loc[p,'Volume'] = round(sum(m.x[s,p]() for s in S), 1)\n"," product_results.loc[p,'octane'] = round(sum(m.x[s,p]()*streams[s]['octane'] for s in S)\n"," /product_results.loc[p,'Volume'], 1)\n"," product_results.loc[p,'RVP'] = round((sum(m.x[s,p]()*streams[s]['RVP']**1.25 for s in S)\n"," /product_results.loc[p,'Volume'])**0.8, 1)\n"," product_results.loc[p,'benzene'] = round(sum(m.x[s,p]()*streams[s]['benzene'] for s in S)\n"," /product_results.loc[p,'Volume'], 1)\n","print(product_results)\n","\n","vol = sum(m.x[s,p]() for s in S for p in P)\n","print(\"Total Profit =\", round(m.profit(), 1), \"dollars.\")\n","print(\"Total Volume =\", round(vol, 1), \"gallons.\")\n","print(\"Profit =\", round(100*m.profit()/vol,1), \"cents per gallon.\")"],"execution_count":14,"outputs":[{"output_type":"stream","name":"stdout","text":[" price octane RVPmin RVPmax benzene\n","Regular 2.75 87.0 0.0 9.0 0.62\n","Premium 2.85 91.0 0.0 9.0 0.62\n"," RON MON RVP benzene cost avail octane\n","Butane 93.0 92.0 54.0 0.00 0.85 30000.0 92.50\n","LSR 78.0 76.0 11.2 0.73 2.05 35000.0 77.00\n","Isomerate 83.0 81.1 13.5 0.00 2.20 0.0 82.05\n","Reformate 100.0 88.2 3.2 1.85 2.80 60000.0 94.10\n","Reformate LB 93.7 84.0 2.8 0.12 2.75 0.0 88.85\n","FCC Naphtha 92.1 77.1 1.4 1.06 2.60 70000.0 84.60\n","Alkylate 97.3 95.9 4.6 0.00 2.75 40000.0 96.60\n","Profit = $ 44493.62\n"," Regular Premium Total Available Unused (Slack)\n","Butane 8187.5 0.0 8187.5 30000.0 21812.5\n","LSR 28305.3 0.0 28305.3 35000.0 6694.7\n","Isomerate 0.0 0.0 0.0 0.0 0.0\n","Reformate 0.0 0.0 0.0 60000.0 60000.0\n","Reformate LB 0.0 0.0 0.0 0.0 0.0\n","FCC Naphtha 60824.3 0.0 60824.3 70000.0 9175.7\n","Alkylate 40000.0 0.0 40000.0 40000.0 0.0\n"," Volume octane RVP benzene\n","Regular 137317.1 87.0 9.0 0.6\n","Premium 0.0 NaN NaN NaN\n","Total Profit = 44493.6 dollars.\n","Total Volume = 137317.1 gallons.\n","Profit = 32.4 cents per gallon.\n"]},{"output_type":"stream","name":"stderr","text":["/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:67: RuntimeWarning: invalid value encountered in double_scalars\n","/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:69: RuntimeWarning: invalid value encountered in double_scalars\n","/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:71: RuntimeWarning: invalid value encountered in double_scalars\n"]}]},{"cell_type":"markdown","metadata":{"tags":["exercise"],"id":"cyX104G75Lca"},"source":["## Exercise 3.\n","\n","Given the new product specifications in Exercise 2, let's consider using different refinery streams. In place of Reformate, the refinery could produce Reformate LB. (That is, one or the other of the two streams could be 60000 gallons per day, but not both). Same for LSR and Reformate. How should the refinery be operated to maximize profitability?"]},{"cell_type":"code","metadata":{"id":"YKqN0OT95Lca","executionInfo":{"status":"ok","timestamp":1631146510037,"user_tz":240,"elapsed":467,"user":{"displayName":"","photoUrl":"","userId":""}},"outputId":"2c9c1c3e-059c-4db1-8264-f274139036f5","colab":{"base_uri":"https://localhost:8080/"}},"source":["products = {\n"," 'Regular' : {'price': 2.75, 'octane': 87, 'RVPmin': 0.0, 'RVPmax': 9.0, 'benzene': 0.62},\n"," 'Premium' : {'price': 2.85, 'octane': 91, 'RVPmin': 0.0, 'RVPmax': 9.0, 'benzene': 0.62},\n","}\n","\n","print(pd.DataFrame.from_dict(products).T)\n","\n","streams = {\n"," 'Butane' : {'RON': 93.0, 'MON': 92.0, 'RVP': 54.0, 'benzene': 0.00, 'cost': 0.85, 'avail': 30000},\n"," 'LSR' : {'RON': 78.0, 'MON': 76.0, 'RVP': 11.2, 'benzene': 0.73, 'cost': 2.05, 'avail': 35000},\n"," 'Isomerate' : {'RON': 83.0, 'MON': 81.1, 'RVP': 13.5, 'benzene': 0.00, 'cost': 2.20, 'avail': 0},\n"," 'Reformate' : {'RON':100.0, 'MON': 88.2, 'RVP': 3.2, 'benzene': 1.85, 'cost': 2.80, 'avail': 0},\n"," 'Reformate LB' : {'RON': 93.7, 'MON': 84.0, 'RVP': 2.8, 'benzene': 0.12, 'cost': 2.75, 'avail': 60000},\n"," 'FCC Naphtha' : {'RON': 92.1, 'MON': 77.1, 'RVP': 1.4, 'benzene': 1.06, 'cost': 2.60, 'avail': 70000},\n"," 'Alkylate' : {'RON': 97.3, 'MON': 95.9, 'RVP': 4.6, 'benzene': 0.00, 'cost': 2.75, 'avail': 40000},\n","}\n","\n","# calculate road octane as (R+M)/2\n","for s in streams.keys():\n"," streams[s]['octane'] = (streams[s]['RON'] + streams[s]['MON'])/2\n"," \n","# display feed information\n","print(pd.DataFrame.from_dict(streams).T)\n","\n","# create model\n","m = pyomo.ConcreteModel()\n","\n","# create decision variables\n","S = streams.keys()\n","P = products.keys()\n","m.x = pyomo.Var(S,P, domain=pyomo.NonNegativeReals)\n"," \n","# objective\n","revenue = sum(sum(m.x[s,p]*products[p]['price'] for s in S) for p in P)\n","cost = sum(sum(m.x[s,p]*streams[s]['cost'] for s in S) for p in P)\n","m.profit = pyomo.Objective(expr = revenue - cost, sense=pyomo.maximize)\n","\n","# constraints\n","m.cons = pyomo.ConstraintList()\n","for s in S:\n"," m.cons.add(sum(m.x[s,p] for p in P) <= streams[s]['avail'])\n","for p in P:\n"," m.cons.add(sum(m.x[s,p]*(streams[s]['octane'] - products[p]['octane']) for s in S) >= 0)\n"," m.cons.add(sum(m.x[s,p]*(streams[s]['RVP']**1.25 - products[p]['RVPmin']**1.25) for s in S) >= 0)\n"," m.cons.add(sum(m.x[s,p]*(streams[s]['RVP']**1.25 - products[p]['RVPmax']**1.25) for s in S) <= 0)\n"," m.cons.add(sum(m.x[s,p]*(streams[s]['benzene'] - products[p]['benzene']) for s in S) <= 0)\n"," \n","# solve\n","solver = pyomo.SolverFactory('cbc')\n","solver.solve(m)\n","print(\"Profit = $\", round(m.profit(),2))\n","\n","stream_results = pd.DataFrame()\n","for s in S:\n"," for p in P:\n"," stream_results.loc[s,p] = round(m.x[s,p](), 1)\n"," stream_results.loc[s,'Total'] = round(sum(m.x[s,p]() for p in P), 1)\n"," stream_results.loc[s,'Available'] = streams[s]['avail']\n"," \n","stream_results['Unused (Slack)'] = stream_results['Available'] - stream_results['Total']\n","print(stream_results)\n","\n","product_results = pd.DataFrame()\n","for p in P:\n"," product_results.loc[p,'Volume'] = round(sum(m.x[s,p]() for s in S), 1)\n"," product_results.loc[p,'octane'] = round(sum(m.x[s,p]()*streams[s]['octane'] for s in S)\n"," /product_results.loc[p,'Volume'], 1)\n"," product_results.loc[p,'RVP'] = round((sum(m.x[s,p]()*streams[s]['RVP']**1.25 for s in S)\n"," /product_results.loc[p,'Volume'])**0.8, 1)\n"," product_results.loc[p,'benzene'] = round(sum(m.x[s,p]()*streams[s]['benzene'] for s in S)\n"," /product_results.loc[p,'Volume'], 1)\n","print(product_results)\n","\n","vol = sum(m.x[s,p]() for s in S for p in P)\n","print(\"Total Profit =\", round(m.profit(), 1), \"dollars.\")\n","print(\"Total Volume =\", round(vol, 1), \"gallons.\")\n","print(\"Profit =\", round(100*m.profit()/vol,1), \"cents per gallon.\")"],"execution_count":15,"outputs":[{"output_type":"stream","name":"stdout","text":[" price octane RVPmin RVPmax benzene\n","Regular 2.75 87.0 0.0 9.0 0.62\n","Premium 2.85 91.0 0.0 9.0 0.62\n"," RON MON RVP benzene cost avail octane\n","Butane 93.0 92.0 54.0 0.00 0.85 30000.0 92.50\n","LSR 78.0 76.0 11.2 0.73 2.05 35000.0 77.00\n","Isomerate 83.0 81.1 13.5 0.00 2.20 0.0 82.05\n","Reformate 100.0 88.2 3.2 1.85 2.80 0.0 94.10\n","Reformate LB 93.7 84.0 2.8 0.12 2.75 60000.0 88.85\n","FCC Naphtha 92.1 77.1 1.4 1.06 2.60 70000.0 84.60\n","Alkylate 97.3 95.9 4.6 0.00 2.75 40000.0 96.60\n","Profit = $ 63791.16\n"," Regular Premium Total Available Unused (Slack)\n","Butane 13906.5 506.3 14412.8 30000.0 15587.2\n","LSR 31086.6 3913.4 35000.0 35000.0 0.0\n","Isomerate 0.0 0.0 0.0 0.0 0.0\n","Reformate 0.0 0.0 0.0 0.0 0.0\n","Reformate LB 60000.0 0.0 60000.0 60000.0 0.0\n","FCC Naphtha 70000.0 0.0 70000.0 70000.0 0.0\n","Alkylate 30352.1 9647.9 40000.0 40000.0 0.0\n"," Volume octane RVP benzene\n","Regular 205345.2 87.0 9.0 0.5\n","Premium 14067.7 91.0 9.0 0.2\n","Total Profit = 63791.2 dollars.\n","Total Volume = 219412.8 gallons.\n","Profit = 29.1 cents per gallon.\n"]}]},{"cell_type":"code","metadata":{"id":"4-SYW2Fb5Lca"},"source":[""],"execution_count":null,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"CFnoHrBF5Lcb"},"source":["\n","< [Design of a Cold Weather Fuel](http://nbviewer.jupyter.org/github/jckantor/CBE30338/blob/master/notebooks/06.07-Design-of-a-Cold-Weather-Fuel.ipynb) | [Contents](toc.ipynb) | [Pyomo Examples](http://nbviewer.jupyter.org/github/jckantor/CBE30338/blob/master/notebooks/06.99-Pyomo-Examples.ipynb) >

\"Open

\"Download\""]}]}