Genetic algorithm resource
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Genetic algorithms (GAs) are a class of optimization and search heuristics inspired by natural selection processes mimicking biological evolution. They are particularly useful for solving problems where objective functions are testable but not easily defined mathematically. GAs traverse large and complex search spaces efficiently, making them valuable tools in various fields, from engineering to artificial intelligence. This article delves into the fundamental mechanisms of genetic algorithms, providing technical details and examples to illuminate their utility and implementation.
Basic Concepts
Chromosomes and Encoding
In genetic algorithms, potential solutions are encoded as "chromosomes," usually represented as strings, like arrays of bits, numbers, or characters. Each part of the chromosome is called a "gene," and together they encode potential solutions to a problem.
Fitness Function
The fitness function evaluates how "good" a candidate solution (chromosome) is in terms of minimizing or maximizing a particular objective. A crucial part of GA design, this function guides the evolutionary process toward optimal solutions.
Selection
Selection mimics natural selection, prioritizing high-fitness candidates for reproduction, which converge towards optimal solutions. Common methods include:
• Roulette Wheel Selection: Assigns a probability proportional to fitness. • Tournament Selection: Randomly selects a subset and picks the fittest. • Rank Selection: Ranks individuals by fitness and selects based on rank.
Crossover
Also known as recombination, crossover exchanges genetic information between parent chromosomes to produce new offspring. Common crossover techniques include:
• Single-point Crossover: A single cut point in parent chromosomes switches the tails between them. • Multi-point Crossover: Multiple cut points create more diverse offspring. • Uniform Crossover: Each gene is chosen randomly from one of the parents.
Mutation
Mutation introduces genetic diversity by altering one or more gene values in a chromosome randomly, helping the algorithm escape local optima:
• Bit Flip Mutation: Changes a bit in a binary string. • Swap Mutation: Swaps two genes in a sequence.
Termination Conditions
GAs terminate based on satisfying certain criteria, such as:
• A solution meeting a defined level of fitness. • A fixed number of generations. • No significant improvement over a set number of generations.
Applications of Genetic Algorithms
Genetic algorithms have diverse applications across various fields, including:
• Engineering Design Optimization: GAs optimize structural designs by adjusting parameters in complex systems like aerodynamics or electronic circuits. • Machine Learning: They optimize neural network architectures and hyperparameters. • Game Theory: GAs find optimal strategies in competitive environments or board game scenarios. • Art and Music Generation: They create aesthetically pleasing art or music pieces by evolving creative traits. • Bioinformatics: GAs analyze genetic data, assisting in gene-sequence alignments and protein structure predictions.
Example of Genetic Algorithm in Action
Consider a simple function optimization problem: maximizing the function where is encoded as a 5-bit binary string.
- Initialization: Randomly generate initial population:
| Chromosome | Decimal Value | Fitness () | |||||
| 10101 | 21 | 441 | |||||
| 11001 | 25 | 625 | |||||
| 11100 | 28 | 784 | 2. Selection: Choose parents based on fitness, using roulette wheel selection. 3. Crossover: Perform crossover to produce offspring: | Parent 1 | Parent 2 | Offspring 1 | Offspring 2 |
| --- | --- | --- | --- | --- | --- | --- | --- |
| 10101 | 11001 | 10101 | 11001 | 4. Mutation: Mutate offspring for genetic diversity. Suppose gene 2 in Offspring 1 flips: | Chromosome | After Mutation | |
| --- | --- | --- | --- | --- | --- | --- | |
| 10101 | 11101 | ||||||
| 11001 | 11001 | 5. Evaluation: Calculate fitness of new population and repeat until termination criteria meet. ## Key Points Summary Here is a table summarizing the key points of genetic algorithms: | Key Component | Description | Examples | ||
| --- | --- | --- | --- | --- | --- | ||
| Chromosome | Encodes potential solutions | Binary strings, real numbers | |||||
| Fitness Function | Measures fitness of a solution | , neural network accuracy | |||||
| Selection | Chooses parents for reproduction | Roulette wheel, tournament, rank | |||||
| Crossover | Combines parents to create offspring | Single-point, multi-point, uniform | |||||
| Mutation | Introduces genetic diversity | Bit flip, swap | |||||
| Termination | Endpoint of the GA process | Max fitness, set generations, no improvement | |||||
| Applications | Fields utilizing genetic algorithms | Engineering, machine learning, bioinformatics, art generation |
Enhancements and Variants
Adaptive Genetic Algorithms
Adaptive GAs dynamically adjust parameters like crossover and mutation rates, enhancing performance by responding to the current state of the search.
Parallel Genetic Algorithms
Implemented over parallel architectures, parallel GAs divide populations to speed up computation, beneficial for large and complex problems.
Multi-objective Genetic Algorithms
These extend GAs to handle multiple conflicting objectives, seeking Pareto-optimal solutions rather than single objective maximization or minimization.
Genetic algorithms are powerful tools for navigating complex search spaces. Their strengths lie in their ability to adapt, parallelize, and handle multiple objectives, ensuring a broad range of applications across the industrial and scientific spectrum.
Related reading
- gensim Doc2Vec vs tensorflow Doc2Vec
- Gensim LDA Coherence `Score` Nan
- Geometric representation of Perceptrons Artificial neural networks
- Get a simple MLP in TensorFlow to model XOR
- Get a filtered list of files in a directory
- Get a permutation as a function of a unique given index in On
- Get a sublist from an ArrayList efficiently
- Get the biggest chronological drop, min and max from an array with On

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.