Beginner’s guide to operations research with Python. Part 1: Linear programming
Introduction
There are several great tools available in the market for operations research problems. The most popular one is the solver plugin for Microsoft Excel. However, no tools match the flexibility, scalability and cost-effectiveness of Python. In this series of articles, I want to help readers get started with solving basic operations research problems using python. Readers may use these examples to develop their understanding to solve bigger, more complex problems as they usually are in the real world.
To follow this series of articles, all you need to have is beginner level proficiency in Python, which means you need to have any tool with which you can write and run Python code. I will use simple business problems as examples to illustrate the tools and concepts.
The business problem
Let us consider a brewery that brews three types of beer, A, B and C.
- One barrel of A, B and C requires 6, 5 and 8 bags of hops respectively.
- One barrel of A, B and C requires 10.5, 20 and 10 bags of barley respectively.
- The brewery can use no more than 60 bags of hops.
- The brewery can use no more than150 bags of barley.
- Because of storage constraints, the brewery can brew only 10 barrels of A.
- The brewery makes profit of ₹500, ₹450 & ₹600 on each barrel of A, B and C respectively.
How many barrels of A, B and C should be brewed to have maximum profit? We will solve this problem using the pulp library in Python.
The solution
To start, we import pulp library.
from pulp import *
We initialize the model as a maximization model as m and name it profit_maximization
m = LpProblem("profit_maximization", LpMaximize)
In this problem we have 3 decision variables A,B and C. corresponding to the number of barrels of beers A, B and C to be brewed respectively. The lower limits of these variables is specified as 0, since the number of barrels cannot be negative.
A = LpVariable('A', lowBound=0)
B = LpVariable('B', lowBound=0)
C = LpVariable('C', lowBound=0)
With this model, we want to maximize the profit, which is ₹500, ₹450 & ₹600 on each barrel of A, B and C respectively. Hence, the objective function is: z = 500*A + 450*B + 600*C
m += 500*A + 450*B + 600*C
The first constraint is that no more than 60 bags of hops can be used. Since a barrel of A, B and C requires 6, 5 and 8 bags of hops respectively, the constraint is: 6*A + 5*B + 8*C <= 60
m += 6*A + 5*B + 8*C <= 60
The second constraint is that no more than 150 bags of barley can be used. Since a barrel of A, B and C requires 10.5, 20 and 10 bags of barley respectively, the constraint is: 10.5*A + 20*B + 10*C <= 150
m += 10.5*A + 20*B + 10*C <= 150
The third constraint is that no more than 10 barrels of A can be brewed. Which is: A <= 10
m += A <= 10
We solve the model and print the solution.
m.solve()
print("objective is " + str(value(m.objective)))
print("A is " + str(value(A.varValue)))
print("B is " + str(value(B.varValue)))
print("C is " + str(value(C.varValue)))
The output is as follows. Hence, the brewery must brew 6.67 barrels of A, 4 barrels of B and no barrels of C
objective is 5133.333350000001
A is 6.6666667
B is 4.0
C is 0.0
Conclusion
In this part, we learned how to formulate and solve a linear programming problem using pulp library in python. You can find complete code on my GitHub here.
In the next part, we will tinker with other interesting operations research problems. Stay tuned.
Cheers!