 

function [Best_X, Best_cost] = Gradient_Based_Optimizer (Func_name,N,Lower,Upper,Maxiter)

fobj = TestFunctions (Func_name); 
D=length(Lower); 
Cost=zeros(1,N);  
X=zeros(N,D);
X1=zeros(1,D);
X2=zeros(1,D);
XNEW=zeros(1,D);
Best_X=zeros(1,D);
Worst_X=zeros(1,D);
Xk=zeros(1,D); 
A1=zeros(1,4);
DM=zeros(1,D); 
Xp=zeros(1,D);
GSR=zeros(1,D);


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

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

for i=1:N
  Cost(i)=fobj(X(i,:));
end

Best_X=X(1,:);
Worst_X=X(N,:);
Best_cost=fobj(Best_X);
Worst_cost=fobj(Worst_X);
  
    iter=1;
    
    while(iter <= Maxiter)
      
         beta=0.2+((1.0-((iter./Maxiter).^3.0)).^2.0);    
         alpha=abs(beta.*sin(3.0.*3.141592./2+sin(beta.*3.*3.141592./2)));
         
         for i=1:N
           
             for t=1:4
               A1(t)=ceil(N*rand);
             end
             r1=A1(1);r2=A1(2);r3=A1(3);r4=A1(4);
             Xm=(X(r1,:)+X(r2,:)+X(r3,:)+X(r4,:))./4;
             ro=alpha.*(2.0.*rand-1.0);
             ro1=alpha.*(2.0.*rand-1.0);
             eps=rand.*5e-3;
             DM=rand.*ro.*(Best_X-X(r1,:));
             Flagg=round(rand);
             GSR=GradientSearchRule(ro1,Best_X,Worst_X,X(i,:),X(r1,:),DM,eps,Xm,Flagg);
             DM=rand.*ro.*(Best_X-X(r1,:));
             X2=(Best_X-GSR+DM); 
             ro=alpha.*(2.0.*rand-1.0);
             X3=X(i,:)-ro.*(X2-X1);
             ra=rand;rb=rand;
             XNEW=ra.*(rb.*X1+(1-rb).*X2)+(1-ra).*X3;
             
             if(rand < 0.5)
                 k=ceil(rand.*N);
                 f1=-1.0+(2.0).*rand;
                 f2=-1.0+(2.0).*rand;
                 Xk=(Upper-Lower).*rand+Lower;
                 mu=rand;
                 
                 if(mu<0.5)
                   u1=2*rand;
                 else
                   u1=1.0;
                 end  
             
                 if(mu<0.5)
                   u2=rand;
                 else
                   u2=1.0;
                 end 
                 
                 if(mu<0.5)
                   u3=rand;
                 else
                   u3=1.0;
                 end
                 
                 mu2=rand;
                 if(mu2<rand)
                    Xp=X(k,:);
                 else
                    Xp=Xk;
                 end
               
                 if(u1<0.5)
                    XNEW=XNEW+f1.*(u1.*Best_X-u2.*Xp)+f2.*ro.*(u3.*(X2-X1)+u2.*(X(r1,:)-X(r2,:)))./2.0;
                 else
                    XNEW=Best_X+f1.*(u1.*Best_X-u2.*Xp)+f2.*ro.*(u3.*(X2-X1)+u2.*(X(r1,:)-X(r2,:)))./2.0;
                 end 
             end
             
             for j=1:D
               if((XNEW(j)<Lower(j))||(XNEW(j)>Upper(j)))
                  XNEW(j)=(Upper(j)-Lower(j))*rand+Lower(j);
                end
             end
             
             XNEW_Cost=fobj(XNEW);
             if(XNEW_Cost<Cost(i))
                 X(i,:)=XNEW;
                 Cost(i)=XNEW_Cost;
                 if (Cost(i)<Best_cost)
                    Best_X=X(i,:);
                    Best_cost=Cost(i); 
                 end
             end
             
             if(Cost(i)>Worst_cost)
                Worst_X=X(i,:);
                Worst_cost=Cost(i);
             end
           
         end
    iter=iter+1;
    
    end
       
    
endfunction

