function [Convergence_curve,Best_Cost,Best_X]=FPA(nP,Max_It,lb,ub,dim,fobj,a,b,p)
%% Initialization
Convergence_curve=zeros(Max_It,1);
X=zeros(nP,dim);
Cost=zeros(nP,1);
for i=1:nP
X(i,:)=unifrnd(lb,ub,1,dim);
Cost(i)=fobj(X(i,:));
end
[Best_Cost,ind] = min(Cost);
Best_X = X(ind,:);
%% Main Loop
for it=1:Max_It
eps = a*exp(-b*it/Max_It);
% Loop over all bats/solutions
for i=1:nP
if rand<p
L = Levy(X(i,:));
dS = L.*(X(i,:)-Best_X);
New_X = X(i,:)+dS;
New_X = min(max(lb,New_X),ub);
else
Rand_ind = randperm(nP);
Rand_ind(Rand_ind==i)=[];
r1 = Rand_ind(1);
New_X = X(i,:)+eps.*(Best_X-X(r1,:));
New_X = min(max(lb,New_X),ub);
end
New_Cost=fobj(New_X);
if New_Cost<Cost(i)
X(i,:) = New_X;
Cost(i) = New_Cost;
if Cost(i)<Best_Cost
Best_X = X(i,:);
Best_Cost = Cost(i);
end
end
end
Convergence_curve(it)=Best_Cost;
% Show Information
disp(['Iteration ' num2str(it) ': BestCost = ' num2str(Convergence_curve(it))]);
end