% fi=Cm-Ji;
[Oderfi,Indexfi]=sort(fi); %Arranging fi small to bigger
Bestfi=Oderfi(Size); %Let Bestfi=max(fi)
BestS=Kpid(Indexfi(Size),:); %Let BestS=E(m), m is the Indexfi belong to max(fi)
kg
BJ
BestS
%****** Step 2 : Select and Reproduct Operation******
fi_sum=sum(fi);
fi_Size=(Oderfi/fi_sum)*Size;
fi_S=floor(fi_Size); % Selecting Bigger fi value
r=Size-sum(fi_S);
Rest=fi_Size-fi_S;
[RestValue,Index]=sort(Rest);
for i=Size:-1:Size-r+1
fi_S(Index(i))=fi_S(Index(i))+1; % Adding rest to equal Size end
k=1;
for i=Size:-1:1 % Select the Sizeth and Reproduce firstly for j=1:1:fi_S(i)
TempE(k,:)=Kpid(Indexfi(i),:); % Select and Reproduce
k=k+1; % k is used to reproduce end
end
%************ Step 3 : Crossover Operation ************
Pc=0.90;
for i=1:2:(Size-1)
temp=rand;
if Pc>temp %Crossover Condition
alfa=rand;
TempE(i,:)=alfa*Kpid(i+1,:)+(1-alfa)*Kpid(i,:);
TempE(i+1,:)=alfa*Kpid(i,:)+(1-alfa)*Kpid(i+1,:);
end
end