CVXPY: Convex optimization, for everyone#

cvxpy logo

CVXPY is an open-source Python tool tailored for convex optimization problems. Unlike many platforms which require you to express your problem in strict formats determined by optimization solvers, CVXPY allows users to lay out their problem in a way that follows the maths.

A significant benefit of this is that CVXPY decouples the problem’s formulation from the solver used to tackle it. This means that once a problem is defined within CVXPY, one can seamlessly switch between or experiment with different solvers without the need to adjust the problem’s representation. Additionally, CVXPY supports an extensive range of both commercial and open-source solvers, providing users with the flexibility to choose the most appropriate tool for their specific challenges, be it for experimentation or scalability. This versatility simplifies the optimization process, making it more accessible and efficient.

import corneto as cn

cn.info()
Installed version:v1.0.0.dev5 (latest stable: v1.0.0-alpha)
Available backends:CVXPY v1.6.5, PICOS v2.6.1
Default backend (corneto.opt):CVXPY
Installed solvers:CVXOPT, GLPK, GLPK_MI, HIGHS, SCIP, SCIPY
Graphviz version:v0.20.3
Installed path:/home/runner/work/corneto/corneto/corneto
Repository:https://github.com/saezlab/corneto
import numpy as np

from corneto.backend import CvxpyBackend

backend = CvxpyBackend()
P = backend.Problem()
A = np.array([[0.12, 0.92, 0.76, 0.98, 0.79], [0.58, 0.57, 0.53, 0.71, 0.55]])
b = np.array([1, 0])
x = backend.Variable("x", A.shape[1])
P += sum(x) == 1, x >= 0

P.add_objectives(sum(A @ x - b))
cvxpy_model = P.solve(verbosity=1)
===============================================================================
                                     CVXPY                                     
                                     v1.6.5                                    
===============================================================================
(CVXPY) May 07 02:02:50 PM: Your problem has 5 variables, 6 constraints, and 0 parameters.
(CVXPY) May 07 02:02:50 PM: It is compliant with the following grammars: DCP, DQCP
(CVXPY) May 07 02:02:50 PM: (If you need to solve this problem multiple times, but with different data, consider using parameters.)
(CVXPY) May 07 02:02:50 PM: CVXPY will first compile your problem; then, it will invoke a numerical solver to obtain a solution.
(CVXPY) May 07 02:02:50 PM: Your problem is compiled with the CPP canonicalization backend.
-------------------------------------------------------------------------------
                                  Compilation                                  
-------------------------------------------------------------------------------
(CVXPY) May 07 02:02:50 PM: Compiling problem (target solver=SCIP).
(CVXPY) May 07 02:02:50 PM: Reduction chain: Dcp2Cone -> CvxAttr2Constr -> ConeMatrixStuffing -> SCIP
(CVXPY) May 07 02:02:50 PM: Applying reduction Dcp2Cone
(CVXPY) May 07 02:02:50 PM: Applying reduction CvxAttr2Constr
(CVXPY) May 07 02:02:50 PM: Applying reduction ConeMatrixStuffing
(CVXPY) May 07 02:02:50 PM: Applying reduction SCIP
(CVXPY) May 07 02:02:50 PM: Finished problem compilation (took 1.108e-02 seconds).
-------------------------------------------------------------------------------
                                Numerical solver                               
-------------------------------------------------------------------------------
(CVXPY) May 07 02:02:50 PM: Invoking solver SCIP  to obtain a solution.
presolving:
   (0.0s) symmetry computation started: requiring (bin +, int +, cont +), (fixed: bin -, int -, cont -)
   (0.0s) no symmetry present (symcode time: 0.00)
presolving (0 rounds: 0 fast, 0 medium, 0 exhaustive):
 0 deleted vars, 0 deleted constraints, 0 added constraints, 0 tightened bounds, 0 added holes, 0 changed sides, 0 changed coefficients
 0 implications, 0 cliques
presolved problem has 5 variables (0 bin, 0 int, 0 impl, 5 cont) and 6 constraints
      6 constraints of type <linear>
Presolving Time: 0.00

 time | node  | left  |LP iter|LP it/n|mem/heur|mdpt |vars |cons |rows |cuts |sepa|confs|strbr|  dualbound   | primalbound  |  gap   | compl. 
* 0.0s|     1 |     0 |     1 |     - |    LP  |   0 |   5 |   6 |   1 |   0 |  0 |   0 |   0 | 7.000000e-01 | 7.000000e-01 |   0.00%| unknown
  0.0s|     1 |     0 |     1 |     - |   608k |   0 |   5 |   6 |   1 |   0 |  0 |   0 |   0 | 7.000000e-01 | 7.000000e-01 |   0.00%| unknown

SCIP Status        : problem is solved [optimal solution found]
Solving Time (sec) : 0.00
Solving Nodes      : 1
Primal Bound       : +7.00000000000000e-01 (1 solutions)
Dual Bound         : +7.00000000000000e-01
Gap                : 0.00 %
-------------------------------------------------------------------------------
                                    Summary                                    
-------------------------------------------------------------------------------
(CVXPY) May 07 02:02:50 PM: Problem status: optimal
(CVXPY) May 07 02:02:50 PM: Optimal value: -3.000e-01
(CVXPY) May 07 02:02:50 PM: Compilation took 1.108e-02 seconds
(CVXPY) May 07 02:02:50 PM: Solver (including time spent in interface) took 3.862e-03 seconds
P.objectives[0].value
np.float64(-0.30000000000000004)
type(P)
corneto.backend._base.ProblemDef
type(cvxpy_model)
cvxpy.problems.problem.Problem
cvxpy_model.status
'optimal'