9.1 General Solver
cp(F[, G, h[, dims[, A, b[, kktsolver]]]])
- Solves a convex optimization problem
The argument F is a function that evaluates the objective and nonlinear constraint functions. It must handle the following calling sequences.
- F() returns a tuple (m, x0), where m is
the number of nonlinear constraints and x0 is a point in the
domain of f. x0 is a dense real matrix of size (n, 1).
- F(x), with x a dense real matrix of size (n,1),
returns a tuple (f, Df).
f is a dense real matrix of size (m+1,1), with
f[k] equal to f_k(x).
(If m is zero, f can also be returned as a number.)
Df is a dense or sparse real matrix of size (m+1, n)
with Df[k,:] equal to the transpose of the gradient
. If x is not in the domain of f, F(x)
returns None or a tuple (None,None).
- F(x,z), with x a dense real matrix of size
(n,1) and z a positive dense real matrix of size
(m+1,1) returns a tuple (f, Df, H).
f and Df are defined as above.
H is a square dense or sparse real matrix of size (n, n),
whose lower triangular part contains the lower triangular part of
If F is called with two arguments, it can be assumed that x is in the domain of f.
The linear inequalities are with respect to a cone C defined as a Cartesian product of a nonnegative orthant, a number of second-order cones, and a number of positive semidefinite cones:
with
Here
denotes a symmetric matrix u stored as a vector
in column major order.
The arguments h and b are real single-column dense matrices. G and A are real dense or sparse matrices. The default values for A and b are sparse matrices with zero rows, meaning that there are no equality constraints. The number of rows of G and h is equal to
The columns of G and h are vectors in
where the last N components represent symmetric matrices stored in column major order. The strictly upper triangular entries of these matrices are not accessed (i.e., the symmetric matrices are stored in the 'L'-type column major order used in the blas and lapack modules).The argument dims is a dictionary with the dimensions of the cones. It has three fields.
- dims['l']:
- l, the dimension of the nonnegative orthant (a nonnegative integer).
- dims['q']:
-
,
a list with the dimensions of the second-order cones (positive integers).
- dims['s']:
-
,
a list with the dimensions of the positive semidefinite cones
(nonnegative integers).
The default value of dims is {'l': h.size[0], 'q': [], 's': []}, i.e., the default assumption is that the linear inequalities are componentwise inequalities.
The role of the optional argument kktsolver is explained in section 9.4.
cp() returns a dictionary with keys 'status', 'x', 'snl', 'sl', 'y', 'znl', 'zl'. The possible values of the 'status' key are:
- 'optimal'
- In this case the
'x' entry of the dictionary is the primal optimal solution,
the 'snl' and 'sl' entries are the corresponding
slacks in the nonlinear and linear inequality constraints, and the
'znl', 'zl' and 'y' entries are the optimal
values of the dual variables associated with the nonlinear
inequalities, the linear inequalities, and the linear equality
constraints. These vectors approximately satisfy the
Karush-Kuhn-Tucker (KKT) conditions
where
,
- 'unknown'
- This indicates that the algorithm reached the maximum number of iterations before a solution was found. The 'x', 'snl', 'sl', 'y', 'znl' and 'zl' entries are None.
cp() requires that the problem is solvable and that
for all x and all positive z. - F() returns a tuple (m, x0), where m is
the number of nonlinear constraints and x0 is a point in the
domain of f. x0 is a dense real matrix of size (n, 1).
- Example: equality constrained analytic centering
The equality constrained analytic centering problem is defined as
The function acent() defined below solves the problem, assumping it is solvable.from cvxopt import solvers
from cvxopt.base import matrix, spdiag, log
def acent(A, b):
m, n = A.size
def F(x=None, z=None):
if x is None: return 0, matrix(1.0, (n,1))
if min(x) <= 0.0: return None
f = -sum(log(x))
Df = -(x**-1).T
if z is None: return f, Df
H = spdiag(z[0] * x**-2)
return f, Df, H
return solvers.cp(F, A=A, b=b)['x']- Example: robust least-squares
The function robls() defined below solves the unconstrained problem
from cvxopt import solvers
from cvxopt.base import matrix, spdiag, sqrt, div
def robls(A, b, rho):
m, n = A.size
def F(x=None, z=None):
if x is None: return 0, matrix(0.0, (n,1))
y = A*x-b
w = sqrt(rho + y**2)
f = sum(w)
Df = div(y, w).T * A
if z is None: return f, Df
H = A.T * spdiag(z[0]*rho*(w**-3)) * A
return f, Df, H
return solvers.cp(F)['x']- Example: analytic centering with cone constraints
-
from cvxopt.base import matrix, log, div, spdiag
from cvxopt import solvers
def F(x = None, z = None):
if x is None: return 0, matrix(0.0, (3,1))
if max(abs(x)) >= 1.0: return None
u = 1 - x**2
val = -sum(log(u))
Df = div(2*x, u).T
if z is None: return val, Df
H = spdiag(2 * z[0] * div(1 + u**2, u**2))
return val, Df, H
G = matrix([ [0., -1., 0., 0., -21., -11., 0., -11., 10., 8., 0., 8., 5.],
[0., 0., -1., 0., 0., 10., 16., 10., -10., -10., 16., -10., 3.],
[0., 0., 0., -1., -5., 2., -17., 2., -6., 8., -17., -7., 6.] ])
h = matrix([1.0, 0.0, 0.0, 0.0, 20., 10., 40., 10., 80., 10., 40., 10., 15.])
dims = {'l': 0, 'q': [4], 's': [3]}
sol = solvers.cp(F, G, h, dims)
print sol['x']
[ 4.11e-01]
[ 5.59e-01]
[-7.20e-01]
