Linear Programming

Sometimes we need optimization in real life too to get the maximum profit. So, optimization techniques belong to deep learning, where we try to achieve the minimum loss. But sometimes, we have limited resources and want to get the maximum profit; then linear programming comes in.

Linear programming is a mathematical model which is generally used in data science for optimization. The optimization means we can understand the meaning like maximum profit and less cost. The company or the organization has mainly two main objectives, minimization, and maximization. The minimization means to cut the extra cost which comes in productions to get the maximize profits. Linear programming is a simple optimization technique that can help in the same way. Linear programming is everywhere around us; for example, when we work on any project, we also make strategies to manage the teamwork to fast-delivery efficiently.

Terminology of the Linear Programming:

  1. Objective function: The objective function will be either to maximize or minimize. The problem which we are going to solve is to maximize the company profits.
  2. Decision variable: Decision variable: These decision variables’ values are unknown. After calculating these values, we find the objective function output below the linear programming program. We calculate x and y decision values and then fit the objective function that gives its final value.
  3. Non-negatively constraint: The values of the decision variables should not be negative or always be equal to zero or greater than zero.

Problem Statement: Consider a company that makes chocolates of two types – A and B. Both the chocolates need two necessary materials – Milk and Choco. To manufacture each chocolate A and B, the following quantities are required:

  • Each unit of A requires 3 units of Milk and 2 units of Choco
  • Each unit of B requires 4 unit of Milk and 1 unit of Choco

The company’s current stock has 25 units of Milk and 10 units of Choco. The company gets profits from each unit of chocolate sale as below:

  • Rs. 25 per unit sale of chocolate A
  • Rs. 20 per unit sale of chocolate B

Now, the company wants to make its maximum profit from the available stocks.

Milk Choco Profit per unit
A 3 2 Rs 25
B 4 1 Rs 10
Total Balance in Stock 25 10

Solution: As from the above chart, we can understand the company wants to increase its profit. So first, we are going to define our maximize function for this problem. So, by using the mathematical model, let’s say we create x units of A and y units of B, then we can say that the maximize function model will look like below:

Let the total number of units produced by A be = x

Let the total number of units produced by B be = y

Now, the total profit is represented by Z

To calculate the maximum profit, we have to multiply the total units of chocolate produced by A and B with their unit profit of Rs. 25 and Rs. 20, respectively.

Profit: Max Z = 25 * x + 20 * y

Now, we have our maximize function Z.

The company always wants to produce as much as possible to get large profits, but the materials are limited. As per the above information table, each unit of A and B requires 3 and 4 units of milk, respectively. So, the formula will be like 3 * x + 4 * y. But there is a limitation of the milk, which is 25 units only in the stock. So, after adding this constraint, the above formula will be:

3 *x + 4 * y ≤ 25

Similarly, each unit of A and B requires 2 and 1 units of choco, respectively. So the formula will be like 2 * x +  y. But there is also a limitation of the choco, which is 20 units only in the stock. So, after adding this constraint, the above formula will be:

2 *x + y ≤ 20

The value given by the A and B is always positive as these are quantities. So, they should be either equal to zero or greater than zero.

x ≥ 0  &  y ≥ 0

So, now our mathematical model of the problem statement is done. Now, we are going to see in the python code the above problem statement.

Python Programming:

So, we have to install the python package PuLP, which solves the linear programming problems.

Line 52: We import the pupl library.

Line 53: We define the problem statement and give the suitable name of our problem. We give the name of our problem, ais chocolate manufacturing profit, and describe the function’s objective in the next variable, which is maximized.

Line 54: We define the Variable to hold the decision variables. The second and third arguments are lower and upper bound values. We also know that there will be no negative value, so we define the lower bound (second argument) value to 0, and in the upper bound (third argument), we mention None. The last statement talks about values being an integer (LpInteger).

Line 57: We define our objective function as given in the problem statement.

Line 58: We created our variables with the constraints as given in the problem statement.

Line 59: We print our problem statement.

Line 60: We save the whole problem data to a file.

Line 61: We called a method solver of the pulp library to solve linear programming.

Line 63 and 64: We print the calculated values, and the final profit shows the Rs. 155.

The below file, which we are saving at Line no. 60

The above file has the output of the objective and constraints which we saved into a file. So next time, we can just load and run the code.

The complete python code in .py format is given below:


We understand basic linear programming examples and how to solve them through python programming. But in real life, more complex problems always come, so instead of solving them manually, the country or company always needs automation to be fast and maximize profits.

About the author

Shekhar Pandey