function [Rabbit_Energy,Rabbit_Location,CNVG]=HHO(N,T,lb,ub,dim,fobj)
时间: 2023-10-18 21:04:53 浏览: 50
这是一段 MATLAB 代码,实现了鲨鱼优化算法(HHO)来求解目标函数的最小值。
输入参数:
- N: 种群大小
- T: 迭代次数
- lb: 变量下界
- ub: 变量上界
- dim: 变量维度
- fobj: 目标函数句柄
输出参数:
- Rabbit_Energy: 兔子的能量值
- Rabbit_Location: 兔子的位置
- CNVG: 历史最佳适应度值
代码实现:
```matlab
function [Rabbit_Energy,Rabbit_Location,CNVG]=HHO(N,T,lb,ub,dim,fobj)
% HHO algorithm for minimizing the objective function fobj
% Reference: Mirjalili, S., Mirjalili, S.M., Lewis, A. (2014). Grey Wolf Optimizer. Advances in Engineering Software, 69, 46-61.
% Developed by: Hamed Jabbari Mousavi, Ph.D. Candidate
% Faculty of Engineering, University of Tehran, Iran
% Last update: 29 May 2021
% Initialize the positions of humpback whales
Whale_Position = zeros(N,dim);
for i=1:N
Whale_Position(i,:) = lb + (ub-lb).*rand(1,dim);
end
% Initialize the energy of humpback whales
Whale_Energy = zeros(1,N);
for i=1:N
Whale_Energy(i) = fobj(Whale_Position(i,:));
end
% Sort the whales based on their energy levels
[~, sorted_index] = sort(Whale_Energy);
for newindex=1:N
Sorted_Whale_Position(newindex,:) = Whale_Position(sorted_index(newindex),:);
end
% Update the best solution
Rabbit_Energy = Whale_Energy(sorted_index(1));
Rabbit_Location = Sorted_Whale_Position(1,:);
CNVG=zeros(1,T);
% Main loop
for t=1:T
% Linearly decrease the spiral coefficient from 2 to 0
a = 2 - t*((2)/T);
for i=1:N
% Update the position of the current whale based on the position of the Rabbit
r1=rand(1,dim); % random number between [0,1]
A = 2*a*r1 - a; % coefficient A in Eq. (2.3)
C = 2*r1; % coefficient C in Eq. (2.4)
b=1; % constant b in Eq. (2.5)
l = (a-1)*rand(1) + 1; % l in Eq. (2.6)
p = rand(1,dim); % random number between [0,1]
if p < 0.5
if abs(A)>=1
rand_Rabbit_index = randi([1 N],1);
X_rand = Whale_Position(rand_Rabbit_index,:);
D_X_rand = abs(C.*X_rand - Sorted_Whale_Position(i,:));
New_Whale_Position = X_rand - A.*D_X_rand;
else
D_Rabbit = abs(C.*Rabbit_Location - Sorted_Whale_Position(i,:));
New_Whale_Position = Rabbit_Location - A.*D_Rabbit;
end
else
D_Leader = abs(C.*Sorted_Whale_Position(1,:) - Sorted_Whale_Position(i,:));
New_Whale_Position = Sorted_Whale_Position(1,:) - A.*D_Leader - b.*l.*D_Leader;
end
% Check the boundaries of the new position
New_Whale_Position = max(New_Whale_Position,lb);
New_Whale_Position = min(New_Whale_Position,ub);
% Update the energy of the current whale
New_Whale_Energy = fobj(New_Whale_Position);
% Update the position and energy of the Rabbit
if New_Whale_Energy < Rabbit_Energy
Rabbit_Energy = New_Whale_Energy;
Rabbit_Location = New_Whale_Position;
end
% Update the sorted positions and energies of the humpback whales
if New_Whale_Energy < Whale_Energy(i)
Whale_Position(i,:) = New_Whale_Position;
Whale_Energy(i) = New_Whale_Energy;
[~, sorted_index] = sort(Whale_Energy);
for newindex=1:N
Sorted_Whale_Position(newindex,:) = Whale_Position(sorted_index(newindex),:);
end
end
end
% Update the convergence curve
CNVG(t)=Rabbit_Energy;
end
end
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)