You are on page 1of 35

Week 5 - Genetic Algorithms.

Genetic Algorithms 1
Basic Genetic Algorithm.

Genetic Algorithms 2
GAs by John Holland.
Holland introduced
• a “population” of binary strings which he called “chromosomes”.
• The “population” evolves using kind of “natural selection” together with the
genetics-inspired operators of crossover, mutation, and inversion.
• Bits in a “chromosome” represent genes, and each “gene” is an instance of a
particular “allele”, 0 or 1.
• The selection operator chooses those chromosomes in the population that will
be allowed to reproduce, and on average the fitter chromosomes produce more
offspring than the less fit ones.
• Crossover exchange subparts of two chromosomes
• Mutation randomly changes the allele values of some locations in the
chromosome.
• Inversion reverses the order of a contiguous section of the chromosome
rearranging the genes order.

Genetic Algorithms 3
Basic Structure of a Genetic Algorithm.
1. Randomly generate initial population of n bit strings (“chromosomes”)
2. Evaluate the fitness of each string in the population

3. Repeat the following steps until next generation of n individual strings


produced
a. Select pair of parent chromosomes from current population
according to their fitness, i.e. chromosomes with higher fitness
are selected more often
b. Apply crossover (with probability)
c. Apply mutation (with probability of occurrence)
4. Apply generational replacement
5. Go to 2 or terminate if termination condition met

Genetic Algorithms 4
Basic Structure of a Genetic Algorithm.
1. Randomly generate initial population of n bit
strings (“chromosomes”)
2. Evaluate the fitness of each string in the
population
3. Repeat the following steps until next generation of
n individual strings produced
a. Select pair of parent chromosomes Each iteration in the cycle
from current population
according to their fitness, i.e.
produces a new “generation” of
chromosomes with higher fitness chromosomes.
are selected more often
The entire set of generations is
b. Apply crossover (with probability)
c. Apply mutation (with probability of
called a run.
Typical GA run is from 50 to
occurrence) 500 or more generations.
4. Apply generational replacement At the end of a run often there
5. Go to 2 or terminate if termination condition met
is at least one highly fit
chromosome in the population.
Genetic Algorithms 5
Example Implementation of a GA.
Let the length of the string
l=8, and the
number of chromosomes in the population (population size)
n = 4.
Fitness function
f(x) is equal to the number of ones in the bit string x.
Selection operator.
Fitness-proportionate selection, i.e. the number of times an
individual is expected to reproduce is equal to its fitness fi divided by the
average fitness of the population f
Ni = f i / f
Crossover probability pc = 0.7
Mutation probability pm = 0.001
Make a run of three generations.
Genetic Algorithms 6
Example of a Genetic Algorithm.
1. The initial randomly generated population might look like
this:
Chromosome Chromosome
Generation 0

index string
1 00000110
2 11101110
3 00100000
n= 4 00110010
l=8

Genetic Algorithms 7
Example of a Genetic Algorithm.
2. Evaluate the fitness of each string in the population
Chromosome Chromosome Fitness = number of ones
index string in the string
Generation 0

1 00000110 f1 = 2
2 11101110 f2 = 6
3 00100000 f3 = 1
n= 4 00110010 f4 = 3

Genetic Algorithms 8
Example of a Genetic Algorithm.
3a. Select pair of parent chromosomes from current population
according to their fitness
Chromosome Chromosome Fitness Relative fitness Probability
Index
Generation 0

1 00000110 f1 = 2 2/12 0.167 0.167


2 11101110 f2 = 6 6/12 0.5 0.0167+0.5=0.667
3 00100000 f3 = 1 1/12  0.083 0.0167+0.5+0.083=0.75

n= 4 00110010 f4 = 3 3/12 0.25
0.0167+0.5+0.083+0.25=1

Total fitness f = (2+6+1+3)=12

Genetic Algorithms 9
Example of a Genetic Algorithm.
3a. Select pair of parent chromosomes from current population
according to their fitness with Roulette Wheel
Chromosome Chromosome Fitness Relative fitness Probability
Index
1 00000110 f1 = 2 2/12 0,167 0,167
2 11101110 f2 = 6 6/12 0,5 0,167+0,5=0,667
3 00100000 f3 = 1 1/12 0,083 0,167+0,5+0,083=0,75
n= 4 00110010 f4 = 3 3/12 0,25
0,167+0,5+0,083+0,25=1

Total fitness f = (2+6+1+3)=12


Use a generator producing random numbers in the
range between 0 and 1 as much as population

10
Genetic Algorithms
Example of a Genetic Algorithm.
r1= 0,7606851416004102
r2 = 0,2395672500297792
r3 = 0,5487477212906403
r4 = 0,8842126807095168

0,167 0,667 0,75 1

11
• Crossover:
Cross-Over (Cross-Marriage) is done by determining the 2
initial and final positions of genes in individuals to be bred
at random. Then exchange the values of parent 1 and
parent 2 genes from the initial position to the final position
to obtain child 1 and child 2.

Genetic Algorithms 12
Example of a Genetic Algorithm.
3b. So from selected parent Apply crossover (with probability)
pc = 0.7

Pairs of “parents” for crossover:


1st pair: strings 4 and 2 2nd pair: strings 2 and 4
index chromosome index chromosome
4 00110010 2 11101110
2 11101110 4 00110010

Genetic Algorithms 13
Example of a Genetic Algorithm.
3b. Apply crossover (with probability) ex: pc = 0.7
Use a generator producing random numbers in the range between 0 and 1
to get a random number r for each pair of parental chromosomes.
If the random number r produced by the generator for the pair of “parents” is
less or equal to the crossover probability pc, then apply crossover to the
“parents” at randomly chosen locus, otherwise the parents do not crossover.
If a pair of parents do not undergo crossover, their offspring are their
identical copies.
Pairs of “parents” for crossover:
Ex: r = 0.4 < pc => Apply crossover r = 0.8 > pc => No crossover

1st pair: strings 4 and 2 2nd pair: strings 2 and 4


index chromosome index chromosome
4 00110010 2 11101110
2 11101110 4 00110010
Genetic Algorithms 14
Example of a Genetic Algorithm.
3b. Apply crossover (with probability) pc = 0.7
Pairs of “parents” for crossover:
r = 0.4 < pc => Apply crossover r = 0.8 > pc => No crossover

1st pair: strings 4 and 2 2nd pair: strings 2 and 4


index chromosome index chromosome
4 00 110010 2 11101110
old

2 11 101110 4 00110010
Ex: with point crossover 2


new

5 00 101110 2 11101110
6 11 110010 4 00110010

Genetic Algorithms 15
Example of a Genetic Algorithm.
3c. Apply mutation (with probability of occurrence) pm = 0.001
Use a generator producing random numbers in the range between 0 and 1
to get a random number r for each new chromosome.
If the random number r is less or equal to the mutation probability pm,
apply mutation to the chromosome, i.e. flip a bit at randomly chosen locus.
Mutation:
1st pair: 2nd pair:
index chromosome index chromosome
r = 0.4 > pm => No mutation r = 0.8 > pm => No mutation
5 00101110 2 11101110
r = 0.1 > pm => No mutation r = 0.0005 < pm => Mutation
old
6 11110010 4 00110010

new
7 00110110
Genetic Algorithms 16
Example of a Genetic Algorithm.
3c. Apply mutation (with probability of occurrence) pm = 0.001
Mutation:
1st pair: 2nd pair:
index chromosome index chromosome
5 00101110 2 11101110
old

6 11110010 4 00110010
 
5 00101110 2 11101110
new

6 11110010 7 00110110

Genetic Algorithms 17
Example of a Genetic Algorithm.
4. Apply generational replacement
Replacement
1st pair: 2nd pair:
index chromosome index chromosome
Parents

4 00110010 2 11101110
2 11101110 4 00110010
 
5 00101110 2 11101110
Offspring

6 11110010 7 00110110

Genetic Algorithms 18
Example of a Genetic Algorithm.
4. Apply generational Replacement
Chromosome Chromosome Fitness = number of ones
Generation 0

index string in the string


1 00000110 f1 = 2
2 11101110 f2 = 6
3 00100000 f3 = 1
n= 4 00110010 f4 = 3


Generation 1

2 11101110
5 00101110
6 11110010
7 00110110

Genetic Algorithms 19
Example of a Genetic Algorithm.
4. Apply generational Replacement
Chromosome Chromosome Fitness
Generation 0

index string
1 00000110 f1 = 2
2 11101110 f2 = 6
3 00100000 f3 = 1
n= 4 00110010 f4 = 3


Generation 1

2 11101110
5 00101110 Run for the second generation
6 11110010 
7 00110110

Genetic Algorithms 20
Example of a Genetic Algorithm.
2. Evaluate the fitness of each string in the population
Chromosome Chromosome Fitness = number of ones
Generation 3

index string in the string


8 11100110 f8 = 5
12 11011110 f12 = 6
13 00101110 f13 = 4
14 01111110 f14 = 6
f Average fitness f = (5+6+4+6)/4 = 5.25

The average fitness in the


population of possible solutions
increases with every generation.

0 1 2 3 Generation №
Genetic Algorithms 21
Example of a Genetic Algorithm.
• The reference word is "GENETICS".
• Each individual states the word conjecture against the
reference word.
• Each individual consists of 8 genes.
• The value of the gene is expressed as an integer number
between 1 to 26 which if converted will get the character.
• 1 is converted to the letter 'A', and so on until 26 is
converted to the letter 'Z'.

Genetic Algorithms 22
Example of a Genetic Algorithm.
• Fungsi Fitness:
The fitness function is expressed as:
8 * 26 - (ac[i] – gen[i])

• Generate Initial Population


Generating a number of individuals, for example a population
consisting of 16 individuals, then generated 16 individuals with 8
integer genes (1 ≤ gen ≤ 26) which were generated randomly.

Genetic Algorithms 23
Example of a Genetic Algorithm.
• Generate Initial Population

Genetic Algorithms 24
Example of a Genetic Algorithm.
• The selection of the roulette wheel to select the parent is done by using
the fitness percentage of each individual, where each individual gets
the area of the section according to the percentage of his fitness value.

Genetic Algorithms 25
Example of a Genetic Algorithm.
Cross-Over (Cross-Marriage) is done by
determining the 2 initial and final
positions of genes in individuals to be bred
at random. Then exchange the values of
parent 1 and parent 2 genes from the initial
position to the final position to obtain
child 1 and child 2.

Genetic Algorithms 26
Example of a Genetic Algorithm.

Mutation is done by shifting the


position of the mutated gene. This
shift is done by adding or
subtracting the position.

Genetic Algorithms 27
Example of a Genetic Algorithm.
• Get New Individual
offspring resulting from cross-breeding and mutations become a
new generation for the regeneration process
In the next generation, the best individual (the greatest
fitness value) can be maintained by a process of elitism

Genetic Algorithms 28
Example of a Genetic Algorithm.

• Example of Genetic Algorithm Results : 1st Generation

Genetic Algorithms 29
Example of a Genetic Algorithm.

• 2nd Generation

Genetic Algorithms 30
Example of a Genetic Algorithm.
• 3rd generation

Genetic Algorithms 31
Example of a Genetic Algorithm.
• 4th Generation

Genetic Algorithms 32
Example of a Genetic Algorithm.
• 5th Generation

Genetic Algorithms 33
Exercise
1. For the maximization problem (finding the maximum value) of a
function as follows
max, y = f(x) = (-x2) + 14x - 13, 0 ≤ x ≤ 15
Complete the following table:
chromosome x y=f(x) fitness
P1 [0011]
P2 [0100]
P3 [1001]
P4 [0101]

To calculate x or encoding x = convert binary to decimal


(without dividing by 255) so that the value of x is
between 0 to 15.

Genetic Algorithms 34
Exercise
2. Make a selection with the roulette wheel then the selected individual is
taken based on the results of generating random numbers 0-1: 0.38, 0.12,
0.67, 0.55.
3. Continue the crossover process, pc = 0.7, by a one-point crossover at
the 2nd point and only ex. Random number for 1st pair = 0.38 and
random number for 2nd pair = 0.8
4. Furthermore, the results of the crossover carry out the mutation process
by changing the 0 gene to 1 or 1 to 0 in point 3.
If Probability mutation or pm = 0.0003. random number for 1st
individual = 0.3, 2nd individual = 0.00001, 3rd individual = 0.05, 4th
individual = 0.000002
5. Recalculate the x and fitness values from the mutation results then sort
the x values from the most optimal to the less optimal.

Genetic Algorithms 35

You might also like