人工蜂群算法

1、ABC算法原理

在基本ABC算法中,人工蜂群包含三种个体:雇佣蜂、观察蜂和侦查蜂

每个雇佣蜂对应一个确定的蜜源(解向量),并在迭代中对蜜源的领域进行搜索。

根据蜜源的丰富程度(适应值的大小)采用轮盘赌的方式雇佣观察蜂采蜜(搜索新蜜源)

如果蜜源多次更新没有改进,则放弃该蜜源,雇佣蜂转为侦查蜂随机搜索新蜜源。

  (1)   随机初始化

  (2)   新蜜源的更新搜索公式

  (3)   观察蜂选择雇佣蜂的概率

(4) 侦查蜂的产生

为了防止算法陷入局部最优,当蜜源迭代limit次没有改进时,便放弃该蜜源,并且将该蜜源记录在禁忌表中,同时该蜜源对应的雇佣蜂转变为侦查蜂按式(1)随机产生一个新的位置代替原蜜源。



2、控制参数

(1)   蜜源的个数(与雇佣蜂或观察蜂相等)SN

(2)   算法终止的最大进化数(maximum evaluation number)MEN

(3)   Limit


3、基本ABC算法的流程为

(1)   根据式(1)初始化种群解xii=1,2…,SN;

(2)   计算种群中各蜜蜂的适应值

(3)   cycle=1

(4)   repeat

(5)   雇佣蜂根据(2)产生新的解vi并计算适应值

(6)   雇佣蜂根据贪心策略选择蜜源

(7)   根据(3)式计算选择蜜源xi的概率pi

(8)   观察蜂根据概率pi选择蜜源xi,根据(2)式在该蜜源附近产生新的蜜源vi,并计算新蜜源vi的适应值

(9)   观察蜂根据贪心算法选择蜜源

(10)   决定是否存在需要放弃的蜜源,如果存在,根据(1)式随机产生一个蜜源代替它

(11)   记录最优解

(12)   cycle = cycle + 1

(13)   until cycle=MCN

4、 算法可能的改进方式

(1)   新蜜源的搜索领域(2)式的改进(如:其他拓扑邻域)

(2)   观察蜂选择雇佣蜂的概率(3)式的改进(如:动态的)


5、C++程序

[cpp]  view plain  copy
  1. #include<iostream>    
  2. #include<time.h>    
  3. #include<stdlib.h>    
  4. #include<cmath>    
  5. #include<fstream>    
  6. #include<iomanip>    
  7. using namespace std;    
  8.     
  9. const int NP=40;//种群的规模,采蜜蜂+观察蜂    
  10. const int FoodNumber=NP/2;//食物的数量,为采蜜蜂的数量    
  11. const int limit=20;//限度,超过这个限度没有更新采蜜蜂变成侦查蜂    
  12. const int maxCycle=10000;//停止条件    
  13.     
  14. /*****函数的特定参数*****/    
  15. const int D=2;//函数的参数个数    
  16. const double lb=-100;//函数的下界     
  17. const double ub=100;//函数的上界    
  18.     
  19. double result[maxCycle]={0};    
  20.     
  21. /*****种群的定义****/    
  22. struct BeeGroup    
  23. {    
  24.     double code[D];//函数的维数    
  25.     double trueFit;//记录真实的最小值    
  26.     double fitness;    
  27.     double rfitness;//相对适应值比例    
  28.     int trail;//表示实验的次数,用于与limit作比较    
  29. }Bee[FoodNumber];    
  30.     
  31. BeeGroup NectarSource[FoodNumber];//蜜源,注意:一切的修改都是针对蜜源而言的    
  32. BeeGroup EmployedBee[FoodNumber];//采蜜蜂    
  33. BeeGroup OnLooker[FoodNumber];//观察蜂    
  34. BeeGroup BestSource;//记录最好蜜源    
  35.     
  36. /*****函数的声明*****/    
  37. double random(doubledouble);//产生区间上的随机数    
  38. void initilize();//初始化参数    
  39. double calculationTruefit(BeeGroup);//计算真实的函数值    
  40. double calculationFitness(double);//计算适应值    
  41. void CalculateProbabilities();//计算轮盘赌的概率    
  42. void evalueSource();//评价蜜源    
  43. void sendEmployedBees();    
  44. void sendOnlookerBees();    
  45. void sendScoutBees();    
  46. void MemorizeBestSource();    
  47.     
  48.     
  49. /*******主函数*******/    
  50. int main()    
  51. {    
  52.     ofstream output;    
  53.     output.open("dataABC.txt");    
  54.     
  55.     srand((unsigned)time(NULL));    
  56.     initilize();//初始化    
  57.     MemorizeBestSource();//保存最好的蜜源    
  58.             
  59.     //主要的循环    
  60.     int gen=0;    
  61.     while(gen<maxCycle)    
  62.     {    
  63.         sendEmployedBees();    
  64.                 
  65.         CalculateProbabilities();    
  66.                 
  67.         sendOnlookerBees();    
  68.                 
  69.         MemorizeBestSource();    
  70.                 
  71.         sendScoutBees();    
  72.                 
  73.         MemorizeBestSource();    
  74.     
  75.         output<<setprecision(30)<<BestSource.trueFit<<endl;    
  76.                 
  77.         gen++;    
  78.     }    
  79.         
  80.     output.close();    
  81.     cout<<"运行结束!!"<<endl;    
  82.     return 0;    
  83. }    
  84.     
  85. /*****函数的实现****/    
  86. double random(double start, double end)//随机产生区间内的随机数    
  87. {       
  88.     return start+(end-start)*rand()/(RAND_MAX + 1.0);    
  89. }    
  90.     
  91. void initilize()//初始化参数    
  92. {    
  93.     int i,j;    
  94.     for (i=0;i<FoodNumber;i++)    
  95.     {    
  96.         for (j=0;j<D;j++)    
  97.         {    
  98.             NectarSource[i].code[j]=random(lb,ub);    
  99.             EmployedBee[i].code[j]=NectarSource[i].code[j];    
  100.             OnLooker[i].code[j]=NectarSource[i].code[j];    
  101.             BestSource.code[j]=NectarSource[0].code[j];    
  102.         }    
  103.         /****蜜源的初始化*****/    
  104.         NectarSource[i].trueFit=calculationTruefit(NectarSource[i]);    
  105.         NectarSource[i].fitness=calculationFitness(NectarSource[i].trueFit);    
  106.         NectarSource[i].rfitness=0;    
  107.         NectarSource[i].trail=0;    
  108.         /****采蜜蜂的初始化*****/    
  109.         EmployedBee[i].trueFit=NectarSource[i].trueFit;    
  110.         EmployedBee[i].fitness=NectarSource[i].fitness;    
  111.         EmployedBee[i].rfitness=NectarSource[i].rfitness;    
  112.         EmployedBee[i].trail=NectarSource[i].trail;    
  113.         /****观察蜂的初始化****/    
  114.         OnLooker[i].trueFit=NectarSource[i].trueFit;    
  115.         OnLooker[i].fitness=NectarSource[i].fitness;    
  116.         OnLooker[i].rfitness=NectarSource[i].rfitness;    
  117.         OnLooker[i].trail=NectarSource[i].trail;    
  118.     }    
  119.     /*****最优蜜源的初始化*****/    
  120.     BestSource.trueFit=NectarSource[0].trueFit;    
  121.     BestSource.fitness=NectarSource[0].fitness;    
  122.     BestSource.rfitness=NectarSource[0].rfitness;    
  123.     BestSource.trail=NectarSource[0].trail;    
  124. }    
  125.     
  126. double calculationTruefit(BeeGroup bee)//计算真实的函数值    
  127. {    
  128.     double truefit=0;    
  129.     /******测试函数1******/    
  130.     truefit=0.5+(sin(sqrt(bee.code[0]*bee.code[0]+bee.code[1]*bee.code[1]))*sin(sqrt(bee.code[0]*bee.code[0]+bee.code[1]*bee.code[1]))-0.5)    
  131.         /((1+0.001*(bee.code[0]*bee.code[0]+bee.code[1]*bee.code[1]))*(1+0.001*(bee.code[0]*bee.code[0]+bee.code[1]*bee.code[1])));    
  132.     
  133.     return truefit;    
  134. }    
  135.     
  136. double calculationFitness(double truefit)//计算适应值    
  137. {    
  138.     double fitnessResult=0;    
  139.     if (truefit>=0)    
  140.     {    
  141.         fitnessResult=1/(truefit+1);    
  142.     }else    
  143.     {    
  144.         fitnessResult=1+abs(truefit);    
  145.     }    
  146.     return fitnessResult;    
  147. }    
  148.     
  149. void sendEmployedBees()//修改采蜜蜂的函数    
  150. {    
  151.     int i,j,k;    
  152.     int param2change;//需要改变的维数    
  153.     double Rij;//[-1,1]之间的随机数    
  154.     for (i=0;i<FoodNumber;i++)    
  155.     {    
  156.             
  157.         param2change=(int)random(0,D);//随机选取需要改变的维数    
  158.     
  159.         /******选取不等于i的k********/    
  160.         while (1)    
  161.         {    
  162.             k=(int)random(0,FoodNumber);    
  163.             if (k!=i)    
  164.             {    
  165.                 break;    
  166.             }    
  167.         }    
  168.     
  169.         for (j=0;j<D;j++)    
  170.         {    
  171.             EmployedBee[i].code[j]=NectarSource[i].code[j];    
  172.         }    
  173.     
  174.         /*******采蜜蜂去更新信息*******/    
  175.         Rij=random(-1,1);    
  176.         EmployedBee[i].code[param2change]=NectarSource[i].code[param2change]+Rij*(NectarSource[i].code[param2change]-NectarSource[k].code[param2change]);    
  177.         /*******判断是否越界********/    
  178.         if (EmployedBee[i].code[param2change]>ub)    
  179.         {    
  180.             EmployedBee[i].code[param2change]=ub;    
  181.         }    
  182.         if (EmployedBee[i].code[param2change]<lb)    
  183.         {    
  184.             EmployedBee[i].code[param2change]=lb;    
  185.         }    
  186.         EmployedBee[i].trueFit=calculationTruefit(EmployedBee[i]);    
  187.         EmployedBee[i].fitness=calculationFitness(EmployedBee[i].trueFit);    
  188.     
  189.         /******贪婪选择策略*******/    
  190.         if (EmployedBee[i].trueFit<NectarSource[i].trueFit)    
  191.         {    
  192.             for (j=0;j<D;j++)    
  193.             {    
  194.                 NectarSource[i].code[j]=EmployedBee[i].code[j];    
  195.             }    
  196.             NectarSource[i].trail=0;    
  197.             NectarSource[i].trueFit=EmployedBee[i].trueFit;    
  198.             NectarSource[i].fitness=EmployedBee[i].fitness;    
  199.         }else    
  200.         {    
  201.             NectarSource[i].trail++;    
  202.         }    
  203.     }    
  204. }    
  205.     
  206. void CalculateProbabilities()//计算轮盘赌的选择概率    
  207. {    
  208.     int i;    
  209.     double maxfit;    
  210.     maxfit=NectarSource[0].fitness;    
  211.     for (i=1;i<FoodNumber;i++)    
  212.     {    
  213.         if (NectarSource[i].fitness>maxfit)    
  214.             maxfit=NectarSource[i].fitness;    
  215.     }    
  216.         
  217.     for (i=0;i<FoodNumber;i++)    
  218.     {    
  219.         NectarSource[i].rfitness=(0.9*(NectarSource[i].fitness/maxfit))+0.1;    
  220.     }    
  221. }    
  222.     
  223. void sendOnlookerBees()//采蜜蜂与观察蜂交流信息,观察蜂更改信息    
  224. {    
  225.     int i,j,t,k;    
  226.     double R_choosed;//被选中的概率    
  227.     int param2change;//需要被改变的维数    
  228.     double Rij;//[-1,1]之间的随机数    
  229.     i=0;    
  230.     t=0;    
  231.     while(t<FoodNumber)    
  232.     {    
  233.             
  234.         R_choosed=random(0,1);    
  235.         if(R_choosed<NectarSource[i].rfitness)//根据被选择的概率选择    
  236.         {            
  237.             t++;    
  238.             param2change=(int)random(0,D);    
  239.                 
  240.             /******选取不等于i的k********/    
  241.             while (1)    
  242.             {    
  243.                 k=(int)random(0,FoodNumber);    
  244.                 if (k!=i)    
  245.                 {    
  246.                     break;    
  247.                 }    
  248.             }    
  249.     
  250.             for(j=0;j<D;j++)    
  251.             {    
  252.                 OnLooker[i].code[j]=NectarSource[i].code[j];    
  253.             }    
  254.                 
  255.             /****更新******/    
  256.             Rij=random(-1,1);    
  257.             OnLooker[i].code[param2change]=NectarSource[i].code[param2change]+Rij*(NectarSource[i].code[param2change]-NectarSource[k].code[param2change]);    
  258.                 
  259.             /*******判断是否越界*******/    
  260.             if (OnLooker[i].code[param2change]<lb)    
  261.             {    
  262.                 OnLooker[i].code[param2change]=lb;    
  263.             }    
  264.             if (OnLooker[i].code[param2change]>ub)    
  265.             {       
  266.                 OnLooker[i].code[param2change]=ub;    
  267.             }    
  268.             OnLooker[i].trueFit=calculationTruefit(OnLooker[i]);    
  269.             OnLooker[i].fitness=calculationFitness(OnLooker[i].trueFit);    
  270.                 
  271.             /****贪婪选择策略******/    
  272.             if (OnLooker[i].trueFit<NectarSource[i].trueFit)    
  273.             {    
  274.                 for (j=0;j<D;j++)    
  275.                 {    
  276.                     NectarSource[i].code[j]=OnLooker[i].code[j];    
  277.                 }    
  278.                 NectarSource[i].trail=0;    
  279.                 NectarSource[i].trueFit=OnLooker[i].trueFit;    
  280.                 NectarSource[i].fitness=OnLooker[i].fitness;    
  281.             }else    
  282.             {    
  283.                 NectarSource[i].trail++;    
  284.             }    
  285.         }     
  286.         i++;    
  287.         if (i==FoodNumber)    
  288.         {    
  289.             i=0;    
  290.         }    
  291.     }    
  292. }    
  293.     
  294.     
  295. /*******只有一只侦查蜂**********/    
  296. void sendScoutBees()//判断是否有侦查蜂的出现,有则重新生成蜜源    
  297. {    
  298.     int maxtrialindex,i,j;    
  299.     double R;//[0,1]之间的随机数    
  300.     maxtrialindex=0;    
  301.     for (i=1;i<FoodNumber;i++)    
  302.     {    
  303.         if (NectarSource[i].trail>NectarSource[maxtrialindex].trail)    
  304.         {    
  305.             maxtrialindex=i;    
  306.         }    
  307.     }    
  308.     if(NectarSource[maxtrialindex].trail>=limit)    
  309.     {    
  310.         /*******重新初始化*********/    
  311.         for (j=0;j<D;j++)    
  312.         {    
  313.             R=random(0,1);    
  314.             NectarSource[maxtrialindex].code[j]=lb+R*(ub-lb);    
  315.         }    
  316.         NectarSource[maxtrialindex].trail=0;    
  317.         NectarSource[maxtrialindex].trueFit=calculationTruefit(NectarSource[maxtrialindex]);    
  318.         NectarSource[maxtrialindex].fitness=calculationFitness(NectarSource[maxtrialindex].trueFit);    
  319.     }    
  320. }    
  321.     
  322. void MemorizeBestSource()//保存最优的蜜源    
  323. {    
  324.     int i,j;    
  325.     for (i=1;i<FoodNumber;i++)    
  326.     {    
  327.         if (NectarSource[i].trueFit<BestSource.trueFit)    
  328.         {    
  329.             for (j=0;j<D;j++)    
  330.             {    
  331.                 BestSource.code[j]=NectarSource[i].code[j];    
  332.             }    
  333.             BestSource.trueFit=NectarSource[i].trueFit;    
  334.         }    
  335.     }    
  336. }  

收敛曲线


6、JAVA程序

[java]  view plain  copy
  1. import java.lang.Math;  
  2.   
  3. public  class beeColony {  
  4.   
  5. /* Control Parameters of ABC algorithm*/  
  6.     int NP=20/* The number of colony size (employed bees+onlooker bees)*/  
  7.     int FoodNumber = NP/2/*The number of food sources equals the half of the colony size*/  
  8.     int limit = 100;  /*A food source which could not be improved through "limit" trials is abandoned by its employed bee*/  
  9.     int maxCycle = 2500/*The number of cycles for foraging {a stopping criteria}*/  
  10.   
  11.     /* Problem specific variables*/  
  12.     int D = 100/*The number of parameters of the problem to be optimized*/  
  13.     double lb = -5.12/*lower bound of the parameters. */  
  14.     double ub = 5.12/*upper bound of the parameters. lb and ub can be defined as arrays for the problems of which parameters have different bounds*/  
  15.   
  16. int runtime = 30;  /*Algorithm can be run many times in order to see its robustness*/  
  17.   
  18.     int dizi1[]=new int[10];  
  19.     double Foods[][]=new double[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*/  
  20.     double f[]=new double[FoodNumber];        /*f is a vector holding objective function values associated with food sources */  
  21.     double fitness[]=new double[FoodNumber];      /*fitness is a vector holding fitness (quality) values associated with food sources*/  
  22.     double trial[]=new double[FoodNumber];         /*trial is a vector holding trial numbers through which solutions can not be improved*/  
  23.     double prob[]=new double[FoodNumber];          /*prob is a vector holding probabilities of food sources (solutions) to be chosen*/  
  24.     double solution[]=new double[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*/  
  25.   
  26. double ObjValSol;              /*Objective function value of new solution*/  
  27.     double FitnessSol;              /*Fitness value of new solution*/  
  28.     int neighbour, param2change;                   /*param2change corrresponds to j, neighbour corresponds to k in equation v_{ij}=x_{ij}+\phi_{ij}*(x_{kj}-x_{ij})*/  
  29.   
  30.     double GlobalMin;                       /*Optimum solution obtained by ABC algorithm*/  
  31.     double GlobalParams[]=new double[D];                   /*Parameters of the optimum solution*/  
  32.     double GlobalMins[]=new double[runtime];              
  33.              /*GlobalMins holds the GlobalMin of each run in multiple runs*/  
  34.     double r; /*a random number in the range [0,1)*/  
  35.   
  36.     /*a function pointer returning double and taking a D-dimensional array as argument */  
  37.     /*If your function takes additional arguments then change function pointer definition and lines calling "...=function(solution);" in the code*/  
  38.   
  39. //  typedef double (*FunctionCallback)(double sol[D]);    
  40.   
  41.     /*benchmark functions */  
  42.   
  43. //  double sphere(double sol[D]);  
  44. //  double Rosenbrock(double sol[D]);  
  45. //  double Griewank(double sol[D]);  
  46. //  double Rastrigin(double sol[D]);  
  47.   
  48.     /*Write your own objective function name instead of sphere*/  
  49. //  FunctionCallback function = &sphere;  
  50.   
  51.     /*Fitness function*/  
  52.     double CalculateFitness(double fun)   
  53.      {  
  54.          double result=0;  
  55.          if(fun>=0)  
  56.          {  
  57.              result=1/(fun+1);  
  58.          }  
  59.          else  
  60.          {  
  61.                
  62.              result=1+Math.abs(fun);  
  63.          }  
  64.          return result;  
  65.      }  
  66.   
  67.     /*The best food source is memorized*/  
  68.     void MemorizeBestSource()   
  69.     {  
  70.        int i,j;  
  71.           
  72.         for(i=0;i<FoodNumber;i++)  
  73.         {  
  74.         if (f[i]<GlobalMin)  
  75.             {  
  76.             GlobalMin=f[i];  
  77.             for(j=0;j<D;j++)  
  78.                GlobalParams[j]=Foods[i][j];  
  79.             }  
  80.         }  
  81.      }  
  82.   
  83.     /*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 */  
  84.     /* Counters of food sources are also initialized in this function*/  
  85.   
  86. void init(int index)  
  87.     {  
  88.        int j;  
  89.        for (j=0;j<D;j++)  
  90.             {  
  91.             r = (   (double)Math.random()*32767 / ((double)32767+(double)(1)) );  
  92.             Foods[index][j]=r*(ub-lb)+lb;  
  93.             solution[j]=Foods[index][j];  
  94.             }  
  95.         f[index]=calculateFunction(solution);  
  96.         fitness[index]=CalculateFitness(f[index]);  
  97.         trial[index]=0;  
  98.     }  
  99.   
  100. /*All food sources are initialized */  
  101.     void initial()  
  102.     {  
  103.         int i;  
  104.         for(i=0;i<FoodNumber;i++)  
  105.         {  
  106.         init(i);  
  107.         }  
  108.         GlobalMin=f[0];  
  109.         for(i=0;i<D;i++)  
  110.         GlobalParams[i]=Foods[0][i];  
  111.   
  112. }  
  113.   
  114.     void SendEmployedBees()  
  115.     {  
  116.       int i,j;  
  117.       /*Employed Bee Phase*/  
  118.        for (i=0;i<FoodNumber;i++)  
  119.             {  
  120.             /*The parameter to be changed is determined randomly*/  
  121.             r = ((double) Math.random()*32767 / ((double)(32767)+(double)(1)) );  
  122.             param2change=(int)(r*D);  
  123.               
  124.             /*A randomly chosen solution is used in producing a mutant solution of the solution i*/  
  125.             r = (   (double)Math.random()*32767 / ((double)(32767)+(double)(1)) );  
  126.             neighbour=(int)(r*FoodNumber);  
  127.   
  128.             /*Randomly selected solution must be different from the solution i*/          
  129.            // while(neighbour==i)  
  130.            // {  
  131.            // r = (   (double)Math.random()*32767 / ((double)(32767)+(double)(1)) );  
  132.            // neighbour=(int)(r*FoodNumber);  
  133.            // }  
  134.             for(j=0;j<D;j++)  
  135.             solution[j]=Foods[i][j];  
  136.   
  137.             /*v_{ij}=x_{ij}+\phi_{ij}*(x_{kj}-x_{ij}) */  
  138.             r = (   (double)Math.random()*32767 / ((double)(32767)+(double)(1)) );  
  139.             solution[param2change]=Foods[i][param2change]+(Foods[i][param2change]-Foods[neighbour][param2change])*(r-0.5)*2;  
  140.   
  141.             /*if generated parameter value is out of boundaries, it is shifted onto the boundaries*/  
  142.             if (solution[param2change]<lb)  
  143.                solution[param2change]=lb;  
  144.             if (solution[param2change]>ub)  
  145.                solution[param2change]=ub;  
  146.             ObjValSol=calculateFunction(solution);  
  147.             FitnessSol=CalculateFitness(ObjValSol);  
  148.               
  149.             /*a greedy selection is applied between the current solution i and its mutant*/  
  150.             if (FitnessSol>fitness[i])  
  151.             {  
  152.               
  153.             /*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*/  
  154.             trial[i]=0;  
  155.             for(j=0;j<D;j++)  
  156.             Foods[i][j]=solution[j];  
  157.             f[i]=ObjValSol;  
  158.             fitness[i]=FitnessSol;  
  159.             }  
  160.             else  
  161.             {   /*if the solution i can not be improved, increase its trial counter*/  
  162.                 trial[i]=trial[i]+1;  
  163.             }  
  164.   
  165. }  
  166.   
  167.             /*end of employed bee phase*/  
  168.   
  169.     }  
  170.   
  171.     /* A food source is chosen with the probability which is proportioal to its quality*/  
  172.     /*Different schemes can be used to calculate the probability values*/  
  173.     /*For example prob(i)=fitness(i)/sum(fitness)*/  
  174.     /*or in a way used in the metot below prob(i)=a*fitness(i)/max(fitness)+b*/  
  175.     /*probability values are calculated by using fitness values and normalized by dividing maximum fitness value*/  
  176.     void CalculateProbabilities()  
  177.     {  
  178.          int i;  
  179.          double maxfit;  
  180.          maxfit=fitness[0];  
  181.       for (i=1;i<FoodNumber;i++)  
  182.             {  
  183.                if (fitness[i]>maxfit)  
  184.                maxfit=fitness[i];  
  185.             }  
  186.   
  187.      for (i=0;i<FoodNumber;i++)  
  188.             {  
  189.              prob[i]=(0.9*(fitness[i]/maxfit))+0.1;  
  190.             }  
  191.   
  192.     }  
  193.   
  194.     void SendOnlookerBees()  
  195.     {  
  196.   
  197.       int i,j,t;  
  198.       i=0;  
  199.       t=0;  
  200.       /*onlooker Bee Phase*/  
  201.       while(t<FoodNumber)  
  202.             {  
  203.   
  204.             r = (   (double)Math.random()*32767 / ((double)(32767)+(double)(1)) );  
  205.             if(r<prob[i]) /*choose a food source depending on its probability to be chosen*/  
  206.             {          
  207.             t++;  
  208.               
  209.             /*The parameter to be changed is determined randomly*/  
  210.             r = ((double)Math.random()*32767 / ((double)(32767)+(double)(1)) );  
  211.             param2change=(int)(r*D);  
  212.               
  213.             /*A randomly chosen solution is used in producing a mutant solution of the solution i*/  
  214.             r = (   (double)Math.random()*32767 / ((double)(32767)+(double)(1)) );  
  215.             neighbour=(int)(r*FoodNumber);  
  216.   
  217.             /*Randomly selected solution must be different from the solution i*/          
  218.             while(neighbour == i)  
  219.             {  
  220.                 //System.out.println(Math.random()*32767+"  "+32767);  
  221.             r = (   (double)Math.random()*32767 / ((double)(32767)+(double)(1)) );  
  222.             neighbour=(int)(r*FoodNumber);  
  223.             }  
  224.             for(j=0;j<D;j++)  
  225.             solution[j]=Foods[i][j];  
  226.   
  227.             /*v_{ij}=x_{ij}+\phi_{ij}*(x_{kj}-x_{ij}) */  
  228.             r = (   (double)Math.random()*32767 / ((double)(32767)+(double)(1)) );  
  229.             solution[param2change]=Foods[i][param2change]+(Foods[i][param2change]-Foods[neighbour][param2change])*(r-0.5)*2;  
  230.   
  231.             /*if generated parameter value is out of boundaries, it is shifted onto the boundaries*/  
  232.             if (solution[param2change]<lb)  
  233.                solution[param2change]=lb;  
  234.             if (solution[param2change]>ub)  
  235.                solution[param2change]=ub;  
  236.             ObjValSol=calculateFunction(solution);  
  237.             FitnessSol=CalculateFitness(ObjValSol);  
  238.               
  239.             /*a greedy selection is applied between the current solution i and its mutant*/  
  240.             if (FitnessSol>fitness[i])  
  241.             {  
  242.             /*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*/  
  243.             trial[i]=0;  
  244.             for(j=0;j<D;j++)  
  245.             Foods[i][j]=solution[j];  
  246.             f[i]=ObjValSol;  
  247.             fitness[i]=FitnessSol;  
  248.             }  
  249.             else  
  250.             {   /*if the solution i can not be improved, increase its trial counter*/  
  251.                 trial[i]=trial[i]+1;  
  252.             }  
  253.             } /*if */  
  254.             i++;  
  255.             if (i==FoodNumber-1)  
  256.             i=0;  
  257.             }/*while*/  
  258.   
  259.             /*end of onlooker bee phase     */  
  260.     }  
  261.   
  262.     /*determine the food sources whose trial counter exceeds the "limit" value. In Basic ABC, only one scout is allowed to occur in each cycle*/  
  263.     void SendScoutBees()  
  264.     {  
  265.     int maxtrialindex,i;  
  266.     maxtrialindex=0;  
  267.     for (i=1;i<FoodNumber;i++)  
  268.             {  
  269.              if (trial[i]>trial[maxtrialindex])  
  270.              maxtrialindex=i;  
  271.             }  
  272.     if(trial[maxtrialindex]>=limit)  
  273.     {  
  274.         init(maxtrialindex);  
  275.     }  
  276.     }  
  277.   
  278. double calculateFunction(double sol[])  
  279. {  
  280. return Rastrigin (sol);   
  281. }  
  282.     double sphere(double sol[])  
  283.     {  
  284.     int j;  
  285.     double top=0;  
  286.     for(j=0;j<D;j++)  
  287.     {  
  288.     top=top+sol[j]*sol[j];  
  289.     }  
  290.     return top;  
  291.     }  
  292.   
  293.     double Rosenbrock(double sol[])  
  294.     {  
  295.     int j;  
  296.     double top=0;  
  297.     for(j=0;j<D-1;j++)  
  298.     {  
  299.     top=top+100*Math.pow((sol[j+1]-Math.pow((sol[j]),(double)2)),(double)2)+Math.pow((sol[j]-1),(double)2);  
  300.     }  
  301.     return top;  
  302.     }  
  303.   
  304.      double Griewank(double sol[])  
  305.      {  
  306.          int j;  
  307.          double top1,top2,top;  
  308.          top=0;  
  309.          top1=0;  
  310.          top2=1;  
  311.          for(j=0;j<D;j++)  
  312.          {  
  313.              top1=top1+Math.pow((sol[j]),(double)2);  
  314.              top2=top2*Math.cos((((sol[j])/Math.sqrt((double)(j+1)))*Math.PI)/180);  
  315.   
  316.          }    
  317.          top=(1/(double)4000)*top1-top2+1;  
  318.          return top;  
  319.      }  
  320.   
  321.      double Rastrigin(double sol[])  
  322.      {  
  323.          int j;  
  324.          double top=0;  
  325.   
  326.          for(j=0;j<D;j++)  
  327.          {  
  328.              top=top+(Math.pow(sol[j],(double)2)-10*Math.cos(2*Math.PI*sol[j])+10);  
  329.          }  
  330.          return top;  
  331.      }  
  332. }  
  333.   
  334. 使用方法是:  
  335. public class test {  
  336.     static beeColony bee=new beeColony();  
  337.       
  338.     public static void main(String[] args) {  
  339.         int iter=0;  
  340.         int run=0;  
  341.         int j=0;  
  342.         double mean=0;  
  343.         //srand(time(NULL));  
  344.         for(run=0;run<bee.runtime;run++)  
  345.         {  
  346.         bee.initial();  
  347.         bee.MemorizeBestSource();  
  348.         for (iter=0;iter<bee.maxCycle;iter++)  
  349.             {  
  350.             bee.SendEmployedBees();  
  351.             bee.CalculateProbabilities();  
  352.             bee.SendOnlookerBees();  
  353.             bee.MemorizeBestSource();  
  354.             bee.SendScoutBees();  
  355.             }  
  356.         for(j=0;j<bee.D;j++)  
  357.         {  
  358.             //System.out.println("GlobalParam[%d]: %f\n",j+1,GlobalParams[j]);  
  359.             System.out.println("GlobalParam["+(j+1)+"]:"+bee.GlobalParams[j]);  
  360.         }  
  361.         //System.out.println("%d. run: %e \n",run+1,GlobalMin);  
  362.         System.out.println((run+1)+".run:"+bee.GlobalMin);  
  363.         bee.GlobalMins[run]=bee.GlobalMin;  
  364.         mean=mean+bee.GlobalMin;  
  365.         }  
  366.         mean=mean/bee.runtime;  
  367.         //System.out.println("Means of %d runs: %e\n",runtime,mean);  
  368.         System.out.println("Means  of "+bee.runtime+"runs: "+mean);  
  369.           
  370.     }  
  371.   
  372. }  

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值