Maximizing the profit of an oil company¶

This tutorial includes everything you need to set up the decision optimization engines and build mathematical programming models.

When you finish this tutorial, you’ll have a foundational knowledge of Prescriptive Analytics.

This notebook is part of Prescriptive Analytics for Python

It requires either an installation of CPLEX Optimizers or it can be run on IBM Watson Studio Cloud (Sign up for a free IBM Cloud account and you can start using Watson Studio Cloud right away).

• An oil company manufactures different types of gasoline and diesel. Each type of gasoline is produced by blending different types of crude oils that must be purchased. The company must decide how much crude oil to buy in order to maximize its profit while respecting processing capacities and quality levels as well as satisfying customer demand.
• Blending problems are a typical industry application of Linear Programming (LP). LP represents real life problems mathematically using an objective function to represent the goal that is to be minimized or maximized, together with a set of linear constraints which define the conditions to be satisfied and the limitations of the real life problem. The function and constraints are expressed in terms of decision variables and the solution, obtained from optimization engines such as IBM� ILOG� CPLEX�, provides the best values for these variables so that the objective function is optimized.
• The oil-blending problem consists of calculating different blends of gasoline according to specific quality criteria.
• Three types of gasoline are manufactured: super, regular, and diesel.
• Each type of gasoline is produced by blending three types of crude oil: crude1, crude2, and crude3.
• The gasoline must satisfy some quality criteria with respect to their lead content and their octane ratings, thus constraining the possible blendings.
• The company must also satisfy its customer demand, which is 3,000 barrels a day of super, 2,000 of regular, and 1,000 of diesel.
• The company can purchase 5,000 barrels of each type of crude oil per day and can process at most 14,000 barrels a day.
• In addition, the company has the option of advertising a gasoline, in which case the demand for this type of gasoline increases by ten barrels for every dollar spent.
• Finally, it costs four dollars to transform a barrel of oil into a barrel of gasoline.

How decision optimization can help¶

• Prescriptive analytics technology recommends actions based on desired outcomes, taking into account specific scenarios, resources, and knowledge of past and current events. This insight can help your organization make better decisions and have greater control of business outcomes.

• Prescriptive analytics is the next step on the path to insight-based actions. It creates value through synergy with predictive analytics, which analyzes data to predict future outcomes.

• Prescriptive analytics takes that insight to the next level by suggesting the optimal way to handle that future situation. Organizations that can act fast in dynamic conditions and make superior decisions in uncertain environments gain a strong competitive advantage.

• For example:

• Automate complex decisions and trade-offs to better manage limited resources.
• Take advantage of a future opportunity or mitigate a future risk.
• Proactively update recommendations based on changing events.
• Meet operational goals, increase customer loyalty, prevent threats and fraud, and optimize business processes.

Use decision optimization¶

Step 1: Import the library¶

Run the following code to import the Decision Optimization CPLEX Modeling library. The DOcplex library contains the two modeling packages, Mathematical Programming (docplex.mp) and Constraint Programming (docplex.cp).

If CPLEX is not installed, install CPLEX Community edition.

Step 2: Model the data¶

• For each type of crude oil, there are capacities of what can be bought, the buying price, the octane level, and the lead level.
• For each type of gasoline or diesel, there is customer demand, selling prices, and octane and lead levels.
• There is a maximum level of production imposed by the factory’s limit as well as a fixed production cost.
• There are inventory costs for each type of final product and blending proportions. All of these have actual values in the model.
• The maginal production cost and maximum production are assumed to be identical for all oil types.

Input data comes as NumPy arrays with two dimensions. NumPy is the fundamental package for scientific computing with Python.

The first dimension of the NumPy array is the number of gasoline types; and for each gasoline type, we have a NumPy array containing capacity, price, octane and lead level, in that order.

Number of gasoline types = 3
Number of crude types = 3


Step 3: Prepare the data¶

Pandas is another Python library that we use to store data. pandas contains data structures and data analysis tools for the Python programming language.

Use basic HTML and a stylesheet to format the data.

Let’s display the data we just prepared.

Gas data:

0 super 3000 70 10 1
1 regular 2000 60 8 2
2 diesel 1000 50 6 1
Oil data:

0 crude1 5000 45 12 0.5
1 crude2 5000 35 6 2.0
2 crude3 5000 25 8 3.0

Step 4: Set up the prescriptive model¶

Create the DOcplex model¶

A model is needed to store all the variables and constraints needed to formulate the business problem and submit the problem to the solve service.

Define the decision variables¶

For each combination of oil and gas, we have to decide the quantity of oil to use to produce a gasoline. A decision variable will be needed to represent that amount. A matrix of continuous variables, indexed by the set of oils and the set of gasolines needs to be created.

We also have to decide how much should be spent in advertising for each time of gasoline. To do so, we will create a list of continuous variables, indexed by the gasolines.

The business constraints are the following:

• The demand for each gasoline type must be satisfied. The total demand includes the initial demand as stored in the data,plus a variable demand caused by the advertising. This increase is assumed to be proportional to the advertising cost.
• The capacity constraint on each oil type must also be satisfied.
• For each gasoline type, the octane level must be above a minimum level, and the lead level must be below a maximum level.
Demand¶
• For each gasoline type, the total quantity produced must equal the raw demand plus the demand increase created by the advertising.
Model: oil_blending
- number of variables: 12
- binary=0, integer=0, continuous=12
- number of constraints: 3
- linear=3
- parameters: defaults
- problem type is: LP

Maximum capacity¶
• For each type of oil, the total quantity used in all types of gasolines must not exceed the maximum capacity for this oil.
Model: oil_blending
- number of variables: 12
- binary=0, integer=0, continuous=12
- number of constraints: 6
- linear=6
- parameters: defaults
- problem type is: LP

• For each gasoline type, the octane level must be above a minimum level, and the lead level must be below a maximum level.
Model: oil_blending
- number of variables: 12
- binary=0, integer=0, continuous=12
- number of constraints: 12
- linear=12
- parameters: defaults
- problem type is: LP

Maximum total production¶
• The total production must not exceed the maximum (here 14000).
Model: oil_blending
- number of variables: 12
- binary=0, integer=0, continuous=12
- number of constraints: 13
- linear=13
- parameters: defaults
- problem type is: LP


Express the objective¶

• The objective or goal is to maximize profit, which is made from sales of the final products minus total costs. The costs consist of the purchase cost of the crude oils, production costs, and inventory costs.
• The model maximizes the net revenue, that is revenue minus oil cost and production cost, to which we subtract the total advertising cost.
• To define business objective, let’s define a few KPIs :
• Total Oil cost
• Total production cost
• Total revenue

Solve with Decision Optimization¶

If you’re using a Community Edition of CPLEX runtimes, depending on the size of the problem, the solve stage may fail and will need a paying subscription or product installation.

We display the objective and KPI values after the solve by calling the method report() on the model.

* model oil_blending solved with objective = 287750.000
*  KPI: Total advertising cost = 750.000
*  KPI: Total Oil cost         = 487500.000
*  KPI: Total production cost  = 54000.000
*  KPI: Total revenue          = 830000.000


Step 5: Investigate the solution and then run an example analysis¶

Displaying the solution¶

First, get the KPIs values and store them in a pandas DataFrame.

Let’s display some KPIs in pie charts using the Python package matplotlib.

Production¶

We see that the most produced gasoline type is by far regular.

Now, let’s plot the breakdown of oil blend quantities per gasoline type. We are using a multiple bar chart diagram, displaying all blend values for each couple of oil and gasoline type.

Notice the missing bar for (crude2, diesel) which is expected since blend[crude2, diesel] is zero in the solution.

We can check the solution value of blends for crude2 and diesel, remembering that crude2 has offset 1 and diesel has offset 2. Note how the decision variable is automatically converted to a float here. This would raise an exception if called before submitting a solve, as no solution value would be present.

* value of blend[crude2, diesel] is 0


Summary¶

You learned how to set up and use IBM Decision Optimization CPLEX Modeling for Python to formulate a Mathematical Programming model and solve it with CPLEX.