目录
1 算法简介
哈里斯鹰算法(Harris Hawks Optimization,HHO),是由Ali Asghar Heidari和Seyedali Mrjaili于2019年提出的一种新型仿生智能优化算法。该算法模仿哈里斯鹰捕食特点,结合Levy飞行(Levy Flights)实现对复杂多维问题求解。研究表明该新型算法具有良好的性能。在HHO中,哈里斯鹰是候选解,猎物随迭代逼近最优解.HHO算法包括两个阶段:全局探索阶段、局部开采阶段.
2 算法数学模型
2.1.全局探索阶段
在这一阶段中,哈里斯鹰处于等待状态。仔细检查和监控搜索空间[b , ub]以发现猎物。根据两种策略在随机的地方寻找猎物,迭代时以概率q进行位置更新。数学表达式如下:
式中,和分别为哈里斯鹰第t+1次和第t次迭代时的位置,Xrabbit,t表示猎物第t次迭代时的位置,q和r1,r2,r3,r4是区间(0,1)内的随机数字,lb是搜索空间的下界,ub是搜索空间的上界,Xrand,t表示第t次迭代时哈里斯鹰的随机位置,Xm,t表示第t次迭代时哈里斯鹰的平均位置,公式如下:
2.2 过渡阶段
该阶段用于保持探索和开采之间适当的平衡。HHO通过猎物的能量方程实现从探索到开采的过渡。
式中,E表示猎物逃跑的能量,是猎物能量的初始状态,公式为E0= 2*rand - 1,rand是(0,1)之间的随机数字,T为最大迭代次数,t为当前迭代次数.当E≥1时,哈里斯鹰算法将执行全局探索;否则,HHO算法进入局部开采。
2.3.局部开采阶段
根据猎物的逃跑行为和哈里斯鹰的追逐策略,HHO算法提出了四种可能的策略来模拟攻击行为.用N表示猎物成功逃脱的概率。
(1)软围攻.当E≥0.5,A≥0.5时,猎物有足够的能量且以跳跃的方式逃脱围捕,而哈里斯鹰会逐渐消耗猎物的能量,然后选择最佳的位置突袭俯冲逮捕猎物.更新位置的方程如下:
式中,Xt是迭代时猎物与哈里斯鹰的位置之差,J=2(1-r5)表示猎物逃跑过程中的随机跳跃,r5是介于0到1之间的随机数字.
(2)硬围攻.当E<0.5,入≥0.5时,猎物筋疲力尽,哈里斯鹰选择迅速突袭.位置更新如下:
(3)累速俯冲式软围攻.当E>0.5,入<0.5时,猎物有足够的能量E逃跑,哈里斯鹰在突袭之前会建立一个软围攻.为了模拟猎物的逃跑模式和跳跃动作,将levy函数LF集成在HHO算法中.更新位置的策略为:
式中,D为问题维度,S为D维随机行向量.
(4)累速俯冲式硬围攻.当E<0.5,入<0.5时,猎物能量E低,哈里斯鹰在突袭前构建硬围攻捕捉猎物,位置更新如:
Levy飞行函数公式如下:
式中,u、v是(0,1)之间的随机数,β取常值1.5。
HHO算法用猎物能量E和因子入调节哈里斯鹰和猎物(兔子)之间的四种围捕机制,来实现优化求解问题.
3 求解步骤与程序框图
3.1 步骤
HHO算法的规则描述如下:
1)每次迭代前,判断是否越界并调整,更新猎物位置与适应度值;
2)在搜索阶段,哈里斯鹰拥有两种不同搜索方式,分别针对发现和未发现猎物;
3)野兔的逃逸能量会随着迭代次数增加而自适应减小;
4)当野兔能量降低到某一阈值,将被哈里斯鹰群发现,狩猎从搜索阶段转为围捕突袭阶段;
5)每一次突袭前,兔子都有一定的概率从包围中逃脱;
6)针对兔子的体力,以及是否逃脱包围圈,哈里斯鹰有四种不同的围捕策略;
7)每次围捕最终兔子将会捕获,每次迭代会产生一个新的猎物,该位置将由新一代种群中适应度值最优者占据。
实现步骤
Step1:初始化种群:包括搜索空间的上限和下限,算法的最大迭代次数T。随机初始化种群位置.
Step2:根据适应度函数计算每个个体的适应度值,保存种群最优个体.
Step3:更新猎物逃逸能量E.
Step4:比较E的大小,按位置更新公式或四种策略追捕猎物,更新位置.Step5:对每个个体,计算适应度,更新种群最优的适应度值.
Step6:判断搜索到的结果是否满足停止条件(达到最大迭代次数或满足精度要求),若满足停止条件则输出最优值,否则转到Step3继续运行直到满足条件为止.
3.2 程序框图
4 matlab代码及结果
4.1 代码
%% 智能优化算法——哈里鹰算法(Matlab实现)
clear
close all
clc
SearchAgents_no = 30 ; % 种群规模
dim = 10 ; % 粒子维度
Max_iter = 1000 ; % 迭代次数
ub = 5 ;
lb = -5 ;
%% 初始化猎物位置和逃逸能量
Rabbit_Location=zeros(1,dim);
Rabbit_Energy=inf;
%% 初始化种群的位置
Positions= lb + rand(SearchAgents_no,dim).*(ub-lb) ;
Convergence_curve = zeros(Max_iter,1);
%% 开始循环
for t=1:Max_iter
for i=1:size(Positions,1)
% Check boundries
FU=Positions(i,:)>ub;FL=Positions(i,:)<lb;Positions(i,:)=(Positions(i,:).*(~(FU+FL)))+ub.*FU+lb.*FL;
% fitness of locations
fitness=sum(Positions(i,:).^2);
% Update the location of Rabbit
if fitness<Rabbit_Energy
Rabbit_Energy=fitness;
Rabbit_Location=Positions(i,:);
end
end
E1=2*(1-(t/Max_iter)); % factor to show the decreaing energy of rabbit
%% Update the location of Harris' hawks
for i=1:size(Positions,1)
E0=2*rand()-1; %-1<E0<1
Escaping_Energy=E1*(E0); % escaping energy of rabbit
if abs(Escaping_Energy)>=1
%% Exploration:
% Harris' hawks perch randomly based on 2 strategy:
q=rand();
rand_Hawk_index = floor(SearchAgents_no*rand()+1);
X_rand = Positions(rand_Hawk_index, :);
if q<0.5
% perch based on other family members
Positions(i,:)=X_rand-rand()*abs(X_rand-2*rand()*Positions(i,:));
elseif q>=0.5
% perch on a random tall tree (random site inside group's home range)
Positions(i,:)=(Rabbit_Location(1,:)-mean(Positions))-rand()*((ub-lb)*rand+lb);
end
elseif 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 hawks
r=rand(); % probablity of each event
if r>=0.5 && abs(Escaping_Energy)<0.5 % Hard besiege
Positions(i,:)=(Rabbit_Location)-Escaping_Energy*abs(Rabbit_Location-Positions(i,:));
end
if r>=0.5 && abs(Escaping_Energy)>=0.5 % Soft besiege
Jump_strength=2*(1-rand()); % random jump strength of the rabbit
Positions(i,:)=(Rabbit_Location-Positions(i,:))-Escaping_Energy*abs(Jump_strength*Rabbit_Location-Positions(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 motions
Jump_strength=2*(1-rand());
X1=Rabbit_Location-Escaping_Energy*abs(Jump_strength*Rabbit_Location-Positions(i,:));
if sum(X1.^2)<sum(Positions(i,:).^2) % improved move
Positions(i,:)=X1;
else % hawks perform levy-based short rapid dives around the rabbit
%Levy flight
beta=1.5;
sigma=(gamma(1+beta)*sin(pi*beta/2)/(gamma((1+beta)/2)*beta*2^((beta-1)/2)))^(1/beta);
u=randn(1,dim)*sigma;v=randn(1,dim);step=u./abs(v).^(1/beta);
o1=0.01*step;
X2=Rabbit_Location-Escaping_Energy*abs(Jump_strength*Rabbit_Location-Positions(i,:))+rand(1,dim).*o1;
if (sum(X2.^2)<sum(Positions(i,:).^2))% improved move
Positions(i,:)=X2;
end
end
end
if 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 rabbit
Jump_strength=2*(1-rand());
X1=Rabbit_Location-Escaping_Energy*abs(Jump_strength*Rabbit_Location-mean(Positions));
if sum(X1.^2)<sum(Positions(i,:).^2) % improved move
Positions(i,:)=X1;
else % Perform levy-based short rapid dives around the rabbit
%Levy flight
beta=1.5;
sigma=(gamma(1+beta)*sin(pi*beta/2)/(gamma((1+beta)/2)*beta*2^((beta-1)/2)))^(1/beta);
u=randn(1,dim)*sigma;v=randn(1,dim);step=u./abs(v).^(1/beta);
o2=0.01*step;
X2=Rabbit_Location-Escaping_Energy*abs(Jump_strength*Rabbit_Location-mean(Positions))+rand(1,dim).*o2;
if (sum(X2.^2)<sum(Positions(i,:).^2))% improved move
Positions(i,:)=X2;
end
end
end
%%
end
end
Convergence_curve(t)=Rabbit_Energy;
% Print the progress every 100 iterations
if mod(t,50)==0
display(['At iteration ', num2str(t), ' the best fitness is ', num2str(Rabbit_Energy)]);
end
end
figure('unit','normalize','Position',[0.3,0.35,0.4,0.35],'color',[1 1 1],'toolbar','none')
subplot(1,2,1);
x = -5:0.1:5;y=x;
L=length(x);
f=zeros(L,L);
for i=1:L
for j=1:L
f(i,j) = x(i)^2+y(j)^2;
end
end
surfc(x,y,f,'LineStyle','none');
xlabel('x_1');
ylabel('x_2');
zlabel('F')
title('Objective space')
subplot(1,2,2);
semilogy(Convergence_curve,'Color','r','linewidth',1.5)
title('Convergence_curve')
xlabel('Iteration');
ylabel('Best score obtained so far');
axis tight
grid on
box on
legend('HHO')
display(['The best solution obtained by HHO is : ', num2str(Rabbit_Location)]);
display(['The best optimal value of the objective funciton found by HHO is : ', num2str(Rabbit_Energy)]);