0% found this document useful (0 votes)
73 views

Genetic Algorithm

Genetic algorithms are optimization techniques inspired by natural selection and genetics. They work by evolving a population of potential solutions over multiple generations using techniques like selection, crossover, and mutation to generate improved offspring. Genetic algorithms simulate the process of natural selection, where the fittest individuals are most likely to reproduce and pass on their traits to subsequent generations.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views

Genetic Algorithm

Genetic algorithms are optimization techniques inspired by natural selection and genetics. They work by evolving a population of potential solutions over multiple generations using techniques like selection, crossover, and mutation to generate improved offspring. Genetic algorithms simulate the process of natural selection, where the fittest individuals are most likely to reproduce and pass on their traits to subsequent generations.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

GENETIC

ALGORITHMS

Dr. Jasmeet Singh


Assistant Professor,
CSED, TIET
GENETIC ALGORITHMS (GAS)
 Genetic algorithms are optimization technique that are
based on the mechanics of natural selection and natural
genetics.
 It is based on t he “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 a n optimization problem where every
successor generation is better.
 It is based on the assumption t h a t if robustness is t he
objective then n at ure is the clear winner a s it h a s
features of self repair, self guidance and reproduction. So,
GAs simulate t he process of n atu ral selection an d
genetics.
DIFFERENCES BETWEEN GAS AND OTHER OPTIMIZATION
METHODS

 GAs work with encoding of parameter set a nd


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

Survived
Indivd u als
Individ u al
reproduce
Populatio GAs
n a n d pass
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 ofChromosome
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 percentageas:
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 isrepeated.
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, ch romosome 2,3 a nd 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 a t the end of th e run).
 This leads to each individual having a n 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 matt er how fit relative


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

 Disadv:- This in t urn 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 r a nk selection, the individuals are sorted by
fitness.
The probability t h a t individual i is selected is
t hen inversely proportional to its position in this
sorted list, i.e. t he individual a t the head of th e
list is more likely to be selected t h a n the next
individual, a nd 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 of
(f(x)= 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 a t random an d
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 t he bit
string is selected a t which swapping occurs;
11101001000 11101010101

00001010101 00001001000

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

00001010101 11001011000
CROSSOVER METHODS IN GAS

Uniform crossover
In uniform crossover, individual bits are chosen a t
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 a t random, an d interchange the
values. This is common in permutation based
encodings.
MUTATION
 Swap Random Piece of path
In this bit flip m u ta tion, 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 an d 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, th e spin is selected and the mating parent s are
selected.
 Let us decide t h a t one with probability more t h a n 25%
is selected twice a nd one with probability less t h a n 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
Chrom Fitness Chrom Ran
# Chrom Fitness # k
Sort
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
P4 14 P5 11111
P6 10 P4 11001
6 5 4 3 24A pril
P2 5 2 1
P6 2015 00110
S OLUTION C ONTD …..
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
 Con sider t h e following t ravellin g salesman problem wh ich u ses
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
S OLUTION C ONTD …..
 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


t h a n 25%.
So mating population for cross over is

P3,P3, P4,P4
C ROSSOVER IN TSP
 Partially Mapped Crossover Operator
 Consider, for example, the two parents tours with
randomly one cut point between 3rd an d 4t h bits an d
other cut point between 6th a nd 7th bits are as follows
(the two cut points marked with “|”):
C ROSSOVER IN TSP
 Partially Mapped Crossover Operator
 Consider, for example, the two parents tours with
randomly one cut point between 3rd an d 4t h bits an d
other cut point between 6th a nd 7th bits are as follows
(the two cut points marked with “|”):
C ROSSOVER IN TSP
 Partially Mapped Crossover Operator
C ROSSOVER 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  pi xi wi and pi are the weight
i1 and profit of ith item.
N xi = 1, if item i is
Subject to w x i i C included in the knapsack,
i1
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 1111 11 0 1 1 1 00
→ →
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 1100 11 0 0 1 1 01
→ →
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 1000 10 1 1

1010

0111 0 4 0 1 1 1 0 1 0 0 0100
1000 3 3
9 1010 11 1
0 0 0 1 0 0 0 0 0
0001 10 3
→ →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
0 0 1 0 0 0 0 1 0
1010 11 2
→ →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