Genetic Algorithms Tutorials
Genetic Algorithms Tutorials
Genetic Algorithms Tutorials
Tutorials
Asst. Prof.
Dr. Mohammed Najm Abdullah
Genetic Algorithm Process
2
Example: Travelling Salesman Problem
• Find a tour of a given set of cities so that 1) each city is visited only
once 2) the total distance traveled is minimised
• Representation is an ordered list of city numbers (known as order-based
GA):
1) London 3) Dunedin 5) Beijing 7) Tokyo
2) Venice 4) Singapore 6) Phoenix 8) Victoria
CityList1 (3 5 7 2 1 6 4 8)
CityList2 (2 5 7 6 8 1 3 4)
• Recombination uses i) crossover and ii) mutation by inversion:
Parent1 (3 5 7 2 1 6 4 8)
Parent2 (2 5 7 6 8 1 3 4) crossover
Child (2 5 7 2 1 6 3 4)
3
Example: Travelling Salesman Problem
• Mutation involves reordering of the list: eg flip elements 3 and 6
• * *
Before: (2 5 7 2 1 6 3 4) mutation
After : (2 5 6 2 1 7 3 4)
4
Example: Travelling Salesman Problem
ICT219 5
Example: Travelling Salesman Problem
ICT219 6
Example: Travelling Salesman Problem
7
Pros and Cons of Genetic Algorithms
• GAs are a flexible, widely applicable optimization process
• Unlike NNs, GAs tend to avoid local minima, and find the global solution
(if you are not in a hurry) because search is not restricted to a single
part of the problem space
•
9
Case Study
Consider the determination of the nearest integer value
( in the range 0 to 15 ) of x that maximises the cost
function :
y( x) 0.2x 1.5x
2
0 0 1 1 0 0 0 1 0 0 0 1
0 0 0 1 0 0 1 1 0 0 1 1
0 0 1 1 0 1 1 1 0 1 0 1
0 1 1 1 0 0 1 1 0 0 1 1
Crossover Mutation
Case Study Solution - Step 3
Evaluate the fitness of the 2nd generation :
0 1 0 1 0 0 1 1 0 0 1 1
0 0 1 1 0 0 1 1 0 0 1 1
0 0 1 1 0 0 1 1 0 0 1 1
Crossover Mutation
Note! Local Minima without mutation
Case Study Solution - Step 5
Evaluate the fitness of the 3rd generation :
18
Genetic Programming
A program in C
• int foo (int time)
{
int temp1, temp2;
if (time > 10)
temp1 = 3;
else
temp1 = 4;
temp2 = temp1 + 1 + 2;
return (temp2);
}
19
Program tree
-1.00 1.00
-0.80 0.84
-0.60 0.76
-0.40 0.76
-0.20 0.84
0.00 1.00
0.20 1.24
0.40 1.56
0.60 1.96
0.80 2.44
1.00 3.00
21
Problem description
Objective: Find a computer program with one
input (independent variable X) whose
output Y equals the given data
x+1 x2 + 1 2 x
23
X Y X+1 |X+1- X2+1 |X2+1- 2 |2-Y| X |X-Y|
Y| Y|
-1.00 1.00 0 1 2 1 2 1 -1.00 2
-0.80 0.84 0.20 0.64 1.64 0.80 2 1.16 -0.80 1.64
-0.60 0.76 0.40 0.36 1.36 0.60 2 1.24 -0.60 1.36
-0.40 0.76 0.60 0.16 1.16 0.40 2 1.24 -0.40 1.16
-0.20 0.84 0.80 0.04 1.04 0.20 2 1.16 -0.20 1.04
0.00 1.00 1.00 0 1 0 2 1 0.00 1
0.20 1.24 1.20 0.04 1.04 0.20 2 0.76 0.20 1.04
0.40 1.56 1.40 0.16 1.16 0.40 2 0.44 0.40 1.16
0.60 1.96 1.60 0.36 1.36 0.60 2 0.04 0.60 1.36
0.80 2.44 1.80 0.64 1.64 0.80 2 0.44 0.80 1.64
1.00 3.00 2.00 1 2 1 2 1 1.00 2
Σ Σ Σ Σ
picking “+”
subtree and
leftmost “x” as
crossover points
25
Mutation
Mutation:
/
picking “2”
as mutation
point
26
Generation 1
Second offspring
First offspring of of crossover of
Mutant of (c) crossover of (a) (a) and (b)
and (b) picking “+” of
Copy of (a) picking “2” picking “+” of parent (a) and
as mutation parent (a) and
point left-most “x” of
left-most “x” of parent (b) as
parent (b) as crossover points
crossover points
27
X Y X+1 |X+1- 1 |1-Y| X |X-Y| X2+X |X2+X+
Y| +1 1-Y|
-1.00 1.00 0 1 1 0 -1.00 2 1 0
-0.80 0.84 0.20 0.64 1 0.16 -0.80 1.64 0.84 0
-0.60 0.76 0.40 0.36 1 0.24 -0.60 1.36 0.76 0
-0.40 0.76 0.60 0.16 1 0.24 -0.40 1.16 0.76 0
-0.20 0.84 0.80 0.04 1 0.16 -0.20 1.04 0.84 0
0.00 1.00 1.00 0 1 0 0.00 1 1 0
0.20 1.24 1.20 0.04 1 0.24 0.20 1.04 1.24 0
0.40 1.56 1.40 0.16 1 0.56 0.40 1.16 1.56 0
0.60 1.96 1.60 0.36 1 0.96 0.60 1.36 1.96 0
0.80 2.44 1.80 0.64 1 1.44 0.80 1.64 2.44 0
1.00 3.00 2.00 1 1 2 1.00 2 3 0
Σ Σ Σ Σ