CVXMOD – CVX code generation

CVX code gen
Experimental feature

CVX code generation is experimental. It was introduced partly to test CVXMOD, and partly for fun. While CVXMOD can produce CVX code for many CVX-representable problems, it will break down in more complicated cases.

CVX is a Matlab tool for solving convex optimization problems. CVXMOD can generate code that can be pasted in Matlab®, for use with CVX.

Syntax

prob.cvx() will return CVX/Matlab code as a string, when prob is a problem

This function is ‘best effort’: it will always return something, which may or may not be valid CVX code. In some cases it can be easily tweaked to make it valid in CVX; in other cases it will not work at all. CVXMOD does not currently issue any warnings or errors when code incompatible with CVX is produced.

Any parameters whose value attribute is not None will also be declared, with Matlab syntax.

CVX code generation
>>> randseed(12)
>>> A = param('A', 2, 3)
>>> A.value = randn(2,3)
>>> b = param('b', rows=2)
>>> b.value = value(A)*matrix((1,2,3.1))
>>> x = optvar('x', 3)

>>> p = problem(minimize(sum(x)), [A*(x - 0.2) == b, x >= -5, norm2(x) <= 6])

>>> print p
minimize(sum(x)), subject to
  A*(x - 0.2) == b
  x >= -5
  norm2(x) <= 6

>>> p.solve()
    # some text cut.
'optimal'

>>> value(p)
-6.131065054149464

>>> print p.cvx()
% CVX representation of problem, generated by CVXMOD.
% WARNING: not all atoms are supported in CVX.
% WARNING: code may require modification.

% Parameter definitions.
A = [  -3.460352  -0.784985   0.293601;
       -2.045772   0.522490  -0.593937];
b = [  -4.120159;
       -2.841996];

cvx_begin
    variable x(3)
    minimize(sum(x))
    subject to
        A*(x - 0.2) == b
        x >= -5
        norm(x) <= 6
cvx_end

Taking this code and pasting it in Matlab (with CVX installed) gives the following result. The optimal values match.

Matlab results
Calling SDPT3: 9 variables, 3 equality constr
    # some text cut.
Status: Solved
Optimal value (cvx_optval): -6.13107