% 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 non-
% separable points {x_1,...,x_N} and {y_1,...,y_M} by allowing some
% misclassification. a and b can be obtained by solving the following
% problem:
%           minimize    1'*u + 1'*v
%               s.t.    a'*x_i - b >= 1 - u_i        for i = 1,...,N
%                       a'*y_i - b <= -(1 - v_i)     for i = 1,...,M
%                       u >= 0 and v >= 0

% data generation
n = 2;
randn('state',2);
N = 50; M = 50;
Y = [1.5+0.9*randn(1,0.6*N), 1.5+0.7*randn(1,0.4*N);
     2*(randn(1,0.6*N)+1), 2*(randn(1,0.4*N)-1)];
X = [-1.5+0.9*randn(1,0.6*M),  -1.5+0.7*randn(1,0.4*M);
      2*(randn(1,0.6*M)-1), 2*(randn(1,0.4*M)+1)];
T = [-1 1; 1 1];
Y = T*Y;  X = T*X;

% Solution via CVX
cvx_begin
    variables a(n) b(1) u(N) v(M)
    minimize (ones(1,N)*u + ones(1,M)*v)
    X'*a - b >= 1 - u;
    Y'*a - b <= -(1 - v);
    u >= 0;
    v >= 0;
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+1)/a(2);
p2 = -a(1)*tt/a(2) + (b-1)/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('Approximate linear discrimination via linear programming');
% print -deps svc-discr.eps
 
Calling sedumi: 203 variables, 100 equality constraints
------------------------------------------------------------
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
Split 3 free variables
eqs m = 100, order n = 207, dim = 207, blocks = 1
nnz(A) = 200 + 600, nnz(ADA) = 100, nnz(L) = 100
Handling 6 + 0 dense columns.
 it :     b*y       gap    delta  rate   t/tP*  t/tD*   feas cg cg  prec
  0 :            5.18E+01 0.000
  1 :   1.53E+01 2.32E+01 0.000 0.4484 0.9000 0.9000   2.92  1  1  1.4E+00
  2 :   1.38E+01 6.94E+00 0.000 0.2989 0.9000 0.9000   1.34  1  1  5.7E-01
  3 :   1.12E+01 2.10E+00 0.000 0.3032 0.9000 0.9000   0.87  1  1  3.0E-01
  4 :   1.03E+01 1.05E+00 0.000 0.4984 0.9000 0.9000   0.79  1  1  2.1E-01
  5 :   9.94E+00 6.88E-01 0.000 0.6561 0.9000 0.9000   0.68  1  1  1.8E-01
  6 :   9.50E+00 2.38E-01 0.000 0.3459 0.9000 0.9000   0.37  1  1  1.8E-01
  7 :   8.22E+00 1.34E-01 0.000 0.5623 0.9000 0.9000   0.13  1  1  1.3E-01
  8 :   7.83E+00 8.96E-02 0.000 0.6698 0.9000 0.9000   0.62  1  1  1.2E-01
  9 :   7.65E+00 7.10E-02 0.000 0.7920 0.9000 0.9000   0.27  1  1  1.2E-01
 10 :   6.79E+00 3.11E-02 0.000 0.4378 0.9000 0.9000   0.79  1  1  6.2E-02
 11 :   6.47E+00 1.43E-02 0.000 0.4609 0.9000 0.9000   0.85  1  1  3.3E-02
 12 :   6.36E+00 7.87E-03 0.000 0.5494 0.9000 0.9000   0.83  1  1  2.2E-02
 13 :   6.21E+00 2.33E-03 0.000 0.2957 0.9000 0.9000   0.93  1  1  6.6E-03
 14 :   6.15E+00 1.05E-04 0.000 0.0452 0.9900 0.9900   0.96  1  1  
iter seconds digits       c*x               b*y
 14      0.1  15.0  6.1485694323e+00  6.1485694323e+00
|Ax-b| =   2.0e-13, [Ay-c]_+ =   5.7E-15, |x|=  9.0e+01, |y|=  2.4e+00

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