Understanding and Creating Chromosomes
Understand the role of chromosomes as solutions in genetic algorithms and learn to create robust chromosome structs in Elixir. This lesson guides you through defining genes, fitness, age, and size within structs, enabling efficient tracking and optimization of genetic populations.
Using structs to represent chromosomes
In the lesson Building a Framework for Genetic Algorithms, we designed a framework for writing genetic algorithms. The framework we designed generalized the steps common to all genetic algorithms. The purpose of this exercise was both to better understand the structure of genetic algorithms and optimization problems and to make it easier for us to write genetic algorithms in the future.
The chromosomes we created in the previous chapters are enumerable objects that represent solutions to a problem. At the most fundamental level, this is correct. However, in practice, this isn’t a viable implementation.
Consider this: we’re attempting to solve a problem in which the age of the chromosome determines its fitness. One reason we’d do this is to ensure enough variance between generations. Ideally, we’d persist older chromosomes between generations to a certain point before killing them off once they’ve reached a certain age. In this respect, we ensure an equal distribution of both old and young chromosomes and, thus, naturally occurring variance in the population.
Solving a problem like this using only an Enum type to represent a chromosome creates unnecessary complexity. It’s often the case that we need a more robust data structure to keep track of a number of metrics at a time. In Elixir, we can accomplish this task using a struct.
A struct is a map with a few additional features. Structs allow us to ...