Genetic Algorithms
Genetic Algorithms
Introduction
Real world optimization problems are complex,
have multiple conflicting objectives, etc.
Large search or design space
Difficult to obtain global optimum in a reasonable
time.
Evolutionary algorithms (EA) can be used.
Genetic Algorithms (GA) make use of EA
approach.
GA uses principle of Darwinian evolution theory i.e.,
survival of the fittest.
GA operates mainly on a population search basis.
Contd
Starts from a population of possible solutions
(called individuals or chromosome) and move
towards the optimal one.
Phenotype: Objects forming possible solution sets
to the original problem.
Genotype: The encoding (representation) of the
individuals.
In GA, the variables are represented as strings of
numbers (normally binary).
Contd
If there are 'n' variables and encoding provides a
string of length 'l' to each variable, then each
solution vector will have a total string length of 'nl'.
Example:
Number of variables = 3 (x1, x2, and x3)
Encoding provides the string length of 4 for each variable.
Then the length of each chromosome is 12.
If x1 = 7, x2 = 4, and x3 = 1, then chromosome will be
0 1 1 1 0 1 0 0 0 0 0 1
x1 x2 x3
Contd
Each chromosome has an associated value known
as fitness (i.e. the quality).
Derived using a fitness function.
Forms the basis of the selection process.
GAs update population iteratively.
By evaluating chromosomes using a fitness function and
keeping only the fitter ones in a new generation.
Some of these individuals are admitted unchanged.
Others are subjected to genetic operators such as
crossover and mutation to create new offspring.
n is the number of
Pseudocode individuals in the population;
is the fraction of the
Algorithm: GA(n, , ) population to be replaced by
1. // Initialise generation 0: crossover in each iteration;
2. k := 0; and is the mutation rate.
3. Pk := a population of n randomly-generated individuals;
4. // Evaluate Pk:
5. Compute fitness(i) for each i Pk;
6. do
7. { // Create generation k + 1:
8. // 1. Copy: (elitist selection) Elitism
9. Select (1 ) n members of Pk and insert
into Pk+1;
Contd
10. // 2. Crossover:
11. Select n members of Pk; pair them up;
produce offspring; insert the offspring into Pk+1;
12. // 3. Mutate:
13. Select n members of Pk+1; invert a
randomly-selected bit in each;
14. // Evaluate Pk+1:
15. Compute fitness(i) for each i Pk+1;
16. // Increment:
17. k := k + 1;
18. }
19. while fitness of fittest individual in Pk is not high enough;
20. return the fittest individual from Pk;
Representation (Encoding)
One of the biggest challenge in GA.
Assigning a letter (or a number) to each of the
simplest element in a solution.
As number of elements in the solution is finite, and
hence each is represented by a string of finite
length (known as a chromosome).
The easiest representation is a bit representation {0,
1} as it simplifies crossover and mutation.
However, depending upon a problem other
representations can also be determined and used.
Example
Find optimal quantity of three ingredients in a recipe.
Use integers {1, 2, 3, ..., 9} denoting the number of table spoons of
each ingredient.
Some possible solutions are 1-1-1, 2-1-4, 3-3-1, etc.
The traveling salesperson problem to traverse 4 cities.
Use integers or alphabets to represent cities.
A solution is any permutation of cities.
Amritsar (A or 1)
Amritsar (A or 1)
Ludhiana (L or 3)
Ludhiana (L or 3)
Jalandhar (J or 2)
Patiala (P or 4) Jalandhar (J or 2)
Patiala (P or 4)
1-4-2-3 (A P J L) 1-2-3-4 (A J L P)
Contd Color Shape Size Object
Red (100) Round (10) Small (10) Apple (1000)
Rule-based Green (010) Square (01) Big (01) Orange (0100)
system. Blue (001) Banana (0010)
Pear (0001)
Each rule is a 11-bit string.
If color = Red, shape = Round, and size = Small then object =
Apple. (100 10 10 1000)
Timetabling Number Faculty Room Time
Chromosomes consists of various 1 Prof. XYZ E202 8:00
triplets '123' means 'Prof. XYZ 2 Mr. ABC F103 9:00
teaches at 10:00 in F103'. 3 PQR G301 10:00
Faculty Room Time
Chromosomes
Contd
A representation for the fire-station location
problem.
fitness ( j )
j 1
Randomly select m
Keep the fittest one
chromosomes
NO Population
filled?
1 0 0 1 0 1
Mutation
Mutation helps to add diversity to the population. It
works as a random experimentation.
Mutation can help the population to avoid local
maximum.
Example:
1. 10111011 2. 11101001000
10111111 11001011000
Example
Consider a GA with chromosomes consisting of six genes xi =
abcdef, and each gene is a number between 0 and 9. Let there be
four chromosomes in the population:
x1 = 4 3 5 2 1 6 x2 = 1 7 3 9 6 5
x3 = 2 4 8 0 1 2 x4 = 9 0 8 1 2 3
f(O1) = f(9 0 8 0 1 2) = (9 + 8 + 1) (0 + 0 + 2) = 16
f(O2) = f(2 4 8 1 2 3) = (2 + 8 + 2) (4 + 1 + 3) = 4
f(O3) = f(2 4 5 2 1 2) = (2 + 5 + 1) (4 + 2 + 2) = 0
f(O4) = f(4 3 8 0 1 6) = (4 + 8 + 1) (3 + 0 + 6) = 4
Summary
GAs are easy to apply to a wide range of problems.
Optimization problems like the traveling
salesperson problem, to inductive concept
learning, scheduling, and layout problems.
Results can be very good on some problems, and
rather poor on others.
Only mutation slows down the algorithm.
Crossover makes the algorithm significantly faster.
0/1 Knapsack