Python Tutorial : Basics of PuLP modeling

preview_player
Показать описание

---

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 problem in Python but relies on a solver to compute a solution. It works with many different solvers.

Let's 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 with each resource is different for each type of cake. Additionally, the profit for the cakes is 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 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 needed 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 2 bakers the total number of days should not exceed 60 days.
Finally, the worker packing is only available 22 days this month.

To solve our example we will model it in PuLP. A common modeling process involves initializing the model, defining the decision variables, defining the objective function, defining the model constraints, and finally we solve it. These steps should feel familiar with 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 LpProblem function. It has two inputs. The first is a text input for the type of problem you are modeling. The second input tells if 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 LpProblem in our Python script and choose to maximize it.

Next, we look at defining the decision variables. For this, you will use the LpVariable class. This class has 5 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 the 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 varValue.

Here is the full script.

In this lesson, we discussed that PuLP is an LP and IP modeling framework.

We reviewed the common 5 steps in the PuLP modeling process.
Finally, we worked through a resource scheduling example.

Alright! Let's practice.What is PuLP

#DataCamp #PythonTutorial #SupplyChainAnalyticsinPython
Рекомендации по теме