% The Whale Optimization Algorithm
function [Leader_score,Leader_pos,Convergence_curve]=WOA(SearchAgents_no,Max_iter,dim,lb,ub,x,y,d0)
% initialize position vector and score for the leader
Leader_pos=zeros(1,1);
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=1;% Loop counter
% 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
for ij = 1:dim
if Positions(i,ij)>ub(ij);
Positions(i,ij)=(ub(ij)-lb(ij))*rand()+lb(ij);
elseif Positions(i,ij)<lb(ij);
Positions(i,ij)=(ub(ij)-lb(ij))*rand()+lb(ij);
end
end
% Calculate objective function for each search agent
fitness=fun(Positions(i,:),x,y,d0);
% 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=2.1*rand(); % r1 is a random number in [0,1]
r2=2.1*rand(); % r2 is a random number in [0,1]
A=2*a*r1-a; % Eq. (2.3) in the paper
C=2*r2; % Eq. (2.4) in the paper
b=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.9
if abs(A)>=1
rand_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)<1
D_Leader=abs(C*Leader_pos(j)-Positions(i,j)); % Eq. (2.1)
Positions(i,j)=Leader_pos(j)-A*D_Leader; % Eq. (2.2)
end
elseif p>=0.9
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
Convergence_curve(t)=Leader_score;
% [t Leader_score]
if rand()<0.5;
n1 = randperm(20);
n2 = 30;
n1 = n1(1);
xfunt = funt(Leader_pos(n1:n2),Leader_pos(n1:n2));
yfunt = Leader_pos(n1:n2);
Leader_pos(n1:n2) = yfunt(xfunt);
end
Leader_score1 = fun(Leader_pos,x,y,d0);
if Leader_score1<Leader_score
Leader_score=Leader_score1;
Convergence_curve(t)=Leader_score;
Positions(i,:)=Leader_pos;
end
end
t=t+1;
end
% Leader_pos = funt(x,y);
[~,Leader_pos] = sort(Leader_pos);
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
基于MATLAB编程,鲸鱼算法求解tsp,代码完整,包含数据,有注释,方便扩展应用 1,如有疑问,不会运行,可以私信, 2,需要创新,或者修改可以扫描二维码联系博主, 3,本科及本科以上可以下载应用或者扩展, 4,内容不完全匹配要求或需求,可以联系博主扩展。
资源推荐
资源详情
资源评论
收起资源包目录
鲸鱼算法求解TSP.rar (7个子文件)
initialization.m 567B
mainshu.m 277B
funt.m 1KB
main.m 557B
WOA.m 3KB
maydata.mat 719B
fun.m 162B
共 7 条
- 1
资源评论
- m0_565994302024-03-25资源内容总结的很到位,内容详实,很受用,学到了~
- qxjie_7724562024-03-07资源内容总结的很到位,内容详实,很受用,学到了~
神经网络机器学习智能算法画图绘图
- 粉丝: 2441
- 资源: 599
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功