 

function [FIRST,FIRSTVAL] = Group_Teaching_Optimization_Algorithm (Func_name,N,Lower,Upper,Maxiter)

 
 
fobj = TestFunctions (Func_name); 
D=length(Lower);
X=zeros(N,D);
XTEACHER=zeros(N/2,D);
XSTUDENT=zeros(N/2,D);
MEANT=zeros(1,D);
MEANX=zeros(1,D);
T=zeros(1,D);
FIRST=zeros(1,D);
SECOND=zeros(1,D);
THIRD=zeros(1,D);
GOOD=zeros(N/2,D);
AVERAGE=zeros(N/2,D);

  
for i=1:N
  for j=1:D
    X(i,j)=Lower(j)+(Upper(j)-Lower(j))*rand;
  end
end

X = sortX (X,fobj,N,D);

FIRST=X(1,:);SECOND=X(2,:);THIRD=X(3,:);
MEANT=(FIRST+SECOND+THIRD)./3;


if(fobj(FIRST)<=fobj(MEANT))
   T=FIRST;
elseif (fobj(FIRST)>fobj(MEANT))
   T=MEANT;
end

for i=1:N/2
  GOOD(i,:)=X(i,:);  
end

for i=((N/2)+1):N
  AVERAGE(i-(N/2),:)=X(i,:);  
end

MEANX=mean(X);

iter=1;

while(iter <= Maxiter)
   
   for i=1:N/2
     a=rand;b=rand;c=1-b;F=round(1+rand);
     XTEACHER(i,:)=GOOD(i,:)+a.*(T-F.*(b.*MEANX+c.*GOOD(i,:)));
   end
   
   XTEACHER = boundary (XTEACHER, Lower,Upper,N/2,D);

   for i=1:N/2
       if(fobj(XTEACHER(i,:)) >= fobj(GOOD(i,:)))
          XTEACHER(i,:)=GOOD(i,:);
       end
   end

   for i=1:(N/2)
     jj=ceil((N/2)*rand);
     while (i==jj)
         jj=ceil((N/2)*rand);
     end
     if (fobj(XTEACHER(i,:)) < fobj(XTEACHER(jj,:)))
        XSTUDENT(i,:)=XTEACHER(i,:)+rand.*(XTEACHER(i,:)-XTEACHER(jj,:))+rand.*(XTEACHER(i,:)-GOOD(i,:));
     elseif (fobj(XTEACHER(i,:)) >= fobj(XTEACHER(jj,:)))
        XSTUDENT(i,:)=XTEACHER(i,:)-rand.*(XTEACHER(i,:)-XTEACHER(jj,:))+rand.*(XTEACHER(i,:)-GOOD(i,:));
     end
  end  
     XSTUDENT = boundary (XSTUDENT, Lower,Upper,N/2,D);
     
     for i=1:(N/2)
       if (fobj(XTEACHER(i,:)) < fobj(XSTUDENT(i,:)))
         X(i,:)=XTEACHER(i,:);
       elseif (fobj(XTEACHER(i,:)) >= fobj(XSTUDENT(i,:)))
         X(i,:)=XSTUDENT(i,:);
       end
     end   
     
     for i=1:N/2
       XTEACHER(i,:)=AVERAGE(i,:)+2.*rand.*(T-AVERAGE(i,:));
     end
     
     XTEACHER = boundary (XTEACHER, Lower,Upper,N/2,D);
   
    for i=1:N/2
       if(fobj(XTEACHER(i,:)) >= fobj(AVERAGE(i,:)))
          XTEACHER(i,:)=AVERAGE(i,:);
       end
    end
   
   for i=1:(N/2)
     jj=ceil((N/2)*rand);
     while (i==jj)
         jj=ceil((N/2)*rand);
     end
     if (fobj(XTEACHER(i,:)) < fobj(XTEACHER(jj,:)))
        XSTUDENT(i,:)=XTEACHER(i,:)+rand.*(XTEACHER(i,:)-XTEACHER(jj,:))+rand.*(XTEACHER(i,:)-AVERAGE(i,:));
     elseif (fobj(XTEACHER(i,:)) >= fobj(XTEACHER(jj,:)))
        XSTUDENT(i,:)=XTEACHER(i,:)-rand.*(XTEACHER(i,:)-XTEACHER(jj,:))+rand.*(XTEACHER(i,:)-AVERAGE(i,:));
     end
   end  
   
       XSTUDENT = boundary (XSTUDENT, Lower,Upper,N/2,D);
       
     for i=1:(N/2)
       if (fobj(XTEACHER(i,:)) < fobj(XSTUDENT(i,:)))
         X(i+(N/2),:)=XTEACHER(i,:);
       elseif (fobj(XTEACHER(i,:)) >= fobj(XSTUDENT(i,:)))
         X(i+(N/2),:)=XSTUDENT(i,:);
       end
     end 
   
   X = sortX (X,fobj,N,D);

   FIRST=X(1,:);SECOND=X(2,:);THIRD=X(3,:);
   MEANT=(FIRST+SECOND+THIRD)./3;


   if(fobj(FIRST)<=fobj(MEANT))
      T=FIRST;
   elseif (fobj(FIRST)>fobj(MEANT))
      T=MEANT;
   end

   for i=1:N/2
      GOOD(i,:)=X(i,:);  
   end

   for i=((N/2)+1):N
       AVERAGE(i-(N/2),:)=X(i,:);  
   end

   MEANX=mean(X);
   
   FIRSTVAL=fobj(FIRST);
   
   iter=iter+1;
 
end

FIRST
FIRSTVAL
 
endfunction
