人工蜂羣算法(二)

ABC算法步驟推薦這個,一步一步講的很通透,http://mf.erciyes.edu.tr/abc/pub/Step%20by%20Step%20Procedure%20of%20ABC.pdf

跟着用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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章