% % Crested Porcupine Optimizer: A new nature-inspired metaheuristic % % %
function [Gb_Fit,Gb_Sol,Conv_curve]=CPO(Pop_size,Tmax,lb,ub,dim,fobj)
%%%%-------------------Definitions--------------------------%%
%%
Conv_curve=zeros(1,Tmax);
ub=ub.*ones(1,dim);
lb=lb.*ones(1,dim);
%%-------------------Controlling parameters--------------------------%%
%%
N=Pop_size; %% Is the initial population size.
N_min=round(0.8*Pop_size); %% Is the minimum population size.
T=2; %% The number of cycles
alpha=0.2; %% The convergence rate
Tf=0.8; %% The percentage of the tradeoff between the third and fourth defense mechanisms
%%---------------Initialization----------------------%%
%%
X=initialization(Pop_size,dim,ub,lb); % Initialize the positions of crested porcupines
t=0; %% Function evaluation counter
%%---------------------Evaluation-----------------------%%
for i=1:Pop_size
fitness(i)=fobj(X(i,:));
end
% Update the best-so-far solution
[Gb_Fit,index]=min(fitness);
Gb_Sol=X(index,:);
%% A new array to store the personal best position for each crested porcupine
Xp=X;
%% Optimization Process of CPO
while t<=Tmax
r2=rand;
for i=1:Pop_size
U1=rand(1,dim)>rand;
if rand<rand %% Exploration phase
if rand<rand %% First defense mechanism
%% Calculate y_t
y=(X(i,:)+X(randi(Pop_size),:))/2;
X(i,:)=X(i,:)+(randn).*abs(2*rand*Gb_Sol-y);
else %% Second defense mechanism
y=(X(i,:)+X(randi(Pop_size),:))/2;
X(i,:)=(U1).*X(i,:)+(1-U1).*(y+rand*(X(randi(Pop_size),:)-X(randi(Pop_size),:)));
end
else
Yt=2*rand*(1-t/(Tmax))^(t/(Tmax));
U2=rand(1,dim)<0.5*2-1;
S=rand*U2;
if rand<Tf %% Third defense mechanism
%%
St=exp(fitness(i)/(sum(fitness)+eps)); % plus eps to avoid division by zero
S=S.*Yt.*St;
X(i,:)= (1-U1).*X(i,:)+U1.*(X(randi(Pop_size),:)+St*(X(randi(Pop_size),:)-X(randi(Pop_size),:))-S);
else %% Fourth defense mechanism
Mt=exp(fitness(i)/(sum(fitness)+eps));
vt=X(i,:);
Vtp=X(randi(Pop_size),:);
Ft=rand(1,dim).*(Mt*(-vt+Vtp));
S=S.*Yt.*Ft;
X(i,:)= (Gb_Sol+(alpha*(1-r2)+r2)*(U2.*Gb_Sol-X(i,:)))-S;
end
end
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.