Genetic Algorithm
Genetic Algorithm
4
4. Recombination.
Recombination combines parts of two or more parental solutions to
create new, possibly better solutions (i.e. offspring).
5. Mutation.
While recombination operates on two or more parental chromosomes,
mutation locally but randomly modifies a solution. It avoids
permanent loss of diversity within the solutions.
6. Replacement.
The offspring population created by selection, recombination, and
mutation replaces the original parental population.
4. Repeat steps 2–6 until a terminating condition is met.
5
G A are different from other optimization and search
procedures in four ways:
1. GA s work with a coding of the parameter set, not with the
parameters themselves.
2. GA s search from a population of points, not a single point.
3. GA s use payoffs (objective function) information, not derivatives
or other auxiliary knowledge.
4. GA s use probabilistic transition rules, not deterministic rules.
6
We will now see some of the selection, recombination, and
mutation operators commonly used in genetic
algorithms.
7
It defines the way individuals in the current
population are selected for reproduction. Selection
procedures can be broadly classified into two
Individual x i will have a
classes:
probability of being
1. Fitness Proportionate Selection chosen
This includes methods such as roulette-wheel 1 2
selection and stochastic universal selection. n
o In roulette-wheel selection, each individual in
the population is assigned a roulette wheel slot
3
sized in proportion to its fitness - in the biased
roulette wheel, good solutions have a larger slot
size than the less fit solutions. 4
o The roulette wheel is spun to obtain a
reproduction candidate. Area is Proportional to
fitness value
8
The Roulette Wheel Selection Scheme
Fitness f 28 18 14 9 26
Probability pi 28/95 = 0.295 0.189 0.147 0.095 0.274
Cumm. Prob 0.295 0.484 0.631 0.726 1.000
10
2. Ordinal Selection
This includes methods such as tournament selection, and truncation
selection.
o In tournament selection two individuals are chosen at random from the
population. A random number r is then chosen between 0 and 1. If r <
k (where k is a parameter, for example 0.75), the fitter of the two
individuals is selected to be a parent; otherwise the less fit individual
is selected. The two are then returned to the original population and
can be selected again.
o Using this selection scheme, n tournaments are required to choose n
individuals.
o In truncation selection, the top (1/s)th of the individuals get s copies
each in the mating pool.
13
The k-point and uniform crossover methods discussed are not
well suited for search problems with permutation codes such as
the ones used in the traveling salesman problem.
Why?
• Single point crossover method randomly selects a crossover point in
the string and swaps the substrings.
• This may produce some invalid offsprings as shown below.
4 1 3 2 5 6 4 1 3 1 5 6
4 3 2 1 5 6 4 3 2 2 5 6
14
o Two parents, P1 & P2 are randomly selected
o Some of the genes for offspring C1 are filled by taking the genes from
parent P1 where there is a one in the template.
_ BC _ _ F _
o At this point C1 is partially filled, but it has some “gaps”. The genes of
parent P1 in the positions corresponding to zeros in the template are
taken and sorted in the same order as they appear in parent P2. The
sorted list is used to fill the gaps in C1.
A_ _ D E _ G after
sorting E _ _ D G _
A Combining the results E B C D G
F A
15
🞉 This operator is different from other genetic sequencing operators
in that it emphasizes adjacency information instead of the order
or position of items in the sequence. Used in TSP.
🞉 The algorithm for the Edge-Recombination Operator involves
constructing an Edge Table first.
🞉 The Edge Table is an adjacency table that lists links into and out of
a city found in the two parent sequences.
🞉 If an item is already in the edge table and we are trying to insert
it again, that element of a sequence must be a common edge and is
represented by inverting it's sign.
16
Parent 1 4 1 3 2 5 6
Parent 2 4 3 2 1 5 6
1 2 3 4 5
2 1 -3 5
3 1 -2 4
4 1 3 -6
5 1 2 -6
6 -4 -5
17
1. Choose the initial city from one of the two parent tours. This is the
current city.
2. Remove all occurrences of the current city from the right hand side of
the edge table.( These can be found by referring to the edge-list for the
current city).
3. If the current city has entries in it's edge-list, go to step 4 otherwise go to
step 5.
4. Determine which of the cities in the edge-list of the current city has the
fewest entries in it's own edge-list. The city with fewest entries becomes
the current city. In case a common edge (negative integer) is present, it is
given preference. Ties are broken randomly. Go to step 2.
5. If there are no remaining unvisited cities, then stop. Otherwise, randomly
choose an unvisited city and go to step 2.
18
Step 2
Step 1
1 2 3 4 5 1 2 3 5
2 1 -3 5 2 1 -3 5
3 1 -2 4 3 1 -2
4 1 3 -6 4 1 3 -6
5 1 2 -6 5 1 2 -6
6 -4 -5 6 -5
4 4 6
1 2 3 5 1 2 3
2 1 -3 5 2 1 -3
3 1 -2 3 1 -2
4 1 3 4 1 3
5 1 2 5 1 2
6 -5 6
4 6 5 4 6 5 1
Parent 1 4 1 3 2 5 6 4 6 5 1 2 3
Edge Table
April 10,
22
April 10,
23
🞉 Mutation is a genetic operator used to maintain genetic diversity from
one generation of a population to the next. It is analogous to biological
mutation.
🞉 Mutation alters one or more gene values in a chromosome from its
initial state.
🞉 In mutation, the solution may change entirely from the previous
solution. Hence G A can come to better solution by using mutation.
🞉 Mutation occurs during evolution according to a user-definable
mutation probability. This probability should be set low. If it is set too
high, the search will turn into a primitive random search.
🞉 One of the most common mutations is the bit-flip mutation. In bitwise
mutation, each bit in a binary string is changed (a 0 is converted to
1, and vice versa) with a certain probability, p m , known as the
mutation probability.
10
Let us find the maximum value of the function (15x - x2)
where parameter x varies between 0 and 15. For
simplicity, we may assume that x takes only integer
values. Thus, chromosomes can be built with only four
genes:
April 10,
106
Suppose that the size of the chromosome population N is 6, the crossover
probability pc equals 0.7, and the mutation probability p m equals 0.001.
The fitness function in our example is defined by
Chromosome Chromosome Decoded Chromosome Fitness
label string integer fitness ratio, %
X1 1 1 0 0 12 36 16.5
X2 0 1 0 0 4 44 20.2
X3 0 0 0 1 1 14 6.4
X4 1 1 1 0 14 14 6.4
X5 0 1 1 1 7 56 25.7
X6 1 0 0 1 9 54 24.8
In nature only the fittest species can survive, breed & pass their genes on
to the next generation. GAs use a similar approach, but unlike nature, the
size of the population remains unchanged.
The last column shows the ratio of the individual chromosome‟s fitness to
the population‟s total fitness. This ratio determines the chromosome‟s
chance of being selected for mating. The chromosome‟s average fitness
April 10, improves from one generation to the next. 107
The most commonly used chromosome selection techniques is the
roulette wheel selection.
C ro s s o v e r
Generation i
1 1 0 0 f = 36 0
X1i X6i 1 0 0 1 1 0 0 X2i
0 1 0 0 f = 44
0 0 0 1 f = 14
X2i 1 1 1 0 f = 14 0 1 1 1
0 1 0 0 X5i
X1i 1
0 1 1 1 f = 56
1 0 0 1 f = 54
X3i X2i 0 1 0 0 0 1 1 1 X5i
Gen era ti on (i + 1)
100 0 1 0 0 0 f = 56 Mutation
X1: 16.5% X4i
X 1 i+1
0 1 0 1 f = 50
X2: 20.2% X6'i 1 0 0 0
X 2 i+1
X3: 1 0 1 1 f = 44
7 5 .2 X2'i
0 1 0 1
6.4% X 5 iX 3 i + 1 0 1 0 0 f = 44
X4: f = 54 0
X 5 0 1 1 0 X1'i 1 1 1 1 1 1
6.4% X 4 i +1
i+1
3 6 .7 f = 56
4 3 .1 0 1 1 1 X1"i
4 9 .5 X5: 25.3% X 6 i+1
X6i
X6: 24.8% 0 1 0 0 1 0
X5'i 0 1 0 0
0 1 1 1
X2i
April 10,
10
X2"i
Example: the M A X O N E problem
o Suppose we want to maximize the number of ones in a string of l
binary digits.
o Seems a trivial problem because we know the answer in advance.
However, we can think of it as maximizing the number of correct
answers, each encoded by 1, to l yes/no difficult questions.
o An individual is encoded as a string of l binary digits.
o The fitness f of a candidate solution to the M A X O N E problem is
the number of ones in its genetic code.
o We start with a population of n random strings. Suppose that l = 10
and n = 6
o We toss a fair coin 60 times and get the initial population.
April 10,
109
s1 = 1111010101 f (s1) = 7 • Next we apply fitness proportionate
selection with the roulette wheel method.
s2 = 0111000101 f (s2) = 5 • We repeat the extraction so that the size of
the population remains the same. (6 in our
s3 = 1110110101 f (s3) = 7
case)
s4 = 0100010011 f (s4) = 4 • Suppose that, after performing selection,
we
s5 = 1110111101 f (s5) = 8 get the following population:
• Next we mate strings for crossover. For
s6 = 0100110000 f (s6) = 3 each couple we decide according to
s 1` = 1111010101 (s 1) crossover probability (for instance 0.6)
whether to actually perform crossover or
s 2` = 1110110101 (s 3)
not
s 3` = 1110111101 (s 5) • Suppose that we decide to actually perform
s 4 ` = 0111000101 (s2) crossover only for couples (s1 `, s2`) and (s5 `,
s6 `). For each couple, we randomly extract
s 5 ` = 0100010011 (s4) a crossover point, for instance 2 for the I
5
andfor the II. 110
Before crossover: After crossover:
s 1 ` = 1111010101 s 2 ` = 1110110101 s 1 `` = 1110110101 s 2 `` = 1111010101
32
The order of the schema S (denoted by o(S)) is the number of fixed
positions (0 or 1) presented in the schema
o o(01*1*) = 3 o(**1*1010) = 5
The order of a schema is useful to calculate survival probability of
the schema for mutations.
If the length of the string is l & its order o(S), then how many different
strings match the template?
If the length of the string is l & the no. of alphabets is k, then how many
different schema can we have?
(k+1)l
33
The defining length of schema S (denoted by (S)) is the distance
between the first and last fixed positions in it
35
This means that, at a given generation, while the G A is explicitly
evaluating the fitnesses of the n strings in the population, it is actually
implicitly estimating the average fitness of a much larger number of
schemas, where the average fitness of a schema is defined to be the
average fitness of all possible instances of that schema.
Eg.,
• in a randomly generated population of n strings, on average half the
strings will be instances of 1***···* and half will be instances of 0
***···*.
• The evaluations of the approximately n/2 strings that are instances of
1***···* give an estimate of the average fitness of that schema
(this is an estimate because the instances evaluated in typical−size
population are only a small sample of all possible instances).
36
Schemas are a useful conceptual tool for several reasons, one being
that they are simply a notational convenience.
Consider a simple one-dimensional problem:
Max|f(x) = x 2 | 0x
511
Obviously f(x) is maximum at111111111
511. Binary equivalent of 511?
11 0101011,
Examples of binary strings approximately equal to 511? 111110100,
110001110
111011101.
000011101 ,
000000000,
Examples of binary strings very far from 511? 000001010
000010010.
Seeing the two sets of binary numbers, it is apparent that the near maximum
values of x are instances of 11*******, which provides a convenient notation
for the numerous strings that represent a near optimal solution to the
problem. 118
Suppose x is an individual that belongs to the schema H , then we say
that x is an instance of H (x ∈ H)
m(H, k) denotes the number of instances of H in the k th generation
f(x) denotes fitness value of x
f(H,k) denotes average fitness of H in the k-th generation
38
Assumption: fitness proportional selection
Selection probability for the individual x
(where the N is the total number of
individuals)
Probability that a string of schema H gets selected for mating?
∑x∈H
p(h ∈ H) =i=
∑ f (x i
f(x) N1
)
Number of strings of schema H that get selected for mating?
∑x∈H f(x)
∑i=f(fx()x N = f
∑x∈H
i 39
Net Effect of Selection
40
Assumption: single-point crossover
Schema H survives crossover operation if
o one of the parents is an instance of the schema H and
o one of the offspring is an instance of the schema H
Given a string with length l and a schema H with the defining length
δ(H), the probability that the crossover occurs within the bits of the
defining length is δ(H)/(l - 1) 122
Suppose H = *1**0
Values of l & δ(H) ?
l = 5 & δ(H) = 5 – 2 = 3
The probability that the crossover occurs within the defining length?
¾
The upper bound of the probability that the schema H being
destroyed is (where pc is the crossover probability)
S m (H) = (1 – pm )o(H)
Schemas with low order are more likely to survive.
Since p m << 1, this probability can be approximated by:
p S M (S) 1 – p m ·o(H)
43
Mathematically
The schema theorem states that the schema with above average
fitness, short defining length and lower order is more likely to
survive
44
Example 1: For the following population, find the survival chances of the
scheme 1****
No. String f(x)
1. 11000 2
2. 10111 4
3. 01011 3
4. 00100 1
m(1****, 0) = 2
f(1****, 0) = (2 + 4)/2 =
3 (2 + 4 + 3 + 1)/4 =
f
2.5
Survival after mutation = 1 – pm o(H) =
1 – 0.2 x 1 = 0.8
Survival after crossover =
1 – 1. (0 / (5 – 1)) =
So,
1
E[m(1****, 1)] ≥ 2 . (3/2.5) .1 . 0.8 = 1.92
45
For f = x 2 , 0 ≤ x ≤ 127, what is the average
fitness of 1#######, O###### and
11#####?
(127)10 = (01111111)2
So, m(1#######, 0) = 0
Average fitness = 0
Same is the case with 11#####
Average fitness = 0
m(0#######, 0) =
128
Average fitness =
(1 + 22 + 32 + … + 1272) /
127
46
1. A genetic algorithm is to be used to evolve a binary string of length n containing
only 1s. The initial population is a randomly generated set of binary strings of
length n.
a) Give a suitable fitness function for this problem.
b) Will the offspring of parents with a high fitness value generally also have a
high fitness value, given your fitness function? Explain your answer.
47