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 sedumi: 19 variables, 4 equality constraints
   For improved efficiency, sedumi is solving the dual problem.
------------------------------------------------------------
SeDuMi 1.21 by AdvOL, 2005-2008 and Jos F. Sturm, 1998-2003.
Alg = 2: xz-corrector, Adaptive Step-Differentiation, theta = 0.250, beta = 0.500
eqs m = 4, order n = 19, dim = 20, blocks = 2
nnz(A) = 66 + 0, nnz(ADA) = 16, nnz(L) = 10
 it :     b*y       gap    delta  rate   t/tP*  t/tD*   feas cg cg  prec
  0 :            4.94E+01 0.000
  1 :  -4.50E-01 1.38E+01 0.000 0.2788 0.9000 0.9000   2.41  1  1  8.3E+00
  2 :  -8.82E-03 4.04E+00 0.000 0.2933 0.9000 0.9000   1.89  1  1  2.3E+00
  3 :   2.12E-01 1.48E+00 0.000 0.3658 0.9000 0.9000  -0.01  1  1  1.4E+00
  4 :   4.41E-01 5.14E-01 0.000 0.3479 0.9000 0.9000   0.79  1  1  4.6E-01
  5 :   5.04E-01 4.10E-02 0.000 0.0799 0.9900 0.9900   0.89  1  1  3.8E-02
  6 :   5.11E-01 2.03E-04 0.000 0.0049 0.9990 0.9990   0.99  1  1  1.9E-04
  7 :   5.11E-01 4.04E-08 0.000 0.0002 0.9999 0.9999   1.00  1  1  4.0E-08
  8 :   5.11E-01 1.01E-08 0.000 0.2494 0.9035 0.9000   1.00  1  1  1.0E-08

iter seconds digits       c*x               b*y
  8      0.0   8.3  5.1122990003e-01  5.1122989767e-01
|Ax-b| =   7.9e-09, [Ay-c]_+ =   1.9E-10, |x|=  9.5e-01, |y|=  1.2e+00

Detailed timing (sec)
   Pre          IPM          Post
0.000E+00    4.000E-02    0.000E+00    
Max-norms: ||b||=1, ||c|| = 1,
Cholesky |add|=0, |skip| = 0, ||L.L|| = 1.75957.
------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): +0.51123