CVXGEN: Code Generation for Convex Optimization

Using the Matlab interface

CVXGEN creates a Matlab MEX interface for use with each custom solver, making it easy to test and use high-speed solvers in simulations and data analysis. The easiest way to use this interface is via the ‘Matlab’ screen in CVXGEN's online interface. Just copy and paste the given two lines of code into Matlab. This will perform the following steps, which you can also do manually.

  1. Download and extract the ‘’ archive for your problem. This will create a subdirectory called cvxgen/.

  2. Inside the cvxgen/ folder in Matlab, call make_csolve. This will use the mex command to compile and build your custom solver.

  3. This creates a csolve.mex* file (the exact extension varies), a help file csolve.m and a companion function cvxsolve.m, for use with CVX.

You are now ready to use the solver. The following are Matlab commands, for use with the simple QP example.

% Show information about the solver, including required parameters.
help csolve

% Attach problem data to the params structure.
params.A = randn(3, 10);
params.c = randn(10, 1);

% Exercise the high-speed solver.
[vars, status] = csolve(params);  % solve, saving results.

% Check convergence, and display the optimal variable value.
if ~status.converged, error 'failed to converge'; end

Parameter checking

The csolve interface will check that all parameters have the correct sizes and shapes. It will not, however, check the parameter attributes (nonnegative, psd etc) you specified when creating your problem. CVXGEN may do something useful if you violate your problem specification, but it's unlikely, and definitely not recommended or guaranteed.

Indexed variables

In your CVXGEN problem specification, you may have specified indexed parameters or variables, such as

  y[i] (3), i=1..3

  x[i] (3), i=0..3

You can provide data for these parameters in Matlab in two different ways. With the first, an underscore separates the index and the variable name. With the second, we index into a Matlab cell array. The latter is more convenient for use with loops.

% Assign initial parameters, option 1.
params.y_1 = ...

% Assign initial parameters, option 2. Equivalent.
params.y{1} = ...
% Could also use within a loop.
for i = 1:3, params.y{i} = ...; end

To retrieve the corresponding variables, we use a similar method. Unfortunately, Matlab doesn't support zero-indices in cell arrays, so only the first option will apply for zero indices.

vars.x_0   % only option, since Matlab doesn't support zero-indexing.
vars.x_1   % option 1.
vars.x{1}  % option 2, equivalent.

Modifying solver settings

If you want to modify solver behavior, you can change the settings. See more information about the settings.

% Create a new settings structure, with some example settings.
settings.verbose = 0;  % disable output of solver progress.
settings.max_iters = 10;  % reduce the maximum iteration count, from 25.
settings.eps = 0.1;  % reduce the required objective tolerance, from 1e-6.
settings.resid_tol = 1e-2;  % reduce the required residual tolerances, from 1e-4.

% Use these settings.
[vars, status] = csolve(params, settings);

If you want to test the performance of your solver outside Matlab, but using data from inside Matlab, try the settings.prepare_for_c = 1 option. This will output C statements that you can paste into testsolver.c, for use with the pure C interface.