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) Jan 20 09:58:19 AM: Your problem has 5 variables, 6 constraints, and 0 parameters.
(CVXPY) Jan 20 09:58:19 AM: It is compliant with the following grammars: DCP, DQCP
(CVXPY) Jan 20 09:58:19 AM: (If you need to solve this problem multiple times, but with different data, consider using parameters.)
(CVXPY) Jan 20 09:58:19 AM: CVXPY will first compile your problem; then, it will invoke a numerical solver to obtain a solution.
(CVXPY) Jan 20 09:58:19 AM: Your problem is compiled with the CPP canonicalization backend.
-------------------------------------------------------------------------------
Compilation
-------------------------------------------------------------------------------
(CVXPY) Jan 20 09:58:19 AM: Compiling problem (target solver=OSQP).
(CVXPY) Jan 20 09:58:19 AM: Reduction chain: CvxAttr2Constr -> Qp2SymbolicQp -> QpMatrixStuffing -> OSQP
(CVXPY) Jan 20 09:58:19 AM: Applying reduction CvxAttr2Constr
(CVXPY) Jan 20 09:58:19 AM: Applying reduction Qp2SymbolicQp
(CVXPY) Jan 20 09:58:19 AM: Applying reduction QpMatrixStuffing
(CVXPY) Jan 20 09:58:19 AM: Applying reduction OSQP
(CVXPY) Jan 20 09:58:19 AM: Finished problem compilation (took 1.828e-02 seconds).
-------------------------------------------------------------------------------
Numerical solver
-------------------------------------------------------------------------------
(CVXPY) Jan 20 09:58:19 AM: 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 6.06e-05s
150 6.9994e-01 1.96e-05 7.82e-06 4.77e+00 1.44e-04s
plsh 7.0000e-01 1.95e-30 0.00e+00 -------- 2.75e-04s
status: solved
solution polish: successful
number of iterations: 150
optimal objective: 0.7000
run time: 2.75e-04s
optimal rho estimate: 9.81e+00
-------------------------------------------------------------------------------
Summary
-------------------------------------------------------------------------------
(CVXPY) Jan 20 09:58:19 AM: Problem status: optimal
(CVXPY) Jan 20 09:58:19 AM: Optimal value: -3.000e-01
(CVXPY) Jan 20 09:58:19 AM: Compilation took 1.828e-02 seconds
(CVXPY) Jan 20 09:58:19 AM: Solver (including time spent in interface) took 1.466e-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'