0

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;
}
beatngu13
  • 7,201
  • 6
  • 37
  • 66
Alex
  • 3
  • 1

1 Answers1

0

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).

  • Thanks , yes i was confusing the individual and population. How do i specify my factory if there is one , and/or how do include the population in my engine. Previously i had a Factory> gtf = Genotype.of(chromossomes) – Alex Feb 13 '18 at 16:24
  • The genotype factory is only creating one individual. So in your example, you will creating it like Factory> gtf = Genotype.of(IntegerChromosome.of(0, 100)). Creating an evolution stream from an initial population will look like this: ISeq> genotypes = ...; engine.stream(genotpyes) ... – Franz Wilhelmstötter Feb 13 '18 at 16:43
  • I actually was talking about what to use in the .builder() from Engine class – Alex Feb 13 '18 at 19:06
  • Just write Engine.builder(MainClass::eval, gtf); – Franz Wilhelmstötter Feb 13 '18 at 19:08
  • So if want to use the initial population, i still have to use " Factory> gtf = Genotype.of(IntegerChromosome.of(0, 100)) " , and then specify in engine.stream() , my inicial stream of individuals. I got it right? – Alex Feb 13 '18 at 19:20
  • Just one last question, in your first reply, that has the stream of individuals, how do you know how many Genes each individual has? – Alex Feb 13 '18 at 19:36
  • You have to define the number of genes per chromosome, depending on your problem encoding. In my example, each chromosome has only one gene. How to create chromosomes with different number of genes is documented in the Javadoc (http://jenetics.io/javadoc/jenetics/index.html). – Franz Wilhelmstötter Feb 13 '18 at 19:39