工蜂群算法(Artificial Bee Colony Algorithm,ABC)是一种模仿蜜蜂采蜜机理而产生的群智能优化算法。其原理相对复杂,但实现较为简单,在许多领域中都有研究和应用。
人工蜂群算法中,每一个蜜源的位置代表了待求问题的一个可行解。蜂群分为采蜜蜂、观察蜂和侦查蜂。采蜜蜂与蜜源对应,一个采蜜蜂对应一个蜜源。观察蜂则会根据采蜜蜂分享的蜜源相关信息选择跟随哪个采蜜蜂去相应的蜜源,同时该观察蜂将转变为侦查蜂。侦查蜂则自由的搜索新的蜜源。每一个蜜源都有开采的限制次数,当一个蜜源被采蜜多次而达到开采限制次数时,在该蜜源采蜜的采蜜蜂将转变为侦查蜂。每个侦查蜂将随机寻找一个新蜜源进行开采,并转变成为采蜜蜂。
下面是我的实现方式(我的答案):
1.三种蜜蜂之间可以相互转化。
采蜜蜂->观察蜂:有观察蜂在采蜜过程中发现了比当前采蜜蜂更好的蜜源,则采蜜蜂放弃当前蜜源转而变成观察蜂跟随优质蜜源,同时该观察蜂转变为采蜜蜂。
采蜜蜂->观察蜂:当该采蜜蜂所发现的蜜源被开采完后,它会转变为观察蜂去跟随其他采蜜蜂。
采蜜蜂->侦查蜂:当所有的采蜜蜂发现的蜜源都被开采完后,采蜜蜂将会变为侦查蜂,观察蜂也会变成侦查蜂,因为大家都无蜜可采。
侦查蜂->采蜜蜂、观察蜂:侦查蜂随机搜索蜜源,选择较好的数个蜜源位置的蜜蜂为采蜜蜂,其他蜜蜂为观察蜂。
2.蜜源的数量上限
蜜源的数量上限等于采蜜蜂的数量上限。初始化时所有蜜蜂都是侦查蜂,在这些侦查蜂所搜索到的蜜源中选出数个较优的蜜源,发现这些蜜源的侦查蜂变为采蜜蜂,其他蜜蜂变为观察蜂。直到所有的蜜源都被开采完之前,蜜源的数量不会增加,因为这个过程中没有产生侦查蜂。所有的蜜源都被开采完后,所有的蜜蜂再次全部转化为侦查蜂,新的一轮蜜源搜索开始。也可以在一个蜜源开采完时马上产生一个新的蜜源补充,保证在整个开采过程中蜜源数量恒定不变。
蜜源的开采实际上就是观察蜂跟随采蜜蜂飞向蜜源的过程。得到的下一代的位置公式如下:
表示第i只观察蜂在第t代时随机选择第r只采蜜蜂飞行一段距离,其中R为(-1,1)的随机数。
一只观察蜂在一次迭代过程中只能选择一只采蜜蜂跟随,它需要从众多的采蜜蜂中选择一只来进行跟随。观察蜂选择的策略很简单,随机跟随一只采蜜蜂,该采蜜蜂发现的蜜源越优,则选择它的概率越大。
是不是很像轮盘赌,对,这就是轮盘赌,同时我们也可以稍作修改,比如将勤劳的小蜜蜂改为懒惰的小蜜蜂,小蜜蜂会根据蜜源的优劣和距离以及开采程度等因素综合来选择跟随哪只采蜜蜂(虽然影响不大,但聊胜于无)。
忘记了轮盘赌的小伙伴可以看一下 优化算法笔记(六)遗传算法 。
下面是我的人工蜂群算法流程图
又到了实验环节,参数实验较多,全部给出将会占用太多篇幅,仅将结果进行汇总展示。
实验1:参数如下
上图分别为采蜜蜂上限为10%总数和50%总数的情况,可以看出当采蜜蜂上限为10%总群数时,种群收敛的速度较快,但是到最后有一个点死活不动,这是因为该点作为一个蜜源,但由于适应度值太差,使用轮盘赌被选择到的概率太小从而没有得到更佳的蜜源位置,而因未开采完,采蜜蜂又不能放弃该蜜源。
看了看采蜜蜂上限为50%总群数时的图,发现也有几个点不动的状态,可以看出,这时不动的点的数量明显多于上限为10%总数的图,原因很简单,采蜜蜂太多,“先富”的人太多,而“后富”的人较少,没有带动“后富者”的“先富者”也得不到发展。
看看结果
嗯,感觉结果并没有什么差别,可能由于问题较简单,迭代次数较少,无法体现出采蜜蜂数对于结果的影响,也可能由于蜜源的搜索次数为60较大,总群一共只能对最多20*50/60=16个蜜源进行搜索。我们将最大迭代次数调大至200代再看看结果
当最大迭代次数为200时,人工蜂群算法的结果如上图,我们可以明显的看出,随着采蜜蜂上限的上升,算法结果的精度在不断的下降,这也印证了之前的结果,由于蜜源搜索次数较大(即搜索深度较深)采蜜蜂数量越多(搜索广度越多),结果的精度越低。不过影响也不算太大,下面我们再来看看蜜源最大开采次数对结果的影响。
实验2:参数如下
上图分别是蜜源开采限度为1,20和4000的实验。
当蜜源开采上限为1时,即一个蜜源只能被开采一次,即此时的人工蜂群算法只有侦查蜂随机搜索的过程,没有观察蜂跟随采蜜蜂的过程,可以看出图中的蜜蜂一直在不断的随机出现在新位置不会向某个点收敛。
当蜜源开采上限为20时,我们可以看到此时种群中的蜜蜂都会向一个点飞行。在一段时间内,有数个点一动不动,这些点可能就是采蜜蜂发现的位置不怎么好的蜜源,但是在几次迭代之后,它们仍会被观察蜂开采,从而更新位置,蜜源开采上限越高,它们停顿的代数也会越长。在所有蜜蜂都收敛于一个点之后,我们可以看到仍会不断的出现其他的随机点,这些点是侦查蜂进行随机搜索产生的新的蜜源位置,这些是人工蜂群算法跳出局部最优能力的体现。
当蜜源开采上限为4000时,即不会出现侦查蜂的搜索过程,观察蜂只会开采初始化时出现的蜜源而不会采蜜蜂不会有新的蜜源产生,可以看出在蜂群收敛后没有出现新的蜜源位置。
看看最终结果,我们发现,当蜜源开采上线大于1时的结果提升,但是好像开采上限为5时结果明显好于开采次数上限为其他的结果,而且随着开采次数不断上升,结果在不断的变差。为什么会出现这样的结果呢?原因可能还是因为问题较为简单,在5次开采的限度内,观察蜂已经能找到更好的蜜源进行开采,当问题较为复杂时,我们无法知晓开采发现新蜜源的难度,蜜源开采上限应该取一个相对较大的值。当蜜源开采限度为4000时,即一个蜜源不可能被开采完(开采次数为20(种群数)*200(迭代次数)),搜索的深度有了但是其结果反而不如开采限度为几次几十次来的好,而且这样不会有侦查蜂随机搜索的过程,失去了跳出局部最优的能力。
我们应该如何选择蜜源的最大开采次数限制呢?其实,没有最佳的开采次数限制,当适应度函数较为简单时,开采次数较小时能得到比较好的结果,但是适应度函数较复杂时,经过试验,得出的结果远差于开采次数较大时。当然,前面就说过,适应度函数是一个黑盒模型,我们无法判断问题的难易。那么我们应该选择一个适中的值,个人的选择是种群数的0.5倍到总群数的2倍作为蜜源的最大开采次数,这样可以保证极端情况下,1-2个迭代周期内小蜜蜂们能将一个蜜源开采完。
人工蜂群算法算是一个困扰我比较长时间的算法,几年时间里,我根据文献实现的人工蜂群算法都有数十种,只能说人工蜂群算法的描述太过模糊,或者说太过抽象,研究者怎么实现都说的通。但是通过实现多次之后发现虽然实现细节大不相同,但效果相差不多,所以我们可以认为人工蜂群算法的稳定性比较强,只要实现其主要思想即可,细节对于结果的影响不太大。
对于人工蜂群算法影响最大的因素还是蜜源的开采次数限制,开采次数限制越大,对同一蜜源的开发力度越大,但是分配给其他蜜源的搜索力度会相对减少,也会降低蜂群算法的跳出局部最优能力。可以动态修改蜜源的开采次数限制来实现对算法的改进,不过效果不显著。
其次对于人工蜂群算法影响是三类蜜蜂的搜索行为,我们可以重新设计蜂群的搜索方式来对算法进行改进,比如采蜜蜂在开采蜜源时是随机飞向其他蜜源,而观察蜂向所选的蜜源靠近。这样改进有一定效果但是在高维问题上效果仍不明显。
以下指标纯属个人yy,仅供参考
目录
上一篇 优化算法笔记(七)差分进化算法
下一篇 优化算法笔记(九)杜鹃搜索算法
优化算法matlab实现(八)人工蜂群算法matlab实现
那个代码不完整,里面用到的Distance和Min函数都没有定义,而从函数的输入输出参数和作用看,并不是系统自带的distance和min函数。
最好的办法的是你再找一找,看有没有更完整的代码,如果找不到,我帮你补了这两个函数,程序可以运行,但我不确定是不是符合算法的本意。
我补的那两个函数放家里了,要是你找不到更好的解决办法,可以追问,我回家后贴出代码。
http://emuch.net/bbs/attachment.php?tid=3808850&aid=11221&pay=yes里面有多个文件
其中之一
%/* ABC algorithm coded using MATLAB language */
%/* Artificial Bee Colony (ABC) is one of the most recently defined algorithms by Dervis Karaboga in 2005, motivated by the intelligent behavior of honey bees. */
%/* Referance Papers*/
%/*D. Karaboga, AN IDEA BASED ON HONEY BEE SWARM FOR NUMERICAL OPTIMIZATION,TECHNICAL REPORT-TR06, Erciyes University, Engineering Faculty, Computer Engineering Department 2005.*/
%/*D. Karaboga, B. Basturk, A powerful and Efficient Algorithm for Numerical Function Optimization: Artificial Bee Colony (ABC) Algorithm, Journal of Global Optimization, Volume:39, Issue:3,pp:459-171, November 2007,ISSN:0925-5001 , doi: 10.1007/s10898-007-9149-x */
%/*D. Karaboga, B. Basturk, On The Performance Of Artificial Bee Colony (ABC) Algorithm, Applied Soft Computing,Volume 8, Issue 1, January 2008, Pages 687-697. */
%/*D. Karaboga, B. Akay, A Comparative Study of Artificial Bee Colony Algorithm, Applied Mathematics and Computation, 214, 108-132, 2009. */
%/*Copyright ?2009 Erciyes University, Intelligent Systems Research Group, The Dept. of Computer Engineering*/
%/*Contact:
%Dervis Karaboga (karaboga@erciyes.edu.tr )
%Bahriye Basturk Akay (bahriye@erciyes.edu.tr)
%*/
clear all
close all
clc
%/* Control Parameters of ABC algorithm*/
NP=20%/* The number of colony size (employed bees+onlooker bees)*/
FoodNumber=NP/2%/*The number of food sources equals the half of the colony size*/
limit=100%/*A food source which could not be improved through "limit" trials is abandoned by its employed bee*/
maxCycle=2500%/*The number of cycles for foraging {a stopping criteria}*/
%/* Problem specific variables*/
objfun='Sphere'%cost function to be optimized
D=100%/*The number of parameters of the problem to be optimized*/
ub=ones(1,D)*100%/*lower bounds of the parameters. */
lb=ones(1,D)*(-100)%/*upper bound of the parameters.*/
runtime=1%/*Algorithm can be run many times in order to see its robustness*/
%Foods [FoodNumber][D]/*Foods is the population of food sources. Each row of Foods matrix is a vector holding D parameters to be optimized. The number of rows of Foods matrix equals to the FoodNumber*/
%ObjVal[FoodNumber] /*f is a vector holding objective function values associated with food sources */
%Fitness[FoodNumber]/*fitness is a vector holding fitness (quality) values associated with food sources*/
%trial[FoodNumber]/*trial is a vector holding trial numbers through which solutions can not be improved*/
%prob[FoodNumber]/*prob is a vector holding probabilities of food sources (solutions) to be chosen*/
%solution [D]/*New solution (neighbour) produced by v_{ij}=x_{ij}+\phi_{ij}*(x_{kj}-x_{ij}) j is a randomly chosen parameter and k is a randomlu chosen solution different from i*/
%ObjValSol/*Objective function value of new solution*/
%FitnessSol/*Fitness value of new solution*/
%neighbour, param2change/*param2change corrresponds to j, neighbour corresponds to k in equation v_{ij}=x_{ij}+\phi_{ij}*(x_{kj}-x_{ij})*/
%GlobalMin/*Optimum solution obtained by ABC algorithm*/
%GlobalParams[D]/*Parameters of the optimum solution*/
%GlobalMins[runtime]/*GlobalMins holds the GlobalMin of each run in multiple runs*/
GlobalMins=zeros(1,runtime)
for r=1:runtime
% /*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 */
Range = repmat((ub-lb),[FoodNumber 1])
Lower = repmat(lb, [FoodNumber 1])
Foods = rand(FoodNumber,D) .* Range + Lower
ObjVal=feval(objfun,Foods)
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,:)
iter=1
while ((iter <= maxCycle)),
%%%%%%%%% EMPLOYED BEE PHASE %%%%%%%%%%%%%%%%%%%%%%%%
for i=1:(FoodNumber)
%/*The parameter to be changed is determined randomly*/
Param2Change=fix(rand*D)+1
%/*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,:)
% /*v_{ij}=x_{ij}+\phi_{ij}*(x_{kj}-x_{ij}) */
sol(Param2Change)=Foods(i,Param2Change)+(Foods(i,Param2Change)-Foods(neighbour,Param2Change))*(rand-0.5)*2
% /*if generated parameter value is out of boundaries, it is shifted onto the boundaries*/
ind=find(sol<lb)
sol(ind)=lb(ind)
ind=find(sol>ub)
sol(ind)=ub(ind)
%evaluate new solution
ObjValSol=feval(objfun,sol)
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
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(t<FoodNumber)
if(rand<prob(i))
t=t+1
%/*The parameter to be changed is determined randomly*/
Param2Change=fix(rand*D)+1
%/*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,:)
% /*v_{ij}=x_{ij}+\phi_{ij}*(x_{kj}-x_{ij}) */
sol(Param2Change)=Foods(i,Param2Change)+(Foods(i,Param2Change)-Foods(neighbour,Param2Change))*(rand-0.5)*2
% /*if generated parameter value is out of boundaries, it is shifted onto the boundaries*/
ind=find(sol<lb)
sol(ind)=lb(ind)
ind=find(sol>ub)
sol(ind)=ub(ind)
%evaluate new solution
ObjValSol=feval(objfun,sol)
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
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)<GlobalMin)
GlobalMin=ObjVal(ind)
GlobalParams=Foods(ind,:)
end
%%%%%%%%%%%% 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)
Bas(ind)=0
sol=(ub-lb).*rand(1,D)+lb
ObjValSol=feval(objfun,sol)
FitnessSol=calculateFitness(ObjValSol)
Foods(ind,:)=sol
Fitness(ind)=FitnessSol
ObjVal(ind)=ObjValSol
end
fprintf('Ýter=%d ObjVal=%g\n',iter,GlobalMin)
iter=iter+1
end % End of ABC
GlobalMins(r)=GlobalMin
end%end of runs
save all
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)