java代码求助
哪位大佬可以告诉我哪里错了啊,代码好像都没有报错!!!感谢!!!
void initial() {//初始化
int i;
for(i=0;i<FoodNumber;i++) {
init(i);
}
GlobalMin=f;
for(i=0;i<D;i++)
GlobalParams=Foods;
}
能把代码都发出来吗? 一枚丶学渣 发表于 2020-10-16 16:39
能把代码都发出来吗?
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;//这是啥意思啊????????????
double Foods[][]=new double;//
double f[]=new double;
double fitness[]=new double ;
double trial[] =new double ;
double prob[]=new double ;
double solution[]=new double ;//新解
double ObjValSol;//求新的蜜源的目标函数值。
double FitnessSol;//求新的蜜源的适应值。
int neighbour,param2change;//param2change 对应j, neighbour 对应k
double GlobalMin;//最优解
double GlobalParams[]=new double ;//最优解的参数
double GlobalMins[]=new double ;//记录每次最优解
double r;//随机数,取值范围【0,1】;
void initial() {//初始化
int i;
for(i=0;i<FoodNumber;i++) {
init(i);
}
GlobalMin=f;
for(i=0;i<D;i++)
GlobalParams=Foods;
}
void init(int index) {
int j;
for(j=0;j<D;j++) {
r=((double)Math.random()*32767/((double)32761+(double)(1)));//随机数在【0,1】之间
Foods=r*(ub-lb)+lb;//不同维度下同一个蜜源的值
solution=Foods;
}
f=calculateFunction(solution);
fitness=CalculateFitness(f);
trial=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, (double)2)-10*Math.cos(2*Math.PI*sol+10));
}
return top;
}
void MemorizeBestSource() {
int i,j;
for(i=0;i<FoodNumber;i++) {
if(f<GlobalMin) {
GlobalMin=f;
for(j=0;j<D;j++) {
GlobalParams=Foods;
}
}
}
}
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=Foods;
}
r=((double)Math.random()*32767/((double)32761+(double)(1)));//随机数在【0,1】之间
solution=Foods+(Foods-Foods)*(r-0.5)*2;
if(solution<lb)
solution=lb;
if(solution>ub)
solution=ub;
ObjValSol=calculateFunction(solution);
FitnessSol=CalculateFitness(ObjValSol);
if(FitnessSol>fitness)
{
trial=0;
Foods=solution;
f=ObjValSol;
fitness=FitnessSol;
}
else
{
trial=trial+1;
}
}
}
void CalculateProbabilities() {
int i;
double maxfit;
maxfit=fitness;
for(i=0;i<FoodNumber;i++)
{
if(fitness>maxfit)
maxfit=fitness;
}
for(i=0;i<FoodNumber;i++)
{
prob=(0.9*(fitness/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) {
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=Foods;
}
r=((double)Math.random()*32767/((double)32761+(double)(1)));//随机数在【0,1】之间
solution=Foods+(Foods-Foods)*(r-0.5)*2;
if(solution<lb)
solution=lb;
if(solution>ub)
solution=ub;
ObjValSol=calculateFunction(solution);
FitnessSol=CalculateFitness(ObjValSol);
if(FitnessSol>fitness)
{
trial=0;
Foods=solution;
f=ObjValSol;
fitness=FitnessSol;
}
else
{
trial=trial+1;
}
}
i++;
if(i==FoodNumber)
i=0;
}
}
void SendScoutBees() {
int maxtrialindex,i;
maxtrialindex=0;
for(i=1;i<FoodNumber;i++) {
if(trial>trial)
maxtrialindex=i;
}
if(trial>=limit) {
init(maxtrialindex);
}
}
}
这个是另一页的类
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);
}
System.out.println((run+1)+".run:"+bee.GlobalMin);
bee.GlobalMins=bee.GlobalMin;
mean=mean+bee.GlobalMin;
}
mean=mean/bee.runtime;
System.out.println("Means of"+bee.runtime+"runs: "+mean);
}
}
/*试试这样*/
int[] dizi = new int; 我就纳了闷了,人家都告诉你了,数组下标越界,自己就不会debug跑一下试试,你这样啥时候能进步?{:5_99:}
页:
[1]