- 40 Algorithms Every Programmer Should Know
- Imran Ahmad
- 435字
- 2025-04-04 12:59:10
Practical application – capacity planning with linear programming
Let's look at a practical use case where linear programming can be used to solve a real-world problem. Let's assume that we want to maximize the profits of a state-of-the-art factory that manufactures two different types of robots:
- Advanced model (A): This provides full functionality. Manufacturing each unit of the advanced model results in a profit of $4,200.
- Basic model (B): This only provides basic functionality. Manufacturing each unit of the basic model results in a profit of $2,800.
There are three different types of people needed to manufacture a robot. The exact number of days needed to manufacture a robot of each type are as follows:

This can be modeled as follows:
- Maximum profit = 4200A + 2800B
- This is subject to the following:
- A ≥ 0: The number of advanced robots produced can be 0 or more.
- B ≥ 0: The number of basic robots produced can be 0 or more.
- 3A + 2B ≤ 20: These are the constraints of the technician's availability.
- 4A+3B ≤ 30: These are the constraints of the AI specialist's availability.
- 4A+ 3B ≤ 44: These are the constraints of the engineers' availability.
First, we import the Python package named pulp, which is used to implement ;linear programming:
import pulp
Then, we call the LpProblem function in this package to instantiate the problem class. We name the instance Profit maximising problem:
# Instantiate our problem class model = pulp.LpProblem("Profit maximising problem", pulp.LpMaximize)
Then, we define two linear variables, A and B. Variable A represents the number of advanced robots that are produced and variable B represents the number of basic robots that are produced:
A = pulp.LpVariable('A', lowBound=0, cat='Integer')
B = pulp.LpVariable('B', lowBound=0, cat='Integer')
We define the objective function and constraints as follows:
# Objective function
model += 5000 * A + 2500 * B, "Profit"
# Constraints
model += 3 * A + 2 * B <= 20
model += 4 * A + 3 * B <= 30
model += 4 * A + 3 * B <= 44
We use the solve function to generate a solution:
# Solve our problem model.solve() pulp.LpStatus[model.status]
Then, we print the values of A and B and the value of the objective function:

Linear programming is extensively used in the manufacturing industry to find the optimal number of products that should be used to optimize the use of available resources.
And here we come to the end of this chapter! Let's summarize what we have learned.