Genetic algorithm
example
Consider the function of maximizing the function f(x) = x²
where x in permitted to vary between 0 to 31
Select encoding technique:
Using a five-bit binary integer, numbers between 0 (00000) and 31 (11111) can be
obtained
● Select Initial Population
● To start with, select initial population are random.
● Here initial population of size 4 is chosen, but any number of
populations can be selected based on the requirement and
application.
String o Initial X value Fitness prob %prob Expected Actual
popula count count
tion f(x)=x^2
1 01100 12 144
m 2 11001 25 625
e 3 00101 5 25
um 4 10011 19 361
sum
average
maximum
String o Initial X value Fitness prob %prob Expecte Actual
populatio d count count
n f(x)=x^2
1 01100 12 144
2 11001 25 625
3 00101 5 25
4 10011 19 361
sum 1155
average 288.75
maximu 625
m
String o Initial X value Fitness prob %prob Expe
populatio d cou
We need to maximize the n f(x)=x^2
value 625 which we got from
initial population 1 01100 12 144
We need to select some of 2 11001 25 625
these particular solutions and 3 00101 5 25
then we need to apply
remaining steps crossover 4 10011 19 361
and mutation sum 1155
For selection we need to average 288.75
calculate the probability of
these chromosomes maximu 625
m
Calculate the fitness value
To calculate the probability use following formula
For 12 prob=144/1155=0.1247
For 25 prob=625/1125=0.5411
String o Initial X value Fitness prob %prob Expecte Ac
populatio d count co
Then calculate sum, n f(x)=x^2
average, 1 01100 12 144 0.1247 12.47
maximum 2 11001 25 625 0.5411 54.11
3 00101 5 25 0.0216 02.16
4 10011 19 361 0.3126 31.26
sum 1155 1.0 100
average 288.75 0.25 25
maximu 625 0.5411 54.11
m
Calculate the expected count
Formula to calculate expected count
For 12 exp count=144/288.75=0.4987
String o Initial X value Fitness prob %prob Expecte Actual
populatio d count count
n f(x)=x^2
1 01100 12 144 0.1247 12.47 0.4987 1
2 11001 25 625 0.5411 54.11 2.1645 2
3 00101 5 25 0.0216 02.16 0.0865 0
4 10011 19 361 0.3126 31.26 1.250 1
sum 1155 1.0 100 4 4
average 288.75 0.25 25 1 1
maximu 625 0.5411 54.11 2.1645 2
m
Actual count indicates no of times chromosome will be selected,
5 will not be selected for next step
String o Mating Crossov Offsprin x-value Fitness Mutatio x-value Fitness
pool er point g after f(x)=x^2 n f(x)=x^2
crossov
er
1 01100 4 01101 13 169 11101 29 841
2 11001 4 11000 24 576 11000 24 576
3 11001 2 11011 27 729 11011 27 729
4 10011 2 10001 17 289 10100 20 400
sum 1763 2546
averag 440.75 636.5
e
maximu 729 841
m