% Boyd & Vandenberghe "Convex Optimization"
% 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}. a and b can be obtained by solving a
% feasibility problem:
%           minimize    0
%               s.t.    a'*x_i - b >=  1     for i = 1,...,N
%                       a'*y_i - b <= -1     for i = 1,...,M

% 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
fprintf('Finding a separating hyperplane...');

cvx_begin
    variables a(n) b(1)
    X'*a - b >= 1;
    Y'*a - b <= -1;
cvx_end

fprintf(1,'Done! \n');

% Displaying results
linewidth = 0.5;  % for the squares and circles
t_min = min([X(1,:),Y(1,:)]);
t_max = max([X(1,:),Y(1,:)]);
t = linspace(t_min-1,t_max+1,100);
p = -a(1)*t/a(2) + b/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(t,p, '-r');
axis equal
title('Simple classification using an affine function');
% print -deps lin-discr.eps
Finding a separating hyperplane... 
Calling Mosek 9.1.9: 16 variables, 3 equality constraints
   For improved efficiency, Mosek is solving the dual problem.
------------------------------------------------------------

MOSEK Version 9.1.9 (Build date: 2019-11-21 11:32:15)
Copyright (c) MOSEK ApS, Denmark. WWW: mosek.com
Platform: MACOSX/64-X86

Problem
  Name                   :                 
  Objective sense        : min             
  Type                   : LO (linear optimization problem)
  Constraints            : 3               
  Cones                  : 0               
  Scalar variables       : 16              
  Matrix variables       : 0               
  Integer variables      : 0               

Optimizer started.
Presolve started.
Linear dependency checker started.
Linear dependency checker terminated.
Eliminator started.
Freed constraints in eliminator : 0
Eliminator terminated.
Eliminator started.
Freed constraints in eliminator : 0
Eliminator terminated.
Eliminator - tries                  : 2                 time                   : 0.00            
Lin. dep.  - tries                  : 1                 time                   : 0.00            
Lin. dep.  - number                 : 0               
Presolve terminated. Time: 0.00    
Problem
  Name                   :                 
  Objective sense        : min             
  Type                   : LO (linear optimization problem)
  Constraints            : 3               
  Cones                  : 0               
  Scalar variables       : 16              
  Matrix variables       : 0               
  Integer variables      : 0               

Optimizer  - threads                : 8               
Optimizer  - solved problem         : the primal      
Optimizer  - Constraints            : 3
Optimizer  - Cones                  : 0
Optimizer  - Scalar variables       : 16                conic                  : 0               
Optimizer  - Semi-definite variables: 0                 scalarized             : 0               
Factor     - setup time             : 0.00              dense det. time        : 0.00            
Factor     - ML order time          : 0.00              GP order time          : 0.00            
Factor     - nonzeros before factor : 6                 after factor           : 6               
Factor     - dense dim.             : 0                 flops                  : 2.06e+02        
ITE PFEAS    DFEAS    GFEAS    PRSTATUS   POBJ              DOBJ              MU       TIME  
0   7.2e+00  2.8e+00  2.1e+01  0.00e+00   -1.600000000e+01  0.000000000e+00   2.0e+00  0.00  
1   1.6e+00  6.2e-01  4.6e+00  -2.36e+00  -5.398997601e+04  0.000000000e+00   4.4e-01  0.01  
2   2.0e-01  7.8e-02  5.9e-01  -9.52e-02  -4.191335959e+02  0.000000000e+00   5.5e-02  0.01  
3   1.1e-03  4.2e-04  3.1e-03  8.17e-01   -2.512600010e+00  0.000000000e+00   3.0e-04  0.01  
4   1.1e-07  4.2e-08  3.1e-07  9.99e-01   -2.516481137e-04  0.000000000e+00   3.0e-08  0.01  
5   1.1e-11  4.2e-12  3.1e-11  1.00e+00   -2.516481278e-08  0.000000000e+00   3.0e-12  0.01  
6   1.1e-15  1.0e-15  3.1e-15  1.00e+00   -2.516481278e-12  0.000000000e+00   3.0e-16  0.01  
Basis identification started.
Primal basis identification phase started.
Primal basis identification phase terminated. Time: 0.00
Dual basis identification phase started.
Dual basis identification phase terminated. Time: 0.00
Basis identification terminated. Time: 0.00
Optimizer terminated. Time: 0.02    


Interior-point solution summary
  Problem status  : PRIMAL_AND_DUAL_FEASIBLE
  Solution status : OPTIMAL
  Primal.  obj: -2.5164812778e-12   nrm: 8e-13    Viol.  con: 2e-12    var: 0e+00  
  Dual.    obj: 0.0000000000e+00    nrm: 2e+03    Viol.  con: 0e+00    var: 2e-13  

Basic solution summary
  Problem status  : PRIMAL_AND_DUAL_FEASIBLE
  Solution status : OPTIMAL
  Primal.  obj: 0.0000000000e+00    nrm: 0e+00    Viol.  con: 0e+00    var: 0e+00  
  Dual.    obj: 0.0000000000e+00    nrm: 1e+01    Viol.  con: 0e+00    var: 2e-13  
Optimizer summary
  Optimizer                 -                        time: 0.02    
    Interior-point          - iterations : 6         time: 0.01    
      Basis identification  -                        time: 0.00    
        Primal              - iterations : 0         time: 0.00    
        Dual                - iterations : 3         time: 0.00    
        Clean primal        - iterations : 0         time: 0.00    
        Clean dual          - iterations : 0         time: 0.00    
    Simplex                 -                        time: 0.00    
      Primal simplex        - iterations : 0         time: 0.00    
      Dual simplex          - iterations : 0         time: 0.00    
    Mixed integer           - relaxations: 0         time: 0.00    

------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): -0
 
Done!