GA Lec2 GA Intro
GA Lec2 GA Intro
Introduction
Sabah Sayed
Department of Computer Science
Faculty of Computers and Artificial Intelligence
Cairo University
Egypt
2/26/2024 1
Biological Evolution Process
“Natural Selection” + “Genetic Inheritance”
2/26/2024 2
Remember
• An individual is characterized by a set of parameters:
Genes
• The genes are joined into a string: Chromosome
2/26/2024 3
What Are Genetic Algorithms (GAs)?
6
Working Mechanism Of GAs
Begin
Initialize population
T =0
Evaluate Solutions
Optimum N
Solution?
Selection
Y
Mutation
Replacement
7
Simple Genetic Algorithm
Simple_Genetic_Algorithm()
{
Initialize the Population;
Mutation; //Mutation …
Replacement
8
Nature to Computer Mapping
Nature Computer
9
Components of a GA
A problem to solve, and ...
• Encoding technique (gene, chromosome)
• Initialization procedure (creation)
• Evaluation function (environment)
• Selection (reproduction, environment)
• Genetic operators (crossover, mutation)
• Parameter settings (practice and art)
Many parameters to set
• Any GA implementation needs to decide on a
number of parameters: Population size (N),
mutation rate (m), crossover rate (c)
• Often these have to be “tuned” based on
results obtained - no general theory to deduce
good values
• Typical values might be: N = 50, m = 0.05, c =
0.9
Example 1: A Very Simple Example
• Generate a bit string for the decimal number 31.
• Traditional Technique:
– Start by 00000 till reaching 11111
• GA Steps:
1. Build an initial population of random individuals
(chromosomes or initial solutions)
2. Evaluate fitness of individuals
3. Select some individuals to
• Apply Crossover and Mutation
4. Build next generation
5. Go to step 2.
2/26/2024 12
Example 1: GA Step 1
• Build an initial population of random individuals
• Generation 0
2/26/2024 13
Example 1: GA Step 2
• Evaluate fitness of individuals
4 20 11 5
2/26/2024 14
Example 1: GA Step 3
• Select some individuals to apply crossover and mutation
4 20 11 5
2/26/2024 15
Example 1: GA Step 3
• Select some individuals to apply crossover and mutation
4 20 11 5
2/26/2024 16
Example 1: GA Step 3
• Select some individuals to apply crossover and mutation
2/26/2024 17
Example 1: GA Step 3 – Trial 1
• Select some individuals to apply crossover and mutation
G11 G12
10011 01100
2/26/2024 18
Example 1: GA Step 3 – Trial 1
• Select some individuals to apply crossover and mutation
20 11
G01 G02 G03 G04
G0 00100 10100 01011 00101
Fitness?
2/26/2024 19
Example 1: GA Step 3 – Trial 2
• Select some individuals to apply crossover and mutation
G11 G12
10111 01000
2/26/2024 20
Example 1: GA Step 3 – Trial 2
• Select some individuals to apply crossover and mutation
20 11
G01 G02 G03 G04
G0 00100 10100 01011 00101
Fitness?
2/26/2024 21
Example 1: GA Step 3 – Trial 2
• Select some individuals to apply crossover and mutation
20 11
G01 G02 G03 G04
G0 00100 10100 01011 00101
2/26/2024 24
G01 G02 G03 G04
G0 00100 10100 01011 00101
2/26/2024 25
G01 G02 G03 G04
G0 00100 10100 01011 00101
2/26/2024 26
Example 1: GA Step 3 – Trial 4
• Select ALL individuals to apply crossover and mutation
G01 G02 G03 G04
G0 00100 10100 01011 00101
4 20 11 5
G11 G12
00011 01100
3 12
2/26/2024 27
Example 1: GA Step 3 – Trial 4
• Select ALL individuals to apply crossover and mutation
G01 G02 G03 G04
G0 00100 10100 01011 00101
4 20 11 5
G11 G13 G12 G14
00011 10101 01100 00100
3 21 12 4
G11 G13 G12 G14
00011 11101 01100 00110
3 21→29 12 4→6
2/26/2024 28
Example 1: GA Step 4
• Build next generation .. Full Replacement or Mix?
G01 G02 G03 G04
G0 00100 10100 01011 00101
4 20 11 5
G11 G13 G12 G14
00011 10101 01100 00100
3 21 12 4
G11 G13 G12 G14
00011 11101 01100 00110
3 29 12 6
2/26/2024 29
Example 1: GA Step 4
• Build next generation .. Mixing
G02 G03 G11 G13
G1 10100 01011 00011 11101
20 11 3 29
G02 G03 G21 G22
10100 01011 00001 11111
20 11 1 31
OUR OBJECTIVE!
2/26/2024 30
Does it really cost one iteration?
• Real world problems are not that easy.
• You could have:
– Hundreds of individuals (chromosomes)
– Huge search space
• You might perform thousands of iterations
(have thousands of generations) before
reaching your objective!
2/26/2024 31
How do you encode a solution?
2/26/2024 34