ABC(Artificial Bee Colony)算法,人工蜂群算法,是由Karaboga于2005年提出的一种新颖的基于群智能的全局优化算法,来源于蜂群的采蜜行为,蜜蜂根据各自的分工进行不同的活动,并实现蜂群信息的共享和交流,从而找到问题的最优解。人工蜂群算法属于群智能优化算法的一种。
该方法主要分为三个阶段:采蜜蜂发现新的蜜源、观察蜂根据采蜜蜂的蜜源寻找新的蜜源,侦查蜂寻找新的蜂源代替多次没有更新的蜜源;
算法流程如下:
1、初始化蜜源及其花蜜量(自变量向量及对应的适应度函数值),最优的蜜源和花蜜量;
2、对于每一个蜜源:利用当前蜜源信息、任一蜜源(随机选择)和最优蜜源寻找新的蜜源,选择更新,同时记录每个蜜源未更新的次数到,同时更新蜂群最优蜜源及花蜜量;
3、对于每一个蜜源:以一定概率更新每个蜜源,这个概率和每个蜜源的花蜜量相关,花蜜量越大的蜜源被更新的可能性越大,同时更新蜂群最优蜜源及花蜜量;;
4、统计每个蜜源更新的次数,最大未更新次数查过预定值,随机初始化蜜源信息;
5、达到终止条件结束,输出最优蜜源和花蜜量;否则转至步骤2;
附一个简单的ABC代码:
function [best_x,best_f,FEs_cut,avgErr_FEs,bestErr_FEs,sussFlag]= ABC(D,Max_FEs,func_num,LB,UB,opt_f,err)
%by zxr 2017/3
NP = D; %No. of foods
SF = 1;
limit = 200;
FoodNumber=ceil(NP/2); %/*The number of food sources equals the half of the colony size*/
% avgErr_FEs = -1*ones(1,Max_FEs);
% bestErr_FEs = -1*ones(1,Max_FEs);
% limit=FoodNumber*D;
% /*All food sources are initialized */
%/*Variables are initialized in the range [LB,UB]. If each parameter has different range, use arrays LB[j], UB[j] instead of LB and UB */
%/* Control Parameters of ABC algorithm*/
Range = UB-LB;
Lower = LB;
% ObjVal=benchmark_func(Foods, func_num);
for FoodID = 1:FoodNumber
Foods(FoodID,:) = rand(1,D) .* Range + Lower;
ObjVal(FoodID) = func(Foods(FoodID,:), func_num, opt_f);
end
FEs = FoodNumber;
Fitness=calculateFitness(ObjVal);
%reset trial counters
trial=zeros(1,FoodNumber);
%/*The best food source is memorized*/
BestInd=find(ObjVal==min(ObjVal));
BestInd=BestInd(end);
GlobalMin=ObjVal(BestInd);
GlobalParams=Foods(BestInd,:);
avgErr_FEs(1:FEs)= mean(ObjVal);
bestErr_FEs(1:FEs)= GlobalMin ;
stop = 0;
FEs_cut = -1;
sussFlag = 0;
while (stop < 1 ),
%%%%%%%%% EMPLOYED BEE PHASE %%%%%%%%%%%%%%%%%%%%%%%%
for i=1:(FoodNumber)
%/*A randomly chosen solution is used in producing a mutant solution of the solution i*/
neighbour=fix(rand*(FoodNumber))+1;
%/*Randomly selected solution must be different from the solution i*/
while(neighbour==i)
neighbour=fix(rand*(FoodNumber))+1;
end;
sol=Foods(i,:);
for j=1:D
sol(j)=Foods(i,j)+(Foods(i,j)-Foods(neighbour,j))*(rand-0.5)*2 * SF;
end
% /*if generated parameter value is out of boundaries, it is shifted onto the boundaries*/
ind=find(solUB);
sol(ind)=UB(ind);
%evaluate new solution
ObjValSol=func(sol, func_num,opt_f);
FEs = FEs + 1;
if (FEs>Max_FEs) % Too many FE
stop=1;
break;
end
if GlobalMin <= err && ~sussFlag
sussFlag = 1;
FEs_cut = FEs;
end
FitnessSol=calculateFitness(ObjValSol);
% /*a greedy selection is applied between the current solution i and its mutant*/
if (FitnessSol>Fitness(i)) %/*If the mutant solution is better than the current solution i, replace the solution with the mutant and reset the trial counter of solution i*/
Foods(i,:)=sol;
Fitness(i)=FitnessSol;
ObjVal(i)=ObjValSol;
trial(i)=0;
else
trial(i)=trial(i)+1; %/*if the solution i can not be improved, increase its trial counter*/
end;
avgErr_FEs(FEs)= mean(ObjVal) ;
bestErr_FEs(FEs)= GlobalMin ;
end;
%%%%%%%%%%%%%%%%%%%%%%%% CalculateProbabilities %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%/* A food source is chosen with the probability which is proportioal to its quality*/
%/*Different schemes can be used to calculate the probability values*/
%/*For example prob(i)=fitness(i)/sum(fitness)*/
%/*or in a way used in the metot below prob(i)=a*fitness(i)/max(fitness)+b*/
%/*probability values are calculated by using fitness values and normalized by dividing maximum fitness value*/
prob=(0.9.*Fitness./max(Fitness))+0.1;
%%%%%%%%%%%%%%%%%%%%%%%% ONLOOKER BEE PHASE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
i=1;
t=0;
while(tif(rand1;
%/*A randomly chosen solution is used in producing a mutant solution of the solution i*/
neighbour=fix(rand*(FoodNumber))+1;
%/*Randomly selected solution must be different from the solution i*/
while(neighbour==i)
neighbour=fix(rand*(FoodNumber))+1;
end;
sol=Foods(i,:);
for j=1:D
sol(j)=Foods(i,j)+(Foods(i,j)-Foods(neighbour,j))*(rand-0.5)*2 * SF;
end
% /*if generated parameter value is out of boundaries, it is shifted onto the boundaries*/
ind=find(solUB);
sol(ind)=UB(ind);
%evaluate new solution
ObjValSol=func(sol, func_num,opt_f);
FEs = FEs + 1;
if (FEs>Max_FEs) % Too many FE
stop=1;
break;
end
if GlobalMin <= err && ~sussFlag
sussFlag = 1;
FEs_cut = FEs;
end
FitnessSol=calculateFitness(ObjValSol);
% /*a greedy selection is applied between the current solution i and its mutant*/
if (FitnessSol>Fitness(i)) %/*If the mutant solution is better than the current solution i, replace the solution with the mutant and reset the trial counter of solution i*/
Foods(i,:)=sol;
Fitness(i)=FitnessSol;
ObjVal(i)=ObjValSol;
trial(i)=0;
else
trial(i)=trial(i)+1; %/*if the solution i can not be improved, increase its trial counter*/
end;
avgErr_FEs(FEs)= mean(ObjVal) ;
bestErr_FEs(FEs)= GlobalMin ;
end;
i=i+1;
if (i==(FoodNumber)+1)
i=1;
end;
end;
%/*The best food source is memorized*/
ind=find(ObjVal==min(ObjVal));
ind=ind(end);
if (ObjVal(ind)%%%%%%%%%%%% SCOUT BEE PHASE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%/*determine the food sources whose trial counter exceeds the "limit" value.
%In Basic ABC, only one scout is allowed to occur in each cycle*/
ind=find(trial==max(trial));
ind=ind(end);
if (trial(ind)>limit)
trial(ind)=0;%Bas(ind)=0;
sol=(UB-LB).*rand(1,D)+LB;
ObjValSol=func(sol, func_num,opt_f);
FEs = FEs + 1;
if (FEs>Max_FEs) % Too many FE
stop=1;
break;
end
if GlobalMin <= err && ~sussFlag
sussFlag = 1;
FEs_cut = FEs;
end
FitnessSol=calculateFitness(ObjValSol);
Foods(ind,:)=sol;
Fitness(ind)=FitnessSol;
ObjVal(ind)=ObjValSol;
avgErr_FEs(FEs)= mean(ObjVal) ;
bestErr_FEs(FEs)= GlobalMin ;
end;
if ( (FEs>=Max_FEs ) )%||((GlobalMin-opt_f) <=err)
stop=1;
end
end % End of ABC
best_x =GlobalParams;
best_f = GlobalMin;