CVXPY: Convex optimization, for everyone#

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()
|
|
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(solver="osqp", verbosity=1)
===============================================================================
CVXPY
v1.5.3
===============================================================================
(CVXPY) Mar 13 03:50:46 PM: Your problem has 5 variables, 6 constraints, and 0 parameters.
(CVXPY) Mar 13 03:50:46 PM: It is compliant with the following grammars: DCP, DQCP
(CVXPY) Mar 13 03:50:46 PM: (If you need to solve this problem multiple times, but with different data, consider using parameters.)
(CVXPY) Mar 13 03:50:46 PM: CVXPY will first compile your problem; then, it will invoke a numerical solver to obtain a solution.
(CVXPY) Mar 13 03:50:46 PM: Your problem is compiled with the CPP canonicalization backend.
-------------------------------------------------------------------------------
Compilation
-------------------------------------------------------------------------------
(CVXPY) Mar 13 03:50:46 PM: Compiling problem (target solver=OSQP).
(CVXPY) Mar 13 03:50:46 PM: Reduction chain: CvxAttr2Constr -> Qp2SymbolicQp -> QpMatrixStuffing -> OSQP
(CVXPY) Mar 13 03:50:46 PM: Applying reduction CvxAttr2Constr
(CVXPY) Mar 13 03:50:46 PM: Applying reduction Qp2SymbolicQp
(CVXPY) Mar 13 03:50:46 PM: Applying reduction QpMatrixStuffing
(CVXPY) Mar 13 03:50:46 PM: Applying reduction OSQP
(CVXPY) Mar 13 03:50:46 PM: Finished problem compilation (took 1.814e-02 seconds).
-------------------------------------------------------------------------------
Numerical solver
-------------------------------------------------------------------------------
(CVXPY) Mar 13 03:50:46 PM: Invoking solver OSQP to obtain a solution.
-----------------------------------------------------------------
OSQP v0.6.3 - Operator Splitting QP Solver
(c) Bartolomeo Stellato, Goran Banjac
University of Oxford - Stanford University 2021
-----------------------------------------------------------------
problem: variables n = 5, constraints m = 6
nnz(P) + nnz(A) = 10
settings: linear system solver = qdldl,
eps_abs = 1.0e-05, eps_rel = 1.0e-05,
eps_prim_inf = 1.0e-04, eps_dual_inf = 1.0e-04,
rho = 1.00e-01 (adaptive),
sigma = 1.00e-06, alpha = 1.60, max_iter = 10000
check_termination: on (interval 25),
scaling: on, scaled_termination: off
warm start: on, polish: on, time_limit: off
iter objective pri res dua res rho time
1 -5.2220e+00 3.68e+00 1.70e+02 1.00e-01 1.25e-04s
150 6.9994e-01 1.96e-05 7.82e-06 4.77e+00 1.70e-04s
plsh 7.0000e-01 1.95e-30 0.00e+00 -------- 1.97e-04s
status: solved
solution polish: successful
number of iterations: 150
optimal objective: 0.7000
run time: 1.97e-04s
optimal rho estimate: 9.81e+00
-------------------------------------------------------------------------------
Summary
-------------------------------------------------------------------------------
(CVXPY) Mar 13 03:50:46 PM: Problem status: optimal
(CVXPY) Mar 13 03:50:46 PM: Optimal value: -3.000e-01
(CVXPY) Mar 13 03:50:46 PM: Compilation took 1.814e-02 seconds
(CVXPY) Mar 13 03:50:46 PM: Solver (including time spent in interface) took 1.295e-03 seconds
P.objectives[0].value
-0.30000000000000004
type(P)
corneto.backend._base.ProblemDef
type(cvxpy_model)
cvxpy.problems.problem.Problem
cvxpy_model.status
'optimal'