function [XMin,FMin,FCount] = SCGA(n,f,U,L)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%           Simplex Coding Genetic Algorithm             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% SCGA = Simplex Coding Genetic Algorithm 
% The DSSA Code is created in March 2002 by A. Hedar 
% Modified on Aug. 13, 2005
% 
% Reference:
%       A. Hedar and M. Fukushima, "Simplex coding genetic algorithm for  
%       for the global optimization of nonlinear functions",  Optimization 
%       Methods and Software 18 (2003) 265-282.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%           function [XMin,FMin,FCount] = SCGA(n,f,U,L)              %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Inputs:
%   Number of the objective variables = n
%   Objective function = f
%   n-Dimension Vector of upper limits of the objective variables = U
%   n-Dimension Vector of lower limits of the objective variables = L
%
% Outputs:
%   Best point obtained by SCGA = XMin
%   Best function value obtained by SCGA = FMin
%	Number of function evaluations = FCount                   
%
%%%%%%%%%%%%%%%%%% Intial Parameters %%%%%%%%%%%%%%%%%%%%
%
m=3;                            % m= Number of simplices per coordinate direction
NoIPop=m^n;                     % Number of the population members
EdgL=0.05*min(U-L);             % Simplex edge length
MaxLocalItr=2;                  % Number of max number of local itrations
FCount=0;                       % Number of function evaluations
tol =1.0D-3;                    % Termanation acurcy
%
%%%%%%%%%%%%%%%%%%% GA Parameters %%%%%%%%%%%%%%%%%%%%%%%
%
EtaMax=1.1;
EtaMin=2-EtaMax;
CProb=0.6;
MProb=0.01;
NoGen=0;
MaxNoGen=min(60,10*n);
%
%%%%%%%%%% Generation of the initial Population %%%%%%%%%
%
% % Generation of the initial main vertix
y=zeros(NoIPop,n+1,n);
for i=1:n;
    y(1,1,i)=L(i)+rand*(U(i)-L(i))/(2*m);
end
Index=1;
idn=eye(n);
for k=1:n;
	for j=1:m;       
        for i=1:(m+1)^(k-1);            
            Index=Index+1;
            for l=1:n; y(Index,1,l)=y(i,1,l)+idn(k,l)*j*(U(l)-L(l))/(2*(m-1)); end
        end
	end
end
if n>=10;
    PopulNo=0;
    for j=1:25:NoIPop;
        PopulNo=PopulNo+1;
        y(PopulNo,1,:)=y(j,1,:);
    end
    NoIPop=PopulNo
end
for j=1:NoIPop;
    id(j,:,:)=eye(n);
    for k=2:n+1;
        sn=(-1)^(round(rand));
        y(j,k,:)=y(j,1,:)+sn*EdgL*id(j,k-1,:);
    end
end
% Local search for the intial population 
for j=1:NoIPop;
    for k=1:n+1;
        fy(j,k)=feval(f,y(j,k,:));
    end
    FCount=FCount+n+1;
end;
for j=1:NoIPop;
    [fs,is]=sort(fy(j,:)); 
    ytmp=y(j,is,:);
    y(j,:,:)=ytmp; fy(j,:)=fs;
end
for j=1:NoIPop;
    [ynew,fnew,FCounter] = LocalSearch(n,f,y(j,:,:),fy(j,:),MaxLocalItr);  
    FCount=FCount+FCounter;
    y(j,:,:)=ynew; fy(j,:)=fnew;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%% Genetic Algorithm Part %%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Order the Population
[fs,is]=sort(fy(:,1)); 
ytmp=y(is,:,:); y=ytmp; 
ftmp=fy(is,:); fy=ftmp;
%Reduce the Population
if (n>2 & NoIPop>4*n);
    NoPop=4*n;
    x=zeros(NoPop,n+1,n);
    for j=1:NoPop;
        x(j,:,:)=y(j,:,:);
        fv(j,:)=fy(j,:);
    end
else
    NoPop=NoIPop;
    x=y; fv=fy;
end
for j=1:NoPop;
    P(j)=(EtaMax-(EtaMax-EtaMin)*(j-1)/(NoPop-1))/NoPop;
end
Sum=0;
for j=NoPop:-1:1;
    Sum=Sum+P(j);
    Prob(j)=Sum;
end
%
%%%%%%%%%%%%%% GA Main Loop %%%%%%%%%%%%%%
%
while (NoGen<MaxNoGen & abs(fv(1,1)-fv(1,n+1))>tol )
    NoGen=NoGen+1;
%
%%%%%%%%%%%%%%% Selection %%%%%%%%%%%%%%%%
%
	RandNums=sort(rand(NoPop,1));
    z=zeros(NoPop,n+1,n);
%Select individuals from the oldPop to the new
    NewIn=1; ChrNo=NoPop;
    while NewIn<=NoPop
        if(RandNums(NewIn)<Prob(ChrNo))            
            z(NoPop-NewIn+1,:,:) = x(ChrNo,:,:);
            NewIn = NewIn+1;
        else
            ChrNo = ChrNo - 1;
        end
	end 
%
%%%%%%%%%%%%%%% Crossover %%%%%%%%%%%%%%%%
%    
	RandNumsC=rand(NoPop,1);
    NoC=0;
    for j=1:NoPop;
        if RandNumsC(j)<=CProb; NoC=NoC+1; end
    end
    NoCIn=1; CIn=1;
    while CIn<NoC-n-1;
        NoChd=round(rand*(n-1))+2;       
        Child=zeros(NoChd,n+1,n);
        fvv=zeros(NoChd,n+1);
        ParIn=zeros(NoChd,1);
        IFlg=0;
        jj=1;
        while IFlg<NoChd;
            if RandNumsC(NoCIn)<=CProb;         
                ParIn(jj)=NoCIn;
                NoCIn=NoCIn+1;
                jj=jj+1;
                IFlg=IFlg+1;                
            else
                NoCIn=NoCIn+1;
            end
        end
        v=zeros(NoChd-1,n); nv=zeros(NoChd-1,1); 
        for j=2:NoChd;            
            for i=1:n; v(j-1,i)=z(ParIn(j),1,i)-z(ParIn(1),1,i); end            
            nv(j-1)=norm(v(j-1,:));
        end
        avx=zeros(1,n+1,n);
        for j=1:NoChd;
            avx(1,:,:)=avx(1,:,:)+z(ParIn(j),:,:);
        end
        avx=avx/NoChd;
        Dist=max(nv);
        for j=1:NoChd;
            Angls=pi*rand(n-1,1); Angls(n,1)=0;
            Prod=1;
            for i=1:n;
                Direction(1,1,i)=Prod*cos(Angls(i));
                Prod=Prod*sin(Angls(i));
            end
            for k=1:n+1; 
                sn=rand*max(Dist)*(-1)^(round(rand));
                Child(j,k,:)=avx(1,k,:)+sn*Direction(1,1,:);                                
            end
%
%%%%%%%%%%%%%% Mutation %%%%%%%%%%%%%%%%
%        
            if rand/(n+1)<MProb;
                NoGene=round(rand*n)+1;
                xbar=zeros(1,1,n);
                for i=1:n;
                    for k=1:n+1; xbar(1,1,i)=xbar(1,1,i)+Child(j,k,i); end
                end
                xbar(1,1,:)=xbar(1,1,:)-Child(j,NoGene,:);
                xbar = xbar/n;
                Child(j,NoGene,:)=xbar+(rand+0.5)*(xbar-Child(j,NoGene,:));
            end        
            for k=1:n+1;
                fvv(j,k)=feval(f,Child(j,k,:));             
            end
            FCount=FCount+n+1;        
            [fcs,ics]=sort(fvv(j,:)); 
            ctmp=Child(j,ics,:);
            Child(j,:,:)=ctmp; fvv(j,:)=fcs;
            [xnew,fnew,FCounter] = LocalSearch(n,f,Child(j,:,:),fvv(j,:),MaxLocalItr);  
            FCount=FCount+FCounter;
            Child(j,:,:)=xnew; fvv(j,:)=fnew;
            if fvv(j,1)<fv(NoPop,1)
                x(NoPop,:,:)=Child(j,:,:);
                fv(NoPop,:)=fvv(j,:);
                [fs,is]=sort(fv(:,1)); 
                xtmp=x(is,:,:); x=xtmp; 
                ftmp=fv(is,:); fv=ftmp;
            end            
        end        
        CIn=CIn+NoChd;
	end
%
%%%%%%%%%%%%%% Reduction of the Population%%%%%%%%%%%
%
	M=mod(NoGen,3*n);
	if M==0 & NoPop>2*n;
        NoPop=NoPop-n;       
	end  
end
%
%%%%%%%%%%%%% Improve the best point %%%%%%%%%%%%%%%%
%
FMin=fv(1,1);
XMin=x(1,1,:);
for j=1:1;
	v=x(j,1,:);
    [x1,FCoun,f1]=NeMe(n,f,v);
	if f1 < FMin
        XMin=x1(:,1);
        FMin=f1;   
	end
    FCount=FCount+FCoun;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%% END %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%