Genetic Algorithms in Elixir: Generation tracking. Chromosome already has "age"

The Chromosome struct has an “age” attribute.

This can be used in the “terminate?/1” function without need to alter the “Genetic evolve” algorithm.

This also means adopting the fix to track the fitness and age of the Chromosome here

In the cargo Problem terminate?/1 function simply…

def terminate?(population), do: hd(population).age == whatever

…and the problem will run with no change to the Genetic code required.

1 Like

Hey, thanks for pointing this out! Theoretically, you could do this in cases where you have full replacement and then have children chromosomes take on the age of their parents; however, there are instances where age among the population becomes different. In later chapters, you’ll learn that sometimes you want to keep a percentage of the population (parent chromosomes), and then replace another percentage of the population with children. This will yield a population where the ages of chromosomes vary slightly. So then this fitness function would stop after the oldest chromosome in the population reaches a certain age, which theoretically could never happen depending on your replacement strategy.

There are some instances in the book where age tracking isn’t correctly maintained. I’m working on fixing those.