31 October 2022 (1,371 words)
In this article we continue the Python Production mix series. Specifically, we build Model 8 using the OR-Tools library.
OR-Tools is a project from Google. The library is freely available, the code is open source, and it is widely used.
Our objective is to compare a model built using OR-Tools 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-8.ipynb
- Python code: production-model-8.py
- Data: productiondata8.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 8
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 8 Python code
The first task is to import the libraries that are needed for our program. As shown in Figure 2, we import the linear solver part of the OR-Tools library, which we've previously installed, along with some other libraries that we'll use.
The data for Model 8 is shown in Figure 3. We're using the json format that we used in some previous models. We also specify the Google Linear Optimization Package (GLOP) solver, which is an open source linear programming solver created by Google.
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 create the model and assign various data to the
Model. This code style differs from most OR-Tools examples, though it is very similar to our previous models in this series.
Define the model
The model definition, as shown in Figure 6, starts with defining the variables. We then define the constraints and objective function in a straightforward manner. Note that we specify the optimization direction using
We've adopted a simple style in this example, though we could use the
def structure that we've used in some previous models. When developing more complex models (which we'll do later, when exploring OR-Tool's constraint optimization capabilities), it can be useful to adopt a
def structure, to make the model more flexible and easier to understand.
Note that we provide a name at the end of each constraint. This name is used in the model output, if we ask OR-Tools to output the model in LP format (which we do below, if there is no solution).
As shown in Figure 7, we define the option to specify a time limit (in seconds) for GLOP. 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 previous models.
Note that GLOP can return a variety of solve status codes.
The code for writing the output, as shown in Figure 9, is very similar to our previous models. The main difference is the syntax for extracting the slack and dual values from the GLOP solution.
When we find an optimal solution, the output is shown in Figure 10. This output is similar to previous models, except for the model's name.
Evaluation of this model
This model is very similar to 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.
Despite the similarities, we tend to prefer Pyomo for solving linear programs, simply because Pyomo offers access to a wider range of solvers for solving a greater variety of model types, and OR-Tools offers no significant advantage over Pyomo for this type of problem.
Although OR-Tools can solve our Production mix linear programming model, the real strength of OR-Tools is in its constraint programming capability. That is, OR-Tools has packages designed to solve specific types of models that are otherwise difficult to solve, including assignment, routing, scheduling, and bin packing problems. We'll explore those capabilities in later articles.
In subsequent articles we'll repeat the model implementation using our other selected libraries. Next on the list is Gekko, which has a focus on machine learning along with optimization of mixed-integer and differential algebraic equations.
In this article we built the Production mix model using the OR-Tools library. Compared with the Pyomo models, the code is quite similar. OR-Tools 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 OR-Tools for this type of model.
In the next article, we'll build the Production mix model using Gekko.
If you would like to know more about this model, or you want help with your own models, then please contact us.