A Linear Programming Approach
Today, we’ll dive into a practical example of petroleum refinery optimization using Python. This is a classic problem in operations research where we need to determine the optimal production mix to maximize profits while respecting various constraints.
Problem Statement
Let’s consider a simplified refinery that processes crude oil into three main products:
- Gasoline (high-octane fuel)
- Diesel (middle distillate)
- Fuel Oil (heavy residual product)
The refinery has two processing units:
- Crude Distillation Unit (CDU): Primary separation of crude oil
- Catalytic Cracking Unit (CCU): Converts heavy fractions to lighter products
Mathematical Formulation
Our objective is to maximize daily profit:
$$\text{Maximize } Z = 80x_1 + 60x_2 + 40x_3$$
Where:
- $x_1$ = barrels of gasoline produced per day
- $x_2$ = barrels of diesel produced per day
- $x_3$ = barrels of fuel oil produced per day
Subject to constraints:
Processing capacity constraints:
$$0.3x_1 + 0.2x_2 + 0.1x_3 \leq 2000 \quad \text{(CDU capacity)}$$
$$0.4x_1 + 0.3x_2 + 0.1x_3 \leq 1800 \quad \text{(CCU capacity)}$$
Market demand constraints:
$$x_1 \leq 4000 \quad \text{(Gasoline demand)}$$
$$x_2 \leq 3000 \quad \text{(Diesel demand)}$$
$$x_3 \leq 2000 \quad \text{(Fuel oil demand)}$$
Non-negativity constraints:
$$x_1, x_2, x_3 \geq 0$$
1 | import numpy as np |
Code Explanation
Let me break down the key components of this petroleum refinery optimization solution:
1. Problem Setup
The code begins by defining the linear programming problem using SciPy’s linprog function. We set up:
- Objective coefficients (
c): Negative values becauselinprogminimizes by default, but we want to maximize profit - Constraint matrix (
A) and bounds (b): Representing processing capacity and market demand limits - Variable bounds: Non-negativity constraints for production quantities
2. Optimization Engine
1 | result = linprog(c, A_ub=A, b_ub=b, bounds=x_bounds, method='highs') |
The HiGHS algorithm efficiently solves this linear programming problem, finding the optimal production mix that maximizes profit while satisfying all constraints.
3. Mathematical Relationships
The constraint equations represent real refinery operations:
- CDU constraint: $0.3x_1 + 0.2x_2 + 0.1x_3 \leq 2000$ reflects different processing intensities
- CCU constraint: $0.4x_1 + 0.3x_2 + 0.1x_3 \leq 1800$ shows gasoline requires more intensive cracking
4. Sensitivity Analysis
The code performs sensitivity analysis by varying gasoline prices from $60 to $100 per barrel, showing how the optimal solution changes with market conditions.
Results
=== Petroleum Refinery Optimization Problem === Maximizing profit from gasoline, diesel, and fuel oil production Solving the optimization problem... === OPTIMIZATION RESULTS === Optimal Production Plan: Gasoline: 1750.00 barrels/day Diesel: 3000.00 barrels/day Fuel Oil: 2000.00 barrels/day Maximum Daily Profit: $400000.00 Resource Utilization: CDU Usage: 1325.00/2000 (66.2%) CCU Usage: 1800.00/1800 (100.0%) === SENSITIVITY ANALYSIS === Analyzing how profit changes with different gasoline prices... Sensitivity analysis complete. Results will be visualized in graphs.

=== DETAILED ANALYSIS === 1. PRODUCTION EFFICIENCY: - Total production: 6,750 barrels/day - Gasoline dominates at 25.9% of total production - This reflects gasoline's higher profit margin ($80/barrel) 2. RESOURCE BOTTLENECKS: - CCU is the limiting factor at 100.0% utilization - CDU has spare capacity at 66.2% utilization - Consider expanding CCU capacity for further optimization 3. MARKET POSITION: - Gasoline: Using 43.8% of market demand - Diesel: Using 100.0% of market demand - Fuel Oil: Using 100.0% of market demand 4. PROFITABILITY INSIGHTS: - Revenue per barrel (weighted avg): $59.26 - Gasoline contributes 35.0% of total profit - High gasoline focus aligns with profit maximization strategy 5. SENSITIVITY INSIGHTS: - Current gasoline price ($80/barrel) vs optimal range - Profit increases linearly with gasoline price in current range - At $100/barrel gasoline price, profit would be $480,000.00 === OPTIMIZATION COMPLETE === The refinery should focus on maximizing gasoline production while efficiently utilizing both processing units to achieve optimal profitability.
Results Interpretation
The optimization reveals several key insights:
Production Strategy: The solution typically favors gasoline production due to its higher profit margin ($80/barrel vs $60 for diesel and $40 for fuel oil).
Resource Utilization: The analysis identifies which processing unit becomes the bottleneck, informing capacity expansion decisions.
Market Dynamics: The sensitivity analysis shows how profit responds to price changes, crucial for strategic planning.
Visualization Analysis
The comprehensive graphs provide multiple perspectives:
- Production Mix Pie Chart: Shows the proportion of each product in the optimal solution
- Revenue Contribution: Highlights which products drive profitability
- Resource Utilization: Identifies bottlenecks and spare capacity
- Sensitivity Analysis: Demonstrates profit elasticity to price changes
- Feasible Region: Visualizes constraint boundaries (2D projection)
- Profit Breakdown: Shows cumulative profit contribution by product
This optimization framework can be extended to include:
- Multiple crude oil types with different yields
- Environmental constraints (sulfur content, emissions)
- Inventory costs and storage limitations
- Seasonal demand variations
- Multiple time periods (dynamic optimization)
The linear programming approach provides a solid foundation for refinery operations optimization, enabling data-driven decision making in this capital-intensive industry.









