12 December 2022

In this article we continue the Python *Production mix* series. Specifically, we build Model 10 using the CVXPY library.

CVXPY is an open source Python package for representing and solving convex optimization problems.

Our objective is to compare a model built using CVXPY 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 model is available on GitHub.

## Formulation for Model 10

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 10 Python code

### Import dependencies

The first task is to import the libraries that are needed for our program. As shown in Figure 2, we import the CVXPY library, which we've previously installed, along with some other libraries that we'll use.

### Data file

The data for Model 10 is shown in Figure 3. We're using the json format that we used in some previous models.

As our engine we specify the cvxopt solver, which is a general purpose convex solver suitable for linear problems. Other solvers can also be used. Our code allows for the use of cbc, glop, glpk, or cvxopt – all of which we have previously installed. Each of these solvers returns the same solution, except that cbc fails to return dual values. When using cbc with Pyomo, we needed to specifically request dual values before running the model (see Production mix - Model 5), but we have not been able to find a similar process with CVXPY.

### Get data

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.

### Declarations

As shown in Figure 5, we assign various data to suitable data structures. Unlike most other models in this series, we don't assign the data to a `Model`

object. This is because when declaring the CVXPY model we must include the objective function and constraints – which we'll do later. Even so, the general structure of this code is very similar to previous models.

### Define the model

The model definition, as shown in Figure 6, starts with defining the variables and the objective function.

The constraints are defined as a list. First we create an empty list, then append each constraint to the list. The variable bounds are explicitly specified as constraints.

Note that CVXPY uses the `@`

symbol to represent matrix multiplication. Unlike most other optimization libraries, we don't need to specify the domain over which we're summing.

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, it can be useful to adopt a `def`

structure, to make the model more flexible and easier to understand.

### Solve model

As shown in Figure 7, we convert our string name for the solver into an engine object, as expected by CVXPY. We also define options that specify how verbose the solver output is and a maximum time limit.

Note that we can obtain a list of installed solvers that CVXPY can use via the code: `print(cp.installed_solvers())`

.

### Process results

The code for processing the solver result, as shown in Figure 8, is similar to the code for previous models.

### Write output

The code for writing the output, as shown in Figure 9, is similar to our previous models. The main difference is the syntax for extracting the slack and dual values from the solver output files.

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 CVXPY model is similar to our Pyomo models. Although the syntax of the two libraries is markedly different, the general structure of the model definitions and solution process is familiar.

CVXPY is a capable modelling library. It is focussed on general convex optimization, offering access to some solvers that are not supported by Pyomo – like CVXOPT (a package for solving convex optimization models) and OSQP (a package for solving convex quadratic models) – which may be important in some situations. For our linear programming model, there is no reason to use CPXPY compared with Pyomo.

## Next steps

In the next article we'll repeat the model implementation using our last selected library SciPy, which is a comprehensive collection of tools and algorithms for a wide range of scientific computing tasks.

## Conclusion

In this article we built the *Production mix* model using the CVXPY library. Compared with the Pyomo models, the code is quite similar though the model specification is markedly different to Pyomo.

For some types of model, CVXPY would be a more appropriate choice compared with Pyomo. Here we're solving a straightfoward linear programming model, for which CVXPY offers no significant advantage compared with Pyomo. Therefore, we tend to prefer Pyomo over CVXPY for this type of model.

In the next article, we'll build the *Production mix* model using SciPy.

If you would like to know more about this model, or you want help with your own models, then please contact us.