Download as pdf or txt
Download as pdf or txt
You are on page 1of 47

STOCHASTIC SEARCH

ALGORTITHMS-II
(GENETIC ALGORITHMS)

Dr. Jasmeet Singh


CSED, TIET
GENETIC ALGORITHMS (GAS)
 This searching technique is based on the mechanics of
natural selection and natural genetics.
 It is based on the “survival of the fittest” theory i.e the
parents (states) which are fittest are allowed to
produce children (new states).
 It simulates the process of evolution i.e. it considers
evolution as an optimization problem where every
successor generation is better.
 It is based on the assumption that if robustness is the
objective then nature is the clear winner as it has
features of self repair, self guidance and
reproduction. So, GAs simulate the process of natural
selection and genetics.
DIFFERENCES BETWEEN GAS AND
OTHER SEARCHING METHODS

 GAs work with encoding of parameter set and


not the parameter themselves.
 GAs search for a population of points, not single
point.
 GAs use objective function value (also called
fitness function) directly rather than any heuristic
function or first or second derivatives, etc.
 GAs use stochastic rules (based on probability or
random numbers) rather then deterministic
algorithm.
GAS
 Survival of
the fittest

Survived
Indivduals
Individual
reproduce
Populatio GAs
and pass
n
their
features

Species
evolution
GENETIC ALGORITHM
 Genetic Algorithm works in following phases:
1) Initial Population: It begins with randomly selected
population (states) which are satisfactory to the problem.
For example, for travelling salesman problem with ten
cities, the initial population may contain any sequence
of states from start city.
Encode the population as strings, strings of bits or in
octal form or hexadecimal form (if required).
Usually for n variables, the size of population varies
from 2n to 4n.
2) Calculate the value of the fitness function (i.e. objective
function) which needs to be maximized or minimized. For
objective function which need to minimized can be
converted into a maximum problem by taking
reciprocal/ negative of the original function.
GENETIC ALGORITHM CONTD…..
3) Apply the following operators on the initial population to generate child
(offspring's) . These child will act as parents for the next iteration.
(a) Selection: Parent Selection is the process of selecting parents
which mate and recombine to create off-springs for the next
generation. Parent selection is very crucial to the convergence rate of
the GA as good parents drive individuals to a better and fitter solutions
(Various methods of selection are explained in the later slides).
(b)Crossover: The crossover operator is analogous to reproduction and
biological crossover. In this more than one parent is selected and one or
more off-springs are produced using the genetic material of the parents
(Various methods of selection are explained in the later slides).
(c) Mutation: Mutation may be defined as a small random change in
the chromosome, to get a new solution. It is used to maintain and
introduce diversity in the genetic population and is usually applied
with a low probability. If the probability is very high, the GA gets
reduced to a random search.
4) Repeat step 3 on the new offspring’s produced to produce offspring’s for
next iteration.
GENETIC ALGORITHM CONTD…..
Nature GA
Population No. of candidate solutions.
Individual Each Candidate solution.
Chromosome Encoding of each indivudal
Gene Each bit of Chromosome
GENETIC ALGORITHMS FLOW CHART
SELECTION METHODS IN GAS
 Roulette Wheel Selection-random number
 Actual Count --- Best Method

 Rank

 K-Tournament Selection
SELECTION METHODS IN GAS
 Roulette Wheel Selection (example explained at the
end)
1) Compute the probability of selection for each population
and convert into percentage as:
X 100%

where
n = no of populations f(x)=fitness value corresponding to a
particular individual in the population
2) Represent these probabilities on the roulette wheel
according to the percentage of their probabilities. A fixed
point is chosen on the wheel circumference as shown and
the wheel is rotated. The region of the wheel which comes
in front of the fixed point is chosen as the parent. For the
second parent, the same process is repeated.
EXAMPLE- ROULETTE WHEEL-RANDOM
 f(x) = 2x ; 0<x<31
 4 random numbers :-

P(i) Prob. Range Quadrant 4 random Random


(0-1) numbers no. lies
in______
.2 0 - .2 1 Quadra
.45 .2 - .65 2 nt
.79 4
.13 .65 - .78 3
.22 .78 – 1 4 .25 2
.71 3
.9 4

 Therefore, chromosome 2,3 and 4(2 times)


selected.
RANK SELECTION METHODS IN GAS
 Rank Selection also works with negative
fitness values and is mostly used when the
individuals in the population have very
close fitness values (this happens usually at
the end of the run).
 This leads to each individual having an almost
equal share of the pie (like in case of fitness
proportionate selection) as shown in the following
image

RANK SELECTION METHODS IN GAS

 Hence each individual no matter how fit relative


to each other has an approximately same
probability of getting selected as a parent.

 Disadv:- This in turn leads to a loss in the


selection pressure towards fitter
individuals, making the GA to make poor
parent selections in such situations.
RANK SELECTION METHODS IN GAS
 Rank Selection Method(Example explained
at the end)
In rank selection, the individuals are sorted by
fitness.
The probability that individual i is selected is
then inversely proportional to its position in this
sorted list, i.e. the individual at the head of the
list is more likely to be selected than the next
individual, and so on through the sorted list.
EXAMPLE-ROULETTE WHEEL-RANK
SELECTION
 f(x) = x^2 ; 0<x<31
 Rank assigned acc. to Probability (Weighing)

Popul x Chromoso Fitne Weightin Rank Prob.(Ra


ation mes ss g(gi) assigned nk/Sum
(f(x)= of Ranks
x^2) i.e. 10)
1 13 01101 169 .14 2 .2
2 24 11000 576 .49 4 .4

3 8 01000 64 .06 1 .1
4 19 10011 361 .31 3 .3

10
EXAMPLE-ROULETTE WHEEL-ACTUAL COUNT
 f(x) = x^2 ; 0<x<31

Popu x Chromos Fitn Weighti Expecte Actual #


latio omes ess ng(gi) d # for for
n (f(x) mating mating
=x^2 pool(4 pool
) times)
1 13 01101 169 .14 0.57 1

2 24 11000 576 .49 1.96 2

3 8 01000 64 .06 0.21 0


4 19 10011 361 .31 1.2 1
K-WAY TOURNAMENT SELECTION
 In K-Way tournament selection, we select K
individuals from the population at random and
select the best out of these to become a parent.

 The same process is repeated for selecting the


next parent.

 Tournament Selection is also extremely popular


in literature as it can even work with negative
fitness values.
K-WAY TOURNAMENT SELECTION
CROSSOVER METHODS IN GAS
Single-Point Crossover
In single-point crossover, a random position in the bit
string is selected at which swapping occurs;
11101001000 11101010101

00001010101 00001001000

Two-Point Crossover
In two-point crossover, two positions are selected and
segments are swapped;
11101001000 00101000101

00001010101 11001011000
CROSSOVER METHODS IN GAS

Uniform crossover
In uniform crossover, individual bits are chosen at
random for swapping

11101001000 10001000100

00001010101 01101011001
MUTATION
 Bit Flip Mutation
In this bit flip mutation, we select one or more
random bits and flip them.

 Swap Mutation
In swap mutation, we select two positions on the
chromosome at random, and interchange the
values. This is common in permutation based
encodings.
MUTATION
 Swap Random Piece of path
In this bit flip mutation, we select one or more
random bits and flip them.
If we have 3 parts chosen out of 8 i.e. L to R, we
have to reverse the order from R to L.

e.g. -> used in TSP.


EXAMPLE 1
 Consider the following initial population and fitness
function values.
Population Chromosome Fitness
P1 10001 50
P2 10010 6
P3 00100 36
P4 11001 30
P5 11111 36
P6 00110 28
 Generate mating pool for crossover operation using
Roulette wheel and ranking selection method.
 Apply crossover operator using single point?
SOLUTION
Roulette Wheel Selection:
First calculate probability of selection of each population
as X 100

Populati Chromoso Fitness Probability Probability


on me %
P1 10001 50 50 / 186 = 0.2688 26.88
P2 10010 6 6 /186 = 0.347 3.47
P3 00100 36 36/ 186 = 0.2081 20.81
P4 11001 30 30/186= 0.1734 17.34
P5 11111 36 36/186 = 0.2081 20.81
P6 00110 28 28/186 = 0.1681 16.18
Total 186
SOLUTION CONTD….

 Represent these probabilities in Roulette Wheel

P1
P2
P3
P4
P5
P6
SOLUTION CONTD….
 Now, the spin is selected and the mating parents are
selected.
 Let us decide that one with probability more than 25%
is selected twice and one with probability less than 5%
is not selected. So the new mating pool for crossover is:
Population Chromosome
P1 10001
P1 10001
P3 00100
P4 11001
P5 11111
P6 00110
RANK SELECTION
2
2
Chrom Fitness Chrom Ran
# Sort Chrom Fitness # k
P1 50 according # Assign raking P1 6
P2 6 to fitness P1 50 P3 5
P3 36 P3 36 P5 4
P4 30 P5 36 P4 3
P5 36 P4 30 P6 2
P6 28 P6 28 P2 1
P2 6
Chrom Prob Roulette Chrom # Mating Pool
wheel
# % P1 10001
P1 29 P1 10001
P3 24
P3 00100
P5 19
P5 11111
P4 14
P6 10 P4 11001
6 5 4 3 24 April
P2 5 2 1
P6 2015 00110
SOLUTION CONTD…..
 Crossover using single point method:
Let P1 and P3 are choose as parents:
10001 10100

00100 00001
Let P1 and P4 are choose as parents:
10001 10001

11001 11001
Let P5 and P6 are choose as parents:
11111 10110

00110 01111
EXAMPLE II
 Consider the following travelling salesman problem which uses
following distance matrix and starting city A.

A B C D E F G H
A 0 1 4 2 9 8 3 2
B 1 0 5 3 7 2 5 1
C 2 5 0 6 1 4 7 7
D 4 3 6 0 5 2 1 6
E 9 7 1 5 0 9 1 1
F 8 2 4 2 9 0 3 5
G 3 5 7 1 1 3 0 2
H 2 1 7 6 1 5 2 0

 GENERATE INITIAL POPULATION AND SELECT PARENTS FOR


FIRST ITERATION USING ROULETTE WHEEL METHOD? (Take
population size 4)
SOLUTION
 Let initial Population be:

Initial Chromosome Diatnace value Fitness = 1/D


Population (Done to
convert into
maximization)
P1 ABCDEFGHA 1+5+6+5+9+3+2+2=33 1/33 = 0.0303
P2 ACBDEFGHA 4+5+3+5+9+3+2+2=33 1/33 = 0.0303
P3 ADBCFEHGA 2+3+5+4+9+1+2+3=29 1/29 = 0.0345
P4 AHGFEDBCA 2+2+3+9+5+3+5+2= 31 1/31= 0.0322
SOLUTION CONTD…..
 Roulette Wheel Selection
Initial Chromosome Fitness Prob Prob %
Population
P1 ABCDEFGHA 0.0303 0.2380 23.80%
P2 ACBDEFGHA 0.0303 0.2380 23.80%
P3 ADBCFEHGA 0.0345 0.2710 27.10%
P4 AHGFEDBCA 0.0322 0.2529 25.29%
Total 0.1273

 Let us keep two samples each with probability greater


than 25%.
 So mating population for cross over is

P3,P3, P4,P4
CROSSOVER IN TSP
 Partially Mapped Crossover Operator
 Consider, for example, the two parents tours with
randomly one cut point between 3rd and 4th bits and
other cut point between 6th and 7th bits are as follows
(the two cut points marked with “|”):
CROSSOVER IN TSP
 Partially Mapped Crossover Operator
 Consider, for example, the two parents tours with
randomly one cut point between 3rd and 4th bits and
other cut point between 6th and 7th bits are as follows
(the two cut points marked with “|”):
CROSSOVER IN TSP
 Partially Mapped Crossover Operator
CROSSOVER IN TSP
 Partially Mapped Crossover Operator
0/1 Knapsack

Using Genetic Algorithm


Problem Statement
• Choose a subset from N items such that the
corresponding profit sum is maximized without
having the weight sum to exceed the capacity C.
N
• Maximize px
i 1
i i
wi and pi are the weight
and profit of ith item.
N xi = 1, if item i is
Subject to w x
i 1
i i C included in the knapsack,
otherwise xi = 0.
where xi {0,1}, i  1,2,...N
Example – 1 Name Weight Value
A 45 3
• Chromosome is a 4-bit string. B 40 5
– {xA xB xC xD} C 50 8
D 90 10
• Population size = 4. C = 100.
• First two fittest chromosomes selected as it is.
• 3rd and 4th fittest use for one-point crossover in the
middle followed by single bit mutation of first
offspring. Bits chosen for mutation follows this
cyclic order (xD, xC, xB, xA).
• Initial population: 1 1 1 1, 1 0 0 0, 1 0 1 0, 1 0 0 1.
• Output the result after 12 iterations.
Name Weight Value
Solution C = 100 A 45 3
B 40 5
Population Fitness Fittest Operations C 50 8
0 1111 0 3 D 90 10
1000 3 2 1 1 1 1 1 1 0 1 1 1 0 0
→ →
1010 11 1 1 0 0 1 1 0 1 1 1 0 1 1
1001 0 4
1 1010 11 1
1000 3 3 1 0 0 0 1 0 1 1 1 0 0 1
→ →
1100 8 2 1 0 1 1 1 0 0 0 1 0 0 0
1011 0 4
2 1010 11 1
1100 8 2 1 0 0 0 1 0 0 1 1 1 0 1
→ →
1001 0 4 1 0 0 1 1 0 0 0 1 0 0 0
1000 3 3
3 1010 11 1
1100 8 2 1 0 0 0 1 0 0 1 0 0 0 1
→ →
1101 0 4 1 1 0 1 1 1 0 0 1 1 0 0
1000 3 3
Name Weight Value
Contd… C = 100 A 45 3
B 40 5
Population Fitness Fittest Operations C 50 8
4 1010 11 1 D 90 10
1100 8 3 1 1 0 0 1 1 0 0 1 1 0 1
→ →
0001 10 2 1 1 0 0 1 1 0 0 1 1 0 0
1100 8 4
5 1010 11 1
0001 10 2 1 1 0 0 1 1 0 1 1 1 1 1
→ →
1101 0 4 1 1 0 1 1 1 0 0 1 1 0 0
1100 8 3
6 1010 11 1
0001 10 2 1 1 0 0 1 1 1 1 1 0 1 1
→ →
1111 0 4 1 1 1 1 1 1 0 0 1 1 0 0
1100 8 3
7 1010 11 1
0001 10 2 1 1 0 0 1 1 1 1 0 1 1 1
→ →
1011 0 4 1 0 1 1 1 0 0 0 1 0 0 0
1100 8 3
Name Weight Value
Contd… C = 100 A 45 3
B 40 5
Population Fitness Fittest Operations C 50 8
8 1010 11 1 D 90 10
0001 10 2 1 0 0 0 1 0 1 1 1 0 1 0
→ →
0111 0 4 0 1 1 1 0 1 0 0 0 1 0 0
1000 3 3
9 1010 11 1
0001 10 3 0 0 0 1 0 0 0 0 0 0 1 0
→ →
1010 11 2 0 1 0 0 0 1 0 1 0 1 0 1
0100 5 4
10 1010 11 1
1010 11 2 0 0 1 0 0 0 0 1 0 1 0 1
→ →
0010 8 3 0 1 0 1 0 1 1 0 0 1 1 0
0101 0 4
11 1010 11 2
1010 11 3 1 0 1 0 1 0 0 1 0 0 0 1
→ →
0101 0 4 0 1 0 1 0 1 1 0 0 1 1 0
0110 13 1
Contd…
Name Weight Value C = 100
A 45 3 Population Fitness Rank
B 40 5 12 0110 13 1
C 50 8 1010 11 3
D 90 10 0001 10 4
0110 13 2
• Output the fittest individual, i.e. 0110.
• Total profit = 0 × 3 + 1 × 5 + 1 × 8 + 0 × 10 = 13.
• Total weight = 0 × 45 + 1 × 40 + 1 × 50 + 0 × 90
= 90 ≤ 100.

You might also like