The Genetic Algorithm (GA)
Genetic Algorithm
- Estimated duration
- 35 minutes
- Related questions
- 13
- Related algorithms
- 1
- Status
- Log in to track
Lesson objectives
- Implementing the full GA cycle.
- Choosing the selection, crossover and mutation operators.
- Applying GA to feature selection.
Explanation
The genetic algorithm is the best-known and most widely applied evolutionary algorithm, and it stands out for its flexible encoding.
The cycle: initialize the population, evaluate the fitness, select the parents (tournament, roulette or rank), apply crossover with probability p_c and mutation with probability p_m per gene, then form the next generation with elitism.
In feature selection: the chromosome is a binary mask as long as the number of features; uniform or one-point crossover; mutation by flipping a bit with probability 1/D; and the fitness is a mix of the validation error and the number of features.
For real variables: SBX and polynomial mutation (Deb) are the common choice, and they are the ones used in the card's code.
The schema theorem (Holland) explains why short, good building blocks spread, but it is not enough to predict performance on specific problems.
Mathematics
Schema theorem
m(H,t+1)\ge m(H,t)\frac{f(H)}{\bar f}\left[1-p_c\frac{\delta(H)}{l-1}-o(H)p_m\right]Python code
import numpy as np
rng = np.random.default_rng(0)
def one_point(a, b):
c = rng.integers(1, len(a))
return np.r_[a[:c], b[c:]], np.r_[b[:c], a[c:]]
def bit_flip(m, pm):
flip = rng.random(len(m)) < pm
return np.where(flip, 1 - m, m)Simplified teaching code that works with NumPy only; for research experiments use a documented library and fix the random seed.
Practical exercise
In the GA simulator, compare mutation rates of 0.01 and 0.2, and in the neural network lab try GA for feature selection.
Common mistakes
- Evaluating fitness on the test data.
- A high mutation rate turns GA into random search.
- Without elitism the best solution is lost.
Summary
GA = selection + crossover + mutation + elitism; its flexible encoding makes it suitable for feature and architecture selection.
References
- Partially verified Adaptation in Natural and Artificial Systems John H. Holland (1992) 10.7551/mitpress/1090.001.0001
Related papers in the repository
- Adaptation in Natural and Artificial Systems Partially verified
Short quiz
5 questions are chosen at random from 13 questions linked to this lesson, and the quiz is graded on the server.
Links within the lab
- Algorithms
- Models
- Optimization types
- Related lessons
- In the paths
