1.鲸鱼优化算法建模
鲸鱼优化算法(woa)是澳大利亚学者mirjaili等于2016年提出的群体智能优化算法,根据座头鲸的捕猎行为实现优化搜索的目的。其中,每个鲸鱼可以看作一个粒子,每个粒子作为不同的决策变量。woa的实现过程主要包括包围猎物、螺旋狩猎和随机搜索3个阶段,其数学模型如下:
1.1 包围猎物
1.2 螺旋狩猎
1.3 搜索猎物
1.4 算法流程图
2.matlab代码实现
2.1 结果
2.2 代码
clear all clc searchagents_no=30; function_name='f1'; % name of the test function that can be from f1 to f23 (table 1,2,3 in the paper) % max_iteration=500; % maximum numbef of iterations max_iteration=500; % load details of the selected benchmark function [lb,ub,dim,fobj]=get_functions_details(function_name); [best_score,best_pos,woabat_cg_curve]=woabat(searchagents_no,max_iteration,lb,ub,dim,fobj); figure('position',[269 240 660 290]) %draw search space subplot(1,2,1); func_plot(function_name); title('parameter space') xlabel('x_1'); ylabel('x_2'); zlabel([function_name,'( x_1 , x_2 )']) %draw objective space subplot(1,2,2); semilogy(woabat_cg_curve,'color','r') title('objective space') xlabel('iteration'); ylabel('best score obtained so far'); axis tight grid on box on legend('woabat') %display(['the best solution obtained by woabat is : ', num2str(best_pos)]); display(['the best optimal value of the objective funciton found by woa is : ', num2str(best_score)]); %display( num2str(best_score));
% the whale optimization algorithm function [leader_score,leader_pos,convergence_curve]=woabat(searchagents_no,max_iter,lb,ub,dim,fobj) % initialize position vector and score for the leader leader_pos=zeros(1,dim); leader_score=inf; %change this to -inf for maximization problems %initialize the positions of search agents positions=initialization(searchagents_no,dim,ub,lb); convergence_curve=zeros(1,max_iter); %bat algorithm addition qmin=0; % frequency minimum qmax=2; % frequency maximum q=zeros(searchagents_no,1); % frequency v=zeros(searchagents_no,dim); % velocities r=0.5; a1=0.5; t=0;% loop counter % summ=0; % main loop while t<max_iter for i=1:size(positions,1) % return back the search agents that go beyond the boundaries of the search space flag4ub=positions(i,:)>ub; flag4lb=positions(i,:)<lb; positions(i,:)=(positions(i,:).*(~(flag4ub+flag4lb)))+ub.*flag4ub+lb.*flag4lb; % calculate objective function for each search agent fitness=fobj(positions(i,:)); % update the leader if fitness<leader_score % change this to > for maximization problem leader_score=fitness; % update alpha leader_pos=positions(i,:); end end a=2-t*((2)/max_iter); % a decreases linearly fron 2 to 0 in eq. (2.3) % a2 linearly dicreases from -1 to -2 to calculate t in eq. (3.12) a2=-1+t*((-1)/max_iter); % update the position of search agents for i=1:size(positions,1) r1=rand(); % r1 is a random number in [0,1] r2=rand(); % r2 is a random number in [0,1] a=2*a*r1-a; c=2*r2; b=1; l=(a2-1)*rand+1; p = rand(); for j=1:size(positions,2) if p<0.5 if abs(a)>=1 rand_leader_index = floor(searchagents_no*rand()+1); x_rand = positions(rand_leader_index, :); q(i)=qmin+(qmin-qmax)*rand; v(i,:)=v(i,j)+(x_rand(j)-leader_pos(j))*q(i); z(i,:)= positions(i,:)+ v(i,:); %%%% problem if rand>r % the factor 0.001 limits the step sizes of random walks z (i,:)=leader_pos(j)+0.001*randn(1,dim); end % evaluate new solutions fnew=fobj(z(i,:)); % update if the solution improves, or not too loud if (fnew<=fitness) && (rand<a1) positions(i,:)=z(i,:); fitness=fnew; end elseif abs(a)<1 q(i)=qmin+(qmin-qmax)*rand; v(i,:)=v(i,j)+(positions(i,:)-leader_pos(j))*q(i); z(i,:)= positions(i,:)+ v(i,:); %%%% problem if rand>r % the factor 0.001 limits the step sizes of random walks z (i,:)=leader_pos(j)+0.001*randn(1,dim); end % evaluate new solutions fnew=fobj(z(i,:)); % update if the solution improves, or not too loud if (fnew<=fitness) && (rand<a1) positions(i,:)=z(i,:); fitness=fnew; end end elseif p>=0.5 distance2leader=abs(leader_pos(j)-positions(i,j)); % eq. (2.5) positions(i,j)=distance2leader*exp(b.*l).*cos(l.*2*pi)+leader_pos(j); end end end t=t+1; convergence_curve(t)=leader_score; [t leader_score] end
% this function draw the benchmark functions function func_plot(func_name) [lb,ub,dim,fobj]=get_functions_details(func_name); switch func_name case 'f1' x=-100:2:100; y=x; %[-100,100] case 'f2' x=-100:2:100; y=x; %[-10,10] case 'f3' x=-100:2:100; y=x; %[-100,100] case 'f4' x=-100:2:100; y=x; %[-100,100] case 'f5' x=-200:2:200; y=x; %[-5,5] case 'f6' x=-100:2:100; y=x; %[-100,100] case 'f7' x=-1:0.03:1; y=x %[-1,1] case 'f8' x=-500:10:500;y=x; %[-500,500] case 'f9' x=-5:0.1:5; y=x; %[-5,5] case 'f10' x=-20:0.5:20; y=x;%[-500,500] case 'f11' x=-500:10:500; y=x;%[-0.5,0.5] case 'f12' x=-10:0.1:10; y=x;%[-pi,pi] case 'f13' x=-5:0.08:5; y=x;%[-3,1] case 'f14' x=-100:2:100; y=x;%[-100,100] case 'f15' x=-5:0.1:5; y=x;%[-5,5] case 'f16' x=-1:0.01:1; y=x;%[-5,5] case 'f17' x=-5:0.1:5; y=x;%[-5,5] case 'f18' x=-5:0.06:5; y=x;%[-5,5] case 'f19' x=-5:0.1:5; y=x;%[-5,5] case 'f20' x=-5:0.1:5; y=x;%[-5,5] case 'f21' x=-5:0.1:5; y=x;%[-5,5] case 'f22' x=-5:0.1:5; y=x;%[-5,5] case 'f23' x=-5:0.1:5; y=x;%[-5,5] end l=length(x); f=[]; for i=1:l for j=1:l if strcmp(func_name,'f15')==0 && strcmp(func_name,'f19')==0 && strcmp(func_name,'f20')==0 && strcmp(func_name,'f21')==0 && strcmp(func_name,'f22')==0 && strcmp(func_name,'f23')==0 f(i,j)=fobj([x(i),y(j)]); end if strcmp(func_name,'f15')==1 f(i,j)=fobj([x(i),y(j),0,0]); end if strcmp(func_name,'f19')==1 f(i,j)=fobj([x(i),y(j),0]); end if strcmp(func_name,'f20')==1 f(i,j)=fobj([x(i),y(j),0,0,0,0]); end if strcmp(func_name,'f21')==1 || strcmp(func_name,'f22')==1 ||strcmp(func_name,'f23')==1 f(i,j)=fobj([x(i),y(j),0,0]); end end end surfc(x,y,f,'linestyle','none'); end
function [lb,ub,dim,fobj] = get_functions_details(f) switch f case 'f1' fobj = @f1; lb=-100; ub=100; % dim=30; 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; 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=-2; ub=2; 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; 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; end end % 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); end o=(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)))); end end % 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)))); end end % 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); end end % 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); end end % 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); end end function o=ufun(x,a,k,m) o=k.*((x-a).^m).*(x>a)+k.*((-x-a).^m).*(x<(-a)); end
% this function initialize the first population of search agents function positions=initialization(searchagents_no,dim,ub,lb) boundary_no= size(ub,2); % numnber of boundaries % if the boundaries of all variables are equal and user enter a single % number for both ub and lb if boundary_no==1 positions=rand(searchagents_no,dim).*(ub-lb)+lb; end % if each variable has a different lb and ub if boundary_no>1 for i=1:dim ub_i=ub(i); lb_i=lb(i); positions(:,i)=rand(searchagents_no,1).*(ub_i-lb_i)+lb_i; end end
以上就是基于matlab实现鲸鱼优化算法的示例代码的详细内容,更多关于matlab鲸鱼优化算法的资料请关注<计算机技术网(www.ctvol.com)!!>其它相关文章!
需要了解更多c/c++开发分享基于Matlab实现鲸鱼优化算法的示例代码,都可以关注C/C++技术分享栏目—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/c-cdevelopment/1081898.html