2. The Cycle of Life: How a Genetic Algorithm “Evolves” a Solution
Genetic Algorithms follow a step-by-step cycle, just like generations in nature, to gradually improve solutions. This iterative process refines a population of candidate solutions until a stopping criterion is met.
Step 1: The First Generation (Initialization)
The process begins by creating an initial Population of candidate solutions (Chromosomes). While this population is often generated completely randomly to ensure maximum genetic diversity, it can also be “seeded” with a few known good-but-imperfect solutions using a heuristic. This gives the algorithm a head start, while the rest of the population is filled randomly to drive the exploration of new possibilities. This initial diversity is the raw material for evolution.
Step 2: The Test of Fitness (Fitness Function)
Next, every Chromosome in the population is evaluated using the Fitness Function. This function acts as the “environment” by assigning a score to each solution based on how well it solves the problem. A higher fitness score means a better solution, and this score directly determines a chromosome’s chance of passing on its “genes” to the next generation.
Step 3: Choosing Who Reproduces (Parent Selection)
This step mimics the principle of “Survival of the Fittest.” Individuals are selected from the population to become parents for the next generation. A common and intuitive method for this is Fitness Proportionate Selection.
- Roulette Wheel Analogy: Imagine a roulette wheel where each slice corresponds to a solution in the population. The size of each slice is proportional to that solution’s fitness score—fitter solutions get larger slices. The wheel is spun, and the solution on which the pointer lands is selected to be a parent. This process gives fitter individuals a higher probability of being chosen, but still gives less fit individuals a small chance, maintaining diversity. While intuitive, this method can struggle when fitness values are very close together, as the selection pressure towards the best individuals diminishes.
Step 4: Creating New Offspring (Crossover and Mutation)
This is the stage where new solutions are created from the selected parents, introducing new possibilities into the population.
- Crossover: This process combines the genetic material of two parents to create one or more children. The idea is that combining two good solutions might create an even better one. In One-Point Crossover, a random point is selected on the parent chromosomes, and their “tails” are swapped to create two new offspring.
- Mutation: This is a small, random tweak made to an individual chromosome. For example, in Bit Flip Mutation, a single bit in a binary chromosome is flipped from 0 to 1 or vice-versa. Mutation is critical for introducing new genetic diversity into the population. It is the primary mechanism for the exploration of the search space, preventing the algorithm from getting stuck with the same set of solutions and ensuring new avenues are always being tested.
These two operations are governed by probabilities. The crossover probability (pc) is typically high (e.g., 0.8-0.95) to encourage the recombination of successful solutions. In contrast, the mutation probability (pm) is kept very low (e.g., 0.01-0.1). This ensures that mutation introduces just enough diversity to explore new areas of the search space without disrupting the evolutionary process and turning it into a purely random search.
Step 5: The Next Generation (Termination)
The new offspring replace existing individuals in the population, and the cycle of fitness evaluation, selection, and reproduction begins again. This process repeats for many generations, with the overall fitness of the population tending to improve over time. The algorithm stops when one of the following conditions is met:
- There has been no improvement in the best solution for a set number of generations.
- A maximum number of generations has been reached.
- A solution is found that meets a target fitness score.
An example of how we represent a problem will make these abstract steps much clearer.