Search code examples
javagenetic-algorithmjenetics

Jenetics library default initial population and fitness calculation


i am using Jenetics in order to get the best individual that maximizes the problem. How does the population size affects the individuals ?

Imagine that i have this initial population read from file put into a list

while( (line = bf.readLine())!=null){
String[] tokens = line.split(",");
chromossomes.add(IntegerChromosome.of(
  IntegerGene.of(Integer.parseInt(tokens[0]),0,100),                                            
  IntegerGene.of(Integer.parseInt(tokens[1]),0,100),                                                  
  IntegerGene.of(Integer.parseInt(tokens[2]),0,100)); 
}

If the file contains i.e. 10 chromossomes, and then i set population to 100, are the remaining 90 individuals created randomly?

I would like to know also if this fitness function is correct

private static int eval(Genotype<IntegerGene> gt) {
   int best=0,fitness=0;
   for(int i=0;i<gt.length();i++) {
       fitness = getFitness(gt.getChromosome(i));
       if (fitness > best){
           best = fitness;
       }
   }
 return best;
}

Solution

  • The answer to the first question is yes. The missing individual of the population are created randomly. But more important, you made a mistake, when creating an initial population from file. I think you would like do something like the following.

    final String[] lines = ...;
    final ISeq<Genotype<IntegerGene>> population = Arrays.stream(lines)
        .mapToInt(Integer::parseInt)
        .mapToObj(i -> IntegerChromosome.of(IntegerGene.of(i, 0, 100)))
        .map(ch -> Genotype.of(ch))
        .collect(ISeq.toISeq());
    

    This will create one individual (Genotype) per line.

    Your second code snipped looks like you are trying to calculate the best value from the chromosomes of one individual. I think you are confusing the Genotype (one individual) with the population, a list of Genotypes. The fitness function always calculates the fitness of one individual (Genotype).