Get startedGet started for free

Solve the PuLP model

1. Solve the PuLP model

In this lesson, we will look at solving the PuLP model.

2. Common modeling process for PuLP

In earlier lessons we have looked at many of the different steps of the common pulp modeling process. One topic we have yet to cover involves solving the model. Solving the model commonly involves calling the solve method, checking the status of the solution, outputting the optimized values of the decision variables, and possibly outputting the optimized value of the objective function.

3. Solve model - solve method

After completing the earlier steps of the modeling process, to solve the model we call the solve method of the LpProblem class. This triggers the solver to attempt to find a solution. This method optionally takes the name of the solver to be used. Remember that PuLP is a framework that can interface with different solvers, and this is where you would specify if you wanted to use something else besides the default solver.

4. Code example - transportation costs

Here we have a code example of a simple LP problem. We are trying to decide which warehouse we should ship from to fulfill customer demand at the lowest cost. Focus on the very end of our code example. We call the solve method. It is this point when the solver attempts to solve the model.

5. Solve model - status of the solution

After calling the solve method, we will want to next check the status of the solution. The status method returns an integer. We can use LpStatus, which is a dictionary, to convert the status integer to text. There are five status types. Not solved is the status before calling the solve method we just discussed. Optimal means that an optimal solution has been found. Infeasible, means that there were no feasible solutions. Unbounded means the constraints are not bounded and maximizing or minimizing the solution will tend towards infinity. Finally, undefined means a solution may exist but the one found may not be it.

6. Code example continued

Going back to our code example we have added a print statement which will print the status of our solution. In this case the solution status is optimal.

7. Solve model - print optimized decision vars.

After calling the solve method and checking the solution status we often want to know the values of our decision variables of the optimized solution. To print all of our variables we can simply use a for loop with the variables method, which returns a list of all of the model variables. The for loop will iterate over the list and using name and varValue prints the variable name and value respectively. Another option is to loop through the model variables and store the output in a Pandas DataFrame. Going back to our code example, we use list comprehension to loop through the warehouses. For each warehouse we create a dictionary with the customers as the key. This gives us a list of dictionaries which we can use to create a DataFrame.

8. Code example continued

In our code example, we are only seeing the portion that focuses on solving the model and its output. The earlier portion is the same as before.

9. Solve model - optimized objective function

Now that we have printed the values of the decision variables, we often are also interested in the value of the objective function. We can quickly output the objective by calling the class attribute named objective. This attribute is passed to the value function which makes sure that the number is in the right format to be displayed.

10. Code example continued

In our code example we print the objective value.

11. Summary

In this lesson we reviewed how to solve the PuLP model, check the status of the solution, print the value of the decision variables, and the value of the objective function.

12. Let's practice!

Now let's practice.

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.