%
%  SVM Classification 2D examples
%
%  15/06/00 AR
function [] = svm_al(X,Y)
% Inputs: X - matrix of data points, in the form [x1 x2 ... xn; y1 y2 ... yn]'
%         Y - matrix of data point labels [l1 l2 ... ln]'
%close all
%clear all
%-------------Data Set-------------------------------------
nbapp=10;
nbtest=0;
sigma=2;
if nargin==0
    X = [-12, -7, 1, 3, 5; -1, -5, -9, 10, 12]'/5;
    Y = [1,1,1,-1,-1]';
end
fprintf(1, 'Press space to choose a new point; Ctrl+c to exit.\n');

% CS 101.2 -- comment this line out if you want a gaussian data distribution
xapp=X;yapp=Y;
plot(xapp(:,1),xapp(:,2),'+k'); 
axis([-4 4 -4 4]);
pause
%-----------------------------------------------------
%   Learning and Learning Parameters
c = inf;
epsilon = .000001;

% CS 101.2 -- change this to change the polynomial kernel's degree
kerneloption= 1;
kernel='poly';
verbose=0;

%Take one point from each class and get a decision boundary
[a,pli]=max(yapp);
[a,mii]=min(yapp);
xs = [xapp(pli,:);xapp(mii,:)];
ys = [1,-1]';
rem = ones(size(xapp,1),1);rem(pli)=0;rem(mii)=0;
xapp = xapp(rem==1,:);
yapp = yapp(rem==1,:);
[xsup,w,b,pos,t,alp]=svmclass(xs,ys,c,epsilon,kernel,kerneloption,verbose);
%--------------Testing Generalization performance ---------------
[xtesta1,xtesta2]=meshgrid([-4:0.1:4],[-4:0.1:4]);
[na,nb]=size(xtesta1);
xtest1=reshape(xtesta1,1,na*nb);
xtest2=reshape(xtesta2,1,na*nb);
xtest=[xtest1;xtest2]';
ypred = svmval(xtest,xsup,w,b,kernel,kerneloption);
ypredmat=reshape(ypred,na,nb);
%----------------------Plotting---------------------------------

ind1=find(ypred>0);
indm1=find(ypred<0);

contourf(xtesta1,xtesta2,ypredmat,50);shading flat;
hold on
[cs,h]=contour(xtesta1,xtesta2,ypredmat,[-1 0 1],'k');
clabel(cs,h);
h1=plot(xapp(yapp==1,1),xapp(yapp==1,2),'+k'); 
set(h1,'LineWidth',2);

h2=plot(xapp(yapp==-1,1),xapp(yapp==-1,2),'xk'); 
set(h2,'LineWidth',2);
h3=plot(xsup(:,1),xsup(:,2),'ok'); 
set(h3,'LineWidth',2);



xlabel('x1');ylabel('x2');
title('Learning Data and Margin ');
axis([-4 4 -4 4]);
pause

% Enter a loop: for each of the remaining unlabeled points,
% compute its margins for its + and - version spaces. Choose
% the points with the greatest min(m+, m-) and actually
% request its label.
while 1==1
    clf
    count = 0;
    dists = [];
    for point = xapp'
        % CS 101.2 -- this is the part to change to get Ratio or MaxMin
        count = count + 1;
        % Get the distance
        dist = abs(svmval(point', xsup, w, b, kernel, kerneloption));
        dists = [dists dist];
    end
    % Choose the point with the greatest smallest margin and actually classify it
    [a,choice] = min(dists);
    xs = [xs; xapp(choice,:)];
    ys = [ys; yapp(choice)];
    rem = ones(size(xapp,1),1);rem(choice)=0;
    xapp = xapp(rem==1,:);
    yapp = yapp(rem==1,:);
    [xsup,w,b,pos,t,alp]=svmclass(xs,ys,c,epsilon,kernel,kerneloption,verbose);

    %--------------Testing Generalization performance ---------------
    [xtesta1,xtesta2]=meshgrid([-4:0.1:4],[-4:0.1:4]);
    [na,nb]=size(xtesta1);
    xtest1=reshape(xtesta1,1,na*nb);
    xtest2=reshape(xtesta2,1,na*nb);
    xtest=[xtest1;xtest2]';
    ypred = svmval(xtest,xsup,w,b,kernel,kerneloption);
    ypredmat=reshape(ypred,na,nb);
    %----------------------Plotting---------------------------------

    ind1=find(ypred>0);
    indm1=find(ypred<0);

    contourf(xtesta1,xtesta2,ypredmat,50);shading flat;
    hold on
    [cs,h]=contour(xtesta1,xtesta2,ypredmat,[-1 0 1],'k');
    clabel(cs,h);
    h1=plot(X(Y==1,1),X(Y==1,2),'+k'); 
    set(h1,'LineWidth',2);

    h2=plot(X(Y==-1,1),X(Y==-1,2),'xk'); 
    set(h2,'LineWidth',2);
    h3=plot(xsup(:,1),xsup(:,2),'ok'); 
    set(h3,'LineWidth',2);



    xlabel('x1');ylabel('x2');
    title('Learning Data and Margin ');
    axis([-4 4 -4 4]);
    pause;
end
