Genetic Algorithm – Detailed Guide (Concept, Stages,
Encoding, and Solved Example)
1. What is a Genetic Algorithm?
Genetic Algorithm (GA) is a search and optimization technique inspired by the process of natural evolution.
In nature, organisms evolve over generations through natural selection, reproduction, and mutation.
Genetic Algorithms apply the same ideas to solve mathematical and computational problems. The
algorithm starts with many possible solutions and gradually improves them over several generations until it
finds a very good or optimal solution.
2. Why Genetic Algorithms Were Invented
Many real-world optimization problems are extremely complex. Traditional algorithms sometimes get stuck
in poor solutions or require too much time to search the entire solution space. Genetic Algorithms were
designed to explore the search space more intelligently by evolving better solutions over time.
3. Key Terminology in Genetic Algorithms
Population: A set of candidate solutions.
Chromosome: One candidate solution.
Gene: A part of a chromosome representing a variable.
Generation: One iteration of evolution.
Fitness Function: A function used to evaluate the quality of each solution.
4. The Five Main Stages of the Genetic Algorithm
1. Initialization – Generate an initial population randomly.
2. Fitness Evaluation – Calculate the fitness value for each chromosome.
3. Selection – Choose the best chromosomes for reproduction.
4. Crossover – Combine two parents to produce new offspring.
5. Mutation – Randomly change small parts of a chromosome to maintain diversity.
5. Why Binary Numbers Are Commonly Used
Binary representation (0 and 1) is simple and efficient for computers. Binary encoding allows easy
crossover operations (swapping segments) and mutation operations (flipping bits). Because of its
simplicity, early Genetic Algorithms widely used binary encoding.
6. Other Encoding Methods
Although binary encoding is common, other encoding methods can also be used:
- Integer Encoding (used when solutions are integer values)
- Real Number Encoding (used in continuous optimization)
- Permutation Encoding (used in scheduling and routing problems like the Traveling Salesman Problem)
- Tree Encoding (used in genetic programming).
7. Example Problem Using Genetic Algorithm
Maximize the function: f(x) = x² where 0 ≤ x ≤ 7.
Binary Representation
Binary Decimal x
000 0
001 1
010 2
011 3
100 4
101 5
110 6
111 7
Initial Population
Chromosome x Fitness f(x)=x²
010 2 4
101 5 25
001 1 1
110 6 36
Selection
Chromosomes with higher fitness values are more likely to be selected. In this example, 110 and 101 are
the strongest candidates.
Crossover Example
Parent 1: 110
Parent 2: 101
After crossover we exchange the last part of the chromosome to produce offspring such as 111 and 100.
Mutation Example
A mutation might flip one bit: 101 → 111. This introduces diversity and helps the algorithm avoid local
optimum solutions.
Final Observation
After several generations, the algorithm will converge toward the best solution. In this example the best
value is x = 7 where f(x) = 49.