Tutorial: Genetic Algorithms
Introduction to Genetic Algorithms
A Genetic Algorithm (GA) is an optimization technique inspired by the principles of natural selection and biological evolution.
Genetic algorithms are useful for finding good solutions to complex optimization problems where traditional mathematical techniques may be difficult to apply.
The basic idea is:
Generate a population of possible solutions → evaluate their quality → select better solutions → create new solutions through crossover and mutation → repeat until a satisfactory solution is found.
Genetic algorithms are commonly used in:
- Optimization
- Scheduling
- Feature selection
- Machine learning
- Engineering design
- Route optimization
- Resource allocation
- Function optimization
1. Basic Terminology
A genetic algorithm uses terminology inspired by biological evolution.
Population
A population is a collection of candidate solutions.
If there are candidate solutions, the population can be represented as:
where each represents one chromosome.
Chromosome
A chromosome represents a complete candidate solution to the problem.
For a binary genetic algorithm, a chromosome may be:
For example:
Each chromosome represents a possible solution.
Gene
A gene is an individual element of a chromosome.
For:
the individual genes are:
Thus, a chromosome consists of multiple genes.
Allele
An allele represents the possible value of a gene.
For a binary genetic algorithm, the possible alleles are:
For example, contains genes whose alleles are either or .
2. Population Initialization
The first step in a genetic algorithm is to generate an initial population.
For example, suppose the chromosome length is and the population size is :
The initial population can be generated randomly.
The algorithm then evaluates every chromosome using a fitness function.
3. Fitness Function
The fitness function determines how good a candidate solution is.
It maps a chromosome to a numerical fitness value:
where:
- = chromosome
- = fitness value
For a maximization problem, a larger fitness value generally represents a better solution.
Example:
| Chromosome | Fitness |
|---|---|
The chromosome has the highest fitness:
and is therefore a strong candidate for selection.
4. Selection
Selection chooses chromosomes from the current population to become parents for reproduction.
The basic principle is:
Better chromosomes should have a higher probability of being selected.
One common method is fitness-proportional selection (also known as roulette-wheel selection).
The probability of selecting chromosome is:
where:
- = probability of selecting chromosome
- = fitness of chromosome
5. Selection Example
Suppose four chromosomes have fitness values:
The total fitness is:
Selection probabilities:
| Chromosome | Fitness | Selection Probability |
|---|---|---|
The chromosome with the highest fitness has the highest selection probability.
6. Other Selection Methods
Several selection strategies can be used in genetic algorithms.
Roulette-Wheel Selection
Selection probability is proportional to fitness:
Tournament Selection
A small group of chromosomes is randomly selected, and the best chromosome from that group becomes a parent.
Rank Selection
Chromosomes are ranked according to fitness, and selection probabilities depend on their ranks rather than their raw fitness values.
7. Crossover
Crossover combines genetic information from two parent chromosomes to produce new offspring.
The two selected chromosomes are called parents.
The resulting chromosomes are called children or offspring.
Example:
1Parent 1: 101|101 2Parent 2: 011|010
If the crossover point is after the third gene:
1Child 1: 101|010 2Child 2: 011|101
Therefore:
Crossover allows useful characteristics from different parents to be combined.
8. Single-Point Crossover
In single-point crossover, one crossover point is selected.
Suppose:
After crossover:
The general representation is:
After crossover:
9. Two-Point Crossover
In two-point crossover, two crossover points are selected.
Example:
1Parent 1: 10|110|01 2Parent 2: 01|001|11
The middle segments are exchanged:
1Child 1: 10|001|01 2Child 2: 01|110|11
Two-point crossover can combine a larger internal section of two parent chromosomes.
10. Crossover Probability
Crossover is usually controlled by a crossover probability:
For example:
means that approximately of selected parent pairs are expected to undergo crossover.
A random number can be generated to determine whether crossover occurs.
11. Mutation
Mutation randomly changes one or more genes in a chromosome.
Mutation helps maintain genetic diversity and prevents the population from becoming too similar.
Example:
1Before: 101101 2After: 101001
The fifth gene has changed:
Therefore, mutation produces a new chromosome.
12. Mutation Probability
Mutation is controlled by a mutation probability:
Usually, mutation probability is relatively small.
For example:
means that each gene has approximately a probability of mutation.
For a binary gene, mutation can be represented as:
13. Mutation Example
Suppose:
If the fifth gene mutates:
The change is:
Only one gene has changed. Mutation introduces new genetic information into the population.
14. Elitism
Elitism is a strategy in which one or more of the best chromosomes are directly copied to the next generation.
Suppose the best chromosome is:
Instead of risking its loss during crossover or mutation, it can be copied directly into the next population.
The principle is:
Elitism helps preserve high-quality solutions.
15. Genetic Algorithm Flow
The basic genetic algorithm follows these steps:
1Initialize Population 2 ↓ 3Calculate Fitness 4 ↓ 5Select Parents 6 ↓ 7Crossover 8 ↓ 9Mutation 10 ↓ 11Evaluate New Population 12 ↓ 13Apply Elitism 14 ↓ 15Termination Check 16 ↓ 17 No ───────→ Repeat 18 ↓ Yes 19 Best Solution
Mathematically, the evolutionary process can be summarized as:
where:
- = current population
- = next population
16. Termination Condition
A genetic algorithm needs a condition to determine when it should stop.
Common termination conditions include:
Maximum Number of Generations
Stop when:
where is the maximum number of generations.
Target Fitness
Stop when:
Convergence
Stop when the population has stopped improving significantly:
where is a small threshold.
17. Complete Genetic Algorithm
Step 1: Initialize
Generate an initial population:
Step 2: Evaluate
Calculate fitness for every chromosome:
Step 3: Select
Select parents based on their fitness. For fitness-proportional selection:
Step 4: Crossover
Combine selected parents:
Step 5: Mutation
Randomly modify genes:
Step 6: Create New Population
Form from the new offspring.
Step 7: Check Termination
If the termination condition is satisfied → Stop
Otherwise:
and repeat the process.
18. Example of a Complete Generation
Consider the following initial population:
| Chromosome | Fitness |
|---|---|
Total fitness:
Selection probabilities:
Suppose the selected parents are:
1Parent 1: 101|101 2Parent 2: 110|011
After single-point crossover:
1Child 1: 101|011 2Child 2: 110|101
Suppose mutation changes one gene in Child 1:
1Before: 101011 2After: 100011
The new offspring are then evaluated using the fitness function. This process forms the next generation.
19. Genetic Algorithm for Optimization
Suppose we want to maximize a function .
The genetic algorithm attempts to find:
where is the best solution found.
If the problem is a minimization problem:
The fitness function may be directly related to the objective function for maximization problems.
For minimization problems, the objective may need to be transformed into a fitness value.
20. Binary Encoding
In a binary genetic algorithm, solutions can be represented using binary chromosomes.
For example:
If the chromosome contains genes, its length is .
Each gene can have one of two values:
Therefore, the chromosome can be represented as:
21. Real-Valued Encoding
Genetic algorithms do not have to use binary chromosomes.
A chromosome can also contain real-valued genes:
This representation is useful for continuous optimization problems.
For example, if , a chromosome could directly represent:
without converting it into a binary string.
22. Advantages of Genetic Algorithms
- They can search large solution spaces.
- They do not necessarily require derivatives.
- They can handle complex objective functions.
- They can escape some local optima through population-based search.
- They can work with discrete and continuous variables.
- They are flexible and applicable to many optimization problems.
23. Limitations of Genetic Algorithms
- They can require many fitness evaluations.
- Results may vary between runs because of randomness.
- Choosing parameters can be difficult.
- They do not always guarantee the global optimum.
- Poor parameter choices can cause premature convergence.
- They can be computationally expensive for large problems.
24. Genetic Algorithms vs Traditional Optimization
| Feature | Genetic Algorithm | Traditional Optimization |
|---|---|---|
| Search strategy | Population-based | Often single-solution based |
| Derivatives | Usually not required | Often required |
| Randomness | Yes | Often less |
| Complex search spaces | Suitable | Depends on method |
| Local optima | Can sometimes escape | May get trapped |
| Global optimum | Not guaranteed | Not always guaranteed |
25. Soft Computing: Neural Networks vs Fuzzy Logic vs GA
Soft computing includes several computational techniques that can deal with complex, uncertain, or difficult problems.
| Technique | Main Purpose | Inspiration |
|---|---|---|
| Neural Networks | Learning | Biological brain |
| Fuzzy Logic | Reasoning | Human reasoning |
| Genetic Algorithms | Optimization | Natural evolution |
Neural Networks
Neural networks learn patterns from data. Their basic computational structure can be represented as:
They are commonly used for classification, regression, computer vision, natural language processing, and pattern recognition.
Fuzzy Logic
Fuzzy logic represents degrees of truth or membership:
It is useful for reasoning with imprecise concepts such as Hot, Cold, Fast, Slow, High, Low.
Genetic Algorithms
Genetic algorithms search for good solutions using evolutionary operations:
They are mainly used for optimization.
26. Important Genetic Algorithm Formulas
Population
Fitness
Fitness-Proportional Selection
Crossover Probability
Mutation Probability
Maximization
Minimization
Binary Gene
27. Summary
A Genetic Algorithm is an optimization technique inspired by biological evolution.
The major components are:
The main evolutionary operations are:
The overall process is:
The most important selection formula is:
The key idea behind a genetic algorithm is simple:
Keep better solutions, combine useful information, introduce small random changes, and repeat the process to search for better solutions.
Genetic algorithms are therefore particularly useful when the search space is large, complex, non-linear, or difficult to optimize using conventional mathematical methods.