The Startup
Published in

The Startup

Solving Linear Programming Problems (LPPs) Using PuLP and Python

Solving linear programming problems (LPPs) using PuLP and Python

In this tutorial, we will learn how to solve linear programming problems (LPPs) using PuLP and Python. At first, we learn how to install PuLP. If PuLP is already not installed in your system, then you can use the following commands to install it.

For Ubuntu, run the following command in the terminal:

pip install pulp
Installing PuLP in Ubuntu
Installing PuLP in Ubuntu

For Windows, run the following command in the command prompt (make sure that pip in your path):

pip install pulp

Example problem
Consider a chocolate manufacturing company that produces only two types of chocolates — A and B. Both the chocolates require Milk and Choco only. To manufacture each unit of A and B, the following quantities are required:

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

The company kitchen has a total of 5 units of Milk and 12 units of Choco. On each sale, the company makes a profit of

Rs 6 per unit A sold,
Rs 5 per unit B sold.

Now, the company wishes to maximize its profit. How many units of A and B should it produce, respectively?

Model this problem by using Python and PuLP and solve it.

Mathematical formulation

The mathematical formulation of the above mentioned LPP is described below.
Decision variables:


x be the total number of units produced by A,

y be the total number of units produced by B.

Objective function:

Maximize 6x+5y


x + y ≤ 5

3x + 2y ≤ 12

x ≥ 0

y ≥ 0

The last two constraints x≥0 and y≥0 are known as the non-negativity constraints.

Now, we solve this LPP using PuLP. At first, we import the required package.

import pulp

We instantiate a problem class named “My_LP_Problem” for the maximization problem.

my_lp_problem = pulp.LpProblem("My_LP_Problem", pulp.LpMaximize)

The decision variables need to be defined. This can be done as follows:

x = pulp.LpVariable('x', lowBound=0, cat='Continuous')
y = pulp.LpVariable('y', lowBound=0, cat='Continuous')

The lower bounds for both the decision variables are set to zero, and they are defined as continuous variables.

The objective function can be defined in the PuLP model as follows:

my_lp_problem += 6*x + 5*y, "Z"

Now, we define the set of constraints as follows:

my_lp_problem += y <= 5 - x, "C1"
my_lp_problem += 2*y <= 12 - 3*x, "C2"

Note that we don’t need to define the non-negativity constraints.

We can also print the created model using the print function.


The output of the above print statement is given below.

My_LP_Problem: MAXIMIZE 6*x + 5*y + 0 SUBJECT TO 
C1: x + y <= 5
C2: 3 x + 2 y <= 12
x Continuous
y Continuous

Now, we solve the LPP model using PuLP.


We can find out the status of the solution as follows:


The output of the above statement is mentioned below:


Here, PuLP solves the given LPP optimally.

We can print the decision variables value in the optimal solution as follows:

for variable in my_lp_problem.variables():
print("{} = {}".format(, variable.varValue))

The output of the above print statement is mentioned below:

x = 2.0
y = 3.0

We can also print the objective function value for the above values of x and y as follows:

print("Objective value (Z):",pulp.value(my_lp_problem.objective))

The output of the above print statement is shown below:

Objective value (Z): 27.0

In this tutorial, we have learned how to construct a PuLP model of a given LPP and solve it using PuLP. For the example problem considered above, PuLP solves it optimally.

Originally, this tutorial was published in my blog here.

The following video is a tutorial based on this blog.

Solving linear programming problems (LPPs) using Python PuLP || Optimization using PuLP

There is another introductory tutorial on Python API of DOcplex for solving linear programming problems. Here is the link.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Dr. Soumen Atta, Ph.D.

Dr. Soumen Atta, Ph.D.

Postdoctoral Researcher at Laboratoire des Sciences du Numérique de Nantes (LS2N), Université de Nantes, IMT Atlantique, Nantes, France.