Skip to content

Usage from MATLAB

Sandro Merkli edited this page Feb 17, 2020 · 2 revisions

How to call ECOS from MATLAB

To solve the convex second-order cone problem

You can directly call ECOS from MATLAB® using its native interface:

[x,y,info,s,z] = ecos(c,G,h,dims,A,b,opts)  

It takes the problem data c,G,h,A,b and the necessary information on the dimension of the cones that is given in the struct dims. Note that A and G have to be given in sparse format. The equality constraints defined by A and b are optional and can be omitted. The dims structure has the following fields:

  • dims.l - scalar, dimension of positive orthant (LP-cone)
  • dims.q - vector with dimensions of second order cones
  • dims.e - scalar, number of exponential cones

The length of dims.q determines the number of second order cones. Use the empty matrix [] to indicate that a cone is not present, for example dims.q = [] if there are no second-order cones in your problem instance. Auxiliary settings can be passed to the solver by means of the struct opts, which is explained in detail here.

Return values

After a solve, ECOS returns the following variables:

ID Description
x Primal variables
y Dual variables for equality constraints
s Slack variables for inequality
z Dual variables associated to
info Struct with detailed information on solve status

Information on Solve Status - the "info" Struct

The struct info contains the following fields:

Field Description
exitflag Numerical value for status of the solve (details see below)
infostring Information for humans on what the exitflag means
pcost Primal objective value
dcost Dual objective value
pres Normalized primal residual
dres Normalized dual residual
pinfres Primal infeasibility measure
dinfres Dual infeasibility measure
pinf Indicates primal infeasibility if
dinf Indicates dual infeasibility if
gap Complementarity gap
relgap Normalized complementarity gap
iter Number of iterations performed
timing Struct with timing information

Note these values refer to the last iterate before ECOS exited because of an exit criterion. See the exitcodes below for more details.

Example: L1 minimization (Linear Programming)

In the following, we show how to solve a L1 minimization problem, which arises for example in sparse signal reconstruction problems (compressed sensing):

where , with . We use the epigraph reformulation to express the L1-norm of ,

where and we minimize . Hence the optimization variables are stacked as follows:

z = [x; u]

With this reformulation, the sparse reconstruction problem can be written as the linear program (LP)

where the inequality is w.r.t. the positive orthant. The following MATLAB® code generates a random instance of this problem, and calls ECOS to compute a solution:

% set dimensions and sparsity of
A
n = 1000; m = 10; density = 0.01;  
  
% linear term
c = [zeros(n,1); ones(n,1)];  
  
% equality constraints
A = sprandn(m,n,density);  
Atilde = [A, zeros(m,n)];  
b = randn(m,1);  
  
% linear inequality constraints
I = speye(n);  
G = [  I -I;  
      -I -I];  
h = zeros(2*n,1);  
  
% cone dimensions (LP cone only)
dims.l = 2*n; dims.q = [];  
  
% call solver
z = ecos(c,G,h,dims,Atilde,b);  
x = z(1:n);  
nnzx = sum(abs(x) > 1e-8);  
  
% print sparsity info
fprintf('Optimal x has %d/%d (%4.2f%%) non-zero entries.\n', nnzx , n,  nnzx/n*100);  

Quadratic Programming with ECOS

The MATLAB® package of ECOS comes with a quadratic programming interface that mimics the standard MATLAB® QP solver interface by quadprog. It supports QPs of the form

where we assume that is positive definite. This is the standard formulation that also MATLAB®'s built-in solver quadprog uses. To deal with the quadratic objective, we have to reformulate it into a second-order cone constraint to directly call ECOS. This conversion is done within a MATLAB® interface called ecosqp. Hence you can simply use

[x,fval,exitflag,output,lambda,t] = ecosqp(H,f,G,h,A,b,lb,ub,opts)

to solve the quadratic program. See help ecosqp for more details. The last output argument indicates the solution time required by the ECOS solver (without the conversion into an SOCP).