function [xnew,fnew,FCounter] = LocalSearch(n,f,x,fv,MaxLocalItr)
% Intial parameters
ItrC=0;
FCounter=0;
rho=1; chi=2; gamma=0.5; sigma=0.5;
FCheck=0;
IFlg=0;
while(ItrC <= MaxLocalItr & IFlg==0)
    ItrC=ItrC+1;    
% reflect
%
    xx(1,:,:)=x; ffv(1,:)=fv;
    xbar=zeros(1,1,n);
    for i=1:n;
        for k=1:n; xbar(1,1,i)=xbar(1,1,i)+xx(1,k,i); end
    end
    xbar = xbar/n;  % centriod of the n best vertices
    xr=(1 + rho)*xbar-rho*xx(1,n+1,:);
    fr=feval(f,xr); FCounter=FCounter+1;
    if(fr >= ffv(1,1) & fr < ffv(1,n)); FCheck = 1; xn=xr; fn=fr; end;
%
% expand
%
    if(FCheck == 0 & fr < ffv(1,1))
        xe = (1 + rho*chi)*xbar - rho*chi*xx(1,n+1,:);
        fe=feval(f,xe); FCounter=FCounter+1;
        if(fe < fr) xn=xe;  fn=fe; FCheck=1; end
        if(fe >=fr) xn=xr;  fn=fr; FCheck=1; end
%
    end
%
% contract
%
    if(FCheck == 0 & fr >= ffv(1,n) & fr < ffv(1,n+1))
%
% outside contraction
%
        xc=(1 + rho*gamma)*xbar - rho*gamma*xx(1,n+1,:);
        fc=feval(f,xc); FCounter=FCounter+1;
        if(fc <= fr) xn=xc; fn=fc; FCheck=1; end;
    end
%
% inside contraction
%
    if(FCheck == 0 & fr >= ffv(n+1))      
        xc=(1 - gamma)*xbar+gamma*xx(1,n+1,:);
        fc=feval(f,xc); FCounter=FCounter+1;
        if(fc < ffv(1,n+1)) FCheck=1; xn=xc; fn=fc; end;   
    end
%
% shrink
%
    if(FCheck==1)
        xx(1,n+1,:)=xn; ffv(1,n+1)=fn;
    end
    if(FCheck == 0)       
        for k=2:n+1;
            xx(1,k,:)=xx(1,1,:)+sigma*(xx(1,k,:)-xx(1,1,:));
            ffv(1,k)=feval(f,xx(1,k,:));
        end
        FCounter=FCounter+n;
        IFlg=1;
    end
    [fs,is]=sort(ffv(1,:)); 
    xtmp=xx(1,is,:); xx(1,:,:)=xtmp; ffv(1,:)=fs;
end
xnew=xx(1,:,:);
fnew=ffv(1,:);