1. The Building Blocks: Meet the Population
To understand how a GA works, you first need to know its basic terminology, which borrows heavily from biology.
Population A set of all candidate solutions for the problem. Analogy: All the contestants in a competition.
Chromosome A single candidate solution from the population. Analogy: One individual contestant.
Gene A single element or position on a chromosome. Analogy: A specific trait of a contestant, like their height or speed.
Encoding the Solution (Genotype vs. Phenotype)
A key concept in GAs is the difference between a real-world solution (Phenotype) and its computational representation (Genotype). The algorithm works with the encoded Genotype.
For example, in the 0/1 Knapsack Problem (where you must choose which items to pack to maximize value without exceeding a weight limit), the real-world solution (Phenotype) might be a list of item numbers to pick. However, the algorithm represents this as a binary string (Genotype), where each position corresponds to an item. A 1 means the item is packed, and a 0 means it is not.
The Judge: The Fitness Function
The Fitness Function is the most critical component of a GA. It acts as the judge, evaluating how “good” each solution (chromosome) is.
Its primary purpose is to take a solution as input and produce a “suitability” score as output. Because this calculation is performed for every solution in every generation, the fitness function must be fast to compute to avoid slowing down the entire algorithm.
With these building blocks in place, we can now walk through the lifecycle of a Genetic Algorithm.