PuLP – Linear Programming with Python

Author: Abhishek Mamidi

Are you wondering whether this would be useful for Data Science or not? The answer is “Yes”. This article will be highly useful in Data Science. In this article, you will learn how to use PuLP and where this can be used.

When solving Data Science problems, we often come to a point where there are a lot of constraints and you are trying to maximize or minimize the cost. Here, you can use PuLP to solve for an optimized solution. The PuLP helps us to write mathematical expressions in Python language. There are many commercial and other open-source solvers available too for solving the optimization.

Let’s see how we can approach and solve a problem using PuLP. This involves a few steps:

  • Understanding the problem
  • Converting the problem into a mathematical expression
  • Solving optimization
  • Post-process to extract the solution

In linear programming, the decision variables should be real, objective and constraints should be linear. We can also do Integer programming, in which some or all of the decision variables are integers.

Now, we will discuss how to convert a problem into a linear program and solve it using PuLP. I will guide you through a simple example, which can be extended to a complicated problem.


For example, you went to a shop. There are ten different chocolates. Your goal is to buy chocolates such that the number of calories is maximized. Some of the constraints are:

  • You can buy only 5 chocolates
  • The cost should be less than or equal to 100

How to solve this? In general, we take 10 variables referring to each chocolate type. After that, we define the above linear constraints and solve the optimization. Let’s do the same thing step by step using PuLP.

Let’s code it in Python using PuLP. We will start by importing libraries.

Read full article by Abhishek Mamidi