鲸鱼优化算法MATLAB代码

  • 论文
    Seyedali Mirjalili,Andrew Lewis. The Whale Optimization Algorithm[J]. Advances in Engineering Software,2016,95.
  • func_plot.m

% This function draw the benchmark functionsfunction 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:Lfor j=1:Lif 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')==0f(i,j)=fobj([x(i),y(j)]);endif strcmp(func_name,'F15')==1f(i,j)=fobj([x(i),y(j),0,0]);endif strcmp(func_name,'F19')==1f(i,j)=fobj([x(i),y(j),0]);endif strcmp(func_name,'F20')==1f(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')==1f(i,j)=fobj([x(i),y(j),0,0]);end          end
endsurfc(x,y,f,'LineStyle','none');end
  • Get_Functions_details.m

% This function containts full information and implementations of the benchmark 
% functions in Table 1, Table 2, and Table 3 in the paper% lb is the lower bound: lb=[lb_1,lb_2,...,lb_d]
% up is the uppper bound: ub=[ub_1,ub_2,...,ub_d]
% dim is the number of variables (dimension of the problem)function [lb,ub,dim,fobj] = Get_Functions_details(F)switch Fcase '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;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;            
endend% F1function o = F1(x)
o=sum(x.^2);
end% F2function o = F2(x)
o=sum(abs(x))+prod(abs(x));
end% F3function o = F3(x)
dim=size(x,2);
o=0;
for i=1:dimo=o+sum(x(1:i))^2;
end
end% F4function o = F4(x)
o=max(abs(x));
end% F5function 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% F6function o = F6(x)
o=sum(abs((x+.5)).^2);
end% F7function o = F7(x)
dim=size(x,2);
o=sum([1:dim].*(x.^4))+rand;
end% F8function o = F8(x)
o=sum(-x.*sin(sqrt(abs(x))));
end% F9function o = F9(x)
dim=size(x,2);
o=sum(x.^2-10*cos(2*pi.*x))+10*dim;
end% F10function 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% F11function o = F11(x)
dim=size(x,2);
o=sum(x.^2)/4000-prod(cos(x./sqrt([1:dim])))+1;
end% F12function 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% F13function 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% F14function 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:25bS(j)=sum((x'-aS(:,j)).^6);
end
o=(1/500+sum(1./([1:25]+bS))).^(-1);
end% F15function 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% F16function 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% F17function 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% F18function 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% F19function 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:4o=o-cH(i)*exp(-(sum(aH(i,:).*((x-pH(i,:)).^2))));
end
end% F20function 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:4o=o-cH(i)*exp(-(sum(aH(i,:).*((x-pH(i,:)).^2))));
end
end% F21function 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:5o=o-((x-aSH(i,:))*(x-aSH(i,:))'+cSH(i))^(-1);
end
end% F22function 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:7o=o-((x-aSH(i,:))*(x-aSH(i,:))'+cSH(i))^(-1);
end
end% F23function 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:10o=o-((x-aSH(i,:))*(x-aSH(i,:))'+cSH(i))^(-1);
end
endfunction o=Ufun(x,a,k,m)
o=k.*((x-a).^m).*(x>a)+k.*((-x-a).^m).*(x<(-a));
end
  • initialization.m
% 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 signle
% number for both ub and lb
if Boundary_no==1Positions=rand(SearchAgents_no,dim).*(ub-lb)+lb;
end% If each variable has a different lb and ub
if Boundary_no>1for i=1:dimub_i=ub(i);lb_i=lb(i);Positions(:,i)=rand(SearchAgents_no,1).*(ub_i-lb_i)+lb_i;end
end
  • WOA
% The Whale Optimization Algorithm
function [Leader_score,Leader_pos,Convergence_curve]=WOA(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);t=0;% Loop counter% Main loop
while t<Max_iterfor i=1:size(Positions,1)% Return back the search agents that go beyond the boundaries of the search spaceFlag4ub=Positions(i,:)>ub;Flag4lb=Positions(i,:)<lb;Positions(i,:)=(Positions(i,:).*(~(Flag4ub+Flag4lb)))+ub.*Flag4ub+lb.*Flag4lb;% Calculate objective function for each search agentfitness=fobj(Positions(i,:));% Update the leaderif fitness<Leader_score % Change this to > for maximization problemLeader_score=fitness; % Update alphaLeader_pos=Positions(i,:);endenda=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;  % Eq. (2.3) in the paperC=2*r2;      % Eq. (2.4) in the paperb=1;               %  parameters in Eq. (2.5)l=(a2-1)*rand+1;   %  parameters in Eq. (2.5)p = rand();        % p in Eq. (2.6)for j=1:size(Positions,2)if p<0.5   if abs(A)>=1rand_leader_index = floor(SearchAgents_no*rand()+1);X_rand = Positions(rand_leader_index, :);D_X_rand=abs(C*X_rand(j)-Positions(i,j)); % Eq. (2.7)Positions(i,j)=X_rand(j)-A*D_X_rand;      % Eq. (2.8)elseif abs(A)<1D_Leader=abs(C*Leader_pos(j)-Positions(i,j)); % Eq. (2.1)Positions(i,j)=Leader_pos(j)-A*D_Leader;      % Eq. (2.2)endelseif p>=0.5distance2Leader=abs(Leader_pos(j)-Positions(i,j));% Eq. (2.5)Positions(i,j)=distance2Leader*exp(b.*l).*cos(l.*2*pi)+Leader_pos(j);endendendt=t+1;Convergence_curve(t)=Leader_score;[t Leader_score]
end
  • main.m
% You can simply define your cost in a seperate file and load its handle to fobj 
% The initial parameters that you need are:
%__________________________________________
% fobj = @YourCostFunction
% dim = number of your variables
% Max_iteration = maximum number of generations
% SearchAgents_no = number of search agents
% lb=[lb1,lb2,...,lbn] where lbn is the lower bound of variable n
% ub=[ub1,ub2,...,ubn] where ubn is the upper bound of variable n
% If all the variables have equal lower bound you can just
% define lb and ub as two single number numbers% To run WOA: [Best_score,Best_pos,WOA_cg_curve]=WOA(SearchAgents_no,Max_iteration,lb,ub,dim,fobj)
%__________________________________________clear all 
clcSearchAgents_no=30; % Number of search agentsFunction_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% Load details of the selected benchmark function
[lb,ub,dim,fobj]=Get_Functions_details(Function_name);[Best_score,Best_pos,WOA_cg_curve]=WOA(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(WOA_cg_curve,'Color','r')
title('Objective space')
xlabel('Iteration');
ylabel('Best score obtained so far');axis tight
grid on
box on
legend('WOA')display(['The best solution obtained by WOA is : ', num2str(Best_pos)]);
display(['The best optimal value of the objective funciton found by WOA is : ', num2str(Best_score)]);
  • 运行结果图
    在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/4659.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

数据结构(王道)——线性表之静态链表顺序表和链表的比较

一、静态链表 定义&#xff1a; 代码实现&#xff1a; 如何定义一个静态链表 静态链表的基本操作思路&#xff1a; 初始化静态链表&#xff1a; 静态链表的查找、插入、删除 静态链表总结&#xff1a; 二、顺序表和链表的比较 逻辑结构对比&#xff1a; 存储结构对比&#xff…

vue3 引入dataV 报错,使用patch-package记录插件包 node_modeule 修改记录。 vite 版DataV

开发数字大屏功能&#xff0c;引用dataV UI组件库比较好用&#xff0c;目前分为Vue2 和 Vue3 两个版本。 Vue2 --DataV版本 yarn add jiaminghi/data-viewVue3 --DataV版本 yarn add dataview/datav-vue3vite – --DataV版本 //不想动手改的&#xff0c;也可以使用此版本&a…

【Zookeeper】

目录 一、Zookeeper 概述1、Zookeeper 定义2、Zookeeper 工作机制3、Zookeeper 特点4、Zookeeper 数据结构5、Zookeeper 应用场景6、Zookeeper 选举机制 二、部署 Zookeeper 集群1.安装前准备1、关闭防火墙2、安装 JDK3、下载安装包 2.安装 Zookeeper1、修改配置文件2、拷贝配置…

被字节拷打了~基础还是太重要了...

今天分享一篇一位同学去字节面试的实习面经&#xff0c;技术栈是java&#xff0c;投了go后端岗位&#xff0c;主要拷打了 redismysql网络系统java算法&#xff0c;面试问题主要集中在 mysql、redis、网络这三部门&#xff0c;因为面试官是搞 go 的&#xff0c;java 只是随便问了…

【微信机器人开发

现在并没有长期免费的微信群机器人&#xff0c;很多都是前期免费试用&#xff0c;后期进行收费&#xff0c;或者核心功能需要付费使用的。 这时如果需要群机器人帮助我们管理群聊&#xff0c;建议大家使有条件的可以自己开发微信管理系统。了解微信群机器人的朋友都知道&#x…

Mysql数据库之事务

目录 一、事务的概念 二、事务的ACID特点 1.原子性&#xff08;Atomicity&#xff09; 2.一致性&#xff08;Consistency&#xff09; 3.隔离性&#xff08;lsolation&#xff09; 4.持久性&#xff08;Durability) 三、并发访问表的一致性问题和事务的隔离级别 1.并发访…

Windows下 创建 FTP 服务器及相关设置

Windows 创建 FTP 服务器 1. 示例功能说明 FTP 服务器根路径下的目录&#xff1a; C:\USERS\SQQIAN\DESKTOP\FTP └─localuser├─FTP1 # 只有用户名为FTP1可以访问&#xff0c;读写均可│ FTP11.txt│├─FTP2 # 只有用户名为FTP2…

好家伙,9:00面试,9:06就出来了,问的实在是太...

从外包出来&#xff0c;没想到死在另一家厂子 自从加入这家公司&#xff0c;每天都在加班&#xff0c;钱倒是给的不少&#xff0c;所以也就忍了。没想到2月一纸通知&#xff0c;所有人不许加班&#xff0c;薪资直降30%&#xff0c;顿时有吃不起饭的赶脚。 好在有个兄弟内推我去…

zabbix-server监控mysql数据库及httpd服务、监控apache、监控ftp

目录 一、监控mysql数据库及httpd服务 1、为server.Zabbix.com添加服务模板 2、server.zabbix.com服务端 操作 3、编辑chk_mysql.sh脚本 4、server.zabbix.com测试 二、监控apache 1、获取键值 2、服务器操作 3、zabbix监控web端导入监控模板 4、server.zabbix.com添加…

前端多行文本省略号

.title {height: 4rem;line-height: 2rem;// 多行文本省略号overflow: hidden;font-size: 1.4rem;display: -webkit-box;-webkit-box-orient: vertical;-webkit-line-clamp: 2;}

Pycharm远程服务器上运行程序报错:Can‘t get remote credentials for deployment server

一、问题描述&#xff1a; 二、解决方法&#xff1a; 1.依次选择 文件 -> 设置 -> 项目 -> Python 解释器,出现如下界面&#xff1a; 2. 点击python 解释器右侧的小齿轮&#xff0c;选择全部显示&#xff1a; 3. 在弹出的窗口中发现存在多个连接到同一个服务器的解释…

【protobuf】socket.io序列化和反序列化

1.背景 后台利用socket.io发送websocket消息&#xff0c;加密用到protobuf 2.反序列化时遇到问题 Traceback (most recent call last): File "D:/locust/Nigeria/test3.py", line 40, in <module> play.ParseFromString(decode_spin_str) google.proto…

FreeSWITCH的强插强拆等

记录如下&#xff1a; 强插强拆都是调用intercept这个appeavesdrop 监听appthree_way 三通app 强插强拆只是主体不同&#xff0c;尝试这样描述&#xff1a; 站在1001的立场上看&#xff0c;1002 bridge 1003&#xff0c; 1001呼入&#xff0c;执行intercept 1003-uuid&#…

chrome edge svg转png

chrome edge svg转png 生成SVG blockdiag Live Preview 导出png 截图&#xff1a; 左上角截取屏幕截图

sessionsManager io.undertow.server.session.InMemorySessionManager

sessionsManager io.undertow.server.session.InMemorySessionManager 记录一次性能优化记录基本情况错误表现错误提示问题原因解决办法 记录一次性能优化记录 基本情况 k8s集群&#xff0c;2台应用&#xff0c;nacos负载均衡&#xff0c;4只交易&#xff0c;2列表&#xff0…

【基于 GitLab 的 CI/CD 实践】02、gitlab-runner 实践

目录 一、gitlab-runner 简介 1.1 要求 1.2 特点 二、GitLab Runner 安装 2.1 使用 GItLab 官方仓库安装 2.2 使用 deb/rpm 软件包 2.3 在容器中运行 GitLab Runner 三、GitLab Runner 注册 3.1 GitLabRunner 类型 3.2 获取 runner token 获取 shared 类型 runner t…

数据结构——链表

数据结构——链表 链表&#xff08;Linked List&#xff09;是一种常见的数据结构&#xff0c;用于存储和组织数据。它由一系列称为节点&#xff08;Node&#xff09;的元素组成&#xff0c;每个节点包含数据和一个指向下一个节点的指针。相比于数组&#xff0c;链表具有动态插…

基于Gerapy部署分布式爬虫管理平台

文章目录 1. 服务器安装scrapyd1.1 scrapyd安装1.2 scrapyd配置允许外网访问1.3 服务器安全组开启端口1.4 服务器防火墙开启端口1.5 scrapyd测试 2. Gerapy 环境搭建2.1 gerapy安装2.2 gerapy测试2.3 项目部署2.4 定时任务2.5 线上代码修改 Gerapy是一个Python的分布式爬虫部署…

机器学习实战学习记录(github)

机器学习实战学习记录&#xff08;github&#xff09; 可见我的github&#xff1a; https://github.com/monkeyhlj/machine_learning_bymyself 刚刚建好&#xff0c;后面的学习记录会一直在这个仓库里面更新。 推荐参考资料&#xff1a;https://www.zhihu.com/column/c_124250…

基于单片机的智能台灯 灯光控制系统人体感应楼梯灯系统的设计与实现

功能介绍 以STM32单片机作为主控系统&#xff1b;主通过光敏采集当前光线强度&#xff1b;通过PMW灯光调节电路&#xff0c;我们可以根据不同的光线亮度&#xff0c;进行3挡调节&#xff1b;通过人体红外检测当前是否有人&#xff1b;通过不同光线情况下使用PWM脉冲电路进行调节…