{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Gasoline Blending\n",
"\n",
"The task is to determine the most profitable blend of gasoline products from given set of refinery streams.\n",
"\n",
"\n",
"\n",
"\n",
"\n",
""
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# install Pyomo and solvers for Google Colab\n",
"import sys\n",
"on_colab = \"google.colab\" in sys.modules\n",
"if on_colab:\n",
" !wget \"https://raw.githubusercontent.com/IDAES/idaes-pse/main/scripts/colab_helper.py\"\n",
" import colab_helper\n",
" colab_helper.install_idaes()\n",
" colab_helper.install_ipopt()"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import pyomo.environ as pyo\n",
"\n",
"# Set default font sizes for plots\n",
"import matplotlib.pyplot as plt\n",
"plt.rcParams['font.size'] = 18"
]
},
{
"cell_type": "markdown",
"metadata": {},
"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",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
"
\n",
"
\n",
"
price
\n",
"
octane
\n",
"
RVPmin
\n",
"
RVPmax
\n",
"
benzene
\n",
"
\n",
" \n",
" \n",
"
\n",
"
Regular
\n",
"
2.75
\n",
"
87.0
\n",
"
0.0
\n",
"
15.0
\n",
"
1.1
\n",
"
\n",
"
\n",
"
Premium
\n",
"
2.85
\n",
"
91.0
\n",
"
0.0
\n",
"
15.0
\n",
"
1.1
\n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" 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"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"products = pd.DataFrame({\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",
"}).T\n",
"\n",
"display(products)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"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",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
"
\n",
"
\n",
"
RON
\n",
"
MON
\n",
"
RVP
\n",
"
benzene
\n",
"
cost
\n",
"
avail
\n",
"
octane
\n",
"
\n",
" \n",
" \n",
"
\n",
"
Butane
\n",
"
93.0
\n",
"
92.0
\n",
"
54.0
\n",
"
0.00
\n",
"
0.85
\n",
"
30000.0
\n",
"
92.50
\n",
"
\n",
"
\n",
"
LSR
\n",
"
78.0
\n",
"
76.0
\n",
"
11.2
\n",
"
0.73
\n",
"
2.05
\n",
"
35000.0
\n",
"
77.00
\n",
"
\n",
"
\n",
"
Isomerate
\n",
"
83.0
\n",
"
81.1
\n",
"
13.5
\n",
"
0.00
\n",
"
2.20
\n",
"
0.0
\n",
"
82.05
\n",
"
\n",
"
\n",
"
Reformate
\n",
"
100.0
\n",
"
88.2
\n",
"
3.2
\n",
"
1.85
\n",
"
2.80
\n",
"
60000.0
\n",
"
94.10
\n",
"
\n",
"
\n",
"
Reformate LB
\n",
"
93.7
\n",
"
84.0
\n",
"
2.8
\n",
"
0.12
\n",
"
2.75
\n",
"
0.0
\n",
"
88.85
\n",
"
\n",
"
\n",
"
FCC Naphtha
\n",
"
92.1
\n",
"
77.1
\n",
"
1.4
\n",
"
1.06
\n",
"
2.60
\n",
"
70000.0
\n",
"
84.60
\n",
"
\n",
"
\n",
"
Alkylate
\n",
"
97.3
\n",
"
95.9
\n",
"
4.6
\n",
"
0.00
\n",
"
2.75
\n",
"
40000.0
\n",
"
96.60
\n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" 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"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"streams = pd.DataFrame({\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",
"}).T\n",
"\n",
"streams['octane'] = (streams['RON'] + streams['MON'])/2\n",
"\n",
"display(streams)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Questions we want to Answer\n",
"\n",
"1. What is the maximum profit possible using the current product specifications and available streams?\n",
"\n",
"2. What are the marginal values of each blending stream? That is, how much would you be willing to pay for each additional gallon of the blending streams?\n",
"\n",
"3. A 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. Would an additional profit be created? What at what price point does the mid-grade product enhance profits?\n",
"\n",
"4. 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. What is the impact on profits?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"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",
"### Objective\n",
"\n",
"The objective is maximize profit, which is the difference between product revenue and stream costs. \n",
"\n",
"\\begin{align}\n",
"\\text{profit} & = \\max_{x_{s,p}}\\left( \\sum_{p\\in P} \\text{Price}_p \\sum_{s\\in S} x_{s,p}\n",
"- \\sum_{s\\in S} \\text{Cost}_s \\sum_{p\\in P} x_{s,p}\\right) \\\\\n",
"& = \\max_{x_{s,p}}\\left(\\sum_{p\\in P}\\sum_{s\\in S}x_{s,p}(\\text{Price}_p - \\text{Cost}_s)\\right)\n",
"\\end{align}\n",
"\n",
"### Raw Materials\n",
"\n",
"The first constraints in any blending problem are normally the limits on available raw materials.\n",
"\n",
"The blending constraint for octane can be written as \n",
"\n",
"\\begin{align}\n",
"\\frac{\\sum_{s \\in S} x_{s,p} \\text{Octane}_s}{\\sum_{s \\in S} x_{s,p}} & \\geq \\text{Octane}_p & \\forall p \\in P\n",
"\\end{align}\n",
"\n",
"where $\\text{Octane}_s$ refers to the octane rating of stream $s$, whereas $\\text{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(\\text{Octane}_s - \\text{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(\\text{Benzene}_s - \\text{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(\\text{RVP}_s^{1.25} - \\text{RVP}_{min,p}^{1.25}\\right) & \\geq 0 & \\forall p \\in P \\\\\n",
"\\sum_{s \\in S} x_{s,p}\\left(\\text{RVP}_s^{1.25} - \\text{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": "markdown",
"metadata": {},
"source": [
"## Pyomo Implementation"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Total Volume = 235000.0 gallons.\n",
"Total Profit = 100425.0 dollars.\n",
"Profit = 42.7 cents per gallon.\n"
]
},
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
"
\n",
"
\n",
"
Regular
\n",
"
Premium
\n",
"
\n",
" \n",
" \n",
"
\n",
"
Butane
\n",
"
2.175457e+04
\n",
"
8245.428
\n",
"
\n",
"
\n",
"
LSR
\n",
"
9.211560e+03
\n",
"
25788.439
\n",
"
\n",
"
\n",
"
Isomerate
\n",
"
0.000000e+00
\n",
"
0.000
\n",
"
\n",
"
\n",
"
Reformate
\n",
"
1.978387e+04
\n",
"
40216.132
\n",
"
\n",
"
\n",
"
Reformate LB
\n",
"
0.000000e+00
\n",
"
0.000
\n",
"
\n",
"
\n",
"
FCC Naphtha
\n",
"
7.000000e+04
\n",
"
0.000
\n",
"
\n",
"
\n",
"
Alkylate
\n",
"
-7.275958e-12
\n",
"
40000.000
\n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Regular Premium\n",
"Butane 2.175457e+04 8245.428\n",
"LSR 9.211560e+03 25788.439\n",
"Isomerate 0.000000e+00 0.000\n",
"Reformate 1.978387e+04 40216.132\n",
"Reformate LB 0.000000e+00 0.000\n",
"FCC Naphtha 7.000000e+04 0.000\n",
"Alkylate -7.275958e-12 40000.000"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import pyomo.environ as pyo\n",
"\n",
"def gas_blending(products, streams):\n",
" ''' Gasoline blending optimization problem\n",
"\n",
" Arguments:\n",
" products: DataFrame with columns ['price', 'octane', 'RVPmin', 'RVPmax', 'benzene']\n",
" streams: DataFrame with columns ['RON', 'MON', 'RVP', 'benzene', 'cost', 'avail']\n",
" max_benzene: maximum benzene content in the final product (float)\n",
" max_RVP: maximum Reid vapor pressure in the final product (float)\n",
" \n",
" Returns:\n",
" m: Pyomo model (solved)\n",
"\n",
" '''\n",
"\n",
" m = pyo.ConcreteModel(\"Gasoline Blending\")\n",
" \n",
" m.PRODUCTS = pyo.Set(initialize=products.index)\n",
" m.STREAMS = pyo.Set(initialize=streams.index)\n",
" \n",
" m.x = pyo.Var(m.STREAMS, m.PRODUCTS, domain=pyo.NonNegativeReals)\n",
" \n",
" @m.Objective(sense=pyo.maximize)\n",
" def profit(m):\n",
" return sum(sum(m.x[s, p]*(products.loc[p, 'price'] - streams.loc[s, 'cost']) for s in m.STREAMS) for p in m.PRODUCTS)\n",
" \n",
" @m.Constraint(m.STREAMS)\n",
" def raw_material_available(m, s):\n",
" return sum(m.x[s, p] for p in m.PRODUCTS) <= streams.loc[s, 'avail']\n",
" \n",
" @m.Constraint(m.PRODUCTS)\n",
" def octane(m, p):\n",
" return sum(m.x[s, p]*(streams.loc[s, 'octane'] - products.loc[p, 'octane']) for s in m.STREAMS) >= 0\n",
" \n",
" @m.Constraint(m.PRODUCTS)\n",
" def benzene(m, p):\n",
" return sum(m.x[s, p]*(streams.loc[s, 'benzene'] - products.loc[p, 'benzene']) for s in m.STREAMS) <= 0\n",
" \n",
" @m.Constraint(m.PRODUCTS)\n",
" def min_reid_vapor_pressure(m, p):\n",
" return sum(m.x[s, p]*(streams.loc[s, 'RVP']**1.25 - products.loc[p, 'RVPmin']**1.25) for s in m.STREAMS) >= 0\n",
" \n",
" @m.Constraint(m.PRODUCTS)\n",
" def max_reid_vapor_pressure(m, p):\n",
" return sum(m.x[s, p]*(streams.loc[s, 'RVP']**1.25 - products.loc[p, 'RVPmax']**1.25) for s in m.STREAMS) <= 0\n",
"\n",
" solver = pyo.SolverFactory('cbc')\n",
" solver.solve(m)\n",
" \n",
" # display results\n",
" vol = sum(m.x[s,p]() for s in m.STREAMS for p in m.PRODUCTS)\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.\")\n",
"\n",
" return m\n",
"\n",
"m = gas_blending(products, streams)\n",
"\n",
" \n",
"soln = pd.DataFrame({s: {p: m.x[s,p]() for p in m.PRODUCTS} for s in m.STREAMS}).T\n",
"display(soln)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Display Results"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Results for each Stream"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
"
],
"text/plain": [
" 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"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"/var/folders/3w/vr4xmyqs451dg23xk88pqcg00000gq/T/ipykernel_97102/2558176032.py:6: RuntimeWarning: invalid value encountered in scalar divide\n",
" product_results.loc[p,'octane'] = round(sum(m.x[s,p]()*streams.loc[s,'octane'] for s in m.STREAMS)\n",
"/var/folders/3w/vr4xmyqs451dg23xk88pqcg00000gq/T/ipykernel_97102/2558176032.py:8: RuntimeWarning: invalid value encountered in scalar divide\n",
" product_results.loc[p,'RVP'] = round((sum(m.x[s,p]()*streams.loc[s,'RVP']**1.25 for s in m.STREAMS)\n",
"/var/folders/3w/vr4xmyqs451dg23xk88pqcg00000gq/T/ipykernel_97102/2558176032.py:10: RuntimeWarning: invalid value encountered in scalar divide\n",
" product_results.loc[p,'benzene'] = round(sum(m.x[s,p]()*streams.loc[s,'benzene'] for s in m.STREAMS)\n"
]
},
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
"
\n",
"
\n",
"
Volume
\n",
"
octane
\n",
"
RVP
\n",
"
benzene
\n",
"
\n",
" \n",
" \n",
"
\n",
"
Regular
\n",
"
137317.1
\n",
"
87.0
\n",
"
9.0
\n",
"
0.6
\n",
"
\n",
"
\n",
"
Premium
\n",
"
0.0
\n",
"
NaN
\n",
"
NaN
\n",
"
NaN
\n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Volume octane RVP benzene\n",
"Regular 137317.1 87.0 9.0 0.6\n",
"Premium 0.0 NaN NaN NaN"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"products2 = pd.DataFrame({\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",
"}).T\n",
"\n",
"display(products2)\n",
"\n",
"m2 = gas_blending(products2, streams)\n",
"\n",
"sr2 = get_stream_results(m2, streams)\n",
"display(sr2)\n",
"\n",
"pr2 = get_product_results(m2, streams, products2)\n",
"display(pr2)"
]
},
{
"cell_type": "markdown",
"metadata": {
"tags": [
"exercise"
]
},
"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 Isomerate. \n",
"\n",
"How should the refinery be operated to maximize profitability? Why is this the best solution? What intutition can you develop from the results?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Scenario A: Reformate and LSR (base)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"