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

Genetic Algorithm

Order crossover operator is used to preserve the order of genes in permutation problems like TSP. Some key points: - It selects two parent chromosomes and a random crossover point - Genes before the crossover point are copied directly from the first parent - Remaining genes are copied from the second parent in the order they appear, filling the gaps in the first offspring - The second offspring gets genes in reverse order to preserve diversity This ensures the relative ordering of genes is preserved after crossover, avoiding infeasible solutions like those from single/uniform crossover.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views

Genetic Algorithm

Order crossover operator is used to preserve the order of genes in permutation problems like TSP. Some key points: - It selects two parent chromosomes and a random crossover point - Genes before the crossover point are copied directly from the first parent - Remaining genes are copied from the second parent in the order they appear, filling the gaps in the first offspring - The second offspring gets genes in reverse order to preserve diversity This ensures the relative ordering of genes is preserved after crossover, avoiding infeasible solutions like those from single/uniform crossover.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 47

| | Shri Hari | |

• A class of probabilistic optimization


algorithms
• Inspired by the biological evolution process
• Uses concepts of “Natural Selection” and “Genetic Inheritance” (Darwin
1859)
• Originally developed by John Holland (1975)
• Particularly well suited for hard problems where little is known about
the underlying search space
• Widely-used in business, science and engineering

Genetic Algorithms : in Search, Optimization & Machine


Learning
David E . Goldberg
 Organism has a set of rules (a blueprint) defining how it is built up
from the tiny building blocks of life.
 Rules are encoded in the genes, which are connected together into
long strings called chromosomes.
 Each gene represents a specific trait of the organism and has
several different settings.
 Genes and their settings are usually referred to as an organism's
genotype. The physical expression of the genotype(the organism
itself ) is called the phenotype. (smart, beautiful, healthy, etc.)
 When two organisms mate, their resultant offspring ends up
having shared genes - recombination.
 Occasionally a gene may be mutated.

 Life on earth has evolved to be as it is through the processes of


natural selection, recombination and mutation.
April 10,
83
🞉 Genetic algorithms (GAs) are search methods based on principles of
natural selection and genetics.
🞉 GAs encode the decision variables of a search problem into finite-length
strings of alphabets.
🞉 The strings which are candidate solutions to the search problem are
referred to as chromosomes, the alphabets are referred to as genes.
The values of genes are called alleles
o E.g., in a problem such as the traveling salesman problem, a
chromosome represents a route, and a gene may represent a city.
🞉 To evolve good solutions and to implement natural selection, we need
a
measure for distinguishing good solutions from bad solutions.
o The fitness measure must determine a candidate solution‟s
relative fitness, which will subsequently be used by the G A to guide
the evolution of good solutions.
🞉 Genetic algorithms rely on a population of candidate solutions. The
population size, which is usually a user-specified parameter, is one of
the
genetic
important
algorithms.
factors affecting the scalability and performance of 84
1. Initialization.
The initial population of candidate solutions is usually generated
randomly across the search space. However, domain-specific knowledge
or other information can be easily incorporated.
2. Evaluation.
Once the population is initialized or an offspring population is created,
the fitness values of the candidate solutions are evaluated.
3. Selection.
The main idea of selection is to prefer better solutions to worse ones.
It thus imposes the survival-of-the-fittest mechanism on the candidate
solutions.

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

1. Evaluate the fitness, fi , of each individual in the population.


2. Compute the probability (slot size), pi , of selecting each member
of the population: p i = fi/j=1 to n f j , where n is the population size.
3. Calculate the cumulative probability, qi , for each individual:
qi = j=1 to i p j
4. Generate a uniform random number, r ∈ (0, 1].
5. If r < q1 then select the first chromosome, x 1 , else select the
individual x i such that qi−1 < r ≤ qi .
6. Repeat steps 4 – 5 n times to create n candidates in the
mating
pool.
9
Example: Consider a population with five individuals (n = 5). Using
Roulette Wheel Selection Scheme produce a new population of 5
chromosomes. Use the following random numbers – 0.585, 0.811, 0.199, 0.
333, 0. 277.
Chromosome
# 1 2 3 4 5

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

Random number = 0.585, then the third chromosome is selected as


q2 = 0.484 < 0.585 ≤ q3 = 0.631.
So, the new population is: 3 5 1 2 1

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.

Tournament Selection with


more than 2 selections
11
🞉 After selection, individuals from the mating pool are recombined (or
crossed over) to create new, hopefully better, offspring.
🞉 Many of the recombination operators used are problem-specific.
🞉 In most recombination operators, two individuals are
randomly selected and are recombined with a probability pc ,
called the crossover probability.
🞉 A uniform random number, r, is generated and if r ≤ pc , the two
randomly selected individuals undergo recombination. Otherwise, if
r > pc , the two offspring are simply copies of their parents.
🞉 The value of pc can either be set experimentally, or can be set based
on
schema-theorem principles
12
o crossover site(s) is (are)
selected at random over the
string length

o Assign 'heads' to one


parent,
'tails' to the other
o Flip a coin for each gene of
the first child
o Make an inverse copy of
the
gene for the second child
o Inheritance is independent
of position.

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 A random binary template is generated

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

Initial City Common Edge 100


Step 3 Step 4

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

Random Choice; both have


Only Item in List two items in the list
101
Step 5 Step 6
1 2 3 1 3
2 -3 2 -3
3 -2 3
4 3 4 3
5 2 5
6 6
4 6 5 1 2 4 6 5 1 2 3
Random Choice; both have one item in the list L a s t Element

Parent 1 4 1 3 2 5 6 4 6 5 1 2 3

Parent 2 4 3 2 1 5 6 Two parents, one child


102
Use the Enhanced Edge Recombination operator to find the
offspring, given the following parents.
[1 2 3 4 5 6 7 8 9] and [9 3 7 8 2 6 5 1 4]

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:

Integer Binary c ode Integer Binary code Integer Binary c o de


1 0 0 0 1 6 0 1 1 0 11 1 0 1 1
2 0 0 1 0 7 0 1 1 1 12 1 1 0 0
3 0 0 1 1 8 1 0 0 0 13 1 1 0 1
4 0 1 0 0 9 1 0 0 1 14 1 1 1 0
5 0 1 0 1 10 1 0 1 0 15 1 1 1 1

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

s 5 ` = 0100010011 s 6 ` = 1110111101 s 5 `` = 0100011101 s 6 `` = 1110110011


The final step is to apply random mutation: for each bit that we are to copy
to the new population we allow a small probability of error (for instance 0.1)
Before applying mutation: After applying mutation:
s 1 `` = 1110110101 s 1 ``` = 1110100101 f (s1 ``` ) = 6
s 2 `` = 1111010101 s 2 ``` = 1111110100 f (s2 ``` ) = 7
s 3 `` = 1110111101 s 3 ``` = 1110101111 f (s3 ``` ) = 8
s 4 `` = 0111000101 s 4 ``` = 0111000101 f (s4 ``` ) = 5
s 5 `` = 0100011101 s 5 ``` = 0100011101 f (s5 ``` ) = 5
s 6 `` = 1110110011 s 6 ``` = 1110110001 f (s6 ``` ) = 6
30
🞉 To describe similarity among individuals in the population we use the
concept of schema (schemata)
🞉 What is a schema?
 Similarity template describing a subset of strings with similarities
at certain positions
 String over the alphabet {0,1,*}
 * is the don‟t care symbol; it is a meta symbol & is never
explicitly processed by the genetic algorithm

Schema ***11 matches, Schema 0***1 matches,


§ 00011 § 00001
§ 00111 § 00011
§ 01011 § 00101
§… §…
§ 11111 § 01111
31
 Schema theorem serves as the analysis tool for the G A process
 Explains why GAs work by showing the expectation of schema
survival
 Applicable to a canonical G A
 binary representation
 fixed length individuals
 fitness proportional selection
 single point crossover
 gene-wise mutation

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?

There are 2 l-o(S) different strings that match S

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

Example:  (01*1*) = 4 – 1 = 3,  (**1*1010) = 8 – 3 = 5


δ(* * * 0 * * *) = ?

Defining the length of a schema is useful to calculate survival


probability of the schema for crossovers.
2l
Number of strings of length l?

Possible subsets of strings?


Can every possible subset of the set of length−l No! Why?
bit strings be described as a schema?

How many schemas are possible? 3l {0,1,*}

So, huge majority of subsets cannot be represented by schemas 115


Any given bit string of length l is an instance of how 2l
many different schemas?

Eg The string „10‟ of length l = 2 belongs to 2l = 22 different schemas


o ** (all four possible bit strings of length 2),
o *0,
o 1*, and
o 10 (the schema that contains only one string, 10).

Any given population of n strings contains between 2l and n × 2 l


how many instances of schemas? different schemas

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 | 0x 
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

Effect of Selection + Effect of Crossover + Effect of Mutation


= Schema Theorem

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

M(H,k) is the expected number of instances of H in the mating pool

Schemas with fitness greater than the population average are


likely to appear more in the next generation.

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

 Suppose a parent is an instance of a schema H . When the crossover is


occurred within the bits of the defining length, it is destroyed unless the
other parent repairs the destroyed portion

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

 The lower bound on the probability S c (H) that H survives is

 Schemas with low order are more likely to survive.


42
 Assumption: mutation is applied gene by gene
 For a schema H to survive, all fixed bits must remain unchanged
 Probability of a gene not being changed is (1 – p m )
where pm is the mutation probability of a gene
 The probability a schema H survives under mutation

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

You might also like