2. The Lifecycle: A Step-by-Step Journey Through a Genetic Algorithm
The Genetic Algorithm is not a one-time calculation but a cyclical process. It is a loop that repeats over many generations, where each generation is intended to be “fitter” or better than the last.
- Step 1: Initialization – Creating the First Generation The process begins by creating an initial population of chromosomes. There are two common approaches to this:
- Random Initialization: The population is filled with completely random solutions. This ensures maximum diversity from the start.
- Heuristic Initialization: The population is “seeded” with a few good solutions created using a known heuristic or rule of thumb for the problem.
- The best strategy is often a mix of both. Seeding the population with a few good solutions can provide a strong starting point, but filling the rest with random solutions is crucial for maintaining the diversity needed to explore the entire solution space and find the best possible answer.
- Step 2: Parent Selection – Choosing Who Reproduces The goal of this step is to select the “fitter” individuals from the current population to serve as parents for the next generation. This process applies selection pressure, favoring better solutions.
- However, a critical danger here is premature convergence. This occurs when one “super” solution is so much better than the others that it dominates the selection process, filling the next generation with its offspring. This kills genetic diversity and can stop the algorithm from finding an even better solution.
- Here are two popular methods for selecting parents:
| Method | How It Works | Key Characteristic |
| Roulette Wheel Selection | Assigns each individual a “slice” of a wheel proportional to its fitness. The wheel is “spun,” and the individual on which it lands is chosen. | Fitter individuals have a higher probability of being chosen, but it doesn’t work for negative fitness values. |
| Tournament Selection | Randomly selects ‘K’ individuals from the population and chooses the best one from that small group to be a parent. This process is repeated. | Extremely popular because it is simple, efficient, and works even with negative fitness values. |
- Step 3: Crossover – Creating Offspring Crossover is the GA’s equivalent of biological reproduction. Genetic material from two selected parents is combined to create one or more new offspring. This operation happens with a high probability, often denoted as pc.
- One-Point Crossover: A single random point is selected on the parent chromosomes. The “tails” of the two chromosomes are then swapped to create two new offspring.
- Uniform Crossover: Each gene is treated separately. For every gene position, a virtual “coin is flipped” to decide which parent will pass its gene to the offspring.
- Step 4: Mutation – Introducing Random Variation Mutation is a small, random tweak made to an individual chromosome. This is the GA’s primary exploration mechanism, introducing new genetic material into the population. It is essential for maintaining diversity and preventing the algorithm from getting stuck. Mutation is applied with a very low probability, denoted as pm.
- Bit Flip Mutation: Used for binary-encoded chromosomes. One or more random bits are simply flipped (e.g., a 0 becomes a 1).
- Swap Mutation: Used for permutation-based chromosomes (like in the traveling salesman problem). Two positions on the chromosome are chosen at random, and their values are interchanged.
- Step 5: Survivor Selection – Forming the Next Generation This policy determines which individuals—from the pool of original parents and new offspring—will survive to form the population for the next generation.
- Elitism: This is a very common and powerful strategy where the single fittest individual from the current generation is guaranteed to be carried over to the next. This ensures that the best solution found so far is never lost.
- Fitness-Based Selection: In this approach, the newly created children tend to replace the least fit individuals from the previous population, constantly raising the overall fitness of the population.
- Step 6: Termination – Knowing When to Stop The cycle of selection, crossover, and mutation does not run forever. The algorithm stops when a pre-defined termination condition is met. Common stopping criteria include:
- When there has been no improvement in the population’s best fitness score for a set number of generations.
- When a maximum number of generations has been reached.
- When the fitness score of the best solution has reached a predefined target value.
Now that we’ve walked through each step of the cycle, let’s look at a formal summary of the entire process.