Genetic Algorithms
Genetic Algorithms
Genetic Algorithms
• A Genetic Algorithm is a population based heuristic technique
• Inspired from the biological reproduction system and used to solve
search and optimization problems
• There are other similar algorithms inspired from the real world, e.g:
• Ant colony optimization
• Particle Swarm optimization
• Bee algorithm
• …etc
Machine Learning
Genetic Algorithms
• Developed by John Holland over the course of the 1960s and 1970s to
simulate Darwinian evolution
Machine Learning
Evolutionary Algorithms
Chromosome (string)
gene
Population
individual
Fitness based
Selection
Crossover
Mutation
Genetic Operators
Machine Learning
Genetic Algorithms
Genetic Algorithm Components:
• Population of individuals
• Fitness Function
• Genetic Operators
Machine Learning
Flowchart
Machine Learning
Genetic Algorithms
Individuals:
gene
0 1 0 0 01 1 1 0 0 1 1 0 1 0 1 1 1 1 0 0 0 1 1
Machine Learning
Genetic Algorithms
Can be a binary representation
Machine Learning
Genetic Algorithms
GAs search from a population of chromosomes
i Chromosome Xi
1 1000
2 0101
3 1001
4 0111
Two issues:
Population size
Initializing the population
Machine Learning
Genetic Algorithms
Fitness Function
• It is analogous to the environment an animal lives in
• Gives a numerical description of how fit the solution encoded in a
particular chromosome is.
• Penalty Functions
• Approximate Function evaluation
• Fitness function is analogous to heuristic function.
Machine Learning
Genetic Algorithms
Every generation, a fitness function plays the role of the environment,
rating chromosomes in terms of their ability to solve the problem.
i chromosome Xi f(Xi)=[decode(Xi)]2
1 1000 64
2 0101 25
3 1001 81
4 0111 49
Total = 219
Average = 54.75
Machine Learning
Genetic Algorithms :Selection
Selection decides which individuals go into the mating pool
Machine Learning
Genetic Algorithms
i chromosome Xi f(Xi)=(Xi)2
1 1000 64
2 0101 25
3 1001 81
4 0111 49
Total = 219
Machine Learning
Genetic operations
Novel solutions are produced using the genetic operators
crossover and mutation.
Crossover
Explores the search space
Mutation
Recaptures “lost” genes due to crossover
Machine Learning
Crossover
Mimics sexual reproduction
Two individuals (parents) are selected randomly from the
mating pool
Cross site selected at random
Offspring are created by swapping genes to right of cross site
Parent 1 Child 1
0 0 1 0 1 0 1 1 0 0 1 0 1 0 0 0
Crossover
Parent 2 Site (5) Child 2
0 1 1 0 1 0 0 0 0 1 1 0 1 0 1 1
Machine Learning
Mutation
Mutation occurs for a given percentage of the children
Mutation can flip, swap or alter the value of a gene
Child 2 Child 2
No mutation
0 1 1 0 1 0 1 1 0 1 1 0 1 0 1 1
Machine Learning
Termination
The GA continues to produce new
generations of individuals until some
termination criterion is satisfied.
Machine Learning
Example 1
Machine Learning
representation
X Binary Encoding
0 0000
1 0001
2 0010
. .
. .
13 1101
14 1110
15 1111
Machine Learning
Search Space Initial Population
0001 0000
0100
1101 1000 0101 1000 f(x)=64
1111
0011 1001 0101 f(x)=25
1110
1011 1001 f(x)=81
0111 0010
1010 0110 0111 f(x)=49
1100
Avg 54
1011
1101
0001 0000
0100
1101 1000 0101 1000 f(x)=64
1111
0011 1001 0101 f(x)=25
1110
1011 1001 f(x)=81
0111 0010
1010 0110 0111 f(x)=49
1100
Avg 54
1011
1101
0001 0000
0100
1101 1000 0101 1 1 1 1 f(x)=225
1111
0011 1001 1 1 1 1 f(x)=225
1011 1110
1 1 0 1 f(x)=169
0101 0010
1010 1110 1 1 1 0 f(x)=196
1100
Avg 203
Machine Learning
A Real Example: TSP
Machine Learning
Representation
CityList1 (3 5 7 2 1 6 4 8)
CityList2 (2 5 7 6 8 1 3 4)
Machine Learning
Crossover
Parent1 (3 5 7 2 1 6 4 8)
Parent2 (2 5 7 6 8 1 3 4)
Child (5 8 7 2 1 6 3 4)
Machine Learning
Mutation
Mutation involves reordering of the list:
Before: (5 8 7 2 1 6 3 4)
After: (5 8 6 2 1 7 3 4)
Machine Learning
TSP Example: 30 Cities
120
100
80
y 60
40
20
0
0 10 20 30 40 50 60 70 80 90 100
x
Machine Learning
Solution i (Distance = 941)
120
100
80
y 60
40
20
0
0 10 20 30 40 50 60 70 80 90 100
x
Machine Learning
Solution j(Distance = 800)
44
62 TSP30 (Performance = 800)
69
67 120
78
64 100
62
54
80
42
50
40 y 60
40
38 40
21
35
67 20
60
60 0
40 0 10 20 30 40 50 60 70 80 90 100
42 x
50
99
Machine Learning
Solution k(Distance = 652)
TSP30 (Performance = 652)
120
100
80
y 60
40
20
0
0 10 20 30 40 50 60 70 80 90 100
x
Machine Learning
Best Solution (Distance = 420)
42
38 TSP30 Solution (Performance = 420)
35
26 120
21
35 100
32
7
80
38
46
44 y 60
58
60 40
69
76
20
78
71
69 0
67 0 10 20 30 40 50 60 70 80 90 100
62 x
84
94
Machine Learning
Genetic Algorithms
0/1 Knapsack problem
Genetic algorithms
• GA questions
• What is the chromosome// vector of city ids in TSP
• Cross-over// no redundancy
• Mutation// swap
• Fitness function // find the distance between each two cities and return the
total distance
• Termination condition// number iterations. convergence
Examples of problems that can be solved
using GA
• TSP (done)
• 8 queen problem
• 0/1 knapsack
• Graph colouring problem
• Find a minima in a function (optimization)
• Sorting an array
0/1 knapsack:
[I1, I2,I3,I6]
[I3,I5,I7]
C1=[0,1,1,0,0,1]
C2=[1,0,1,0,1,1]
• Mutation
C1=[0,1,1,0,0,1] ➔ C1’[0,1,1,1,0,1] randomly flip one bit
finess
• Convergence
8-Queen problem
representation
• [1, 6, 2, 5, 7, 4, 8, 3].
Crossover and mutation