Professional Documents
Culture Documents
GENETIC ALGORITHM
Intro to GA
https://www.youtube.com/watch?v=vg1FTkWHNSg
https://www.youtube.com/watch?v=cxweR4i0ejA
https://www.youtube.com/watch?v=XcinBPhgT7M
https://www.youtube.com/watch?v=uQj5UNhCPuo
&t=9s
Application of GA
Upload a video for any application using Genetic
Algorithm.
Upload in Padlet page given before end of class
today.
https://padlet.com/normaniha_ghani/z2kyp89s839uz
4lm
History
Introduced in the 1960s by I. Rechenberg in his
work "Evolution strategies" (Evolutions strategie in
original).
Genetic Algorithms (GAs) were invented by
John Holland and developed by him and his
students and colleagues. → Holland's book
"Adaption in Natural and Artificial Systems"
published in 1975.
History (2)
In 1992 John Koza has used genetic algorithm to
evolve programs to perform certain tasks. →
"genetic programming" (GP).
LISP programs were used.
loop
read in an expression from the console;
evaluate the expression;
print the result of evaluation to the console;
end loop.
For example, if you want to compute the value of (2 * cos(0) * (4 + 6)), you type in:
Search techniques
Direct methods Indirect methods Evolutionary algorithms Simulated annealing Dynamic programming
Parallel Sequential
children
reproduction modification
modified
parents children
population evaluation
evaluated children
deleted
members
discard
Population
population
parents
population
Mutation
Accepting
Crossover
Crossover selects genes from parent chromosomes
and creates a new offspring. The simplest way how
to do this is to choose randomly some crossover
point and everything before this point copy from a
first parent and then everything after a crossover
point copy from the second parent.
Crossover can then look like this ( | is the crossover
point): • 16 genes of chromosome
• One-point Crossover
operation at point 5
Mutation
Mutation is to prevent falling all solutions in
population into a local optimum of solved problem.
Mutation changes randomly the new offspring. For
binary encoding we can switch a few randomly
chosen bits from 1 to 0 or from 0 to 1. Mutation can
then be following:
Evaluation
modified
evaluated children
children
evaluation
discard
Generational GA:
entire populations replaced with each iteration
Steady-state GA:
a few members replaced each generation
GA Example
GA Example
Evaluate the fitness of each individual. Show all
your calculations and arrange them in order with
the highest value of fitness first and the least value
of fitness last.
Population Gene
X1 (6+5) - (4*3) = -1
X2 (1+5) - (7*3) = -15
X3 (2+9) – (0*1) = 11
X4 (1+5) – (3*8) = -18
GA Example (3)
New offsprings (updated population)
Population Gene
(2+9) - (4*3) = -1
(6+5) - (0*1) = 11
(6+5) – (7*3) = -10
(1+5) – (4*3) = -7
x y
GA – Case Study
We also choose the size of the chromosome population, for
instance 6, and randomly generate an initial population.
The next step is to calculate the fitness of each chromosome. This is
done in two stages.
First, a chromosome, that is a string of 16 bits, is partitioned into
two 8-bit strings:
1 0 0 0 1 0 1 0 and 0 0 1 1 1 0 1 1
Then these strings are converted from binary (base 2) to decimal
(base 10):
(10001010) 2 = 1 27 + 0 26 + 0 2 5 + 0 2 4 + 1 23 + 0 2 2 + 1 21 + 0 2 0 = (138)10
and
(00111011) 2 = 0 2 7 + 0 2 6 + 1 25 + 1 2 4 + 1 2 3 + 0 2 2 + 1 21 + 1 2 0 = (59)10
GA – Case Study
Case Study 2: Finding Maximum
Value of a Function
Finding Maximum Values of A
Function
1 0 1 1 1 1
X Y
Finding Maximum Values of A
Function (ctd)
a) Find the fitness value of each population. Arrange
from the maximum value to the minimum value.
b) For the 1st generation do the following
(i) Cross the fittest two individuals using one-point
crossover at the middle point.
(ii) Cross the second and third fittest individuals using a
two-point crossover (point’s b and d).
(iii) Find the fitness value of new population and evaluate
the performance after the 1st generation
Answer
Population
8.5
6.85
5.22
15.45
Answer
a) For the 1st generation do the following
(i) Cross the fittest two individuals using one-point crossover at the middle point.
(ii) Cross the second and third fittest individuals using a two-point crossover
(point’s b and d).
Answer
(iii) Find the fitness value of new population and evaluate
the performance after the 1st generation
Population Population
10.15 8.5
8.5 6.85
8.5 5.22
10.37 15.45
After 1st generation Initial Population
GA – Case Study
◼ Using decoded values of x and y as inputs in the mathematical
function, the GA calculates the fitness of each chromosome.
◼ To find the maximum of the “peak” function, we will use crossover
with the probability equal to 0.7 and mutation with the
probability equal to 0.001.
◼ As we mentioned earlier, a common practice in GAs is to specify
the number of generations.
◼ Suppose the desired number of generations is 100. That is, the
GA will create 100 generations of 6 chromosomes before
stopping.
Chromosome locations on the surface of the
“peak” function: initial population
Chromosome locations on the surface of the
“peak” function: first generation
Chromosome locations on the surface of the
“peak” function: local maximum
Chromosome locations on the surface of the
“peak” function: local maximum
Performance graphs for 100 generations of 6
chromosomes: local maximum
pc = 0.7, pm = 0.001
0. 7
0. 6
0. 5
0. 4
Fitness
0. 3
0. 2
0. 1
Best
0 Average
-0.1
0 10 20 30 40 50 60 70 80 90 100
Generations
Performance graphs for 100 generations of 6
chromosomes: global maximum
pc = 0.7, pm = 0.01
1.8
1.6
1.4
1.2
Fitness
1.0
0.8
0.6
Best
0.4 Average
0.2
0 10 20 30 40 50 60 70 80 90 100
Generations
Performance graphs for 20 generations of
60 chromosomes
pc = 0.7, pm = 0.001
1.8
1.6
1.4
1. 2
Fitness
1.0
0.8
0.6
Best
0.4 Average
0.2
0 2 46 8 10 12 14 16 18 20
Generations
Global and Local Maxima
Example Examination Question1
Type Chromosome
1 0001
The ideal chromosome
2 0010
(High speed and medium
3 0111 pressure) is 1101.
4
4 1011
1011
W11
Lk
j=1 k=1
Layer i Hidden
Layer k
Layer j
Population Chromosome
1 0 0 0 1 0 1 1 0 1 0 0
1 1 0 0 0 1 0 1 1 1 1 1
1 1 1 1 1 0 1 1 0 0 0 1
1 0 1 0 1 0 1 1 0 0 1 1
The weight between all layers has range between -0.5 and 0.5. The initial
population is listed in Table 2.
(i) Calculate the output value of 𝐿𝑘 for all population using the FIRST (1st) data
pattern. (NOTE : Sigmoid function is represented as f(x) = (1+e-x)-1)
(ii) Calculate the error for each population. Arrange the population from the
minimum error (NOTE : called the fittest individual) to the maximum error
(NOTE : called the less fit individual).
(iii) For the 1st generation do the following :
a) Cross the fittest two individuals using one-point crossover at the middle
point.
b) Cross the less fit individuals using a two-point crossover (point ‘d’ and
‘h’).
c) Mutate the FIFTH (5th) gen of the less fit individual.
(iv) Find the error of the new population obtained in (iii) and evaluate the
performance after the FIRST (1st ) generation.
END OF CHAPTER 5
PREPARE FOR :
Quiz 4
&
FINAL EXAM
Conclusions
Search Techniqes
Source: http://www.engineering.lancs.ac.uk
GA Components With Example
58
❑ Encoding
❑ An individual is encoded (naturally) as a string of l
binary digits
➢ Let’s say L = 10. Then, 1 = 0000000001 (10 bits)
Algorithm
60
❑ aka Crossover
❑ For each couple we decide according to
crossover probability (for instance 0.6) whether
to actually perform crossover or not
❑ Suppose that we decide to actually perform
crossover only for couples (s1`, s2`) and (s5`,
s6`).
❑ For each couple, we randomly extract a
crossover point, for instance 2 for the first and
5 for the second
Recombination (2)
69
Algorithm
70
mutate individuals
evaluate the fitness of the modified individuals
generate a new population
End while
Mutation (1)
71
37
Example (End)
75
f(x) = 15 x x2
The fitness function and chromosome locations
Chromosome Chromosome Decoded Chromosome Fitness
label string integer fitness ratio, %
X1 1 1 00 12 36 16.5
X2 0 1 00 4 44 20.2
X3 0 0 01 1 14 6.4
X4 1 1 10 14 14 6.4
X5 0 1 11 7 56 25.7
X6 1 0 01 9 54 24.8
60 60
f(x)
50 50
40 40
30 30
20 20
10 10
0 0
0 5 10 15 0 5 10 15
x x
(a) Chromosome initial locations. (b) Chromosome final locations.
◼ In natural selection, only the fittest species can survive,
breed, and thereby pass their genes on to the next generation.
GAs use a similar approach, but unlike nature, the size of the
chromosome population remains unchanged from one
generation to the next.
100 0
X1: 16.5%
X2: 20.2%
75.2 X3: 6.4%
X4: 6.4%
X5: 25.3%
36.7 X6: 24.8%
49.5 43.1
Crossover operator
◼ In our example, we have an initial population of 6
chromosomes. Thus, to establish the same population in the
next generation, the roulette wheel would be spun six
times.
X6i 1 0 00 1 0 1 00 00 X2i
X1i 0 11 00 00
1 0 11 11 11 X5i
X2i 0 1 0 0 0 1 1 1 X5i
Mutation operator
◼ Mutation represents a change in the gene.
◼ Mutation is a background operator. Its role is to
provide a guarantee that the search algorithm is not
trapped on a local optimum.
X2'i 0 1 0 1
0
X1'i 1
0 1 1 1 1 1 X1"i
X5'i 0 1 0
1 0
1
X2i 0 1 0 0 1 0 X2"i
X5i 0 1 1 1
The genetic algorithm cycle
C rossover
Generation i
X1i 1 1 0 0 f = 36 X6i 1 0 00 1 0 1 00 00 X2i
X2i 0 1 0 0 f = 44
X3i 0 0 0 1 f = 14
X4i 1 1 1 0 f = 14 X1i 10 11 00 00 0 11 11 11 X5i
X5i 0 1 1 1 f = 56
X6i 1 0 0 1 f = 54
X2i 0 1 0 0 0 1 1 1 X5i
Generation (i + 1)
X1i+1 1 0 0 0 f = 56 Mutation
X2i+1 0 1 0 1 f = 50 X6'i 1 0 0 0
X3i+1 1 0 1 1 f = 44
X2'i 0 1 0 10
X4i+1 0 1 0 0 f = 44
X1'i 10 1 1 1 1 1 X1"i
X5i+1 0 1 1 0 f = 54
X6i+1 0 1 1 1 f = 56 X5'i 0 1 01 01
X2i 0 1 0 0 1 0 X2"i
X5i 0 1 1 1
Remarks
The new subject has inherited the intended genome : his paws
have become flippers.
We can then see that the principle of genetic algorithms is
simple :
1. Encoding of the problem in a binary string.
2. Random generation of a population. This one includes a
genetic pool representing a group of possible solutions.
3. Reckoning of a fitness value for each subject. It will directly
depend on the distance to the optimum.
4. Selection of the subjects that will mate according to their
share in the population global fitness.
5. Genomes crossover and mutations.
6. And then start again from point 3.
End of
Today’s Class
Biological Background
Chromosome
All living organisms consist of cells.
In each cell there is the same set of chromosomes.
Chromosomes are strings of DNA and serves as a
model for the whole organism.
A chromosome consist of genes, blocks of DNA.
Each gene encodes a particular protein. Basically
can be said, that each gene encodes a trait, for
example color of eyes. Possible settings for a trait
(e.g. blue, brown) are called alleles.
Chromosome (2)
Each gene has its own position in the chromosome.
This position is called locus.
Complete set of genetic material (all chromosomes)
is called genome.
Particular set of genes in genome is
called genotype.
Reproduction
During reproduction, first
occurs recombination (or crossover).
Genes from parents form in some way the whole
new chromosome.
The new created offspring can then be mutated.
Mutation means, that the elements of DNA are a bit
changed. This changes are mainly caused by errors
in copying genes from parents.
The fitness of an organism is measured by success
of the organism in its life.
Basic genetic algorithms
Step 1: Represent the problem variable domain as
a chromosome of a fixed length, choose the size
of a chromosome population N, the crossover
probability pc and the mutation probability pm.