%seoakmeans.m
X1=randn(1,20)*0.1+1;
X2=randn(1,20)*0.1+2;
X3=randn(1,20)*0.1+3;
X=[X1 X2 X3];
EI=ones(1,200);
%EI indicates Emotion Index %Initialize population 
for i=1:1:200 
    M{i}=rand(1,3)*5;
end
BESTCOL=[]; 
BESTVAL=[];
VAL=[]; 
for i=1:1:200
    VAL=[VAL socialalgo(X,M{i})];
end
[P,Q]=sort(VAL);
L=3;
GLOBALBEST=M{Q(1)};
GLOBALBESTVAL=P(1);
%Obtain the next population. 
for i=1:1:200 
    M1{i}=M{i}-(1/10)*rand*(M{i}-M{Q(100)})-(1/10)*rand*(M{i}-M{Q(99)})-(1/10)*rand*(M{i}-M{Q(98)}) 
end
for i=1:1:200 
    if (socialalgo(X,M1{i})<socialalgo(X,M{i})) 
        LOCALBEST{i}=M1{i};
    else
        LOCALBEST{i}=M{i}; 
    end
end
for iteration=1:1:50
    for i=1:1:200
        if(socialalgo(X,M1{i})>socialalgo(X,M{i}))
            EI(i)=EI(i)-0.01;
        end
        if(socialalgo(X,M1{i})<GLOBALBESTVAL) 
            EI(i)=1;
        end
    end
VAL=[]; 
for i=1:1:200 
    VAL=[VAL socialalgo(X,M1{i})]; 
end
[P,Q]=sort(VAL);
if(VAL(Q(1))<socialalgo(X,GLOBALBEST))
    GLOBALBEST=M1{Q(1)}; 
    GLOBALBESTVAL=VAL(Q(1)); 
end
for i=1:1:200
    if (socialalgo(X,M1{i})<LOCALBEST{i})
        LOCALBEST{i}=M1{i}
    end
end
for i=1:1:200 
    if(EI(i)<0.25)
        M2{i}=M1{i}+(1/10)*rand*(M1{i}-GLOBALBEST);
    elseif (0.25<EI(i)<0.75) 
        M2{i}=M1{i}+(1/10)*rand*(M1{i}-GLOBALBEST)+(1/10)*rand*(M1{i}-LOCALBEST{i})-(1/10)*rand*(M1{i}-M1{Q(100)})-(1/10)*rand*(M1{i}-M1{Q(99)})-(1/10)*rand*(M1{i}-M1{Q(98)});
    else
        M2{i}=M1{i}+(1/10)*rand*(M1{i}-LOCALBEST{i}); 
    end
end
M=M1; 
M1=M2;
BESTVAL=[BESTVAL GLOBALBESTVAL];
BESTSOL{iteration}=GLOBALBEST;
end
FINALSOL=BESTSOL{50};
G=[];
for i=1:20:60
    [p,q]=min((repmat(X(i),1,length(FINALSOL))-FINALSOL).^2);
    G=[G q];
end
figure
plot(X1,'r*-')
hold on 
plot(X2,'b*-') 
plot(X3,'k*-') 
plot(ones(1,length(X1))*FINALSOL(G(1)),'rd-') 
plot(ones(1,length(X2))*FINALSOL(G(2)),'bd-')
plot(ones(1,length(X3))*FINALSOL(G(3)),'kd-')
plot(ones(1,length(X1))*1,'r-') 
plot(ones(1,length(X2))*2,'b-')
plot(ones(1,length(X3))*3,'k-') 
