来自同一方法的不同变量输出JAVA遗传算法

时间:2016-11-26 04:03:32

标签: java oop artificial-intelligence genetic-algorithm

所以我试图在java上建立一个遗传算法,我坚持得到 我的人口适合我的项目3个班级:

Class Individu

public class Individu {

int Popsize=4;
int Health[]= new int[Popsize];   
int Attack[]= new int[Popsize];
int Atspeed[]= new int[Popsize];    
int Move[]= new int[Popsize];

int health,attack,lifetime,dmgdone,attspeed,range,move;
double fitness;
double Pitness[]= new double[20];

Random random = new Random();

public int setHealth(){

health = random.nextInt(150 - 75) + 75;

return health;
}
public int setAttack(){
attack = random.nextInt(10 - 5) + 10;

return attack;
}
public int setAttspeed(){
attspeed = random.nextInt(3 - 1) + 3;

return attspeed;
}
public int setMoveSpeed(){
move = random.nextInt(8 - 4) + 1;

return move;
}

public int getGeneHealth(int index) {

return Health[index];
}
public int getGeneAttack(int index) {

return Attack[index];
}
public int getGeneAtspedd(int index) {

return Atspeed[index];
}
public int getGeneMove(int index) {

return Move[index];
}

public void setGene(int index, int value) {
Health[index]=value;
Attack[index]=value;
Atspeed[index]=value;
Move[index]=value;

fitness = 0;
}
public int size() {
return Popsize;
}



public double[] GenerateIndividual(){
for (int i = 0; i <Popsize; i++) {
Health[i]=setHealth();
Attack[i]=setAttack();
Atspeed[i]=setAttspeed();
Move[i]=setMoveSpeed();


}
return Pitness;   
}

Class Fitness

 public class Fitness {
 Individu individu= new Individu();

 double fitness;
 double Pitness[]= new double[20];
 public double getFitness(){
 individu.GenerateIndividual();
 for (int i = 0; i <=3; i++) {
 fitness=
 individu.getGeneHealth(i)+individu.getGeneAtspedd(i)+
 individu.getGeneAttack(i)+
 individu.getGeneMove(i));
 fitness=fitness/171;
 Pitness[i]=fitness;
 System.out.println("Health from class
 fitness"+individu.Health[i]);
 }
 return fitness;
 }

 }

主类

public class main {
public static void main(String[] args) {
Individu aaa=new Individu();
Fitness bbb= new Fitness();
bbb.getFitness();
aaa.GenerateIndividual();



DecimalFormat df = new DecimalFormat();
df.setMaximumFractionDigits(3);

for (int i=0; i<=3; i++){
//System.out.println("Fitness         ");
System.out.println("Generasi ke         :"+i+1);
System.out.println("Health          "+aaa.getGeneHealth(i));
System.out.println("Attackspeed     "+aaa.getGeneAtspedd(i));
System.out.println("Attack          "+aaa.getGeneAttack(i));
System.out.println("movementSpeed   "+aaa.getGeneMove(i));
}     
}
}

我奋斗的是当我运行这个脚本时,我从1变量得到2个双倍值第一个值来自Fitness类,因为我在这里打印

 System.out.println("Health from class fitness"+individu.Health[i]);

和我从Main Class打印的第二个变量

 System.out.println("Health "+aaa.getGeneHealth(i));

2变量总是有不同的值导致我的健康,而我的世代并不相互关联。 我的问题是如何使这个2变量打印相同的值?

1 个答案:

答案 0 :(得分:0)

嗯,除了我可以检测到的遗传算法的基本问题之外,我看到'individu'和'aaa'是两个不同的Java对象。

Individu aaa=new Individu();
aaa.GenerateIndividual();

Individu individu= new Individu();
individu.GenerateIndividual();

由于您的健康与健身是在GenerateIndividual()上随机生成的,因此'aaa'和'individu'都会获得不同的健康值。

我强烈建议您查看GA要点,因为我可以在您的系统中看到许多概念错误。