Genetic Algorithm Assignment
Genetic Algorithm Assignment
Q1. Suppose a Genetic Algorithm uses chromosomes of the form x=abcdefgh with a
fixed length of eight genes. Each gene can be any digit between 0 and 9 . Let the fitness
of individual x be calculated as:
f(x) =(a+b)-(c+d)+(e+f)- ( g+h)
And let the initial population consist of four individuals x1, ..., x4 with the following
chromosomes :
X1 = 6 5 4 1 3 5 3 2
X2 = 8 7 1 2 6 6 0 1
X3 = 2 3 9 2 1 2 8 5
X4= 4 1 8 5 2 0 9 4
Evaluate the fitness of each individual, showing all your workings, and arrange them in
order with the fittest first and the least fit last.
For above problem perform the following crossover operations:
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 f).
F(x1) = (6+5)-(4+1)+(3+5)-(3+2)
= 11-5+8-5
=9
F(x2) = (8+7)-(1+2)+(6+6)-(0+1)
= 15-3+12-1
=23
F(x3) = (2+3)-(9+2)+(1+2)-(8+5)
= 5-11+3-13
=-16
F(x4) = (4+1)-(8+5)+(2+0)-(9+4)
= 5-13+2-13
=-19
Order with the fittest first and the least fit last.
X2, X1, X3, X4
Cross the fittest two individuals using one–point crossover at the middle point.
Crossover will be performed between X2 and X1
Cross the second and third fittest individuals using a two–point crossover (points b and
f).
Second and third fittest individuals are: X1 and X3
Q2. Suppose the 5 chromosomes at a given generation have fitness values listed below.
f(x1) = 55
f(x2) = 24
f(x3) = 8
f(x4) = 19
f(x5) = 42
Individual f(X)
X1 55 0.37
X2 24 0.17
X3 8 0.054
X4 19 0.12
X5 42 0.28
Sum of fi 148
Step 3: After performing (a) do the first iteration in which crossover is done between
6th string and 2nd string at 3rd position onwards
Strings according to their fitness values
No. Decimal values Fitness Value Pi
Sl. No. Individual
of individual
1 x5 0111 7 56 0.256
2 x6 1001 9 54 0.247
3 x2 0100 4 44 0.201
4 x1 1100 12 36 0.165
5 x3 0001 1 14 0.064
6 x4 1110 14 14 0.064
Iteration -1: Crossover between 6th string and 2nd string at 3rd position onwards
In the given problem, single point crossover will be performed between x4 and x6 i.e.
1110 and 0100
X1: 1 1 1 1
X2: 1 0 0 0
Crossover between 1st string and 5th string at 2nd position onwards
In the given problem, single point crossover will be performed between x5 and x3 i.e.
0111 and 0001
O3:
0 1 0 1
O4: 0 0 1 1
Iteration 2: Mutation in 1st and 2nd string at 2nd and 3rd position.
First string x5, i.e. 0111
Fifth string x6, i.e. 1001
After first and second iteration, we have following individuals
Decimal values
Sl. No. Individual Fitness Value Pi
of individual
O1 1111 15 0 0.0
O2 1000 8 56 0.358
O3 0101 5 50 0.320
O4 0011 3 36 0.230
X5’ 0001 1 14 0.09
X6’ 1111 15 0 0
In first generation the maximum fitness is 26% while in second generation it is 36%
There is a improvement of 10%
Q4: Consider the scenario suppose the size of the chromosome is 8. The fitness
function is the sum of positions of 1 in the chromosome. Position of most significant bit
is considered as 1 and least significant bit position is 8.
For Example in a chromosome 11000110 the fitness function will be 1+2+6+7= 16.
Binary coded strings are 10100000, 10101100, 10101110, 10101010, 11101010,
11001010, 00111010 and 10111010.
a) Find the fitness value and draw the roulette wheel.
b) Generate the mating pool of size five if random numbers to select the individual
from the population are 0.42, 0.61, 0.81, 0.002 and 0.95 to
For Individual 1
1 2 3 4 5 6 7 8
1 0 1 0 0 0 0 0
F(x1) = 1+3 = 4
For Individual 2
1 2 3 4 5 6 7 8
1 0 1 0 1 1 0 0
F(x2) = 1+3+5+6 = 15
For Individual 3
1 2 3 4 5 6 7 8
1 0 1 0 1 1 1 0
F(x3) = 1+3+5+6+7 = 22
For Individual 4
1 2 3 4 5 6 7 8
1 0 1 0 1 0 1 0
F(x4) = 1+3+5+7 = 16
For Individual 5
1 2 3 4 5 6 7 8
1 1 1 0 1 0 1 0
F(x5) = 1+2+3+5+7 = 18
For Individual 6
1 2 3 4 5 6 7 8
1 1 0 0 1 0 1 0
F(x6) = 1+2+5+7 = 15
For Individual 7
1 2 3 4 5 6 7 8
0 0 1 1 1 0 1 0
F(x7) = 3+4+5+7 = 19
For Individual 8
1 2 3 4 5 6 7 8
1 0 1 1 1 0 1 0
F(x8) = 1+3+4+5+7 = 20
Pi
Random
Sl. No. Individual Fitness value pi (cumulative
number(r)
pi)
x1 10100000 4 0.031 0.031 0.42
x2 10101100 15 0.116 0.147 0.61
x3 10101110 22 0.17 0.317 0.81
x4 10101010 16 0.124 0.441 0.002
x5 11101010 18 0.139 0.58 0.95
x6 11001010 15 0.116 0.696
x7 00111010 19 0.147 0.843
x8 10111010 20 0.155 0.998
Q5: Suppose the problem is to evolve a binary string of length n which is symmetric.
If the string positions are numbered from 0, then a symmetric string will have a 1 in
position i if and only if there is a 1 in position (n − 1) − i. For example, 001100 is
symmetric since it has a 1 at index 2 and a 1 at index (6 − 1) − 2 = 3. Similarly, 110011
is symmetric, and 011011 is not. The initial population is a randomly generated set of
binary strings of length n, where n is an even number.
(i) Give a suitable fitness function for this problem.
(ii) Will the offspring of parents with a high fitness value generally also have a high
fitness value, given your fitness function? Explain your answer.
To find the 0 on the very left of the chromosome, single point crossover is performed at
position 1. After crossover
1 1 1 1 1
C1:
C2: 0 0 1 1 1
0 0 1 1 1
C3:
C4: 0 1 1 0 0
P1 A B C D E F G H I J
J E F J H B C I A D G
T 1 0 1 1 0 0 0 1 0 1
Answer:
P1 E F J H B C I A D G
P2 A B C D E F G H I J
T 1 0 1 1 0 0 0 1 0 1
C2 E B J H E F G A I G
P1 A B C D E F G H I J
P2 E F J H B C I A D G
T 1 0 1 1 0 0 0 1 0 1
C1 A E C D F B I H G J
P1 E F J H B C I A D G
P2 A B C D E F G H I J
T 1 0 1 1 0 0 0 1 0 1
C2 E B J H C D F A I G