Linear Programming with Python

Linear programming is a mathematical optimization technique which is used to find the best outcome in a mathematical model with linear…

Linear Programming with Python
Photo by Emile Perron on Unsplash

Linear programming is a mathematical optimization technique which is used to find the best outcome in a mathematical model with linear relationships, its used in fields where the resources to achieve something (production, logistics, etc) is limited. In this article we will discover how we can do linear programming with the help of Python!

Needed Libraries

First we need to install pulp a Python library which allows us to solve linear programming problems

python -m pip install pulp

Modeling the problem

Consider the following production problem: Suppose you have a factory that creates two type of products, product A and product B. The factory has limited resources and you want to maximize profit while considering resource constraints.

Here are the problem details:

  • Product A has a profit of 10$ per unit.
  • Product B has a profit of 15$ per unit.

The function that calculates the profit from given products is

Z = (A*10) + (B*15)

This function is called “objective function” which represents the goal of the optimization which can be either to minimize or maximize a certain quantity, the objective function is expressed as a combination of decision variables multiplied by a coefficient, in this case decision variables are the quantities of each product A and B and coefficients are the profits per product 10 and 15 respectively.

The decision variables are variables that you want to determine in the optimization problem, they represent the quantities or actions you can change in order to control the desired outcome, they must be non-negative.

Constraints

Lets now assume that we have the following resource constraints

  • Each unit of Product A requires 2 hours of labor and 3 hours of machine time.
  • Each unit of Product B requires 4 hours of labor and 2 hours of machine time.
  • The factory has a total of 80 hours of labor and 60 hours of machine time available per day.

Those constraints are modeled mathematically as:

Labour constraints

(2*A) + (4*B) <= 80

Machine constraints

(3*A) + (2*B) <= 60

And in any case A and B should be greater than zero since you cannot have negative production.

Solving the Problem with Python

Save the following file as lp.py

import pulp 
lp_problem = pulp.LpProblem("Factory_Production", pulp.LpMaximize) 
A = pulp.LpVariable('Product_A', lowBound=0, cat='Integer') 
B = pulp.LpVariable('Product_B', lowBound=0, cat='Integer')   
lp_problem += 10 * A + 15 * B, "Total_Profit" 
lp_problem += 2 * A + 4 * B <= 80, "Labor_Constraint" 
lp_problem += 3 * A + 2 * B <= 60, "Machine_Time_Constraint" 
lp_problem.solve() 
 
print("Status:", pulp.LpStatus[lp_problem.status]) 
print("Optimal production plan:") 
print("Product A =", A.varValue) 
print("Product B =", B.varValue) 
print("Optimal profit =", pulp.value(lp_problem.objective))

The first line instructs Python to use the pulp library

import pulp

Second line creates a linear programming problem

lp_problem = pulp.LpProblem("Factory_Production", pulp.LpMaximize)

Lines 2 and 3 define the decision variables, lowBound=0 is the minimum limit of the variable and cat='Integer’ means that we cannot have count of products as floats which makes sense!

A = pulp.LpVariable('Product_A', lowBound=0, cat='Integer') 
B = pulp.LpVariable('Product_B', lowBound=0, cat='Integer')

4th Line is the definition of the objective function which in our case is the maximization of profit from the products.

lp_problem += 10 * A + 15 * B, "Total_Profit"

Lines 5 and 6 define the constraints we have in labour and machine time

lp_problem += 2 * A + 4 * B <= 80, "Labor_Constraint" 
lp_problem += 3 * A + 2 * B <= 60, "Machine_Time_Constraint"

Line 7 asks pulp to solve the problem

lp_problem.solve()

Running The script

To run the script we enter the following

python ./lp.py

It will print a lot of output in the screen but the most important are the things we print from our script

Status: Optimal 
Optimal production plan: 
Product A = 10.0 
Product B = 15.0 
Optimal profit = 325.0

Each corresponding line is printed by these lines of code

print("Status:", pulp.LpStatus[lp_problem.status]) 
print("Optimal production plan:") 
print("Product A =", A.varValue) 
print("Product B =", B.varValue) 
print("Optimal profit =", pulp.value(lp_problem.objective))
  • pulp.LpStatus[lp_problem.status] value expresses if the solution is optimal or not
  • A.varValue and B.varValue are the calculated values for the decision variables
  • pulp.value(lp_problem.objective) holds the calculated profit based on the decision variables

Conclusion

I hope you enjoyed this article as much I enjoyed writing it! linear programming is a powerful tool for making decisions in situations where resources are limited and must be allocated efficiently. It’s widely used in business and operations research to optimize production, distribution, scheduling, and other resource allocation problems.

In Plain English

Thank you for being a part of our community! Before you go: