07 Fundamentals of Genetic Algorithms
07 Fundamentals of Genetic Algorithms
1. In tro duction
Solving problems mean looking for solutions, which is best among others.
possible solutions. The set of possible solutions defines the search space
(also called state space) for a given problem. Solutions or partial solutions
are viewed as points in the search space.
04
SC – GA - I ntroduction
• Why G e n e tic A lgorithm s ?
- unlike older AI systems, the GA's do not break easily even if the
inputs changed slightly, or in the presence of reasonable noise.
05
SC – GA - I ntroduction
1.1 O ptimization
Examples:
1. In manufacturing, we want to maximize the profit or minimize
the cost .
2. In designing an automobile panel, we want to maximize the
strength.
■ A set of unkn ow n s or variables : that affect the objective function,
Examples:
be non-negative.
2 In the panel design, we want to limit the weight and put
the constraints.
06
SC – GA - I ntroduction
• O p tim iza tio n M ethods
Many optimization methods exist and categorized as shown below.
low cost,
high performance,
low loss
These characteristics necessarily obtainable, and requires
are not
knowledge about the
problem.
Op tim iz a tion
M eth od s
functions,
08
SC – GA - I ntroduction
■ Non- Linea r Pro gram m i n g
Indirect methods.
which means
Evolutionary Strategies (ES),
The two other search methodologies, shown below, the Classical and
the Enumerative methods, are first briefly explained. Later the Stochastic
methods are discussed in detail. All these methods belong to Non-
Linear search.
Search
O ptimization
◊ Indirect methods :
does search for local extremes by solving usually non-linear
set of equations resulting from setting the gradient of the
11
SC – GA - I ntroduction
• Enu m e rativ e Search
Here the search goes through every point (one point at a time) related
to the function's domain space.
At each point, all possible solutions are generated and tested to find
optimum solution.
It is easy to implement but usually require significant computation.
Thus these techniques are not suitable for applications with large
domain spaces.
In the field of artificial intelligence, the enumerative methods are
subdivided into two categories : Uninformed and Informed methods.
◊ Informed methods :
example: Alpha-Beta and A* ,
12
SC – GA - I ntroduction
[Ref : previous slide Enumerative search]
there are many control structures for search; the depth-first search
and breadth-first search are two common search strategies.
the taxonomy of
search algorithms in AI domain is given below.
En u merative Search
G (State, Operator, Cost)
13
SC – GA - I ntroduction
• S tocha stic Search
Here the search methods, include heuristics and an element of
randomness (non-determinism) in traversing the search space. Unlike
the previous two search methodologies
the stochastic search algorithm moves from one point to another in
the search space in a non-deterministic manner, guided by heuristics.
the stochastic search techniques are usually called Guided random
search techniques.
The stochastic search techniques are grouped into two major subclasses :
Simulated annealing and
Evolutionary algorithms.
14
SC – GA - I ntroduction
• Taxo nom y of Search Optim izatio n
Fig. below shows different types of Search Optimization algorithms.
Search
O p tim iza tio n
Indirect Direct
method Uninformed In formed
method
Search Search
Newton Finonacci
Genetic Genetic
P r ogram m in g A lgo ri th m s
15
SC – GA - I ntroduction
1.3 Evolu tionary A lgorithm (EA s)
D e velo pm en t H isto ry
EC = GP + ES + EP + GA
Evolutionary Genetic Evolution Evolutionary Genetic
Computing P rogramming Strategies P rogramming A lgorithm s
16
SC – GA - I ntroduction
1.4 G enetic Algorithm s (G A s) - Basic Con cepts
problems.
GAs, although randomized, exploit historical information to direct the
search into the region of better performance within the search space.
17
SC – GA - I ntroduction
• B io lo gical B ackgroun d – Basic Genetics
‡
Every organism has a set of rules, describing how that organism
is built. All living organisms consist of cel l s .
‡
In each cell there is same set of chrom oso m e s. Chromosomes are
strings of DNA and serve as a model for the whole organism.
‡ A chromosome consists of genes, blocks of DNA.
‡ Possible settings for a trait (e.g. blue, brown) are called all el es .
‡
Each gene has its own position in the chromosome called its lo cu s .
‡
Complete set of genetic material (all chromosomes) is called a
g en om e .
‡ Particular set of genes in a genome is called g en otype .
‡
The physical expression of the genotype (the organism itself after
birth) is called the ph en otyp e , its physical and mental characteristics,
such as eye color, intelligence etc.
‡
When two organisms mate they share their genes; the resultant
offspring may end up having half the genes from one parent and half
from the other. This process is called recom bin ation (cross over) .
‡
The new created offspring can then be mutated. Mu tation means,
that the elements of DNA are a bit changed. This changes are mainly
caused by errors in copying genes from parents.
‡
The fitness of an organism is measured by success of the organism
in its life (survival).
18
SC – GA - I ntroduction
[ continued from previous slide - Biological background ]
P aren ts
P a rents
I n itia liz atio n
R ecom bin ati on
P o pulation
M u tation
O ffspring
Survivo r
P seu d o-Cod e
BEGIN
1. SELECT parents;
2. RECOMBINE pairs of parents;
3. MUTATE the resulting offspring;
4. SELECT individuals or the next generation;
END.
19
SC – GA - I ntroduction
• S ea rch Sp ace
In solving problems, some solution will be the best among others. The
space of all feasible solutions (among which the desired solution
resides) is called search space (also called state space).
Each point in the search space represents one possible solu tio n .
Each possible solution can be "marked" by its value (or fitn ess ) for
the problem.
The GA looks for the best solu tio n among a number of possible
solutions represented by one point in the search space.
Looking for a solution is then equal to looking for some extreme value
20
SC – GA - I ntroduction
• W o rk in g P rin cip les
Before getting into GAs, it is necessary to explain few terms.
It is problem specific.
W orkin g principle s :
Genetic algorithm begins with a set of solutions (represented by
chromosomes) called the population.
Solutions from one population are taken and used to form a new
population. This is motivated by the possibility that the new population
21
SC – GA - I ntroduction
• O utlin e of the Basic G enetic A lgorith m
1.
[Start] Generate random population of n chromosomes (i.e. suitable
solutions for the problem).
2.
[Fitness] Evaluate the fitness f(x) of each chromosome x in the
population.
a new population by repeating following
3.
[New population] Create
steps until the new population is complete.
population
(a) [Selection] Select two parent chromosomes from a
according to their fitness (better the fitness, bigger the chance to
be selected)
(b) [Crossover] With a crossover probability, cross over the parents
to form new offspring (children). If no crossover was performed,
5.
[Test] If the end condition is satisfied, stop, and return the best
solution in current population
6.
[Loop] Go to step 2
Start
No
R e pro du ctio n Use crossover operator
R eco m bin a tion to produce off- springs Cro s so ver
Y es
S u rv iv al of F itte st
N atu ral Select one o ff-spring
Apply replacement Y es No
operator to incorporate Selectio n
new individual into
population Apply Mutation operator
M u tatio n to produce Mutated
offspring
No
Term in ate ?
M utatio n Scoring : a ssign
Y es F in ishe d? fitn e ss to off- spring
Fin ish
of 1's and 0's, where the digit at each position represents the value of
Exa m ple :
A Gene represents some data (eye color, hair color, sight, etc.).
A chromosome is an array of genes. In binary form
a Gene looks like : ( 11 10 001 0)
There are many other ways of encoding, e.g., encoding values as integer or
24
SC – GA - Encoding
• B in a ry Encoding
This encoding is often not natural for many problems and sometimes
Exam ple 1:
0 0 00 0 6 0 11 0 12 1 10 0
1 0 0 0 1 7 0 1 1 1 13 1 1 0 1
2 0 01 0 8 1 00 0 14 1 11 0
3 0 0 1 1 9 1 0 0 1 15 1 1 1 1
4 0 1 0 0 10 1 0 1 0
5 0 1 0 1 11 1 0 1 1
25
SC – GA - Encoding
[ continued binary encoding ]
Exam ple 2 :
Two variable function represented by 4 bit string for each variable.
Let two variables X1 , X2 as (1011 0110) .
L U
Every variable will have both upper and lower limits as X i
Xi Xi
Sn-1 . . . . S3 S2 S1 S0
4-bit string Xi = (1010) would represent. First get decoded value for
Si = 1010 = 23 x 1 + 22 x 0 + 21 x 1 + 20 x 0 = 10 then
(17 -2)
Xi = 2 + ----------- x 10 = 12
(24 - 1)
The Value encoding can be used in problems where values such as real
numbers are used. Use of binary encoding for this type of problems
would be difficult.
27
SC – GA - Encoding
• P e rm u tatio n Enco din g
Permutation encoding can be used in ordering problems, such as traveling
salesman problem or task ordering problem.
1. In permutation
encoding, every chromosome is a string of numbers
that represent
a position in a sequence.
Chrom oso m e A 1 5 3 2 6 4 7 9 8
Chrom oso m e B 8 5 6 7 2 3 1 4 9
Exampl es :
28
SC – GA - Encoding
• Tree Encoding
+ do untill
x /
step w a ll
5 y
( + x ( / 5 y ) ) ( do u n til step w a l l )
M u tatio n .
Population size says how many chromosomes are in population (in one
generation).
If there are only few chromosomes, then GA would have a few possibilities
to perform crossover and only a small part of search space is explored.
Research shows that after some limit, it is not useful to increase population
size, because it does not help in solving the problem faster. The population
size depends on the type of encoding and the problem.
30
SC – GA - Operators
1. R epro duction, or Selectio n
performed.
Fitness function quantifies the optimality of a solution (chromosome) so
that a particular solution may be ranked against all the other solutions.
The function depicts the closeness of a given ‘solution’ to the desired
result.
Many reproduction operators exists and they all essentially do same thing.
They pick from current population the strings of above average and insert
their multiple copies in the mating pool in a probabilistic manner.
Tournament selection,
The Roulette wheel and Boltzmann selections methods are illustrated next.
31
SC – GA - Operators
• Exa m ple of Selection
32
SC – GA - Operators
• R o u le tte w h e e l se lectio n (Fitness-Proportionate Selection)
In fitness-proportionate selection :
the chance of an individual's being selected is proportional to its
fitness, greater or less than its competitors' fitness.
33
SC – GA - Operators
• B oltzm a n n Selection
Simulated annealing is a method used to minimize or maximize a function.
the others are Two Point, U niform , Arith m e tic, and H e u ristic cro sso v e rs .
The operators are selected based on the way chromosomes are encoded.
35
SC – GA - Operators
• O ne- Po in t Crossover
Parent 1 1 10 1 1 | 0 0 1 0 01 1 01 1 0
Parent 2 1 10 1 1 | 1 1 0 0 00 1 11 1 0
Offspring 1 1 1 01 1 | 1 1 0 0 00 1 1 1 1 0
Offspring 2 1 1 01 1 | 0 0 1 0 0 1 10 1 1 0
36
SC – GA - Operators
• T w o -P o in t Crossover
Parent 1 110 1 1 | 0 0 10 0 1 1 | 01 1 0
Parent 2 110 1 1 | 1 1 00 0 0 1 | 11 1 0
Offspring 1 1 1 01 1 | 0 0 1 0 01 1 | 0 1 1 0
Offspring 2 1 1 01 1 | 0 0 1 0 01 1 | 0 1 1 0
37
SC – GA - Operators
• U n ifo rm Crossover
If the mixing ratio is 0.5 approximately, then half of the genes in the
offspring will come from parent 1 and other half will come from parent 2.
The possible set of offspring after uniform crossover would be:
11 12 02 11 11 12 12 02 01 01 02 11 12 11 11 02
Offspring 1
12 11 01 12 12 01 01 11 02 02 11 12 01 12 12 01
Offspring 2
Note: The subscripts indicate which parent the gene came from.
38
SC – GA - Operators
• A rith m e tic
O ffspring1 = a * P a re n t1 + (1- a) * P a re n t2
O ffsprin g2 = (1 – a) * P are n t1 + a * P a re n t2
39
SC – GA - Operators
• H euristic
Heuristic crossover operator uses the fitness values of the two parent
chromosomes to determine the direction of the search.
40
SC – GA - Operators
3. M utation
Mutation alters one or more gene values in a chromosome from its initial
state. This can result in entirely new gene values being added to the
gene pool. With the new gene values, the genetic algorithm may be able
to arrive at better solution than was previously possible.
41
SC – GA - Operators
• Flip B it
The mutation operator simply inverts the value of the chosen gene.
i.e. 0 goes to 1 and 1 goes to 0 .
This mutation operator can only be used for binary genes.
Original offspring 1 1 1 0 1 1 1 1 0 0 0 0 1 1 1 1 0
Original offspring 2 1 1 0 1 1 0 0 1 0 0 1 1 0 1 1 0
0 to 1 and 1 to 0
Invert the value of the chosen gene as
42
SC – GA - Operators
• B oun dary
The mutation operator replaces the value of the chosen gene with either
the upper or lower bound for that gene (chosen randomly).
This mutation operator can only be used for integer and float genes.
• N on-U niform
The mutation operator increases the probability such that the amount of
the mutation will be close to 0 as the generation number increases. This
mutation operator prevents the population from stagnating in the early
stages of the evolution then allows the genetic algorithm to fine tune the
solution in the later stages of evolution.
This mutation operator can only be used for integer and float genes.
• U niform
The mutation operator replaces the value of the chosen gene with a
uniform random value selected between the user-specified upper and
lower bounds for that gene.
This mutation operator can only be used for integer and float genes.
• Gaussia n
The mutation operator adds a unit Gaussian distributed random value to
the chosen gene. The new gene value is clipped if it falls outside of the
user-specified lower or upper bounds for that gene.
This mutation operator can only be used for integer and float genes.
43
SC – GA - Examples
4. Basic Genetic A lgorithm :
• E x a m p le 1 :
Maximize the function f(x) = x2 over the range of integers from 0 . . . 31 .
One way is to :
Start from any integer x in the domain of f
Repeat until x = 3 1
function f(x) is simple, so it is easy to use the f(x) value itself to rate the
fitness of a solution; else we might have considered a more simpler
heuristic that would more or less serve the same purpose.
3. Coding - Binary and the String length :
By generating 4 uniform (0, 1) random values and seeing which bin they fall
into we pick the four strings that will form the basis for the next generation.
Random No Falls into bin Chosen string
Random number generator decides for us to mate the first two strings
together and the second two strings together.
8.
Within each pair swap parts of the members solutions to create
offspring which are a mixture of the parents :
For the first pair of strings: 0 1 1 0 1 , 1 1 0 00
We randomly select the crossover point to be after the fourth digit.
Crossing these two strings at that point yields:
01 1 0 1 ⇒ 0 1 1 0 |1 ⇒ 0 1 1 0 0
1 1 00 0 ⇒ 1 1 0 0 |0 ⇒ 1 1 0 0 1
1 0 0 1 1 ⇒ 1 0 |0 1 1 ⇒ 1 0 0 0 0
46
SC – GA - Examples
9. Randomly mutate a very small fraction of genes in the population :
With a typical mutation probability of per bit it happens that none of the bits
in our population are mutated.
Observe that :
1. Initial populations : At start step 5 were
0 1 1 0 1 , 1 1 0 0 0 , 0 1 0 0 0 , 1 0 0 1 1
After one cycle, new populations, at step 10 to act as initial population
0 1 1 0 0 , 1 1 0 0 1 , 1 1 0 11 , 1 0 0 0 0
2. The total fitness has gone from 1170 to 1754 in a single generation.
3. The algorithm has already come up with the string 11011 (i.e x = 27) as
a possible solution.
47
SC – GA - Examples
• Exa m ple 2 : Two bar pendu lu m
Hence, the binary coding and the corresponding angles Xi are given as
XiU - X i L th
Xi = Xi L + ----------- Si where Si is decoded Value of the i chromosome.
24 - 1
e.g. the 6th chromosome binary code (0 1 0 1) would have the corresponding
angle given by Si = 0 1 0 1 = 23 x 0 + 22 x 1 + 21 x 0 + 20 x 1 = 5
90 - 0
Xi = 0 + ----------- x 5 = 30
15
The binary coding and the angles are given in the table below.
S. No. Binary code Angle S. No. Binary code Angle
Si Xi Si Xi
1 0 0 0 0 0 9 1 0 0 0 48
2 0 0 0 1 6 10 1 0 0 1 54
3 0 0 1 0 12 11 1 0 1 0 60
4 0 0 1 1 18 12 1 0 1 1 66
5 0 1 0 0 24 13 1 1 0 0 72
6 0 1 0 1 30 14 1 1 0 1 78
7 0 1 1 0 36 15 1 1 1 0 84
8 0 1 1 1 42 16 1 1 1 1 90
Since the objective function is –ve , instead of minimizing the function f let us
maximize -f = f ’ . The maximum value of f ’ = 8 when 1 and 2 are zero.
48
SC – GA - Examples
[ continued from previous slide ]
First randomly generate 8 population with 8 bit strings as shown in table below.
These angles and the corresponding to fitness function are shown below.
1 =0 1 =12 1 =6 1 =12
2 =0 2 =6 2 =30 2 =48
5. "Practical genetic algorithms", by Randy L. Haupt, (2004), John Wiley & Sons
Inc, Chapter 1- 5, page 1- 127.
50