【优化算法】果蝇算法(FOA)【含Matlab源码 1568期】

举报
海神之光 发表于 2022/05/29 00:31:12 2022/05/29
【摘要】 一、获取代码方式 获取代码方式1: 通过订阅紫极神光博客付费专栏,凭支付凭证,私信博主,可获得此代码。 获取代码方式2: 完整代码已上传我的资源:【优化算法】果蝇算法(FOA)【含Matlab源码 1...

一、获取代码方式

获取代码方式1:
通过订阅紫极神光博客付费专栏,凭支付凭证,私信博主,可获得此代码。

获取代码方式2:
完整代码已上传我的资源:【优化算法】果蝇算法(FOA)【含Matlab源码 1568期】

备注:
订阅紫极神光博客付费专栏,可免费获得1份代码(有效期为订阅日起,三天内有效);

二、果蝇优化算法简介

果蝇优化算法(FOA)是一种基于果蝇觅食行为推演出寻求全局优化的新方法。果蝇本身在感官知觉上优于其他物种,尤其是嗅觉和视觉上。果蝇的嗅觉器官能很好的搜集漂浮在空气中的各种气味,甚至能够嗅到40公里以外的食物源。然后,飞到食物位置附近后亦可使用敏锐的视觉发现食物和同伴聚集的位置,并且向该方向飞去。
果蝇算法可应用于求解最优解。
在这里插入图片描述
果蝇群体迭代搜寻食物的步骤如下:
(1)随机初始化果蝇群体位置。
Init X_axis
Init Y_axis

(2)赋予果蝇个体利用嗅觉搜寻食物的随机距离与方向。
Xi = X_axis + Random Value
Yi = Y_axis + Random Value

(3)由于无法得知食物的位置,因此先估计与原点的距离(Dist),再计算味道浓度判定值(S),此值为距离的倒数。
Disti = sqrt(Xi^2 + Yi^2)
Si = 1 / Disti

(4)味道浓度判定值(S)代入味道浓度判定函数(或称为Fitness function)以求出该果蝇个体位置的味道浓度(Smelli)。
Smelli = Function(Si)

(5)找出该果蝇群体中味道浓度最高的果蝇(求极大值)。
[bestSmell bestIndex] = max(Smell)

(6)保留最佳味道浓度值与x、y的坐标,此时果蝇群体利用视觉往该位置飞去。
Smellbest = bestSmell
X_axis = X(bestIndex)
Y_axis = Y(bestIndex)

(7)进入迭代寻优,重复执行步骤2-5,并判断味道浓度是否优于前一迭代味道浓度,若是则实行步骤6。

三、部分源代码

% Fruit Fly Optimization Algorithm,FOA.

% The standard verison programmed by Prof.Pan is a simplifed version which
% is used to test a very easy function.In my opinion,in order to enhance
% the FOA application field,it is necessary to change it into a general
% version,which may be simple for students and scholars to use.

%
function [Smellbest,X,Y] = FOA(n,maxt,lb,ub,dim)

% Parameters setting
if nargin < 1
    n = 20; % Population size
    maxt = 5e2; % Max iterations
    dim = 30; % Dimension of test function
    lb = -100 * ones(1,dim); % Lower bound of test function
    ub = 100 * ones(1,dim); % Upper bound of test function
end




% X = zeros(1 * dim);
% Y = zeros(1 * dim);
% new_X = zeros(1 * dim);
% new_Y = zeros(1 * dim);
% D = zeros(1 * dim);
% Sol = zeros(1 * dim);
% Fitness = zeros(n * 1);

% Initialize the original position
for i = 1:n
    X(i,:) = lb+(ub-lb).*rand(1,dim); % the position of X axis
    Y(i,:) = lb+(ub-lb).*rand(1,dim); % the position of Y axis
    D(i,:) = (X(i,:).^2 + Y(i,:).^2).^0.5; % Caculate the distance
    Sol(i,:) = 1./D(i,:); % the solution set
    Fitness(i) = fun(Sol(i,:)); % Caculate the fitness
end


[bestSmell,index] = min(Fitness); % Get the min fitness and its index
new_X = X(index,:); % the X axis of min fitness
new_Y = Y(index,:); % the Y axis of min fitness
Smellbest = bestSmell;
best = Sol(index,:);

% Start main loop
for t = 1:maxt
    for i = 1:n
        % Refer to the process of initializing
        X(i,:) = new_X + (ub - lb).*rand(1,dim);
        Y(i,:) = new_Y + (ub - lb).*rand(1,dim);
        D(i,:) = (X(i,:).^2 + Y(i,:).^2).^0.5;
        Sol(i,:) = 1./D(i,:);
        Fitness(i) = fun(Sol(i,:));
    end
    [bestSmell,index] = min(Fitness);
    % If the new value is smaller than the best value,update the best value
    if (bestSmell < Smellbest)
        X(i,:) = X(index,:);
        Y(i,:) = Y(index,:);
        Smellbest = bestSmell;
    end
    
    % Out put result each 100 iterations
    if round(t/100) == (t/100)
        Smellbest;
    end
    
    cg_curve(t) = Smellbest;
end

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71

四、运行结果

在这里插入图片描述

五、matlab版本及参考文献

1 matlab版本
2014a

2 参考文献
[1] 包子阳,余继周,杨杉.智能优化算法及其MATLAB实例(第2版)[M].电子工业出版社,2016.
[2]张岩,吴水根.MATLAB优化算法源代码[M].清华大学出版社,2017.

文章来源: qq912100926.blog.csdn.net,作者:海神之光,版权归原作者所有,如需转载,请联系作者。

原文链接:qq912100926.blog.csdn.net/article/details/121777506

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。