%GApartition.m
clear all 
close all 
%Identifying the line that %divides the two class as follows.
x=randn(2,50)+2; 
y=randn(2,50)-2; 
z=[x y];
plot(x(1,:),x(2,:),'r*')
hold on
plot(y(1,:),y(2,:),'k*')
t=[ones(1,50) ones(1,50)*(-1)];
%Generate data 
IV=[]; 
nod=5;
for i=1:1:nod
    w{i}=rand(1,2)*10-5;
    b(i)=rand*10-5;
    IV=[IV fga(w{i},z,b(i),t')];
end
[A,B]=max(IV);
BESTVAL=[A];
BESTSOL=[w{B} b(B)]; 
index=1; 
for iteration=1:1:5 
    F=[];
    %Cross-over 
    for i=1:2:2*nod 
        r1=round(rand*(nod-1))+1; 
        r2=round(rand*(nod-1))+1;
        r=rand;
        DATAW{i}=r*w{r1}+(1-r)*w{r2};
        DATAW{i+1}=(1-r)*w{r1}+r*w{r2};
        DATAb(i)=r*b(r1)+(1-r)*b(r2);
        DATAb(i+1)=(1-r)*b(r1)+r*b(r2); 
        F=[F fga(DATAW{i},z,DATAb(i),t') fga(DATAW{i+1}, z,DATAb(i+1),t')] 
    end
%Selection from the generated data %Formulation of Roulette wheel 
F=F-min(min(F)); 
F=F/(sum(F)+eps); 
F=cumsum(F); 
VAL=[]; 
for i=1:1:nod
[P,Q]=find((F-rand)>0);
if(isempty(Q)==1) 
    Q=1; 
end
w{i}=[DATAW{Q(1)}]; 
b(i)=DATAb(Q(1));
VAL=[VAL fga(w{i},z,b(i),t')];
end
[M,N]=max(VAL);
if(BESTVAL(index)<M)
    index=index+1;
    BESTVAL=[BESTVAL M];
    BESTSOL=[BESTSOL;w{N} b(N)];
end
end
INITIALSOL=BESTSOL(1,:);
winitial=INITIALSOL(1:2); 
binitial=INITIALSOL(3); 
x1=-5:0.1:5;
y1=(ones(1,length(x1))-winitial(1)*x1-ones(1,length(x1))* binitial(1))/winitial(1);
hold on 
plot(x1,y1,'b-')
FINALSOL=BESTSOL(size(BESTSOL,1),:);
wfinal=FINALSOL(1:2);
bfinal=FINALSOL(3);
x1=-5:0.1:5; 
y1=(ones(1,length(x1))-wfinal(1)*x1-ones(1,length(x1))* bfinal(1))/wfinal(1);
hold on 
plot(x1,y1,'r-') 
figure 
plot(1./BESTVAL)
