function [BEST,fitBEST] = Barnacles_Mating_Optimizer (Func_name,N,Lower,Upper,Maxiter)
  
fobj = TestFunctions (Func_name); 
D=length(Lower);
Positions=zeros(N,D);
Pos_new=zeros(N,D);
Barnacles=zeros(2*N,D);
fitness=zeros(1,N);
 
pop1=zeros(1,N);
pop2=zeros(1,N);
difference=zeros(1,N);
pop1new=zeros(N,D);
pop2new=zeros(N,D);

BEST=zeros(1,D); 

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

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

iter=1;

while(iter <= Maxiter)

  pop1=randperm(N);
  pop2=randperm(N);
     
     for i=1:N
         pop1new(i,:)=Positions(pop1(i),:);
         pop2new(i,:)=Positions(pop2(i),:);
     end
 
     for i=1:N
         difference(i)=abs(pop1(i)-pop2(i));
     end
  
     t=0;
     for i=1:N
       if (difference(i)>7)
         t=t+1;
       endif
     endfor
     
     over7=zeros(1,t);
     m=1;
     for i=1:t
       if (difference(i)>7)
         over7(m)=i;
         m=m+1;
       endif
     endfor
     
     for i=1:N
       for j=1:D
         Pos_new(i,j)=randn*pop1new(i,j)+(1-randn)*pop2new(i,j);
       endfor
     endfor
     
     o7length=length(over7);
     
     for i=1:o7length
       if(over7(i) != 0.0)
          Pos_new(over7(i),:)=pop2new(over7(i),:)*rand;
       end
     end
     
     Barnacless=zeros(N+o7length,D);
     for i=1:N
       for j=1:D
         Barnacless(i,j)=Positions(i,j);
       end
     end
     tt=1;
     for i=N:(N+o7length)
        Barnacless(i,:)=Pos_new(tt,:);
        tt=tt+1;
     end
     
     Barnacless=boundary(Barnacless, Lower,Upper,N,D);
     
     Barnacless = sortX (Barnacless,fobj,(N+o7length),D);
     for i=1:N
       Positions(i,:)=Barnacless(i,:);
     end
     
     BEST=Positions(1,:);
     fitBEST=fobj(BEST);
     iter=iter+1;
end         
     
  
  

endfunction
