LEC14 (IS)
LEC14 (IS)
Intelligent Systems
Lecture 14
Genetic Algorithms (GAs)
Lecturer
Dr. Faris S. Alghareb
PhD in Computer Engineering @ UCF
email: [email protected]
Copyright © 2020 Faris S. Alghareb. All rights reserved.
Genetic Algorithms – Learning objectives
v Genetic operators
◆ be able to compute crossover and mutation
v Each chromosome represents an individual in the population, and this individual a one
solution (algorithm) used to built a software system.
! Binary-Coded GAs encode the chromosome using distinct fields of bit strings
− Each field encodes one orthogonal attribute
− e.g. {0010|0100}, {0101|0000}, {0001|0001}, etc. are three individuals in the population
each of which is encoding two fields with a range of 16 values (24) per field.
◆ Non-binary codings use one or more distinct tuples (sets) of real or integer values
! (1,5) ; (3,2) ; etc.
Replacement ..
.
offspring
Population of
candidate solutions fitness Evaluate the fitness
Mutation function of individuals
No
parents Selection of
parents
🛑stop
This is because the population size increases over time, thus it is then required to
select survivors to maintain fixed population size across generations
EECIE20-S4305 Intelligence Systems & Software Engineering: Genetic Algorithms Slide 5
Representing Candidate Solutions
v Representation of an individual can be using discrete values (binary, integer, or any other
system with a discrete set of values)
Individual
(Chromosome)
1 0 0 0 0 1 0 0
Gene
g g+1
selection
reproduction
mutation
Recombination
(crossover)
◆ In mutation, the solution may change entirely from the previous solution.
◆ The purpose of mutation in GAs is to introduce diversity into the sampled population
◆ Hence GA can come to find a better solution when applying mutation
Mutation Operators: The conventional way of a mutation operator involves a probability that
an arbitrary bit in a genetic sequence will be flipped (inverted).
v Bit inversion
◆ Used in the binary encoding of chromosomes.
◆ The mutation of bit strings ensue through bit flips at random positions
11001001 à10001001
before a
1 1 1 1 1 1 1 b
AND f
c
d
a
after
b
1 1 1 0 1 1 1 OR f
c
d
mutation gene
Population:
cut cut
1 1 1 1 1 1 1 0 0 0 0 0 0 0 parents
1 1 1 0 0 0 0 0 0 0 1 1 1 1 offspring
Ø Parent 1: X X| X XXXX
Ø Parent 2: Y Y| Y YYYY
v Creates by single-point crossover between the 2nd and 3rd bits from the left:
Ø Offspring 1: X XY YYYY
Ø Offspring 2: Y YX XXXX
Example:
Ø Parent1 = 00100110
Ø Parent2 = 10101010
v Assume crossover occurred between leftmost and next leftmost bit to create 2 offspring:
Ø Offspring1 = 10100110
Ø Offspring2 = 00101010
v Thus, sometime the searching algorithm is stuck a local minimum or maximum point to be
found as the global one.
v Applying a mutation operator can help to solve this issue, widening the searching range
through generating diverse genies
EECIE20-S4305 Intelligence Systems & Software Engineering: Genetic Algorithms Slide 13
Genetic Algorithm (GA) ⎼ Example (1/5)
✏ Find the maximum value if the fitness function is f(x) = X2 in the searching range of [0,
31]. Use the Single Point Crossover when generating the offspring of 2nd and 3rd
generations.
ANS:
● Find the number of bits that required to represent the strings as 2N
2N = 32 à 5-bit
● Note that number of strings is arbitrary such as 4 strings (No. of strings = number of bits
– 1)
● Selecting four numbers randomly
● Calculation the new generations (offspring) based on the fitness function given in the
question.
Binary Numbers
Decimal
(Randomly selected)
01101 13
11000 24
01000 8
10011 19
EECIE20-S4305 Intelligence Systems & Software Engineering: Genetic Algorithms Slide 14
Genetic Algorithm (GA) ⎼ Example (2/5)
Find the maximum value if the fitness function is f(x) = X2 in the searching range of [0, 31]. Use the
Single Point Crossover when generating the offspring of 2nd and 3rd generations.
ANS:
First Generation
1st Generation X Fitness Function 𝐗𝟐 𝐗𝟐
No. Round
(binary) (decimal) 𝒇 𝒙 = 𝑿𝟐 ∑ 𝐗𝟐 𝐀𝐕𝐄
1 01101 13 169 0.14 0.58 1
2 11000 24 576 0.49 1.97 2
3 01000 8 64 0.06 0.22 0
4 10011 19 361 0.31 1.23 1
∑ "! ∑ X $ = 1170
Ø Average (AVE) #
= 293
Second Generation
2nd Generation X Fitness Function 𝐗𝟐 𝐗𝟐
No. Round
(binary) (decimal) 𝒇 𝒙 = 𝑿𝟐 ∑ 𝐗𝟐 𝐀𝐕𝐄
1 01100 12 144 0.08 0.32 0
2 11001 25 625 0.35 1.42 1
3 11011 27 729 0.41 1.66 2
4 10000 16 256 0.14 0.58 1
∑ "!
∑ X $ = 1754
Ø Average (AVE) #
= 439
Third Generation
3rd Generation X Fitness Function 𝐗𝟐 𝐗𝟐
No. Round
(binary) (decimal) 𝒇 𝒙 = 𝑿𝟐 ∑ 𝐗𝟐 𝐀𝐕𝐄
1 11001 25 625 0.27 1.09 1
2 11011 27 729 0.32 1.27 1
3 11000 24 576 0.25 1.00 1
4 10011 19 361 0.15 0.63 1
Ø Average (AVE)
∑ ""
= 572.75 ∑ X $ = 2291
#
Fitness Function
Generations
Fitness Function
Generations
Generations
Note: here you should apply the mutation operator instead of the crossover to
find the offspring for next generations
v First, let us see what the probability of the possible solutions will be if we need to
choose from a set that has 3 choices
Ø We need to choose one out of three, then one out of two, and the remaining one
à (3 x 2 x 1 = 6), there are different 6 routes to chose from
Ø Thus, the number of possible routes is a factorial of the number of locations to visit
Factorial of 10 è 3,628,800
Factorial of 20 è 2,432,902,008,176,640,000
quintillion, quadrillion, trillion, billion, million, thousand
v Solving this problem using modern computers is impractical, and for bigger problems, it
may be even impossible.
v The nearest neighbor algorithm and swarm optimization are usually used to quickly
find a suitable and good-enough solution to the TSP.
v If a route contains a single location visited more than once, then it is not a valid route to
be considered
v The metrics for this kind of problem will be the computation time total distance.
v To ensure that, the GA requires special types of mutation and crossover methods to
be applied.
v The mutation method should only be capable of shuffling the route and ever add or
remove a location from the route, otherwise, it would risk creating an invalid solution.
v The fitness function is to minimize the total distance while visiting all 20 cities.
v With swap mutation, two locations in the route are selected at random then their
positions are simply swapped (changing the order in which they will be visited).
1 2 3 4 5 6 7 8 9
1 2 8 4 5 6 7 3 9
v Here, the position of location 3 and 8 were switched to create a new list with exact same
values/candidates, just with a different order.
v Swap mutation is only swapping pre-existing values; thus, it will never create a list that
has missing, or duplicate values as compared to the originals
Parent 2 9 8 7 6 5 4 3 2 1
Note: ignore genes (6, 7, 8) because these are already selected (visited) from parent 1.
Offspring 9 5 4 3 2 6 7 8 1
v Implementing this correctly will end up finding a route that contains all the positions which
the parents have with no missing or duplicated position (city).
EECIE20-S4305 Intelligence Systems & Software Engineering: Genetic Algorithms Slide 24
Traveling Salesman Problem (TSP)
v Initial distance: 1996
v Final distance: 940 , where the fitness function is to minimize the distance
v An optimal solution was found in 100 generations
v Rank for cities to visit is: {(60, 200), (20, 160), (40, 120), (60, 80), (20, 40), (20, 20), (60, 20),
(100, 40), (160, 20), (200, 40), (180, 60), (120, 80), (140, 140), (180, 100), (200, 160), (180, 200),
(140, 180), (100, 120), (100, 160), (80, 180)}
Start here
Stop
here
an optimal
solution
was found in
100
generations