01.引言
基于领导者优化的哈里斯鹰优化算法leader Harris hawks optimization (LHHO),以增强Harris hawks optimization (HHO)的搜索能力。通过在探索阶段的适应性栖息和每一代哈里斯鹰的基于领导者的突变选择,可以实现更多的探索。使用经典的23个基准函数对LHHO的性能进行了评估,发现LHHO的性能优于HHO。
02.代码流程图
03.部分代码
%% The Leader Harris hawks optimization (LHHO) function
function [Rabbit_Energy,Rabbit_Location,CNVG]=LHHO(N,T,lb,ub,dim,fobj)
% disp('HHO is now tackling your problem')
%tic
% initialize the location and Energy of the rabbit
Rabbit_Location=zeros(1,dim);
Rabbit_Energy=inf;
fitnessAll=zeros(N,1);
%Initialize the locations of Harris' hawks
X=initialization(N,dim,ub,lb);
CNVG=zeros(1,T);
t=0; % Loop counterfor i=1:size(X,1)% Check boundriesFU=X(i,:)>ub;FL=X(i,:)<lb;X(i,:)=(X(i,:).*(~(FU+FL)))+ub.*FU+lb.*FL;% fitness of locationsfitness=fobj(X(i,:));fitnessAll(i,1)=fitness;% Update the location of Rabbitif fitness<Rabbit_EnergyRabbit_Energy=fitness;Rabbit_Location=X(i,:);endend
while t<Tsortedfitness=sort(fitnessAll);E1=2*(1-(t/T)); % factor to show the decreaing energy of rabbit% Update the location of Harris' hawksfor i=1:size(X,1)E0=2*rand()-1; %-1<E0<1%E0=rand;Escaping_Energy=E1*(E0); % escaping energy of rabbitpepr=(abs(fitnessAll(i)-sortedfitness(1)))/abs((sortedfitness(end)-sortedfitness(1))); %adaptive perch probabilityif abs(Escaping_Energy)>=1%% Exploration:% Harris' hawks perch randomly based on 2 strategy:q=rand();rand_Hawk_index = floor(N*rand()+1);X_rand = X(rand_Hawk_index, :);if q<pepr% perch based on other family membersX(i,:)=(Rabbit_Location(1,:)-mean(X))-rand()*((ub-lb)*rand+lb);elseif q>=pepr% perch on a random tall tree (random site inside group's home range)X(i,:)=X_rand-rand()*abs(X_rand-2*rand()*X(i,:));endelseif abs(Escaping_Energy)<1%% Exploitation:% Attacking the rabbit using 4 strategies regarding the behavior of the rabbit%% phase 1: surprise pounce (seven kills)% surprise pounce (seven kills): multiple, short rapid dives by different hawksr=rand(); % probablity of each eventif r>=0.5 && abs(Escaping_Energy)<0.5 % Hard besiegeX(i,:)=(Rabbit_Location)-Escaping_Energy*abs(Rabbit_Location-X(i,:));endif r>=0.5 && abs(Escaping_Energy)>=0.5 % Soft besiegeJump_strength=2*(1-rand()); % random jump strength of the rabbitX(i,:)=(Rabbit_Location-X(i,:))-Escaping_Energy*abs(Jump_strength*Rabbit_Location-X(i,:));end%% phase 2: performing team rapid dives (leapfrog movements)if r<0.5 && abs(Escaping_Energy)>=0.5, % Soft besiege % rabbit try to escape by many zigzag deceptive motionsJump_strength=2*(1-rand());X1=Rabbit_Location-Escaping_Energy*abs(Jump_strength*Rabbit_Location-X(i,:));FU=X1>ub;FL=X1<lb;X1=(X1.*(~(FU+FL)))+ub.*FU+lb.*FL;if fobj(X1)<fobj(X(i,:)) % improved move?X(i,:)=X1;else % hawks perform levy-based short rapid dives around the rabbitX2=Rabbit_Location-Escaping_Energy*abs(Jump_strength*Rabbit_Location-X(i,:))+rand(1,dim).*Levy(dim);FU=X2>ub;FL=X2<lb;X2=(X2.*(~(FU+FL)))+ub.*FU+lb.*FL;if (fobj(X2)<fobj(X(i,:))), % improved move?X(i,:)=X2;endendendif r<0.5 && abs(Escaping_Energy)<0.5, % Hard besiege % rabbit try to escape by many zigzag deceptive motions% hawks try to decrease their average location with the rabbitJump_strength=2*(1-rand());X1=Rabbit_Location-Escaping_Energy*abs(Jump_strength*Rabbit_Location-mean(X));FU=X1>ub;FL=X1<lb;X1=(X1.*(~(FU+FL)))+ub.*FU+lb.*FL;if fobj(X1)<fobj(X(i,:)) % improved move?X(i,:)=X1;else % Perform levy-based short rapid dives around the rabbitX2=Rabbit_Location-Escaping_Energy*abs(Jump_strength*Rabbit_Location-mean(X))+rand(1,dim).*Levy(dim);FU=X2>ub;FL=X2<lb;X2=(X2.*(~(FU+FL)))+ub.*FU+lb.*FL;if (fobj(X2)<fobj(X(i,:))) % improved move?X(i,:)=X2;endendend%%endendfor i=1:size(X,1)% Check boundriesFU=X(i,:)>ub;FL=X(i,:)<lb;X(i,:)=(X(i,:).*(~(FU+FL)))+ub.*FU+lb.*FL;% fitness of locationsfitness=fobj(X(i,:));fitnessAll(i,1)=fitness;% Update the location of Rabbitif fitness<Rabbit_EnergyRabbit_Energy=fitness;Rabbit_Location=X(i,:);endend[fitS,fitSind]=sort(fitnessAll);Xa=X(fitSind(1),:); Xb=X(fitSind(2),:); Xd=X(fitSind(3),:);for i=1:size(X,1)%Leader based mutation and SeclectionXnew=X(i,:)+E1*(2*rand-1)*(2*Xa-Xb-Xd)+(2*rand-1)*(Xa-X(i,:));FU=Xnew>ub;FL=Xnew<lb;Xnew=(Xnew.*(~(FU+FL)))+ub.*FU+lb.*FL;fitness=fobj(Xnew);if fitness<fitnessAll(i)X(i,:)=Xnew;endif fitness<Rabbit_EnergyRabbit_Energy=fitness;Rabbit_Location=Xnew;endendt=t+1;CNVG(t)=Rabbit_Energy;
end
end
04.代码效果图
获取代码请关注MATLAB科研小白的个人公众号(即文章下方二维码),并回复:本公众号致力于解决找代码难,写代码怵。各位有什么急需的代码,欢迎后台留言~不定时更新科研技巧类推文,可以一起探讨科研,写作,文献,代码等诸多学术问题,我们一起进步。