跟着用MATLAB 写了下代码。
%/* ABC algorithm coded using MATLAB language */
clear all
close all
clc
global objfun D ub lb
%注:这里没有说明objfun是什么,但是屏现有的知识,可以判断,它对应学习资料《step by step procedure of
%ABC》上的f(x)= (x_1)^2+(x_2)^2
%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*/
% /*汉语解释Foods[][],它代表了食物源集,二维矩阵,不难理解,每行代表某食物源的D个属性,行数FoodNumber等于食物源数量*/
%ObjVal[FoodNumber]; /*f is a vector holding objective function values associated with food sources */
% /*ObjVal[]是个向量,存储了目标函数值*/
%Fitness[FoodNumber]; /*fitness is a vector holding fitness (quality) values associated with food sources*/
% /*Fitness[]是个向量,存储了fitness 值*/
%trial[FoodNumber]; /*trial is a vector holding trial numbers through which solutions can not be improved*/
% /*trial 是个向量,证明哪个食物源没被提高*/
%prob[FoodNumber]; /*prob is a vector holding probabilities of food sources (solutions) to be chosen*/
% /*prob 存储概率*/
%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*/
% /*solution [D] 产生一个新的食物源,用的公式跟资料上的一模一样*/
%ObjValSol; /*Objective function value of new solution*/
% /*新食物源的目标函数值*/
%FitnessSol; /*Fitness value of new solution*/
% /*新食物源的fitness值*/
%neighbour, param2change; /*param2change corrresponds to j, neighbour corresponds to k in equation v_{ij}=x_{ij}+\phi_{ij}*(x_{kj}-x_{ij})*/
% /*这两个参数对应着产生新食物源公式的j,k参数*/
%GlobalMin; /*Optimum solution obtained by ABC algorithm*/
% /*利用ABC算法得到的最佳的食物源?*/
%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; %这里隐藏知识,因为+号要求左右两端横纵维数相同,所以得知,Range和Lower 都是FoodNumber行,D列,所以得知上面,ub和lb分别是1行D列的
ObjVal=feval(objfun,Foods);
Fitness=calculateFitness(ObjVal); %注:这里的calculateFitness并没有实现,但是源码作者想让读者明白,执行的是计算Fitness值的运算。
%reset trial counters
trial=zeros(1,FoodNumber);
%/*The best food source is memorized*/
%注:下面这个find(ObjVal==min(ObjVal)) 套式经常用,找到最小值所处的所有位置
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,:); %sol代表solution 一个食物源,在matlab语言中 x(i,:)代表x矩阵中第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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%/*注:这段代码跟楼上一样,其实在onlooker bee 阶段,做的工作跟employed bee一样的。*/
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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%/*注:其实在scout bee阶段,就是根据trial counter 做处理*/
%/*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