社区微信群开通啦,扫一扫抢先加入社区官方微信群
社区微信群
为了解决多变量函数优化问题Karaboga在2005年提出了人工蜂群算法ABC模型。
1、 蜜蜂采蜜机理
蜜蜂是一种群居昆虫,虽然单个昆虫的行为极其简单,但是由单个简单的个体所组成的群体却表现出极其复杂的行为。真实的蜜蜂种群能够在任何环境下,以极高的效率从食物源(花朵)中采集花蜜;同时,它们能适应环境的改变。
蚁群产生群体智慧的最小搜索模型包含基本的三个组成要素:食物源、被雇佣的蜜蜂和未被雇佣的蜜蜂。两种最基本的行为模型:为食物源招募蜜蜂和放弃某个食物源。
(1) 食物源
食物源的价值由多方面因素决定,如:离蜂巢的远近、包含花蜜的丰富程度和获得花蜜的难易程度。使用单一的参数,食物源的“收益率”来代表以上各个因素。
(2) 被雇佣的蜜蜂
也称引领蜂,其与所采集的食物源一一对应。引领蜂储存有食物源的相关信息(相对与蜂巢的距离、方向和食物源的丰富程度等)并把这些信息以一定的概率与其他蜜蜂分享。
(3) 未被雇佣的蜜蜂
其主要任务是寻找和开采食物源。有两种未被雇佣的蜜蜂:侦查蜂和跟随蜂。侦查蜂搜索附近的新食物源;跟随蜂等在蜂巢里面并通过与引领蜂分享相关信息找到食物源。一般情况下,侦查蜂的数量是蜂群的5%--20%
在群体智慧形成过程中,蜜蜂间交换信息是最重要的一环。舞蹈区是蜂巢中最为重要的信息交换地。蜜蜂的舞蹈也叫摇摆舞。食物源的信息在舞蹈区通过摇摆舞的形式与其他蜜蜂共享,引领蜂通过摇摆舞的持续时间等来表现食物源的收益率,故跟随蜂可以观察到大量的舞蹈并依据收益率来选择到哪个食物源采蜜。收益率与食物源被选择的可能性成正比。因而,蜜蜂被招募到一个食物源的概率与食物源的收益率成正比。
初始时刻,蜜蜂以侦查蜂的方式搜索。其搜索可以由系统的先验知识决定,也可以完全随机。经过一轮侦查后,若蜜蜂找到食物源,蜜蜂利用它本身的存储能力记录位置信息并开始采蜜。此时,蜜蜂将称为“被雇佣者”。蜜蜂在食物源采蜜后回到蜂巢卸下蜂蜜,然后将有如下选择:
(1) 放弃食物源而成为非雇佣蜂;
(2) 跳摇摆舞为所对应的食物源招募更多的蜜蜂,然后回到食物源采蜜;
(3) 继续在食物源采蜜而不进行招募。
对于非雇佣蜂有如下选择:
(1) 转变为侦查蜂并搜索蜂巢附近的食物源,其搜索可以由先验知识决定也可以完全随机;
(2) 在观察完摇摆舞以后,被雇佣成为跟随蜂,开始搜索对应食物源领域并采蜜。
2、 ABC算法原理
在基本ABC算法中,人工蜂群包含三种个体:雇佣蜂、观察蜂和侦查蜂
每个雇佣蜂对应一个确定的蜜源(解向量),并在迭代中对蜜源的领域进行搜索。
根据蜜源的丰富程度(适应值的大小)采用轮盘赌的方式雇佣观察蜂采蜜(搜索新蜜源)
如果蜜源多次更新没有改进,则放弃该蜜源,雇佣蜂转为侦查蜂随机搜索新蜜源。
(1) 随机初始化
(2) 新蜜源的更新搜索公式
(3) 观察蜂选择雇佣蜂的概率
(4) 侦查蜂的产生
为了防止算法陷入局部最优,当蜜源迭代limit次没有改进时,便放弃该蜜源,并且将该蜜源记录在禁忌表中,同时该蜜源对应的雇佣蜂转变为侦查蜂按式(1)随机产生一个新的位置代替原蜜源。
3、 控制参数
(1) 蜜源的个数(与雇佣蜂或观察蜂相等)SN
(2) 算法终止的最大进化数(maximum evaluation number)MEN
(3) Limit
基本ABC算法的流程为
(1) 根据式(1)初始化种群解xi,i=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++程序#include<iostream>
#include<time.h>
#include<stdlib.h>
#include<cmath>
#include<fstream>
#include<iomanip>
using namespace std;
const int NP=40;//种群的规模,采蜜蜂+观察蜂
const int FoodNumber=NP/2;//食物的数量,为采蜜蜂的数量
const int limit=20;//限度,超过这个限度没有更新采蜜蜂变成侦查蜂
const int maxCycle=10000;//停止条件
/*****函数的特定参数*****/
const int D=2;//函数的参数个数
const double lb=-100;//函数的下界
const double ub=100;//函数的上界
double result[maxCycle]={0};
/*****种群的定义****/
struct BeeGroup
{
double code[D];//函数的维数
double trueFit;//记录真实的最小值
double fitness;
double rfitness;//相对适应值比例
int trail;//表示实验的次数,用于与limit作比较
}Bee[FoodNumber];
BeeGroup NectarSource[FoodNumber];//蜜源,注意:一切的修改都是针对蜜源而言的
BeeGroup EmployedBee[FoodNumber];//采蜜蜂
BeeGroup OnLooker[FoodNumber];//观察蜂
BeeGroup BestSource;//记录最好蜜源
/*****函数的声明*****/
double random(double, double);//产生区间上的随机数
void initilize();//初始化参数
double calculationTruefit(BeeGroup);//计算真实的函数值
double calculationFitness(double);//计算适应值
void CalculateProbabilities();//计算轮盘赌的概率
void evalueSource();//评价蜜源
void sendEmployedBees();
void sendOnlookerBees();
void sendScoutBees();
void MemorizeBestSource();
/*******主函数*******/
int main()
{
ofstream output;
output.open("dataABC.txt");
srand((unsigned)time(NULL));
initilize();//初始化
MemorizeBestSource();//保存最好的蜜源
//主要的循环
int gen=0;
while(gen<maxCycle)
{
sendEmployedBees();
CalculateProbabilities();
sendOnlookerBees();
MemorizeBestSource();
sendScoutBees();
MemorizeBestSource();
output<<setprecision(30)<<BestSource.trueFit<<endl;
gen++;
}
output.close();
cout<<"运行结束!!"<<endl;
return 0;
}
/*****函数的实现****/
double random(double start, double end)//随机产生区间内的随机数
{
return start+(end-start)*rand()/(RAND_MAX + 1.0);
}
void initilize()//初始化参数
{
int i,j;
for (i=0;i<FoodNumber;i++)
{
for (j=0;j<D;j++)
{
NectarSource[i].code[j]=random(lb,ub);
EmployedBee[i].code[j]=NectarSource[i].code[j];
OnLooker[i].code[j]=NectarSource[i].code[j];
BestSource.code[j]=NectarSource[0].code[j];
}
/****蜜源的初始化*****/
NectarSource[i].trueFit=calculationTruefit(NectarSource[i]);
NectarSource[i].fitness=calculationFitness(NectarSource[i].trueFit);
NectarSource[i].rfitness=0;
NectarSource[i].trail=0;
/****采蜜蜂的初始化*****/
EmployedBee[i].trueFit=NectarSource[i].trueFit;
EmployedBee[i].fitness=NectarSource[i].fitness;
EmployedBee[i].rfitness=NectarSource[i].rfitness;
EmployedBee[i].trail=NectarSource[i].trail;
/****观察蜂的初始化****/
OnLooker[i].trueFit=NectarSource[i].trueFit;
OnLooker[i].fitness=NectarSource[i].fitness;
OnLooker[i].rfitness=NectarSource[i].rfitness;
OnLooker[i].trail=NectarSource[i].trail;
}
/*****最优蜜源的初始化*****/
BestSource.trueFit=NectarSource[0].trueFit;
BestSource.fitness=NectarSource[0].fitness;
BestSource.rfitness=NectarSource[0].rfitness;
BestSource.trail=NectarSource[0].trail;
}
double calculationTruefit(BeeGroup bee)//计算真实的函数值
{
double truefit=0;
/******测试函数1******/
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)
/((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])));
return truefit;
}
double calculationFitness(double truefit)//计算适应值
{
double fitnessResult=0;
if (truefit>=0)
{
fitnessResult=1/(truefit+1);
}else
{
fitnessResult=1+abs(truefit);
}
return fitnessResult;
}
void sendEmployedBees()//修改采蜜蜂的函数
{
int i,j,k;
int param2change;//需要改变的维数
double Rij;//[-1,1]之间的随机数
for (i=0;i<FoodNumber;i++)
{
param2change=(int)random(0,D);//随机选取需要改变的维数
/******选取不等于i的k********/
while (1)
{
k=(int)random(0,FoodNumber);
if (k!=i)
{
break;
}
}
for (j=0;j<D;j++)
{
EmployedBee[i].code[j]=NectarSource[i].code[j];
}
/*******采蜜蜂去更新信息*******/
Rij=random(-1,1);
EmployedBee[i].code[param2change]=NectarSource[i].code[param2change]+Rij*(NectarSource[i].code[param2change]-NectarSource[k].code[param2change]);
/*******判断是否越界********/
if (EmployedBee[i].code[param2change]>ub)
{
EmployedBee[i].code[param2change]=ub;
}
if (EmployedBee[i].code[param2change]<lb)
{
EmployedBee[i].code[param2change]=lb;
}
EmployedBee[i].trueFit=calculationTruefit(EmployedBee[i]);
EmployedBee[i].fitness=calculationFitness(EmployedBee[i].trueFit);
/******贪婪选择策略*******/
if (EmployedBee[i].trueFit<NectarSource[i].trueFit)
{
for (j=0;j<D;j++)
{
NectarSource[i].code[j]=EmployedBee[i].code[j];
}
NectarSource[i].trail=0;
NectarSource[i].trueFit=EmployedBee[i].trueFit;
NectarSource[i].fitness=EmployedBee[i].fitness;
}else
{
NectarSource[i].trail++;
}
}
}
void CalculateProbabilities()//计算轮盘赌的选择概率
{
int i;
double maxfit;
maxfit=NectarSource[0].fitness;
for (i=1;i<FoodNumber;i++)
{
if (NectarSource[i].fitness>maxfit)
maxfit=NectarSource[i].fitness;
}
for (i=0;i<FoodNumber;i++)
{
NectarSource[i].rfitness=(0.9*(NectarSource[i].fitness/maxfit))+0.1;
}
}
void sendOnlookerBees()//采蜜蜂与观察蜂交流信息,观察蜂更改信息
{
int i,j,t,k;
double R_choosed;//被选中的概率
int param2change;//需要被改变的维数
double Rij;//[-1,1]之间的随机数
i=0;
t=0;
while(t<FoodNumber)
{
R_choosed=random(0,1);
if(R_choosed<NectarSource[i].rfitness)//根据被选择的概率选择
{
t++;
param2change=(int)random(0,D);
/******选取不等于i的k********/
while (1)
{
k=(int)random(0,FoodNumber);
if (k!=i)
{
break;
}
}
for(j=0;j<D;j++)
{
OnLooker[i].code[j]=NectarSource[i].code[j];
}
/****更新******/
Rij=random(-1,1);
OnLooker[i].code[param2change]=NectarSource[i].code[param2change]+Rij*(NectarSource[i].code[param2change]-NectarSource[k].code[param2change]);
/*******判断是否越界*******/
if (OnLooker[i].code[param2change]<lb)
{
OnLooker[i].code[param2change]=lb;
}
if (OnLooker[i].code[param2change]>ub)
{
OnLooker[i].code[param2change]=ub;
}
OnLooker[i].trueFit=calculationTruefit(OnLooker[i]);
OnLooker[i].fitness=calculationFitness(OnLooker[i].trueFit);
/****贪婪选择策略******/
if (OnLooker[i].trueFit<NectarSource[i].trueFit)
{
for (j=0;j<D;j++)
{
NectarSource[i].code[j]=OnLooker[i].code[j];
}
NectarSource[i].trail=0;
NectarSource[i].trueFit=OnLooker[i].trueFit;
NectarSource[i].fitness=OnLooker[i].fitness;
}else
{
NectarSource[i].trail++;
}
}
i++;
if (i==FoodNumber)
{
i=0;
}
}
}
/*******只有一只侦查蜂**********/
void sendScoutBees()//判断是否有侦查蜂的出现,有则重新生成蜜源
{
int maxtrialindex,i,j;
double R;//[0,1]之间的随机数
maxtrialindex=0;
for (i=1;i<FoodNumber;i++)
{
if (NectarSource[i].trail>NectarSource[maxtrialindex].trail)
{
maxtrialindex=i;
}
}
if(NectarSource[maxtrialindex].trail>=limit)
{
/*******重新初始化*********/
for (j=0;j<D;j++)
{
R=random(0,1);
NectarSource[maxtrialindex].code[j]=lb+R*(ub-lb);
}
NectarSource[maxtrialindex].trail=0;
NectarSource[maxtrialindex].trueFit=calculationTruefit(NectarSource[maxtrialindex]);
NectarSource[maxtrialindex].fitness=calculationFitness(NectarSource[maxtrialindex].trueFit);
}
}
void MemorizeBestSource()//保存最优的蜜源
{
int i,j;
for (i=1;i<FoodNumber;i++)
{
if (NectarSource[i].trueFit<BestSource.trueFit)
{
for (j=0;j<D;j++)
{
BestSource.code[j]=NectarSource[i].code[j];
}
BestSource.trueFit=NectarSource[i].trueFit;
}
}
}
收敛曲线
import java.lang.Math;
public class beeColony {
/* Control Parameters of ABC algorithm*/
int NP=20; /* The number of colony size (employed bees+onlooker bees)*/
int FoodNumber = NP/2; /*The number of food sources equals the half of the colony size*/
int limit = 100; /*A food source which could not be improved through "limit" trials is abandoned by its employed bee*/
int maxCycle = 2500; /*The number of cycles for foraging {a stopping criteria}*/
/* Problem specific variables*/
int D = 100; /*The number of parameters of the problem to be optimized*/
double lb = -5.12; /*lower bound of the parameters. */
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*/
int runtime = 30; /*Algorithm can be run many times in order to see its robustness*/
int dizi1[]=new int[10];
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*/
double f[]=new double[FoodNumber]; /*f is a vector holding objective function values associated with food sources */
double fitness[]=new double[FoodNumber]; /*fitness is a vector holding fitness (quality) values associated with food sources*/
double trial[]=new double[FoodNumber]; /*trial is a vector holding trial numbers through which solutions can not be improved*/
double prob[]=new double[FoodNumber]; /*prob is a vector holding probabilities of food sources (solutions) to be chosen*/
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*/
double ObjValSol; /*Objective function value of new solution*/
double FitnessSol; /*Fitness value of new solution*/
int neighbour, param2change; /*param2change corrresponds to j, neighbour corresponds to k in equation v_{ij}=x_{ij}+phi_{ij}*(x_{kj}-x_{ij})*/
double GlobalMin; /*Optimum solution obtained by ABC algorithm*/
double GlobalParams[]=new double[D]; /*Parameters of the optimum solution*/
double GlobalMins[]=new double[runtime];
/*GlobalMins holds the GlobalMin of each run in multiple runs*/
double r; /*a random number in the range [0,1)*/
/*a function pointer returning double and taking a D-dimensional array as argument */
/*If your function takes additional arguments then change function pointer definition and lines calling "...=function(solution);" in the code*/
// typedef double (*FunctionCallback)(double sol[D]);
/*benchmark functions */
// double sphere(double sol[D]);
// double Rosenbrock(double sol[D]);
// double Griewank(double sol[D]);
// double Rastrigin(double sol[D]);
/*Write your own objective function name instead of sphere*/
// FunctionCallback function = &sphere;
/*Fitness function*/
double CalculateFitness(double fun)
{
double result=0;
if(fun>=0)
{
result=1/(fun+1);
}
else
{
result=1+Math.abs(fun);
}
return result;
}
/*The best food source is memorized*/
void MemorizeBestSource()
{
int i,j;
for(i=0;i<FoodNumber;i++)
{
if (f[i]<GlobalMin)
{
GlobalMin=f[i];
for(j=0;j<D;j++)
GlobalParams[j]=Foods[i][j];
}
}
}
/*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 */
/* Counters of food sources are also initialized in this function*/
void init(int index)
{
int j;
for (j=0;j<D;j++)
{
r = ( (double)Math.random()*32767 / ((double)32767+(double)(1)) );
Foods[index][j]=r*(ub-lb)+lb;
solution[j]=Foods[index][j];
}
f[index]=calculateFunction(solution);
fitness[index]=CalculateFitness(f[index]);
trial[index]=0;
}
/*All food sources are initialized */
void initial()
{
int i;
for(i=0;i<FoodNumber;i++)
{
init(i);
}
GlobalMin=f[0];
for(i=0;i<D;i++)
GlobalParams[i]=Foods[0][i];
}
void SendEmployedBees()
{
int i,j;
/*Employed Bee Phase*/
for (i=0;i<FoodNumber;i++)
{
/*The parameter to be changed is determined randomly*/
r = ((double) Math.random()*32767 / ((double)(32767)+(double)(1)) );
param2change=(int)(r*D);
/*A randomly chosen solution is used in producing a mutant solution of the solution i*/
r = ( (double)Math.random()*32767 / ((double)(32767)+(double)(1)) );
neighbour=(int)(r*FoodNumber);
/*Randomly selected solution must be different from the solution i*/
// while(neighbour==i)
// {
// r = ( (double)Math.random()*32767 / ((double)(32767)+(double)(1)) );
// neighbour=(int)(r*FoodNumber);
// }
for(j=0;j<D;j++)
solution[j]=Foods[i][j];
/*v_{ij}=x_{ij}+phi_{ij}*(x_{kj}-x_{ij}) */
r = ( (double)Math.random()*32767 / ((double)(32767)+(double)(1)) );
solution[param2change]=Foods[i][param2change]+(Foods[i][param2change]-Foods[neighbour][param2change])*(r-0.5)*2;
/*if generated parameter value is out of boundaries, it is shifted onto the boundaries*/
if (solution[param2change]<lb)
solution[param2change]=lb;
if (solution[param2change]>ub)
solution[param2change]=ub;
ObjValSol=calculateFunction(solution);
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*/
trial[i]=0;
for(j=0;j<D;j++)
Foods[i][j]=solution[j];
f[i]=ObjValSol;
fitness[i]=FitnessSol;
}
else
{ /*if the solution i can not be improved, increase its trial counter*/
trial[i]=trial[i]+1;
}
}
/*end of employed bee phase*/
}
/* 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*/
void CalculateProbabilities()
{
int i;
double maxfit;
maxfit=fitness[0];
for (i=1;i<FoodNumber;i++)
{
if (fitness[i]>maxfit)
maxfit=fitness[i];
}
for (i=0;i<FoodNumber;i++)
{
prob[i]=(0.9*(fitness[i]/maxfit))+0.1;
}
}
void SendOnlookerBees()
{
int i,j,t;
i=0;
t=0;
/*onlooker Bee Phase*/
while(t<FoodNumber)
{
r = ( (double)Math.random()*32767 / ((double)(32767)+(double)(1)) );
if(r<prob[i]) /*choose a food source depending on its probability to be chosen*/
{
t++;
/*The parameter to be changed is determined randomly*/
r = ((double)Math.random()*32767 / ((double)(32767)+(double)(1)) );
param2change=(int)(r*D);
/*A randomly chosen solution is used in producing a mutant solution of the solution i*/
r = ( (double)Math.random()*32767 / ((double)(32767)+(double)(1)) );
neighbour=(int)(r*FoodNumber);
/*Randomly selected solution must be different from the solution i*/
while(neighbour == i)
{
//System.out.println(Math.random()*32767+" "+32767);
r = ( (double)Math.random()*32767 / ((double)(32767)+(double)(1)) );
neighbour=(int)(r*FoodNumber);
}
for(j=0;j<D;j++)
solution[j]=Foods[i][j];
/*v_{ij}=x_{ij}+phi_{ij}*(x_{kj}-x_{ij}) */
r = ( (double)Math.random()*32767 / ((double)(32767)+(double)(1)) );
solution[param2change]=Foods[i][param2change]+(Foods[i][param2change]-Foods[neighbour][param2change])*(r-0.5)*2;
/*if generated parameter value is out of boundaries, it is shifted onto the boundaries*/
if (solution[param2change]<lb)
solution[param2change]=lb;
if (solution[param2change]>ub)
solution[param2change]=ub;
ObjValSol=calculateFunction(solution);
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*/
trial[i]=0;
for(j=0;j<D;j++)
Foods[i][j]=solution[j];
f[i]=ObjValSol;
fitness[i]=FitnessSol;
}
else
{ /*if the solution i can not be improved, increase its trial counter*/
trial[i]=trial[i]+1;
}
} /*if */
i++;
if (i==FoodNumber-1)
i=0;
}/*while*/
/*end of onlooker 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*/
void SendScoutBees()
{
int maxtrialindex,i;
maxtrialindex=0;
for (i=1;i<FoodNumber;i++)
{
if (trial[i]>trial[maxtrialindex])
maxtrialindex=i;
}
if(trial[maxtrialindex]>=limit)
{
init(maxtrialindex);
}
}
double calculateFunction(double sol[])
{
return Rastrigin (sol);
}
double sphere(double sol[])
{
int j;
double top=0;
for(j=0;j<D;j++)
{
top=top+sol[j]*sol[j];
}
return top;
}
double Rosenbrock(double sol[])
{
int j;
double top=0;
for(j=0;j<D-1;j++)
{
top=top+100*Math.pow((sol[j+1]-Math.pow((sol[j]),(double)2)),(double)2)+Math.pow((sol[j]-1),(double)2);
}
return top;
}
double Griewank(double sol[])
{
int j;
double top1,top2,top;
top=0;
top1=0;
top2=1;
for(j=0;j<D;j++)
{
top1=top1+Math.pow((sol[j]),(double)2);
top2=top2*Math.cos((((sol[j])/Math.sqrt((double)(j+1)))*Math.PI)/180);
}
top=(1/(double)4000)*top1-top2+1;
return top;
}
double Rastrigin(double sol[])
{
int j;
double top=0;
for(j=0;j<D;j++)
{
top=top+(Math.pow(sol[j],(double)2)-10*Math.cos(2*Math.PI*sol[j])+10);
}
return top;
}
}
使用方法是:
public class test {
static beeColony bee=new beeColony();
public static void main(String[] args) {
int iter=0;
int run=0;
int j=0;
double mean=0;
//srand(time(NULL));
for(run=0;run<bee.runtime;run++)
{
bee.initial();
bee.MemorizeBestSource();
for (iter=0;iter<bee.maxCycle;iter++)
{
bee.SendEmployedBees();
bee.CalculateProbabilities();
bee.SendOnlookerBees();
bee.MemorizeBestSource();
bee.SendScoutBees();
}
for(j=0;j<bee.D;j++)
{
//System.out.println("GlobalParam[%d]: %fn",j+1,GlobalParams[j]);
System.out.println("GlobalParam["+(j+1)+"]:"+bee.GlobalParams[j]);
}
//System.out.println("%d. run: %e n",run+1,GlobalMin);
System.out.println((run+1)+".run:"+bee.GlobalMin);
bee.GlobalMins[run]=bee.GlobalMin;
mean=mean+bee.GlobalMin;
}
mean=mean/bee.runtime;
//System.out.println("Means of %d runs: %en",runtime,mean);
System.out.println("Means of "+bee.runtime+"runs: "+mean);
}
}
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!