function [Best_flame_score, Best_flame_pos, Convergence_curve] = MFO(Moth_pos, N, Max_iteration, lb, ub, dim, fobj)
% % 初始化飞蛾位置
% Moth_pos = initialization(N, dim, ub, lb);
Convergence_curve = zeros(1, Max_iteration);
Iteration = 1;
%% 迭代
while Iteration < Max_iteration+1
%% 弃焰行为
% Eq. (3.14) in the paper--火焰自适应数量
Flame_no = round(N-Iteration*((N-1)/Max_iteration));
for i = 1:N
% 边界处理
Flag4ub = Moth_pos(i, :) > ub;
Flag4lb = Moth_pos(i, :) < lb;
Moth_pos(i, :) = (Moth_pos(i, :).*(~(Flag4ub+Flag4lb)))+ub.*Flag4ub+lb.*Flag4lb;
% 计算适应度值
Moth_fitness(i) = fobj(Moth_pos(i, :));
end
if Iteration == 1
% 排序
[fitness_sorted, I] = sort(Moth_fitness);
sorted_population = Moth_pos(I, :);
% 更新火焰
best_flames = sorted_population;
best_flame_fitness = fitness_sorted;
else
% 排序
double_population = [previous_population; best_flames];
double_fitness = [previous_fitness best_flame_fitness];
[double_fitness_sorted,I] = sort(double_fitness);
double_sorted_population = double_population(I, :);
fitness_sorted = double_fitness_sorted(1:N);
sorted_population = double_sorted_population(1:N, :);
% 更新火焰
best_flames = sorted_population;
best_flame_fitness = fitness_sorted;
end
% 更新全局最优解
Best_flame_score = fitness_sorted(1);
Best_flame_pos = sorted_population(1,:);
previous_population = Moth_pos;
previous_fitness = Moth_fitness;
%% 捕焰行为
% a从-1到-2线性递减,以计算公式(3.12)中的t
a = -1+Iteration*((-1)/Max_iteration);
for i = 1:N
for j = 1:dim
if i <= Flame_no % 更新飞蛾相对于相应火焰的位置
% D in Eq. (3.13)
distance_to_flame = abs(sorted_population(i,j)-Moth_pos(i,j));
b = 1;
t = (a-1)*rand+1;
% Eq. (3.12)
Moth_pos(i, j) = distance_to_flame*exp(b.*t).*cos(t.*2*pi)+sorted_population(i, j);
else % 更新飞蛾相对于相应火焰的位置
% Eq. (3.13)
distance_to_flame = abs(sorted_population(i, j)-Moth_pos(i, j));
b = 1;
t = (a-1)*rand+1;
% Eq. (3.12)
Moth_pos(i, j) = distance_to_flame*exp(b.*t).*cos(t.*2*pi)+sorted_population(Flame_no, j);
end
end
end
Convergence_curve(Iteration) = Best_flame_score;
% 显示迭代信息
display(['MFO:At iteration ', num2str(Iteration), ' the best fitness is ', num2str(Best_flame_score)]);
Iteration = Iteration+1;
end
[1]刘倩, 毛耀. 基于高斯变异的改进型飞蛾火焰优化算法[J]. 电脑编程技巧与维护, 2020(11):2.
部分理论引用网络文献,若有侵权联系博主删除。