Python Tutorial : Basics of PuLP modeling

PuLP is a Python library for linear and integer programming that relies on external solvers. Learn the five modeling steps through a bakery case study: initialize a model, define variables with bounds, set an objective function, add constraints, and solve to find optimal production quantities.
Chapters
In this lesson we discuss IP and LP modeling in pulp. This course will focus on using the Python PULP library. It is a framework for linear and integer programming problems. The library is maintained by the COIN or Foundation. PULP models the problems in Python, but relies on a solver to compute a solution. It works with many different solvers. Lets jump right into an example that focuses on resource scheduling. Imagine that you are a consultant for a cake bakery that sells only two types of cakes. You are attempting to schedule the resources of the bakery for the next 30 days. There is an oven, two bakers and a person who packages the cakes. In this case, we assume the person packaging will only work 22 of the next 30 days due to vacation. The amount of time needed for each resource is different for each type of cake. Additionally, the profit for the cakes are different. We want to know how many of each type of cake we should make to maximize our profits. Remember that our profits are subject to the different constraints. First, the number of cakes produced must be greater than zero. The number of cakes of each type produced multiplied by the time kneading on the oven gives the total number of days and this cannot exceed 30 days. A similar situation exists for the bakers. However, because there are two bakers, the total number of days should not exceed 60 days. Finally, the worker packaging is only available 22 days this month. To solve our example, we will model it in pulp. A common modeling process involves initializing a model, defining the decision variables, defining the objective function, defining the model constraints, and finally we solve it. These steps should feel familiar from the lesson on LP and IP modeling. Initializing the model is the first step in the modeling process and for that you will use the LPP function. It has two inputs. The first is a text input for the type of problem you are modeling. The second input tells that the model should look to maximize or minimize the objective function. For example, when modeling delivery times, you will likely choose to minimize. After importing the package, we initialize the model with LPP in our Python script and choose to maximize. Next we look at defining the decision variables. For this you will use the LP variable class. This class has five inputs. The first is the name of the variable. The next two set the lower and upper bounds of the variable. Their default value is none, which sets the bounds to negative infinity for lower bound or positive infinity for the upper bound. The CAT input categorizes the variable as either an integer, binary or continuous. The last input is related to column based modeling which is outside the scope of this course. In our example, the variables are how many A and B cakes are produced. We only set the lower bounds and force them to be an integer variable. Next, we define the objective function using our variables. Then we define the constraints. PULP is able to identify which equations are constraints because of the inequalities. Finally, solve the model. The optimized values are stored in var value. Here is the full script. In this lesson, we discussed that PULP is a LP and IP modeling framework. We review the five common steps in the PULP modeling process. Finally, we work through a resource scheduling example. Alright, let's.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







