哈里斯鹰算法MATLAB实战

文摘   科技   2024-07-25 11:46   贵州  

    今天给大家分享的哈里斯鹰算法来自后台粉丝的投稿,下面几期将分享多目标粒子群算法和基因遗传算法。需要了解matlab编程知识的记得后台私信我~哈里斯鹰算法已在本文中展示,也可在以下链接中下载:

HHO (wzu.edu.cn)

链接:https://pan.baidu.com/s/1rvlN91RpYuOCO2wPzB3AEw
提取码:14a5

    需要了解更多算法代码的,可以点击文章左下角的阅读全文,进行获取哦~需要了解智能算法、机器学习、深度学习和信号处理相关理论的可以后台私信哦,下一期分享的内容就是你想了解的内容~


一、算法原理

    里斯鹰算法(Harris Hawks Optimizer,HHO)是由Heidari、Mirjalili等人于2019年提出的一种元启发式算法。从算法行为的角度来看,HHO有几个有效的功能:

1、逃逸能量参数具有动态随机时变性质,可以进一步改善和协调HHO的探索性和开发性模式。这一因素也支持HHO在勘探和开采之间平稳过渡。

2、关于鹰平均位置的不同探索机制可以增加HHO在整个初始迭代中的探索趋势。

3、具有短长度跳跃的基于 LF 的各种模式丰富了 HHO 在指导本地搜索时的剥削行为。

4、渐进式选择方案支持搜索代理逐步推进其位置,只选择更好的位置,可以提高解决方案的优越性和HHO在整个优化过程中的强化能力。

5、HHO显示一系列搜索策略,然后选择最佳移动步骤。这一特征也对HHO的开采倾向产生了建设性的影响。

6、随机跳跃强度可以帮助候选解决方案协调勘探和开发倾向。

7、自适应和时变组合的应用使HHO能够处理特征空间的困难,包括局部最优解,多模态和欺骗性最优。

    HHO算法主要包括探索阶段、探索到开发的转换阶段和开发阶段。其中,开发阶段包括软围攻、硬围攻、渐进式快速俯冲的软包围和渐进式快速俯冲的硬包围四个部分。HHO算法的流程图如下:

HHO算法的伪代码如下:

具体的算法原理我给大家推荐几篇文献,大家可自行下载阅读,当然最好是阅读算法原文哦

[1]Heidari A A, Mirjalili S, Faris H, et al. Harris hawks optimization: Algorithm and applications[J]. Future Generation Computer Systems. 2019, 97: 849-872.

[2]汤安迪,韩统,徐登武,等. 混沌精英哈里斯鹰优化算法[J]. 计算机应用. 2021: 1-10.

[3]HHO (wzu.edu.cn)


二、算法实战

% Developed in MATLAB R2013b% Source codes demo version 1.0% _____________________________________________________
% Author, inventor and programmer: Ali Asghar Heidari,% PhD research intern, Department of Computer Science, School of Computing, National University of Singapore, Singapore% Exceptionally Talented Ph. DC funded by Iran's National Elites Foundation (INEF), University of Tehran% 03-03-2019
% Researchgate: https://www.researchgate.net/profile/Ali_Asghar_Heidari
% e-Mail: as_heidari@ut.ac.ir, aliasghar68@gmail.com,% e-Mail (Singapore): aliasgha@comp.nus.edu.sg, t0917038@u.nus.edu% _____________________________________________________% Co-author and Advisor: Seyedali Mirjalili%% e-Mail: ali.mirjalili@gmail.com% seyedali.mirjalili@griffithuni.edu.au%% Homepage: http://www.alimirjalili.com% _____________________________________________________% Co-authors: Hossam Faris, Ibrahim Aljarah, Majdi Mafarja, and Hui-Ling Chen
% Homepage: http://www.evo-ml.com/2019/03/02/hho/% _____________________________________________________
% Please refer to the main paper:% Ali Asghar Heidari, Seyedali Mirjalili, Hossam Faris, Ibrahim Aljarah, Majdi Mafarja, Huiling Chen% Harris hawks optimization: Algorithm and applications% Future Generation Computer Systems, DOI: https://doi.org/10.1016/j.future.2019.02.028% _____________________________________________________%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear all %#ok<CLALL>close allclc
N=30; % Number of search agents
Function_name='F1'; % Name of the test function
T=500; % Maximum number of iterations
% Load details of the selected benchmark function[lb,ub,dim,fobj]=Get_Functions_details(Function_name);
[Rabbit_Energy,Rabbit_Location,CNVG]=HHO(N,T,lb,ub,dim,fobj);

%Draw objective spacefigure,hold onsemilogy(CNVG,'Color','b','LineWidth',4);title('Convergence curve')xlabel('Iteration');ylabel('Best fitness obtained so far');axis tightgrid offbox onlegend('HHO')
display(['The best location of HHO is: ', num2str(Rabbit_Location)]);display(['The best fitness of HHO is: ', num2str(Rabbit_Energy)]);

% Main paper:% Harris hawks optimization: Algorithm and applications% Ali Asghar Heidari, Seyedali Mirjalili, Hossam Faris, Ibrahim Aljarah, Majdi Mafarja, Huiling Chen% Future Generation Computer Systems, % DOI: https://doi.org/10.1016/j.future.2019.02.028% _____________________________________________________
function [X]=initialization(N,dim,up,down)
if size(up,1)==1 X=rand(N,dim).*(up-down)+down;endif size(up,1)>1 for i=1:dim high=up(i);low=down(i); X(:,i)=rand(1,N).*(high-low)+low; endendend

% Harris's hawk optimizer: In this algorithm, Harris' hawks try to catch the rabbit.
% T: maximum iterations, N: populatoin size, CNVG: Convergence curve% To run HHO: [Rabbit_Energy,Rabbit_Location,CNVG]=HHO(N,T,lb,ub,dim,fobj)
function [Rabbit_Energy,Rabbit_Location,CNVG]=HHO(N,T,lb,ub,dim,fobj)
disp('HHO is now tackling your problem')tic% initialize the location and Energy of the rabbitRabbit_Location=zeros(1,dim);Rabbit_Energy=inf;
%Initialize the locations of Harris' hawksX=initialization(N,dim,ub,lb);
CNVG=zeros(1,T);
t=0; % Loop counter
while t<T for i=1:size(X,1) % Check boundries FU=X(i,:)>ub;FL=X(i,:)<lb;X(i,:)=(X(i,:).*(~(FU+FL)))+ub.*FU+lb.*FL; % fitness of locations fitness=fobj(X(i,:)); % Update the location of Rabbit if fitness<Rabbit_Energy Rabbit_Energy=fitness; Rabbit_Location=X(i,:); end end E1=2*(1-(t/T)); % factor to show the decreaing energy of rabbit % Update the location of Harris' hawks for i=1:size(X,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(N*rand()+1); X_rand = X(rand_Hawk_index, :); if q<0.5 % perch based on other family members X(i,:)=X_rand-rand()*abs(X_rand-2*rand()*X(i,:)); elseif q>=0.5 % perch on a random tall tree (random site inside group's home range) X(i,:)=(Rabbit_Location(1,:)-mean(X))-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 X(i,:)=(Rabbit_Location)-Escaping_Energy*abs(Rabbit_Location-X(i,:)); end if r>=0.5 && abs(Escaping_Energy)>=0.5 % Soft besiege Jump_strength=2*(1-rand()); % random jump strength of the rabbit X(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 motions Jump_strength=2*(1-rand()); X1=Rabbit_Location-Escaping_Energy*abs(Jump_strength*Rabbit_Location-X(i,:)); if fobj(X1)<fobj(X(i,:)) % improved move? X(i,:)=X1; else % hawks perform levy-based short rapid dives around the rabbit X2=Rabbit_Location-Escaping_Energy*abs(Jump_strength*Rabbit_Location-X(i,:))+rand(1,dim).*Levy(dim); if (fobj(X2)<fobj(X(i,:))), % improved move? X(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(X)); if fobj(X1)<fobj(X(i,:)) % improved move? X(i,:)=X1; else % Perform levy-based short rapid dives around the rabbit X2=Rabbit_Location-Escaping_Energy*abs(Jump_strength*Rabbit_Location-mean(X))+rand(1,dim).*Levy(dim); if (fobj(X2)<fobj(X(i,:))), % improved move? X(i,:)=X2; end end end %% end end t=t+1; CNVG(t)=Rabbit_Energy;% Print the progress every 100 iterations% if mod(t,100)==0% display(['At iteration ', num2str(t), ' the best fitness is ', num2str(Rabbit_Energy)]);% endendtocend
% ___________________________________function o=Levy(d)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,d)*sigma;v=randn(1,d);step=u./abs(v).^(1/beta);o=step;end

function [lb,ub,dim,fobj] = Get_Functions_details(F)

switch F case 'F1' fobj = @F1; lb=-100; ub=100; dim=30; case 'F2' fobj = @F2; lb=-10; ub=10; dim=30; case 'F3' fobj = @F3; lb=-100; ub=100; dim=30; case 'F4' fobj = @F4; lb=-100; ub=100; dim=30; case 'F5' fobj = @F5; lb=-30; ub=30; dim=30; case 'F6' fobj = @F6; lb=-100; ub=100; dim=30; case 'F7' fobj = @F7; lb=-1.28; ub=1.28; dim=30; case 'F8' fobj = @F8; lb=-500; ub=500; dim=30; case 'F9' fobj = @F9; lb=-5.12; ub=5.12; dim=30; case 'F10' fobj = @F10; lb=-32; ub=32;% dim=30; dim=30; case 'F11' fobj = @F11; lb=-600; ub=600; dim=30; case 'F12' fobj = @F12; lb=-50; ub=50; dim=30; case 'F13' fobj = @F13; lb=-50; ub=50; dim=30; case 'F14' fobj = @F14; lb=-65.536; ub=65.536; dim=2; case 'F15' fobj = @F15; lb=-5; ub=5; dim=4; case 'F16' fobj = @F16; lb=-5; ub=5; dim=2; case 'F17' fobj = @F17; lb=[-5,0]; ub=[10,15]; dim=2; case 'F18' fobj = @F18; lb=-5; ub=5; dim=2; case 'F19' fobj = @F19; lb=0; ub=1; dim=3; case 'F20' fobj = @F20; lb=0; ub=1; dim=6; case 'F21' fobj = @F21; lb=0; ub=10; dim=4; % dim=4; case 'F22' fobj = @F22; lb=0; ub=10; dim=4; case 'F23' fobj = @F23; lb=0; ub=10; dim=4; end
end
% F1
function o = F1(x)o=sum(x.^2);end
% F2
function o = F2(x)o=sum(abs(x))+prod(abs(x));end
% F3
function o = F3(x)dim=size(x,2);o=0;for i=1:dim o=o+sum(x(1:i))^2;endend
% F4
function o = F4(x)o=max(abs(x));end
% F5
function o = F5(x)dim=size(x,2);o=sum(100*(x(2:dim)-(x(1:dim-1).^2)).^2+(x(1:dim-1)-1).^2);end
% F6
function o = F6(x)o=sum(abs((x+.5)).^2);end
% F7
function o = F7(x)dim=size(x,2);o=sum([1:dim].*(x.^4))+rand;end
% F8
function o = F8(x)o=sum(-x.*sin(sqrt(abs(x))));end
% F9
function o = F9(x)dim=size(x,2);o=sum(x.^2-10*cos(2*pi.*x))+10*dim;end
% F10
function o = F10(x)dim=size(x,2);o=-20*exp(-.2*sqrt(sum(x.^2)/dim))-exp(sum(cos(2*pi.*x))/dim)+20+exp(1);end
% F11
function o = F11(x)dim=size(x,2);o=sum(x.^2)/4000-prod(cos(x./sqrt([1:dim])))+1;end
% F12
function o = F12(x)dim=size(x,2);o=(pi/dim)*(10*((sin(pi*(1+(x(1)+1)/4)))^2)+sum((((x(1:dim-1)+1)./4).^2).*...(1+10.*((sin(pi.*(1+(x(2:dim)+1)./4)))).^2))+((x(dim)+1)/4)^2)+sum(Ufun(x,10,100,4));end
% F13
function o = F13(x)dim=size(x,2);o=.1*((sin(3*pi*x(1)))^2+sum((x(1:dim-1)-1).^2.*(1+(sin(3.*pi.*x(2:dim))).^2))+...((x(dim)-1)^2)*(1+(sin(2*pi*x(dim)))^2))+sum(Ufun(x,5,100,4));end
% F14
function o = F14(x)aS=[-32 -16 0 16 32 -32 -16 0 16 32 -32 -16 0 16 32 -32 -16 0 16 32 -32 -16 0 16 32;...-32 -32 -32 -32 -32 -16 -16 -16 -16 -16 0 0 0 0 0 16 16 16 16 16 32 32 32 32 32];
for j=1:25 bS(j)=sum((x'-aS(:,j)).^6);endo=(1/500+sum(1./([1:25]+bS))).^(-1);end
% F15
function o = F15(x)aK=[.1957 .1947 .1735 .16 .0844 .0627 .0456 .0342 .0323 .0235 .0246];bK=[.25 .5 1 2 4 6 8 10 12 14 16];bK=1./bK;o=sum((aK-((x(1).*(bK.^2+x(2).*bK))./(bK.^2+x(3).*bK+x(4)))).^2);end
% F16
function o = F16(x)o=4*(x(1)^2)-2.1*(x(1)^4)+(x(1)^6)/3+x(1)*x(2)-4*(x(2)^2)+4*(x(2)^4);end
% F17
function o = F17(x)o=(x(2)-(x(1)^2)*5.1/(4*(pi^2))+5/pi*x(1)-6)^2+10*(1-1/(8*pi))*cos(x(1))+10;end
% F18
function o = F18(x)o=(1+(x(1)+x(2)+1)^2*(19-14*x(1)+3*(x(1)^2)-14*x(2)+6*x(1)*x(2)+3*x(2)^2))*... (30+(2*x(1)-3*x(2))^2*(18-32*x(1)+12*(x(1)^2)+48*x(2)-36*x(1)*x(2)+27*(x(2)^2)));end
% F19
function o = F19(x)aH=[3 10 30;.1 10 35;3 10 30;.1 10 35];cH=[1 1.2 3 3.2];pH=[.3689 .117 .2673;.4699 .4387 .747;.1091 .8732 .5547;.03815 .5743 .8828];o=0;for i=1:4 o=o-cH(i)*exp(-(sum(aH(i,:).*((x-pH(i,:)).^2))));endend
% F20
function o = F20(x)aH=[10 3 17 3.5 1.7 8;.05 10 17 .1 8 14;3 3.5 1.7 10 17 8;17 8 .05 10 .1 14];cH=[1 1.2 3 3.2];pH=[.1312 .1696 .5569 .0124 .8283 .5886;.2329 .4135 .8307 .3736 .1004 .9991;....2348 .1415 .3522 .2883 .3047 .6650;.4047 .8828 .8732 .5743 .1091 .0381];o=0;for i=1:4 o=o-cH(i)*exp(-(sum(aH(i,:).*((x-pH(i,:)).^2))));endend
% F21
function o = F21(x)aSH=[4 4 4 4;1 1 1 1;8 8 8 8;6 6 6 6;3 7 3 7;2 9 2 9;5 5 3 3;8 1 8 1;6 2 6 2;7 3.6 7 3.6];cSH=[.1 .2 .2 .4 .4 .6 .3 .7 .5 .5];
o=0;for i=1:5 o=o-((x-aSH(i,:))*(x-aSH(i,:))'+cSH(i))^(-1);endend
% F22
function o = F22(x)aSH=[4 4 4 4;1 1 1 1;8 8 8 8;6 6 6 6;3 7 3 7;2 9 2 9;5 5 3 3;8 1 8 1;6 2 6 2;7 3.6 7 3.6];cSH=[.1 .2 .2 .4 .4 .6 .3 .7 .5 .5];
o=0;for i=1:7 o=o-((x-aSH(i,:))*(x-aSH(i,:))'+cSH(i))^(-1);endend
% F23
function o = F23(x)aSH=[4 4 4 4;1 1 1 1;8 8 8 8;6 6 6 6;3 7 3 7;2 9 2 9;5 5 3 3;8 1 8 1;6 2 6 2;7 3.6 7 3.6];cSH=[.1 .2 .2 .4 .4 .6 .3 .7 .5 .5];
o=0;for i=1:10 o=o-((x-aSH(i,:))*(x-aSH(i,:))'+cSH(i))^(-1);endend
function o=Ufun(x,a,k,m)o=k.*((x-a).^m).*(x>a)+k.*((-x-a).^m).*(x<(-a));end

实验结果

参考文献
[1] Aaha B ,  Sm C ,  Hf D , et al. Harris hawks optimization: Algorithm and applications[J]. Future Generation Computer Systems, 2019, 97:849-872.


    部分知识来源于网络,如有侵权请联系作者删除~


    今天的分享就到这里了,后续想了解智能算法、机器学习、深度学习和信号处理相关理论的可以后台私信哦~希望大家多多转发点赞加收藏,你们的支持就是我源源不断的创作动力!


作 者 | 华 夏

编 辑 | 华 夏

校 对 | 华 夏


matlab学习之家
分享学习matlab建模知识和matlab编程知识
 最新文章