0% found this document useful (0 votes)
36 views67 pages

GA Examples 1-7 Updated (2018)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views67 pages

GA Examples 1-7 Updated (2018)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 67

Genetic Algorithm

Examples
Crossover operator

 Crossover operator is based on crossover probability.


 It depends on representation also.
 The recombination must produce valid chromosomes.
 Types of crossover
1. Single point crossover
2. Double point crossover
3. Uniform crossover
4. Arithmetic crossover etc….
Types of crossover(cont…)

Single point crossover

0 1 1 1 0 1 0 1 1 0

1 1 0 1 1 0 0 1 0 1

0 1 1 1 0 1 0 1 0 1

1 1 0 1 1 0 0 1 1 0
Types of crossover(cont…)

Double point crossover

0 1 1 1 0 1 0 1 1 0

1 1 0 1 1 0 0 1 0 1

0 1 1 1 1 0 0 1 1 0

1 1 0 1 0 1 0 1 0 1
Types of crossover(cont…)
Uniform crossover
 Assign 'heads' to one parent, 'tails' to the other
 Flip a coin for each gene of the first child
 Make an inverse copy of the gene for the second child
 Inheritance is independent of position
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Parents
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

00 1 0
0 0 1 0 1 1 0 00 0
0 1 0
0 1 1 0 0 1
Children
1 00 1 1 00 1 0 0 1 1 1 00 1 00 00 1 1 00
Reproduction(cont…)
Types of crossover

Arithmetic crossover
a b c d e f

A B C D E F

(a+A)/2 (b+B)/2 (c+C)/2 (d+D)/2 (e+E)/2 (f+F)/2


Mutation operator
Parent 10110101
Mutation chromosome 10001001
 Flipping Child 00111100
 Flipping of a bit involves changing 0 to 1 and 1 to 0 based on a
mutation chromosome generated.
 Interchanging
 Two random positions of the string are chosen and the bits Parent 10110101
corresponding to those positions are interchanged Child 11110001
 Reversing
 A random position is chosen and the bits next to that position are
reversed and child chromosome is produced Parent 10110101
Child 10110110
Mutation operator
Some imp points…
 Provided gene j needs to be mutated, we make a bit-flip
change for gene j, i.e., (1 to 0) or (0 to 1). This operator a
bit-flip mutation.
 The individual after mutation is called the mutant.
 Mutation should not occur very often, because then GA will
in fact change to random search.
 Mutation is important in the role of recovering the lost
information and randomly disturbing the genetic
information.
Examples of GA
Example 1
Max(x2+1) over {0,1,…….31}

GA Approach
 Representation : binary code eg: 01101 13
 Population size:4 x2+1
1200
 1-point crossover 1000
800
 Roulette wheel Selection 600
Random Optimization
400

200
0
0 5 10 15 20 25 30 35
Cond…

 Range – {0 to 31} -> {00000 to 11111} [ 11111 : 1*24 + 1*23 + 1*22 + 1*21 + 1*20 = 31 ]

 Therefore, chromosome length should be ‘5’


 Lets randomly generate ‘4’ strings as initial population. (pop-size given as 4)

x2+1
1. 01101 13 1200
1000
2. 11000 24 800

3. 01000 8 600
400
4. 10010 18 200
0
0 5 10 15 20 25 30 35
(X2+1) Example Selection

String no. Fitness Prob Expected Actual


Initial X
f(x)=(x2+1) Count Count
Population value fi f i
f i f avg
Roulette Wheel
1 01101 13 170 .15 .60 1
2 11000 24 577 .50 2.00 2 29%
15%

3 01000 8 65 .06 .24 0 6%

4 10010 18 325 .29 1.16 1


50%

Sum
f i 1137 1.00 1 2 3 4

Average f avg 284.25 .25


Maximum 577
Expected count is less than Average probability (crossover probability)
Best for now
0*24+1*23+1*22+0*21+1*20= 13 132+1 =170 170/1137 = .15 0.15/0.25 = 0.6
Cond…
Actual Mating
Initial
Count Pool
Population

01101 1 01101
11000 2 11000
01000 0 11000
10010 1 10010

• Next operation is cross over (Given : Single point cross over)

• Randomly select cross over point between 1-4 for string 1 & 2: 4

• Randomly select cross over point between 1-4 for string 3 & 4: 2

These can be shuffled


randomly
(X2+1) Example Crossover
String No. Mating Crossover Offspring X Value Fitness
Pool point After F(x)=x2+1
Crossover

1 01101 4 01100 12 145


2 11000 4 11001 25 626
2 11000 2 11010 26 677
4 10010 2 10000 16 257
Sum
1705
Average
426.25
Maximum
677 Best for now
Cond…

X Value Fitness
F(x)=x2+1

12 145
25 626 677

26 677
16 257

Solution improved from 577 to 677 after crossover.


Mutation
 Mutation probability : 1/5=0.2. (We can also consider :0.01, 0.1 etc.,)
 Generate random number for all 20 genes.

String No. Random numbers


1 0.6787 0.7577 0.7431 0.3922 0.6555

2 0.7712 0.7060 0.1318 0.2769 0.0462


3 0.5971 0.8235 0.6948 0.3171 0.9502
4 0.3644 0.4387 0.3816 0.7655 0.7952

 Genes in the above highlighted places are to be flipped.


(X2+1) Example Mutation
String No. Offspring Offspring X Fitness
After After Value
Crossover Mutation f(x)=X2+1
1 01100 01100 12 145
2 11001 11100 28 785 x2+1
1200
3 11010 11010 26 677
1000
4 10000 10000 16 257 800
600
Sum 1864 400
200
Average 466
0
0 5 10 15 20 25 30 35
Maximum 785

Best in this iteration (final)


Example 2
 Solving the following problem with GA

Maximize sin( x ),
Subject to, 0  x  .

In this problem 5 bit string is needed to represent the variable with one
decimal place of accuracy.
Assume the population size = 4
String (00000) represent the x= 0 solution and String (11111) represents the
x= π solution.
To get x from decoded value we use

x  xmin  (( xmax  xmin ) /(2l  1)) DV


Example-Contd..
For Initial Population
String DV x f(x)=sin(x) fi/favg AC Mating Pool
01001 9 0.912 0.791 1.39 1 01001
10100 20 2.027 0.898 1.58 2 10100
00001 1 0.101 0.101 0.18 0 10100
11010 26 2.635 0.485 0.85 1 11010
favg 0.569

01001  0  2 4  1 23  0  2 2  0  21  1 2 0  9
String value
l   0 
x  xmin  (( xmax  xmin ) / (2  1)) DV  0  9  5   0.912
 2  1
Function value DV Decoded value
f ( x)  sin(0.912)  0.791 AC Actual count
fi 0.791
  1.39
f avg 0.569
 The proportionate reproduction scheme assigns a number of
copies according to a string’s fitness .
 The expected number of copies for each string is calculated.
 For example string (10100), the expected number of copies is
equal to 2 and for string(00001), the expected number of
copies is zero because the fitness of the string is very small as
compared to the average fitness of the population and it is
eliminated.
 The second string is good one and made two copies in the
mating pool.
Example-3
 Simple problem max (x)1/2
Subject to
1<x<16
 G A approach:
-Representation: binary Code, e.g. 100101 37
-Population size :6
-1-point Crossover, bitwise mutation
-Roulette wheel selection
-Random Initialization
We show one generation done by hand:
Example (x)1/2 selection

Strin Initial Decoded x f(x)= Pselection Expected


g Population value value (x)1/2 count
No. fi /  f (fi/favg)

1 100101 37 9.81 3.13 0.18 1.07


2 011010 26 7.19 2.68 0.15 0.91
3 010110 22 6.24 2.50 0.14 0.85
4 111010 58 14.81 3.85 0.22 1.31
5 101100 44 11.48 3.39 0.19 1.16
6 001101 13 4.09 2.02 0.12 0.71

Avg=0.166
sum=17.57
Average f=2.92
3.13/ 17.57 = 0.18 maximum f=3.85

From Roulette Wheel select the strings into mating pool for cross over. These are least values and they
got rejected by Roulette Wheel
For string 100101

100101 = 1*25+0*24+0*23+1*22+0*21+1*20 = 37

The value of x 1  37 
16  1
 9.81
26  1
Fitness value = (9.81) (1/2) = 3.13
Total fitness = 3.13+2.68+2.50+3.85+3.39+2.02 = 17.57
Avg fitness = 17.57/6 = 2.92
Maximum fitness = 3.85
Probability = 3.13/ 17.57 = 0.18
Avg probability = (.18+.15+.14+.22+.19+.12)/6 = .166
Example-Contd..

From the calculation, it is clear that string 3 and 6 are not considered for next step
because they have lesser probability as compared to avg fitness probability.
There are two new copies formed by string 4 and 5 for cross over.
Roulette wheel selection and crossover and mutation

Actual Mating Parents Cross Crossov Children Mutation


count pool over er site String
Roulette point
wheel
1 100101 100101 2 100101 101010 101010
1 011010 111010 2 111010 110101 110101
0 111010 011010 3 011010 011100 011100
2 111010 101100 3 101100 101010 101000
2 101100 111010 3 111010 111100 111100
0 101100 101100 3 101100 101010 101010

Shuffled Random number ( in between 1 and


length of string)
Decoding Decoded X f(x)=(X)1/2
value Value

42 11.00 3.32
53 13.62 3.69
28 7.67 2.77
40 10.52 3.24
60 15.28 3.91
42 11.00 3.32

Sum f =20.25
Average f=3.37
Maximum f=3.91

From the table it is clear that the average fitness is greater than the previous population.
Example-4
 A supply chain of a computer manufacturing industry.
 Demand of each customer zone is satisfied exactly by one production plant
 Each production plant supplies to exactly one customer zone

Customer Customer Customer Customer


Zone Zone Zone Zone
1 2 3 4
Plant 1 20 25 17 22
Plant 2 14 9 19 6
Plant 3 13 15 23 30
Plant 4 8 25 12 7

Distance (in Km) matrix as shown


Objective function-
Min. Distance
Cont……
Average fitness of
 Fitness value (f) of a string = (100 –D) all string
 Selection Random
Randomno noofofString
String 13
and
and24isisless
lessthan
thanCCp,p,So
So
 expected count (=f/f avg)
String
String
3and
1 and
4 is 2selected
is
selected
for for
crossover
crossover
 Crossover Procedure
a b c d Pr1 Ch1 l j k i

i j k l Ch2 d b c a
Pr2

Before crossover After crossover


Selection Criteria:- String No Iteration 1 Iteration 2
Let , Crossover Probability (CP) = 0.8
1 0.98 0.47
2 0.93 0.55
Random no. used for crossover:- 3 0.45 0.88
4 0.57 0.93
Cont……

 Initial Population

String
1) 1 2 3 4
2) 2 4 1 3
3) 3 1 4 2
4) 4 3 2 1

Objective function and fitness value


For string 1
D = 20 + 9 +23 +7 = 59
fitness (f) = (100-D)= 100-59 = 41
For string 2
D = 14 +25+17+30 = 86
fitness (f) = (100-D)= 100 – 86 = 14
For string 3
D = 13 +25 +12+6 = 56
fitness (f) = (100-D) = 100 -56 = 44
Cont……

For string 4
D = 8 +15 +19+22 = 64
fitness (f) = (100-64) = 100 -64 = 36
String Objective function Fitness Expected
value (D) value(f) count
1 1 2 3 4 59 41 1.21≈ 1
2 2 4 1 3 86 14 0.41≈ 0
3 3 1 4 2 56 44 1.30≈2
4 4 3 2 1 64 36 1.07≈ 1
ftotal= 135
favg= 33.75
Cont……

 Mating Pool

String
1) 1 2 3 4
2) 3 1 4 2
3) 3 1 4 2
4) 4 3 2 1
Crossover:- Between string 3 & 4
3) 3 1 4 2 Ch1 1 3 2 4
4) 4 3 2 1 Ch2 2 1 4 3
After Crossover

String
1) 1 2 3 4
2) 3 1 4 2
3) 1 3 2 4
4) 2 1 4 3
Cont……

Iteration 2-
Objective function & fitness value calculation
For string 1
D = 20 + 9 +23 +7 = 59
fitness (f) = (100-D)= 100-59 = 41
For string 2
D = 13 +25+12+6 = 56
fitness (f) = (100-D)= 100 – 56 = 44
For string 3
D = 20 +15 +19+7 = 61
fitness (f) = (100-D) = 100 -61 = 39
For string 4
D = 14 +25 +12+30 = 81
fitness (f) = (100-D) = 100 -81 = 19
Cont……

String Objective function Fitness Expected count


value (D) value(f)
1 1 2 3 4 59 41 1.14≈ 1
2 3 1 4 2 56 44 1.23≈ 2
3 1 3 2 4 61 39 1.03≈1
4 2 1 4 3 81 19 0.53≈ 0
ftotal= 143
favg= 35.75
Mating Pool
1) 1 2 3 4
2) 3 1 4 2
3) 3 1 4 2
4) 1 3 2 4

Crossover:- String 1 & 2 participate in crossover


5) 1 2 3 4 Ch1 2 1 4 3

2) 3 1 4 2 Ch2 4 2 3 1
Cont……
 After Crossover

String
1) 2 1 4 3
2) 4 2 3 1
3) 3 1 4 2
4) 1 3 2 4
Objective function and fitness value
For string 1
D = 14 + 25 +12 +30 = 81
fitness (f) = (100-D)= 100-81 = 19
For string 2
D = 8 +9+23+22 = 62
fitness (f) = (100-D)= 100 – 62 = 38
For string 3
D = 13 +25 +12+6 = 56
fitness (f) = (100-D) = 100 -56 = 44
For string 4
D = 20 +15 +19+7 = 61
fitness (f) = (100-D) = 100 – 61= 39
Cont……

String Objective Fitness Expected count


function value (D) value(f)
1 2 1 4 3 81 19 0.54≈ 0
2 4 2 3 1 62 38 1.08≈ 1
3 3 1 4 2 56 44 1.25≈2
4 1 3 2 4 61 39 1.11≈ 1
ftotal= 140
favg= 35

Maximum fitness value = 44


Best solution is 56.
EXAMPLE 5
 Solving the following problem with GA

2
Maximize cos ( x ),

0 x .
Subject to, 2
Min value for x is 0Max
and value
this will
for x is /2 and this will
correspond to 00000 correspond
in binary string
to 11111 in binary string
In this problem we use 5 bit string to represent the variable x.
Assume initial population size = 4 and of each is 5 bit long
To get x from decoded value we use
x  xmin  DV  ( xmax  xmin ) / (2l  1)
EXAMPLE-CONTD..
First Iteration: 0*24+ 1*23+ 0*22+ 0*21+ 20= 9
x  xmin  DV  ( xmax  xmin ) / (2l  1)
Cos2(0.46) f=i/f0.806
avg= 0.806/0.506=1.59
5
Initial Population x  0  9 (  0) / (2  1)
2
x  0.46
String DV x f(x) fi/f avg AC Mating Pool
01001 9 0.46 0.806 1.59 1 01001
10100 20 1.01 0.280 0.55 1 10100
00101 5 Max
0.25= 0.937 1.85 2 00101
11110 30 1.52 0.003 0.01 0 11110
00101
favg= 0.506
DV Decoded value Chromosome corresponding to it will be
AC Actual count of strings in the population replaced by better chromosome from the
gene pool
EXAMPLE-CONTD..
Crossover after gene 2
Crossover Operation:
Crossover after gene 3

Gene # Selected Genes Gene # Genes Gene # Offspring


1 01001 1 01|001 1 01101
2 10100 3 00|101 2 00001
3 00101 2 101|00 3 10101
4 00101 4 001|01 4 00100
Offspring DV X Value Fitness
After F(x)=cos2(
Crossover x)
01101 13 0.66 0.625
00001 1 0.05 =
Max 0.997
10101 21 1.06 0.236
00100 4 0.20 0.959
Fitness after crossover
EXAMPLE-CONTD..
Mutation operation: Randomly selected: String 4 Gene 4:
String 00100 becomes 00110

Gene # Offspring Gene # Offspring


1 01101 1 01101
2 00001 2 00001
3 10101 3 10101
4 00100 4 00110
EXAMPLE-CONTD..

Second Iteration:
New Gene Pool

String DV x f(x) fi/favg AC New Mating Pool


01101 13 0.66 0.625 0.90 1 01101
00001 1 Max0.05
= 0.997 1.44 2 00001
10101 21 1.06 0.236 0.34 0
00110 6 0.30 0.910 1.32 1 00110
favg 0.692

Offspring 00001 is giving us a near optimal value for Max: cos2 (x) ,
which is 0.997
Offspring after crossover & Mutation
NEXT EXAMPLE:
EXAMPLE 6
 Solving the following problem with GA

Maximize x2
  3x
10
Subject to, 0  x  31.
Min value for x is 0 and
Maxthis willfor x is 31 and this will
value
correspond to 00000 incorrespond
binary string
to 11111 in binary string
In this problem we use 5 bit string to represent the variable x.
Take population size = 4 and of each is 5 bit long
To get x from decoded value we use
x  xmin  DV  ( xmax  xmin ) / (2l  1)
EXAMPLE-CONTD..
First Iteration:
x  xmin  DV  ( xmax  xmin ) / (2l  1)
(-x2/10)+3x= 18.9
Initial Population x  0  9  (31  0) / (25  1)
x9
String DV x f(x) fi/f avg AC Mating Pool
01001 9 9.00 18.900 1.47 1 01001
10100 20 Max
20.00= 20.000 1.56 2 10100
00101 5 5.00 12.500 0.97 1 00101
11110 30 30.00 0.000 0.00 0 11110
10100
favg= 12.85
DV Decoded value Chromosome corresponding to it will be
AC Actual count of strings in the population replaced by better chromosome from the
gene pool
EXAMPLE-CONTD..
Crossover after gene 2
Crossover Operation: one point crossover, selection at random
Crossover after gene 3

Gene # Selected Genes Gene # Genes Gene # Offspring


1 01001 1 01|001 1 01100
2 10100 2 10|100 2 10001
3 00101 3 001|01 3 00100
4 10100 4 101|00 4 10101
Offspring DV X Value Fitness F(x)=
After (-x2/10)+3x
Crossover

01100 12 12.00 21.600


10001 17 17.00
Max = 22.100
00100 4 4.00 10.400
10101 21 21.00 18.900
Fitness after crossover
EXAMPLE-CONTD..
Mutation operation: Randomly selected: String 3 Gene 4:
String 00100 becomes 00110

Gene # Offspring Gene # Offspring


1 01100 1 01100
2 10001 2 10001
3 00100 3 00110
4 10101 4 10101
EXAMPLE-CONTD..

Second Iteration:
New Gene Pool

String DV x f(x) fi/favg AC New Mating Pool


01100 12 12.00 21.600 1.12 1 01100
10001 17 17.00
Max= 22.100 1.15 2 10001
00110 6 6.00 14.400 0.75 0 10001
10101 21 21.00 18.900 0.98 1 10101
favg 19.250

Offspring 10001 is giving us a near optimal value for


objective function, which is 22.10
Offspring after crossover & Mutation
Example 7
  
How many bits each chromosome
should contain??
Example(cont…)
 
Example(cont…)

 For m1
(12.1-(-3)) x 10,000 = 1,51,000  

217 < 1,51,000 < 218 – 1

 So m1 = 18 and
 Similarly m2 = 15

 Therefore total length of chromosome is


m1 + m2 = 33
Lets see…
converting back from binary to decimal
Example(cont…)

 
Example(cont…)

0 0 0 0 0 1 0 1 0 1 0 0 1 0 1 0 0 1 1 0 1 1 1 1 0 1 1 1 1 1 1 1 0

Binary Number Decimal Number


 
Example(cont…)
Initial population : initial population is randomly generated as follows.

Example

 
Example(cont…)

  
Example(cont…)
  
Example(cont…)

Selection : In most cases Roulette Wheel approach is adopted.

F = Sum of all fitness values


=(19.805119+17.380769+…………….+10.252480)
=178.135372

Probability of chromosome k = fitness of k/ F


Example(cont…)

Chromosome No Probability Cumulative probability


1 0.111180 0.111180
2 0.097515 0.208695
3 0.053839 0.262534
4 0.165077 0.427611
5 0.088057 0.515668
6 0.066806 0.582475
7 0.100815 0.683290
8 0.110945 0.794234
9 0.148211 0.942446
10 0.057554 1.000000
Example(cont…)
Now we spin the roulette wheel 10 times, and each time we select a single chromosome
for new population. Let these numbers are

v4 v4 v8
0.301451 0.322062 0.766505
See corresponding cumulative
v9 0.881893 0.350871 v4 0.583392 v7 probability.
0.177618 0.343242 v4 0.032685
v2 v1
0.197577
v2

For 0.301451 we select chromosome 4 because it’s cumulative probability lies in this
range.
Example(cont…)
  
Example(cont…)
  
Example(cont…)
  
Example(cont…)
 

bit_ no Chromosome no Bit no of chromosome Random no

105 4 6 0.009857
164 5 32 0.003113
199 7 1 0.000946
329 10 32 0.001282
Example(cont…)
  
Example(cont…)
Corresponding decimal values of variables are
f(6.159951,4.109598) = 29.406122
f(6.159951,4.109598) = 29.406122 Example- for chromosome 1
here
f(-0.330256,4.694977)=19.763190
f(11.907206,4.694977)=5.702781  

f(8.024130,4.170248) = 19.91025
f(9.342067,5.121702)= 17.958717
f(6.159951,4.109598)= 29.406122
f(6.159951,4.109598)= 29.406122
f(-2.687969,5.361653)=19.805119
f(0.474101,4.170248)= 17.370896
Example(cont…)
  

 
Thank You

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy