For this model, we're using the same general formulation that we used for previous models in this series, as shown in Figure 1.
Model 7 Python code
The first task is to import the libraries that are needed for our program. As shown in Figure 2, we import the pulp library, which we've previously installed, along with some other libraries that we'll use.
The data for Model 7 is shown in Figure 3. We're using the json format that we used in some previous models. The only difference is that PuLP does not allow spaces in the model name, so we use underscores instead.
We import the data from the json file using the code shown in Figure 4. This code is the same as the code we used for previous json files, apart from the filename.
As shown in Figure 5, we declare the model as a linear program, and specify that it is a maximization problem. The data is assigned to a Model object, in a way that is similar to previous models. The syntax is simpler than we used in, for example, our Pyomo Model 5, though Pyomo offers a richer object model.
Define the model
The model definition, as shown in Figure 6, is similar to how we defined the Pyomo Model 5 and Model 6. That is, we use def functions to define each constraint and the objective.
Like with our Pyomo models, it is not necessary to use def functions in this way. However, in more complex models, this approach gives us more control over the definitions – espeically when we need to make decisions about what terms in include in a constraint or the objective function.
Note that at the end of each constraint we provide a name. This name is used in the model output.
As shown in Figure 7, we define options specific to a solver – in this case, a time limt (in seconds) for either CBC or GLPK. We then solve the model and record the solve status.
The code for processing the solver result, as shown in Figure 8, is similar to the code for Model 6 except that we've simpiflied it to only write a solution if the solver status is optimal.
Note that the Status we recorded above is an integer. We could use that value to process the results. Alternatively, as we do below, we can ask PuLP to provide a text status, such as "Optimal".
The code for writing the output, as shown in Figure 9, is very similar to our previous models.
When we find an optimal solution, the output is shown in Figure 10. This output is similar to previous models, except for the model name and the slack values are defined in only one direction.
Evaluation of this model
There is a close similarity between this PuLP model and our Pyomo models. Although the syntax of the two libraries is somewhat different, the general structure of the model definitions and solution process is familiar. This isn't surprising, as both PuLP and Pyomo are COIN-OR projects.
Despite the similarities, we tend to prefer Pyomo simply because PuLP offers no significant advantages, while Pyomo provides easier access to a wider range of solvers – enabling us to solve a greater variety of model types.
In subsequent articles we'll repeat the model implementation using our other selected libraries. Next on the list is OR-Tools, which takes quite a different approach to this type of modelling.
In this article we built the Production mix model using the PuLP library. Compared with the Pyomo models, the code is quite similar. PuLP is a capable modelling library that is easy to use. However, since it offers no significant advantage compared with Pyomo, we tend to prefer Pyomo over PuLP.
In the next article, we'll build the Production mix model using OR-Tools.
If you would like to know more about this model, or you want help with your own models, then please contact us.