4 October 2022 (1,325 words)
In this article we continue the Python Production mix series. Specifically, we build Model 7 using the PuLP library.
PuLP, like Pyomo, is a COIN-OR project. The library is freely available, the code is open source, and it is widely used.
Our objective is to compare a model built using PuLP with the same model built using Pyomo.
Articles in this series
Articles in the Python Production mix series:
- Python optimization Rosetta Stone
- Production mix - Model 1, Pyomo concrete
- Production mix - Model 2, Pyomo separate data
- Production mix - Model 3, Pyomo external data
- Production mix - Model 4, Pyomo json file
- Production mix - Model 5, Pyomo using def
- Production mix - Model 6, Pyomo abstract
- Production mix - Model 7, PuLP
- Production mix - Model 8, OR-Tools
- Production mix - Model 9, Gekko
- Production mix - Model 10, CVXPY
- Production mix - Model 11, SciPy
- Production mix - Conclusions
Download the model
The Python code and data for this model are available in the following files:
- Jupyter notebook: production-model-7.ipynb
- Python code: production-model-7.py
- Data: productiondata7.json
The "Jupyter notebook" file contains a formatted combination of Python code and markdown code – this file should be opened and run in Jupyter Lab. We describe setting up our Python environment, including Jupyter Lab and various Python libraries, in the article Set up a Python modelling environment.
The "Python code" file is a plain text file containing only the Python code of this model. Download this file if you have a non-Jupyter environment for running Python programs.
The "Data" file is a plain text file containing the model's data, in json format.
The model files are also available on GitHub.
Formulation for Model 7
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.