|
楼主 |
发表于 2020-10-16 20:05:57
|
显示全部楼层
package test;
public class test {
static beeColony bee=new beeColony();
public static void main(String[] args) {
// TODO Auto-generated method stub
int iter=0;
int run=0;
int j=0;
double mean=0;
for(run=0;run<bee.runtime;run++) {
bee.initial();
bee.MemorizeBestSource();
for(iter=0;iter<bee.maxCycle;iter++) {
bee.SendEmployedBee();
bee.CalculateProbabilities();
bee.SendOnlookerBees();
bee.MemorizeBestSource();
bee.SendScoutBees();
}
for(j=0;j<bee.D;j++) {
System.out.println("GlobalParam["+(j+1)+"]:"+bee.GlobalParams[j]);
}
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"+bee.runtime+"runs: "+mean);
}
}
类:package test;
import java.lang.Math;
public class beeColony {
int NP=20;//定义雇佣蜂以及跟随蜂,雇佣蜂数量=跟随蜂数量
int FoodNumber=2/NP;//定义初始蜜源数量
int limit=100;
int maxCycle=2500;//最大迭代次数
int D=100;
double lb=-5.12;
double ub=+5.12;
int runtime=30;
int dizi[]=new int[10];//这是啥意思啊????????????
double Foods[][]=new double[FoodNumber][D];//
double f[]=new double[FoodNumber];
double fitness[]=new double [FoodNumber];
double trial[] =new double [FoodNumber];
double prob[]=new double [FoodNumber];
double solution[]=new double [D];//新解
double ObjValSol;//求新的蜜源的目标函数值。
double FitnessSol;//求新的蜜源的适应值。
int neighbour,param2change;//param2change 对应j, neighbour 对应k
double GlobalMin;//最优解
double GlobalParams[]=new double [D];//最优解的参数
double GlobalMins[]=new double [runtime];//记录每次最优解
double r;//随机数,取值范围【0,1】;
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 init(int index) {
int j;
for(j=0;j<D;j++) {
r=((double)Math.random()*32767/((double)32761+(double)(1)));//随机数在【0,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;
}
double CalculateFitness(double fun) {//计算目标函数的适应值
double result=0;
if(fun>=0)
{
result=1/(fun+1);
}
else
{
result=1+Math.abs(fun);
}
return result;
}
double calculateFunction(double sol[]) {
return Rastrigin(sol);
}
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;
}
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];
}
}
}
}
void SendEmployedBee() {
int i,j;
for(i=0;i<FoodNumber;i++) {
r=((double)Math.random()*32767/((double)32761+(double)(1)));//随机数在【0,1】之间
param2change=(int)(r*D);//某一维度
r=((double)Math.random()*32767/((double)32761+(double)(1)));//随机数在【0,1】之间
neighbour=(int)(r*FoodNumber);//某一个值
for(j=0;j<D;j++) {
solution[j]=Foods[i][j];
}
r=((double)Math.random()*32767/((double)32761+(double)(1)));//随机数在【0,1】之间
solution[param2change]=Foods[i][param2change]+(Foods[i][param2change]-Foods[neighbour][param2change])*(r-0.5)*2;
if(solution[param2change]<lb)
solution[param2change]=lb;
if(solution[param2change]>ub)
solution[param2change]=ub;
ObjValSol=calculateFunction(solution);
FitnessSol=CalculateFitness(ObjValSol);
if(FitnessSol>fitness[i])
{
trial[i]=0;
Foods[i][j]=solution[j];
f[i]=ObjValSol;
fitness[i]=FitnessSol;
}
else
{
trial[i]=trial[i]+1;
}
}
}
void CalculateProbabilities() {
int i;
double maxfit;
maxfit=fitness[0];
for(i=0;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;
while(t<FoodNumber) {
r=((double)Math.random()*32767/((double)32761+(double)(1)));//随机数在【0,1】之间
if(r<prob[i]) {
t++;
r=((double)Math.random()*32767/((double)32761+(double)(1)));//随机数在【0,1】之间
param2change=(int)(r*D);//某一维度
r=((double)Math.random()*32767/((double)32761+(double)(1)));//随机数在【0,1】之间
neighbour=(int)(r*FoodNumber);//某一个值
while(neighbour==i) {
r=((double)Math.random()*32767/((double)32761+(double)(1)));//随机数在【0,1】之间
neighbour=(int)(r*FoodNumber);//某一个值
}
for(j=0;j<D;j++) {
solution[j]=Foods[i][j];
}
r=((double)Math.random()*32767/((double)32761+(double)(1)));//随机数在【0,1】之间
solution[param2change]=Foods[i][param2change]+(Foods[i][param2change]-Foods[neighbour][param2change])*(r-0.5)*2;
if(solution[param2change]<lb)
solution[param2change]=lb;
if(solution[param2change]>ub)
solution[param2change]=ub;
ObjValSol=calculateFunction(solution);
FitnessSol=CalculateFitness(ObjValSol);
if(FitnessSol>fitness[i])
{
trial[i]=0;
Foods[i][j]=solution[j];
f[i]=ObjValSol;
fitness[i]=FitnessSol;
}
else
{
trial[i]=trial[i]+1;
}
}
i++;
if(i==FoodNumber)
i=0;
}
}
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);
}
}
}
|
|