83% found this document useful (6 votes)
6K views

Genetic Algorithm Assignment

1. The document provides information about genetic algorithms and fitness functions. It gives the chromosomes and fitness values for 4 initial individuals. 2. The individuals are ordered from most fit to least fit. One-point and two-point crossovers are performed on the most fit individuals. 3. A second question provides fitness values for 5 chromosomes and constructs a roulette wheel for selection. 4. A third question performs iterations of a genetic algorithm, calculating fitness values, performing crossovers and mutations, and updating the roulette wheel.

Uploaded by

Aprna Tripathi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
83% found this document useful (6 votes)
6K views

Genetic Algorithm Assignment

1. The document provides information about genetic algorithms and fitness functions. It gives the chromosomes and fitness values for 4 initial individuals. 2. The individuals are ordered from most fit to least fit. One-point and two-point crossovers are performed on the most fit individuals. 3. A second question provides fitness values for 5 chromosomes and constructs a roulette wheel for selection. 4. A third question performs iterations of a genetic algorithm, calculating fitness values, performing crossovers and mutations, and updating the roulette wheel.

Uploaded by

Aprna Tripathi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Assignment - I

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).

Fitness value for Individuals

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

Construct the “roulette wheel” for selection of parents for crossover.

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

Roulette- wheel marked for five individual according to fitness


Q-3 Giver a scenario suppose that the size of the chromosome population N is 6,
and the fitness function is 15x-x2, and the binary code for the strings is 1100,
0100, 0001, 1110,0111,1001 here x in the fitness function is the decimal value
of the given string. Perform the following:
a) Find the fitness values of the strings and draw the Roulette wheel.
b) After performing (a) do the first iteration in which crossover is done between 6th
string and 2nd string at 3rd position. Onwards
c) In second iteration perform mutation in 1st and 5th string at 2nd and 3rd position.
d) Now draw the roulette wheel and tell the improvement fitness percentage.
Given that Population size N= 6 and
Decimal values of
Sl. No. Individual
individual
x1 1100 12
x2 0100 4
x3 0001 1
x4 1110 14
x5 0111 7
x6 1001 9
Step 1: computation of fitness value
F(x) = 15x-x2

F(x1) = 15*12 - 12*12


=180 - 144
=36
F(x2) = 15*4 - 4*4
= 60 – 16
= 44
F(x3) = 15*1 – 1 *1
= 15 -1
= 14
F(x4) = 15* 14 – 14 * 14
= 14 (15 -14)
= 14
F(x5) = 15*7 – 7*7
= 105 – 49
= 56
F(x6) = 15*9 – 9*9
= 135 – 81
= 54
Decimal values of Fitness Value
Sl. No. Individual
individual
x1 1100 12 36
x2 0100 4 44
x3 0001 1 14
x4 1110 14 14
x5 0111 7 56
x6 1001 9 54
Step 2: draw the Roulette Wheel
Individuals are arranged based on their fitness value, first is the maximum fittest and
last is least fit. To draw the roulette wheel first we have to compute the probability from

Decimal values Fitness Value Pi


Sl. No. Individual
of individual
x1 1100 12 36 0.165
x2 0100 4 44 0.201
x3 0001 1 14 0.064
x4 1110 14 14 0.064
x5 0111 7 56 0.256
x6 1001 9 54 0.247

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

After performing single point crossover at 3rd position offspring’s will be

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

No. Decimal values


Sl. No. Individual
of individual
1 O1 1111 15
2 O2 1000 8
3 O3 0101 5
4 O4 0011 3
5 X5’ 0001 1
6 X6’ 1111 15

F(O1) = 15*15 – 15*15


=225-225
=0
F(O2) = 15*8 - 8*8
= 120 – 64
= 56
F(O3) = 15*5 – 5 *5
= 75 -25
= 50
F(O4) = 15*3 – 3*3
= 45 – 9
= 36
F(X5’) = 15*1 – 1 *1
= 15 -1
= 14
F(x6’) = 15*15 – 15*15
= 225 – 225
=0
Decimal values of
Sl. No. Individual Fitness Value
individual
O1 1111 15 0
O2 1000 8 56
O3 0101 5 50
O4 0011 3 36
X5’ 0001 1 14
X6’ 1111 15 0
Step 2: draw the Roulette Wheel
To draw the roulette wheel first we have to compute the probability from

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

Sl. No. Individual


x1 10100000
x2 10101100
x3 10101110
x4 10101010
x5 11101010
x6 11001010
x7 00111010
x8 10111010

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

Sl. No. Individual Fitness value


x1 10100000 4
x2 10101100 15
x3 10101110 22
x4 10101010 16
x5 11101010 18
x6 11001010 15
x7 00111010 19
x8 10111010 20

(a) Draw the roulette wheel


To draw the roulette wheel first we have to compute the probability from

Sl. No. Individual Fitness value pi Pi


(cumulative
pi)
x1 10100000 4 0.031 0.031
x2 10101100 15 0.116 0.147
x3 10101110 22 0.17 0.317
x4 10101010 16 0.124 0.441
x5 11101010 18 0.139 0.58
x6 11001010 15 0.116 0.696
x7 00111010 19 0.147 0.843
x8 10111010 20 0.155 0.998

Roulette Wheel marked for eight individual according to fitness


(b) The given random numbers are: 0.42, 0.61, 0.81, 0.002 and 0.95

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

According to the roulette wheel method, individual j will be selected if


Pj-1 < r < Pj
Pi
Random Selected
Sl. No. Individual Fitness value pi (cumulative
number(r) Individual
pi)
x1 10100000 4 0.031 0.031 0.42 x4
x2 10101100 15 0.116 0.147 0.61 x6
x3 10101110 22 0.17 0.317 0.81 x7
x4 10101010 16 0.124 0.441 0.002 x1
x5 11101010 18 0.139 0.58 0.95 x8
x6 11001010 15 0.116 0.696
x7 00111010 19 0.147 0.843
x8 10111010 20 0.155 0.998

Mating pool will be 10101010, 11001010, 00111010, 10100000 and 10111010.

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.

Answer. A possible fitness function is n − x, where x is the number of unmatched 1s in


both halves of the string.
(ii) For the function above, no (the probability that the result of crossover of two almost
symmetric strings is almost symmetric is low).
For example: x1: 11111111 f(x1)= 8-0 = 8
x2: 11100111 f(x2)= 8-0 = 8
After performing single point crossover at fourth position
O1: 11110111 f(O1) = 8- 1 = 7
O2: 11101111 f(O2) = 8-1 = 7
While if we perform single point crossover at 3rd position then
O1: 11100111 f(O1) = 8- 0 = 8
O2: 11111111 f(O2) = 8-0 = 8
So, it is not necessary that offspring of parents with a high fitness value also have a
high fitness value.

Q6: Assume we have the following function


F(x)= x3 – 60*x2 +900*x+100
Where x is considered to 0..31. We wish to maximize f(x) (the optimal is x=10), using a
binary representation we can represent x using five binary digits. Given the four
chromosomes as 11100, 01111, 10111 and 00100, give the value of x and f(x)
a) If p3 and p2 are chosen as parents and we apply one point crossover show the
resulting children C1 and C2. Use a crossover of 1(where 0 is to the very left of
the chromosome),
b) Do the same using P4 and P2 with a crossover point of 2 and create C3 and C4.
c) Calculate the value of x and f(x) for C1.. C4.
d) Assume the initial population was x= 17, 21, 4 and 28. Using one point
crossover, what is the probability to finding the optimal solution? Explain your
reasons.

Chromosome String Value (x) Fitness value f(x)


P1 11100 28 212
P2 01111 15 3475
P3 10111 23 1227
P4 00100 4 2804

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

Chromosome String Value (x) Fitness value f(x)


C1 11111 31 131
C2 00111 7 3803
C3 00111 7 3803
C4 01100 12 3998

As given the optimal solution is at x=10 and four following strings


String Value (x)
10001 17
10101 21
00100 4
11100 28
Binary for x=10 is 01010
To achieve the x=10 we required 1 in second and 4th position, while in the given strings
no string have 1 at 4th position.
Thus, we can’t find the optimal solution by applying any of the crossovers.
So, mutation is only the solution to finding the optimal solution
On performing mutation on 00100 at position 2nd, 3rd and 4th position we can get 01010.

Q7: Given the following parents, P1 and P2 and the template T

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

Perform the following crossover


a) Uniform crossover
b) order-based crossover

Answer:

Since in the uniform crossover


When there is 1 in the template Gene, bit is copied from the first parent and
When there is 0 in the template Gene, bit is copied from the second parent
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 F C D B C I H D 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 E F G A I G

Order based crossover


In the order based crossover, Firstly we fill all the positions for bit 1 and bit is copied
from the first parent.
For the bit value 0, Bit is copied from the second parent from the starting position of
second parent and with the condition that bit value is not already present in the
offspring

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

You might also like