人工蜂群算法(Artificial Bee Colony, ABC)MATALAB代码详细解析

该代码是Matlab官网的代码,非常适合学习模拟退火算法的原理和实现。

理论参考另一篇博客
模拟退火算法(SA)、遗传算法(GA)、布谷鸟算法(CS)、人工蜂群算法(ABC)学习笔记—附MATLAB注释代码

一、人工蜂群算法理论

二、算法流程图

人工蜂群算法(Artificial Bee Colony, ABC)MATALAB代码详细解析_第1张图片

二、代码讲解

1、 ABC.m文件

% Copyright (c) 2015, Yarpiz (www.yarpiz.com)
% All rights reserved. Please read the "license.txt" for license terms.
%
% Project Code: YPEA114
% Project Title: Implementation of Artificial Bee Colony in MATLAB
% Publisher: Yarpiz (www.yarpiz.com)
% 
% Developer: S. Mostapha Kalami Heris (Member of Yarpiz Team)
% 
% Contact Info: [email protected], [email protected]
%

clc;
clear;
close all;

%% Problem Definition

CostFunction=@(x) Sphere(x);        % Cost Function函数句柄,未知数是x,相当于建立了一个函数文件,类似在C语言中的函数定义。该方法在Sphere.m中定义

nVar=5;             % Number of Decision Variables问题的维度

VarSize=[1 nVar];   % Decision Variables Matrix Size定义一个问题维度大小的矩阵

VarMin=-10;         % Decision Variables Lower Bound函数的下限
VarMax= 10;         % Decision Variables Upper Bound上限

%% ABC Settings

MaxIt=1000;              % Maximum Number of Iterations迭代次数上限

nPop=100;               % Population Size (Colony Size)初始雇佣蜂数量

nOnlooker=nPop;         % Number of Onlooker Bees初始观察蜂数量

L=round(0.6*nVar*nPop); % Abandonment Limit Parameter (Trial Limit)  round():四舍五入取整,表示蜜源试验次数上限,如果达到此上限,舍弃该蜜源,侦查蜂来做的这一步
a=1;                    % Acceleration Coefficient Upper Bound蜜源变换的加速系数的最大值

%% Initialization

% Empty Bee Structure
empty_bee.Position=[];%%蜜源
empty_bee.Cost=[];%%蜜源适应度

% Initialize Population Array
pop=repmat(empty_bee,nPop,1);%把矩阵empty_bee作为pop的nPop×1矩阵的元素(矩阵中的矩阵),表示有100个雇佣蜂干活

% Initialize Best Solution Ever Found因为是求函数的最小值,所以预定义最坏适应度是无穷大
BestSol.Cost=inf;

% Create Initial Population在定义域范围内,随机初始化蜜源位置
for i=1:nPop
    pop(i).Position=unifrnd(VarMin,VarMax,VarSize);%%unifrnd(-10,10,[1 5]):表示产生均匀分布的随机数,产生一个1*5的随机数矩阵,其值在-10到10之间均匀分布
    pop(i).Cost=CostFunction(pop(i).Position);
    if pop(i).Cost<=BestSol.Cost%%蜜源适应度较好的保留
        BestSol=pop(i);
    end
end

% Abandonment Counter
C=zeros(nPop,1);%%用来记录蜜源的试验限制,达到L次数,就舍弃蜜源,用新的替代这个蜜源
% Array to Hold Best Cost Values
BestCost=zeros(MaxIt,1);%%记录迄今为止最好的蜜源的适应度

%% ABC Main Loop

for it=1:MaxIt
    
    %%%%%%%%%%%%%%%%% 雇佣蜂Employed Bees  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    for i=1:nPop
      
        % Choose k randomly, not equal to i
        %k=[1:i-1 i+1:10]:生成不包括i的1到10之间的数
        K=[1:i-1 i+1:nPop];%生成1~i的数和i+1~100的数,组成数组
        k=K(randi([1 numel(K)]));%%从K的数组随机取一个数
        
        % Define Acceleration Coeff.定义加速系数
        phi=a*unifrnd(-1,+1,VarSize);%%生成一个1×5的矩阵,数的范围是-1~1
        % New Bee Position更具加速系数更新一个蜜源位置
        newbee.Position=pop(i).Position+phi.*(pop(i).Position-pop(k).Position);
        
        % Evaluation计算新蜜源的适应度值
        newbee.Cost=CostFunction(newbee.Position);
        
        % Comparision
        if newbee.Cost<=pop(i).Cost
            pop(i)=newbee;
        else
            C(i)=C(i)+1;%%不好的蜜源,浏览该蜜源次数加一,浏览次数达到L次就舍弃该蜜源
        end
        
    end
    
    % Calculate Fitness Values and Selection Probabilities
    F=zeros(nPop,1);%以下是公式去计算蜜源的选择概率
    MeanCost = mean([pop.Cost]);
    for i=1:nPop
        F(i) = exp(-pop(i).Cost/MeanCost); % Convert Cost to Fitness
    end
    P=F/sum(F);%%蜜源的选择概率
    %%%%%%%%%%%%%%%%% 雇佣蜂Employed Bees  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %%%%%%%%%%%%%%%%% 观察蜂Onlooker Bees  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    for m=1:nOnlooker%让每个观察蜂去干该干的事情
        
        % Select Source Site根据选择概率 进行轮盘赌选择一个蜜源
        i=RouletteWheelSelection(P);
        %%%%%%%%%%%%%%%%%%%%%%在该蜜源附近选择一个新蜜源来替代它%%%%%%%%%%%%%%%
        % Choose k randomly, not equal to i
        K=[1:i-1 i+1:nPop];
        k=K(randi([1 numel(K)]));
        
        % Define Acceleration Coeff.定义加速系数
        phi=a*unifrnd(-1,+1,VarSize);
        
        % New Bee Position替代这个蜜源
        newbee.Position=pop(i).Position+phi.*(pop(i).Position-pop(k).Position);
        
        % Evaluation计算适应度值
        newbee.Cost=CostFunction(newbee.Position);
        
        % Comparision判断蜜源留下还是舍弃
        if newbee.Cost<=pop(i).Cost
            pop(i)=newbee;
        else
            C(i)=C(i)+1;
        end
        %%%%%%%%%%%%%%%%%%%%%%又重新更新蜜源信息%%%%%%%%%%%%
    end
    %%%%%%%%%%%%%%%%% 侦查蜂Onlooker Bees  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    
    %%%%%%%%%%%%%%%%% Scout Bees %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    for i=1:nPop
        if C(i)>=L%%判断蜜源是否达到试验上限,达到了就丢弃蜜源
            pop(i).Position=unifrnd(VarMin,VarMax,VarSize);%重新建立一个蜜源替代原来的蜜源
            pop(i).Cost=CostFunction(pop(i).Position);
            C(i)=0;
        end
    end
    
    % Update Best Solution Ever Found
    for i=1:nPop
        if pop(i).Cost<=BestSol.Cost
            BestSol=pop(i);%%寻找最佳蜜源
        end
    end
    %%%%%%%%%%%%%%%%% Scout Bees %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % Store Best Cost Ever Found
    BestCost(it)=BestSol.Cost;%%存储最佳蜜源
    
    % Display Iteration Information
    %%disp(['Iteration ' num2str(it) ': Best Cost = ' num2str(BestCost(it))]);
    
end
    
%% Results
BestSol.Position%%打印解的结果
%%%适应度值变换曲线图
plot(BestCost,'LineWidth',2);
semilogy(BestCost,'LineWidth',2);
xlabel('Iteration');
ylabel('Best Cost');
grid on;

2、Sephere.m文件

% Copyright (c) 2015, Yarpiz (www.yarpiz.com)
% All rights reserved. Please read the "license.txt" for license terms.
%
% Project Code: YPEA114
% Project Title: Implementation of Artificial Bee Colony in MATLAB
% Publisher: Yarpiz (www.yarpiz.com)
% 
% Developer: S. Mostapha Kalami Heris (Member of Yarpiz Team)
% 
% Contact Info: [email protected], [email protected]
%
%%代价函数
function z=Sphere(x)

    z=sum(x.^2);

end

3、RouletteWheelSelection.m文件

理论是
人工蜂群算法(Artificial Bee Colony, ABC)MATALAB代码详细解析_第2张图片

代码如下:

% Copyright (c) 2015, Yarpiz (www.yarpiz.com)
% All rights reserved. Please read the "license.txt" for license terms.
%
% Project Code: YPEA114
% Project Title: Implementation of Artificial Bee Colony in MATLAB
% Publisher: Yarpiz (www.yarpiz.com)
% 
% Developer: S. Mostapha Kalami Heris (Member of Yarpiz Team)
% 
% Contact Info: [email protected], [email protected]
%
%%轮盘赌选择算法
function i=RouletteWheelSelection(P)

    r=rand;
    
    C=cumsum(P);
    %%%返回满足r<=C的第一个元素在C中的位置
    i=find(r<=C,1,'first');

end

二、源码下载

人工蜂群算法MATLAB详细注释.zip

你可能感兴趣的:(算法学习笔记,算法,机器学习,深度学习)