ABC算法
ABC算法步骤推荐这个,⼀步⼀步讲的很通透,
跟着⽤MATLAB 写了下代码。
[plain]
01. %/* ABC algorithm coded using MATLAB language */
02. clear all
03. close all
04. clc
05. global objfun D ub lb
06.
07.
08. %注:这⾥没有说明objfun是什么,但是屏现有的知识,可以判断,它对应学习资料《step by step procedure of
09. %ABC》上的f(x)= (x_1)^2+(x_2)^2
10.
11. %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 ro
12. %                      /*汉语解释Foods[][],它代表了⾷物源集,⼆维矩阵,不难理解,每⾏代表某⾷物源的D个属性,⾏数
FoodNumber等于⾷物源数量*/
13. %ObjVal[FoodNumber];  /*f is a vector holding objective function values associated with food sources */
14. %                    /*ObjVal[]是个向量,存储了⽬标函数值*/
15. %Fitness[FoodNumber]; /*fitness is a vector holding fitness (quality) values associated with food sources*/
16. %                    /*Fitness[]是个向量,存储了fitness 值*/
17. %trial[FoodNumber]; /*trial is a vector holding trial numbers through which solutions can not be improved*/
18. %                  /*trial 是个向量,证明哪个⾷物源没被提⾼*/
19. %prob[FoodNumber]; /*prob is a vector holding probabilities of food sources (solutions) to be chosen*/
20. %                  /*prob 存储概率*/
21. %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*/
22. %              /*solution [D] 产⽣⼀个新的⾷物源,⽤的公式跟资料上的⼀模⼀样*/
23. %ObjValSol; /*Objective function value of new solution*/
24. %          /*新⾷物源的⽬标函数值*/
25. %FitnessSol; /*Fitness value of new solution*/
26. %          /*新⾷物源的fitness值*/
27. %neighbour, param2change; /*param2change corrresponds to j, neighbour corresponds to k in equation v_{ij}=x_{ij}+\phi_{ij}*
(x_{kj}-x_{ij})*/
28. %          /*这两个参数对应着产⽣新⾷物源公式的j,k参数*/
29. %GlobalMin; /*Optimum solution obtained by ABC algorithm*/
30. %          /*利⽤ABC算法得到的最佳的⾷物源?*/
31. %GlobalParams[D]; /*Parameters of the optimum solution*/
32. %          /*最佳⾷物源参数*/
33. %GlobalMins[runtime]; /*GlobalMins holds the GlobalMin of each run in multiple runs*/
34. %          /**/
35.
36. GlobalMins=zeros(1,runtime);
37.
38. for r=1:runtime
39.
40. % /*All food sources are initialized */
41. %/*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 */
42.
43. Range = repmat((ub-lb),[FoodNumber 1]);
44. Lower = repmat(lb, [FoodNumber 1]);
45. Foods = rand(FoodNumber,D) .* Range + Lower; %这⾥隐藏知识,因为+号要求左右两端横纵维数相同,所以得知,Range
和Lower 都是FoodNumber⾏,D列,所以得知上⾯,ub和lb分别是1⾏D列的
46.
47. ObjVal=feval(objfun,Foods);
48. Fitness=calculateFitness(ObjVal); %注:这⾥的calculateFitness并没有实现,但是源码作者想让读者明⽩,执⾏的是计算
Fitness值的运算。
49.
50. %reset trial counters
51. trial=zeros(1,FoodNumber);
52.
53. %/*The best food source is memorized*/
54. %注:下⾯这个find(ObjVal==min(ObjVal)) 套式经常⽤,到最⼩值所处的所有位置
55. BestInd=find(ObjVal==min(ObjVal));
56. BestInd=BestInd(end);
57.
58. GlobalMin=ObjVal(BestInd);
59. GlobalParams=Foods(BestInd,:);
60.
61. iter=1;
62. while ((iter <= maxCycle)),
63.
64. %%%%%%%%% EMPLOYED BEE PHASE %%%%%%%%%%%%%%%%%%%%%%%%
65.    for i=1:(FoodNumber)
66.
67.        %/*The parameter to be changed is determined randomly*/
68.        Param2Change=fix(rand*D)+1;
69.
70.        %/*A randomly chosen solution is used in producing a mutant solution of the solution i*/
71.        neighbour=fix(rand*(FoodNumber))+1;
72.
73.        %/*Randomly selected solution must be different from the solution i*/
74.            while(neighbour==i)
75.                neighbour=fix(rand*(FoodNumber))+1;
76.            end;
77.
78.        sol=Foods(i,:); %sol代表solution ⼀个⾷物源,在matlab语⾔中 x(i,:)代表x矩阵中第i⾏的所有元素
79.        %  /*v_{ij}=x_{ij}+\phi_{ij}*(x_{kj}-x_{ij}) */
80.        sol(Param2Change)=Foods(i,Param2Change)+(Foods(i,Param2Change)-Foods(neighbour,Param2Change))*(rand-
0.5)*2;
81.
82.        %  /*if generated parameter value is out of boundaries, it is shifted onto the boundaries*/
83.        ind=find(sol<lb);
84.        sol(ind)=lb(ind);
85.        ind=find(sol>ub);
86.        sol(ind)=ub(ind);
87.
88.        %evaluate new solution
89.        ObjValSol=feval(objfun,sol);
90.        FitnessSol=calculateFitness(ObjValSol);
91.
92.        % /*a greedy selection is applied between the current solution i and its mutant*/
93.        if (FitnessSol>Fitness(i)) %/*If the mutant solution is better than the current solution i, replace the solution with the mutant and reset the
94.            Foods(i,:)=sol;
95.            Fitness(i)=FitnessSol;
96.            ObjVal(i)=ObjValSol;
97.            trial(i)=0;
98.        else
99.            trial(i)=trial(i)+1; %/*if the solution i can not be improved, increase its trial counter*/
100.        end;
101.
102.
103.    end;
104.
105. %%%%%%%%%%%%%%%%%%%%%%%% CalculateProbabilities %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 106. %/* A food source is chosen with the probability which is proportioal to its quality*/
107. %/*Different schemes can be used to calculate the probability values*/
108. %/*For example prob(i)=fitness(i)/sum(fitness)*/
109. %/*or in a way used in the metot below prob(i)=a*fitness(i)/max(fitness)+b*/
110. %/*probability values are calculated by using fitness values and normalized by dividing maximum fitness value*/
111.
112. prob=(0.9.*Fitness./max(Fitness))+0.1;
113.
114. %%%%%%%%%%%%%%%%%%%%%%%% ONLOOKER BEE PHASE %%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%% 115. %/*注:这段代码跟楼上⼀样,其实在onlooker bee 阶段,做的⼯作跟employed bee⼀样的。*/
116. i=1;
117. t=0;
118. while(t<FoodNumber)
119.    if(rand<prob(i))
120.        t=t+1;
121.        %/*The parameter to be changed is determined randomly*/
122.        Param2Change=fix(rand*D)+1;
123.
124.        %/*A randomly chosen solution is used in producing a mutant solution of the solution i*/
125.        neighbour=fix(rand*(FoodNumber))+1;
126.
127.        %/*Randomly selected solution must be different from the solution i*/
128.            while(neighbour==i)
parameter是什么意思啊129.                neighbour=fix(rand*(FoodNumber))+1;
130.            end;
131.
132.        sol=Foods(i,:);
133.        %  /*v_{ij}=x_{ij}+\phi_{ij}*(x_{kj}-x_{ij}) */
134.        sol(Param2Change)=Foods(i,Param2Change)+(Foods(i,Param2Change)-Foods(neighbour,Param2Change))*(rand-
0.5)*2;
135.
136.        %  /*if generated parameter value is out of boundaries, it is shifted onto the boundaries*/
137.        ind=find(sol<lb);
138.        sol(ind)=lb(ind);
139.        ind=find(sol>ub);
140.        sol(ind)=ub(ind);
141.
142.        %evaluate new solution
143.        ObjValSol=feval(objfun,sol);
144.        FitnessSol=calculateFitness(ObjValSol);
145.
146.        % /*a greedy selection is applied between the current solution i and its mutant*/
147.        if (FitnessSol>Fitness(i)) %/*If the mutant solution is better than the current solution i, replace the solution with the mutant and reset the 148.            Foods(i,:)=sol;
149.            Fitness(i)=FitnessSol;
150.            ObjVal(i)=ObjValSol;
151.            trial(i)=0;
152.        else
153.            trial(i)=trial(i)+1; %/*if the solution i can not be improved, increase its trial counter*/
154.        end;
155.    end;
156.
157.    i=i+1;
158.    if (i==(FoodNumber)+1)
159.        i=1;
160.    end;
161. end;
162.
163.
164. %/*The best food source is memorized*/
165.          ind=find(ObjVal==min(ObjVal));
166.          ind=ind(end);
167.          if (ObjVal(ind)<GlobalMin)
168.          GlobalMin=ObjVal(ind);
169.          GlobalParams=Foods(ind,:);
170.          end;
171.
172.
173. %%%%%%%%%%%% SCOUT BEE PHASE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 174. %/*注:其实在scout bee阶段,就是根据trial counter 做处理*/
175. %/*determine the food sources whose trial counter exceeds the "limit" value.
176. %In Basic ABC, only one scout is allowed to occur in each cycle*/
177.
178. ind=find(trial==max(trial));
179. ind=ind(end);
180. if (trial(ind)>limit)
181.    Bas(ind)=0;
182.    sol=(ub-lb).*rand(1,D)+lb;
183.    ObjValSol=feval(objfun,sol);
184.    FitnessSol=calculateFitness(ObjValSol);
185.    Foods(ind,:)=sol;
186.    Fitness(ind)=FitnessSol;
187.    ObjVal(ind)=ObjValSol;
188. end;
189.
190.
191.
192. fprintf('ter=%d ObjVal=%g\n',iter,GlobalMin);
193. iter=iter+1;
194.
195. end % End of ABC
196.
197. GlobalMins(r)=GlobalMin;
198. end; %end of runs
199.
200. save all