Figure 8.9: Robust linear discrimination problem

% Section 8.6.1, Boyd & Vandenberghe "Convex Optimization"
% Original by Lieven Vandenberghe
% Adapted for CVX by Joelle Skaf - 10/16/05
% (a figure is generated)
%
% The goal is to find a function f(x) = a'*x - b that classifies the points
% {x_1,...,x_N} and {y_1,...,y_M} with maximal 'gap'. a and b can be
% obtained by solving the following problem:
%           maximize    t
%               s.t.    a'*x_i - b >=  t     for i = 1,...,N
%                       a'*y_i - b <= -t     for i = 1,...,M
%                       ||a||_2 <= 1

% data generation
n = 2;
randn('state',3);
N = 10; M = 6;
Y = [1.5+1*randn(1,M); 2*randn(1,M)];
X = [-1.5+1*randn(1,N); 2*randn(1,N)];
T = [-1 1; 1 1];
Y = T*Y;  X = T*X;

% Solution via CVX
cvx_begin
    variables a(n) b(1) t(1)
    maximize (t)
    X'*a - b >= t;
    Y'*a - b <= -t;
    norm(a) <= 1;
cvx_end

% Displaying results
linewidth = 0.5;  % for the squares and circles
t_min = min([X(1,:),Y(1,:)]);
t_max = max([X(1,:),Y(1,:)]);
tt = linspace(t_min-1,t_max+1,100);
p = -a(1)*tt/a(2) + b/a(2);
p1 = -a(1)*tt/a(2) + (b+t)/a(2);
p2 = -a(1)*tt/a(2) + (b-t)/a(2);

graph = plot(X(1,:),X(2,:), 'o', Y(1,:), Y(2,:), 'o');
set(graph(1),'LineWidth',linewidth);
set(graph(2),'LineWidth',linewidth);
set(graph(2),'MarkerFaceColor',[0 0.5 0]);
hold on;
plot(tt,p, '-r', tt,p1, '--r', tt,p2, '--r');
axis equal
title('Robust linear discrimination problem');
% print -deps linsep.eps
 
Calling SDPT3: 19 variables, 4 equality constraints
   For improved efficiency, SDPT3 is solving the dual problem.
------------------------------------------------------------

 num. of constraints =  4
 dim. of socp   var  =  3,   num. of socp blk  =  1
 dim. of linear var  = 16
 number of nearly dependent constraints = 1
 To remove these constraints, re-run sqlp.m with OPTIONS.rmdepconstr = 1.
*******************************************************************
   SDPT3: Infeasible path-following algorithms
*******************************************************************
 version  predcorr  gam  expon  scale_data
    NT      1      0.000   1        0    
it pstep dstep pinfeas dinfeas  gap      mean(obj)   cputime
-------------------------------------------------------------------
 0|0.000|0.000|6.9e+01|2.4e+01|7.8e+02| 1.732051e+00| 0:0:00| chol  1  1 
 1|0.969|1.000|2.1e+00|1.0e-01|3.5e+01|-5.110194e+00| 0:0:00| chol  1  1 
 2|1.000|0.582|1.6e-07|4.8e-02|1.2e+01|-3.232142e+00| 0:0:00| chol  1  1 
 3|0.985|0.878|2.5e-07|6.7e-03|1.6e+00| 6.987393e-01| 0:0:00| chol  1  1 
 4|0.770|1.000|9.6e-08|1.0e-04|8.8e-01| 4.376654e-01| 0:0:00| chol  1  1 
 5|1.000|0.908|3.0e-09|1.8e-05|1.3e-01| 5.367985e-01| 0:0:00| chol  1  1 
 6|0.605|1.000|2.3e-09|1.0e-06|6.1e-02| 5.178489e-01| 0:0:00| chol  1  1 
 7|0.983|0.967|1.7e-09|1.3e-07|3.7e-03| 5.117908e-01| 0:0:00| chol  1  1 
 8|0.988|0.987|2.1e-10|1.2e-08|4.4e-05| 5.112359e-01| 0:0:00| chol  1  1 
 9|0.988|0.981|6.8e-11|2.7e-10|6.4e-07| 5.112299e-01| 0:0:00| chol  1  1 
10|1.000|0.993|1.4e-11|1.5e-11|2.4e-08| 5.112299e-01| 0:0:00|
  stop: max(relative gap, infeasibilities) < 1.49e-08
-------------------------------------------------------------------
 number of iterations   = 10
 primal objective value =  5.11229916e-01
 dual   objective value =  5.11229892e-01
 gap := trace(XZ)       = 2.39e-08
 relative gap           = 1.18e-08
 actual relative gap    = 1.18e-08
 rel. primal infeas     = 1.41e-11
 rel. dual   infeas     = 1.54e-11
 norm(X), norm(y), norm(Z) = 9.6e-01, 1.2e+00, 8.5e+00
 norm(A), norm(b), norm(C) = 1.7e+01, 2.0e+00, 2.0e+00
 Total CPU time (secs)  = 0.2  
 CPU time per iteration = 0.0  
 termination code       =  0
 DIMACS: 1.4e-11  0.0e+00  1.5e-11  0.0e+00  1.2e-08  1.2e-08
-------------------------------------------------------------------
------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): +0.51123