, ga_struct. mutate() parent - , , , .
public ga_struct mutate(ga_struct parent)
{
Random r= new Random();
...... do some modification to the parent
return parent;
}
ga_struct, , father ( , ):
for (int i = 1; i < 10; i++) {
ga_struct ng = new ga_struct();
ng=newSpecies.mutate(father); //the new ga_struct is overwritten
ng.fitness=i;
newSpecies.vector.add(ng);
father=ng;
System.out.println(newSpecies.vector.get(i).gene+" with fitness factor "+newSpecies.vector.get(i).fitness);
}
, , , father , . , , () List.
, , , , 10 List.
My suggestion is to change mutate()to return a new instance ga_struct- you could either create a new object, or set it in the field geneas a mutated field genefrom parent. Or you can , and then change the clone gene string. In any case, you will return a new instance that should fix the problem.clone parentga_struct
public ga_struct mutate(ga_struct parent)
{
Random r= new Random();
ga_struct mutant = parent.clone();
//or
//ga_struct mutant = new ga_struct();
//mutant.gene = parent.gene;
...... do some modification to the mutant
return mutant; //now you'll be returning a new object not just a modified one
}
source
share