I am making a genetic algorithm that develops an array charin "Hello World". The problem is that whenever I initialize an object Chromosomeand call a method generateChromosome, all the chromosomes of the “test” population remain the same?
public class Chromosome{
private static int defaultLength = 11;
private static char []genes = new char[defaultLength]; <--- this remains the same for each object :/
public void generateChromosome(){
char []newGene = new char[defaultLength];
for(int x = 0; x<size(); x++){
char gene = (char)(32+Math.round(96*Math.random()));
newGene[x] = gene;
}
genes = newGene;
}
public char getGene(int index){
return genes[index];
}
public char[] getChromosome(){
return genes;
}
public void setGene(char value, int index){
genes[index] = value;
}
public static void setDefaultLength(int amount){
defaultLength = amount;
}
public static int getDefaultLength(){
return defaultLength;
}
public int size(){
return genes.length;
}
@Override
public String toString(){
String geneString = "";
for(int x= 0; x<genes.length; x++){
geneString += genes[x];
}
return geneString;
}
}
source
share