Genetic algorithms
Introduction
• Based on mechanics of natural genetics & natural
selection
• GA’s are difficult from classical optimization methods
– Deterministic tendency is replaced by probabilistic tendency to
evolve the solution
• One could get global solution using GA
– deterministic technique can give only for strictly convex or
concave problems
• First thing to learn is the working principle of GA
Working principle
Problem statement:
Maximize f(x)
such that
x i xi x i
L U
Here xi is a vector for variables i numbered 1, 2, 3……….N
The first step in GA is to encode the continuous variable xi into binary
string. This process is called coding
Working principle
Coding:
Binary coded strings having 1’s and 0’s are used
The length of the string is usually determined according to
the desired solution accuracy
Illustration: If Four bits are used to code each variable in a
two variable problem, strings (0000, 0000) and (1111, 1111)
would represent the points
L L U U
[ x1 , x2 ]T [ x1 , x2 ]T
Why ? Because (0000) and (1111) represent minimum and
maximum decoded values
Working principle
Conversion from string to real value:
xi xi
U L
xi xi Li
L
[ Decoded value( si )]
2 1
li 1
Decoded value (s i ) 2i si
i0
where s i [0 1]
string s represente d by (s l-1s l-2 ........s2 s1s0 )
First parameter (1100100000) decodes to 800. Corresponding
parameter value is 0 + (6-0)*800/1023 = 4.692
Second parameter (1110010000) decodes to 912. Corresponding
parameter value is 0+ (6-0)*912/1023 = 5.349
Working principle
Conversion from string to real value: Example
Substring s i 0111 :
Decoded value (s i ) [(1)2 0 (1)21 (1)2 2 (0)23 ]
7
Remarks
Not all variables in the problem need same string length
String length for a particular variable depends on the level of accuracy
desired in the solution
[ xi xi ]
U L
Accuracy obtained in li bit string is given as
2 li
Working principle
Coding: Summary
Each variable is defined with a discrete set of binary set of sub-strings.
These sub-strings can be called as population of sub-strings
Each sub-string corresponds to a precision value of the corresponding actual
value of the variable
The length of the sub-string defines the accuracy in the solution
All we are saying here is that we are discretizing all the vector in which
solution vector is embedded
The string is therefore, a combination of different sub-strings. It is the string
that we will try to manipulate in future with different genetic operators.
The length of sub-string is dependent on the accuracy required in the variable
that has to be optimized.
Working principle
Fitness function
GA principle: Survival of the fitttest
Therefore, we would maximize a function only in GA never minimize it
The function that has to be maximized is called as Fitness function F(x)
If f(x) is to be minimized, then F(x) is developed from f(x) as
1
F ( x)
1 f ( x)
Fitness value of a string is called string’s fitness
Working principle
Genetic algorithm: Summary
Generate Initial Population
Fitness Function
Evaluate Fitness
Yes
Termination Condition? Best Individual
No
Select Parents
Crossover, Mutation
Generate New Offspring
Working principle
Genetic algorithm: Explanation
String manipulation is done using three operators
Reproduction : Rejects many worst strings and selects few
strings whose fitness is well above average
Cross over: Select a couple of parents strings and perform
changes by crossing over to get two offspring strings
When reproduction is done between two intelligent genes,
the probability to produce an intelligent gene is high
Mutation: Select a particular string and randomly perform
changes for one or more characters in the string
Change 0 to 1 or 1 to 0 in a random fashion for one of the
character.
Working principle
Genetic algorithm: Terminology
Population: The number of strings available at any time that could
be subject to genetic operation
Termination criteria: Based on
Maximum number of generations (one combination of three
operators applied to population)
No improvement in the fitness value after a fixed number of
iterations
Reproduction allows move randomly out of the neighbourhood.
Mutation allows refining the location of the string in the
neighbourhood.
Working principle
Genetic operator: Reproduction
Also known as selection operator
Selects good strings in the population and forms a mating pool
Above average strings are picked from the current population
Multiple copies are inserted in the mating pool in a probabilistic
manner
Proportionate reduction operator:
String selection based on the probability evaluated from fitness
Probability for selecting ith string is given as
F
p n
i
F
i
i
i 1
n is the population size
Working principle
Genetic operator: Reproduction
In order to choose n strings, n random numbers between 0 to 1 are
created random
The cumulative probability is calculated based on the individual string
probabilities
A string that represents chosen random number in the cumulative
probability range (calculated from fitness values) is copied to the mating
pool
String with higher fitness represents larger range in the cumulative
probability values.
String with lower fitness represents smaller range in the cumulative
probability values.
Good strings are probabilistically assigned a large number of copies
Working principle
Genetic operator: Crossover
During reproduction, no new strings are generated
During crossover, new strings are created
By exchanging information among strings in the mating pool
General crossover mechanism
Two strings are picked up from the mating pool
Some portions of the strings are exchanged between the strings
Single point cross over
Randomly choose a crossing site
Exchange all the bits on the right side of the crossing site
Working principle
Genetic operator: Crossover
Example
00000 0 0 111
11111 1 1 000
Parent Children
strings strings
Idea: Good parent strings are combined to form a better child string if
an appropriate site is chosen
But good children strings may or may not be produced.
We do not worry much about this, because bad children strings
would be discarded in the future mating pool based on their fitness.
Working principle
Genetic operator: Mutation
Mutation operator changes 0 to 1 and vice versa with a probability pm
Simulation
Choose a random number between 0 and 1
If the random number is less than pm perform mutation
Alter the chosen string at the random site
Other wise, keep the bit un altered
Mutation creates a point in the neighbourhood of the current point
A local search around the current solution is perormed
Mutation also maintains diversity in the population
Working principle
Genetic operators: Summary
Reproduction: Good strings are selected
Cross over: Good strings are combined to create a better child stirng
Mutation: Alters a string locally to hopefully create a better child string
What happens to bad strings created ?
Eliminated in reproduction phase in future iterations
What happens to good strings created ?
Emphasized in an increasing manner due to the fitness probability
Solutions close to global optimality would be obtained due to
which GAs will be attractive.
Algorithm
Step 1: i) Choose a coding to represent problem parameters, a selection
operator, a crossover operator, and a mutation operator.
ii) Choose population size n, crossover probability pc and mutation probability
pm.
iii) Initialize a random population of strings of size l.
iv) Choose a maximum allowable generation number tmax. Set t = 0
Step 2: Evaluate each string in the population
Step 3: If t > tmax or other termination criteria is satisfied, Terminate.
Step 4: Perform reproduction on the population
Step 5: Perform crossover on random pairs of strings
Step 6: Perform mutation on every string
Step 7: Evaluate strings in the new population. Set t = t +1 and go to Step 3
Illustrative example
Problem statement
Minimize the function
f ( x , x ) (x x 11) ( x x 7)
2 2 2 2
1 2 1 2 1 2
0 x ,x 6
1 2
True solution is (3, 2)T with f value of about zero at this point
Illustrative example
Step 2 Evaluate each string in the population
Evaluate each string in the population. Fitness of each string.
xi xi
U L
First string: xi xi
L
2 Li 1
[ Decoded value( si )]
First parameter (1100100000) decodes to 800.
Corresponding parameter value is 0 + (6-0)*800/1023 = li 1
Decoded value (s i ) 2i si
4.692 i0
Second parameter (1110010000) decodes to 912. where s i [0 1]
Corresponding parameter value is 0 + (6-0)*912/1023 =
5.349 string s represente d by (s l-1s l-2 ........s2 s1s0 )
The function value is f(x(1)) = 959.689.
Fitness value F(x) = 1/(1+f(x)) = 1/(1+959.680) =
0.001
Similarly, evaluate other strings are evaluated and fitness
is calculated.
Note that the strings are chosen randomly initially and assigned to a string
Illustrative example
Step 2: Summary of evaluations
Illustrative example
Step 3: If t > tmax or other termination criteria is
satisfied, Terminate.
Since t = 0 < tmax = 30 we
proceed to step 4
Illustrative example
Step 4:Perform reproduction on the population
Select good strings and place them in the mating pool
For proportionate selection, first calculate the average fitness of
the initial population
Average fitness = (Sum of fitness values)/Population size
= 0.008
Expected count for each string is given as F(x)/Average
fitness
Probability of each string = Expected count/Population size
Evaluate cumulative probability
Illustrative example
Step 4: Perform reproduction on the population
Population EXPECTED STRING CUMULATIVE
Tabulated string counter COUNT PROBABILITY PROBABILITY
1 0.13 0.007 0.007
probabilities 2
3
1.10
0.98
0.055
0.049
0.062
0.111
4 1.85 0.093 0.204
5 0.25 0.013 0.217
• Average fitness = (Sum of 6 1.71 0.086 0.303
fitness values)/Population size 7 1.34 0.067 0.370
= 0.008 8 0.12 0.006 0.376
• Expected count for each string 9 0.49 0.025 0.401
is given as F(x)/Average fitness 10 2.96 0.148 0.549
• Probability of each string = 11 0.14 0.007 0.556
Expected count/Population size 12 2.84 0.142 0.698
• Evaluate cumulative probability 13 0.36 0.018 0.716
14 0.24 0.012 0.728
15 0.37 0.019 0.747
16 0.61 0.030 0.777
17 1.22 0.060 0.837
18 1.09 0.054 0.891
19 2.08 0.103 0.994
20 0.13 0.006 1.000
Illustrative example
Step 4:Perform reproduction on the population
For forming good mating pool
Create random numbers from 0 to 1
Identify the particular string which is specified by each of
these random numbers
If random number is 0.472 is created, tenth strings gets
a copy in the mating pool
The selection procedure is repeated n times
The number of selected copies for each string is
counted after the selection procedure is finished.
Illustrative example
Step 4: Mating pool selection
Original Matting pool
papulation
pool
1 2
2 3
3 4
4 4 You could
5 6
see that
6 6
7 7
both
8 9 expected
9 10 count and
10 10
11 10
true count
12 12 agree more
13 12 or less.
14 12
15 13
16 14
17 17
18 18
19 19
20 20
Illustrative example
Step 4: Figure of initial population and mating pool
Conclusion
Inferior points have
been probabilistically
eliminated for further
consideration.
No. Chrom- z f A B C D E F Mating
-osome function 1/(1+z) f/f avg A/Psize Bcumu randon( 0,1) pool
1 Kr1 5.320 0.158 0.940 0.063 0.063 0.356 6.000 0.000 Kr2
2 Kr2 4.904 0.169 1.010 0.067 0.130 0.871 14.000 2.000 Kr2
3 Kr3 4.691 0.176 1.050 0.070 0.200 0.128 2.000 1.000 Kr3
4 Kr4 5.013 0.166 0.990 0.066 0.266 0.325 5.000 1.000 Kr4
5 Kr5 4.955 0.168 1.000 0.067 0.333 0.249 4.000 3.000 Kr5
6 Kr6 4.762 0.174 1.030 0.069 0.402 0.642 10.000 2.000 Kr5
7 Kr7 5.215 0.161 0.960 0.064 0.466 0.333 5.000 0.000 Kr5
8 Kr8 4.838 0.171 1.020 0.068 0.534 0.864 13.000 0.000 Kr6
9 Kr9 4.866 0.171 1.010 0.067 0.601 0.371 6.000 0.000 Kr6
10 Kr10 5.115 0.164 0.970 0.065 0.666 0.275 5.000 2.000 Kr10
11 Kr11 5.352 0.157 0.930 0.062 0.728 0.712 11.000 1.000 Kr10
12 Kr12 4.821 0.172 1.020 0.068 0.796 0.170 3.000 0.000 Kr11
13 Kr13 4.691 0.176 1.050 0.070 0.866 0.910 14.000 1.000 Kr13
14 Kr14 5.111 0.164 0.970 0.065 0.931 0.125 2.000 2.000 Kr14
15 Kr15 4.745 0.174 1.040 0.069 1.000 0.605 10.000 0.000 Kr14
fAvg 0.167993 1.000 15.000
Illustrative example
Step 5: Cross over operation
Single point cross-over: Two strings are selected at random and crossed at
random site
Pick pairs of strings from the top of the list
(3, 10) first cross over, (2, 14) second cross over, (10, 19) third cross
over, (19, 17) fourth cross over etc.
Recall total string length is 20.
For first cross over, Choose a random number and see if the random
number is less than pc = 0.8
Say this time it is true, and therefore cross over has to be carried out
Choose a random site between (0, l-1) or ( 0 , 19). That turns out to be 11.
Cross the strings at the site 11 and create two new strings.
Illustrative example
Step 5: Single point first cross over Illustration
Parent strings from mating pool
0010100100 1010101010 1010100001 0111001000
Crossover site No. 9.
001010010 0 1010101010 101010000 1 0111001000
Children strings
0010100101 0111001000 1010100000 1010101010
Illustrative example
Step 5: Single point Fourth cross over Illustration
Parent strings from mating pool
0011100010 1011000011 0111000010 1011000110
Random number < pc NO. This implies no cross-over.
Children strings (Same as parent strings)
0011100010 1011000011 0111000010 1011000110
Illustrative example
Step 5: Cross over summary
Population size = 20,
pc = 0.8. Expected
no. of cross over =
0.8*20/2 = 8.
However, we only
performed seven
cross overs with 3
cases where parent
strings are child
strings.
Illustrative example
Step 5: Cross over Contour
Some good points are
created
Some bad points are
created
Points far away from
parent points are created
Points closer to parent
points are created.
Conclusion: All random
moves are included that
allow variegated
distribution.
Illustrative example
Step 6: Mutation
pm = 0.05
If random number < pm then do mutation else copy the parent string
as child string
For pm = 0.05, n = 20, l = 20, we can expect to alter 0.05*20*20 or 20
bits in the population.
Note: Here we carry out bit wise mutation. We can complicate
mutation process by allowing more sites to undergo mutation in a
single generation
Those where random numbers are less than pm mutation is not carried
out and parent strings are placed as children strings.
Illustrative example
Step 6: Mutation summary
Illustrative example
Step 6: Mutation contour
Mutation operator changes a
point locally some times
Mutation also brings large
changes
Like cross over, mutation
created both good and bad points.
Such flexibility is required to
explore the search space properly
Illustrative example
Step 7: New population evaluation
Resulting population becomes the new population
Evaluate each string as before by first identifying the
substrings of each variable and mapping the decoded values
of the substrings in the chosen intervals
This completes one iteration of the genetic algorithms
We increment the generation counter to t = 1 and proceed
to step 3 for the next iteration.
Illustrative example
Step 7: New population summary
• New population shown in
empty boxes
•In one iteration some
good points have been
found
Illustrative example
Step 7: New population summary
Average fitness of new population = 0.015
Average fitness of initial population = 0.008
Best point fitness in new population 0.050
Best point fitness in initial population 0.024
Do Step 3 until maximum
allowable generation is reached
or some other termination
criteria is met.
Illustrative example
Population after 25 generations
Best point (3.003, 1.994) with
function value 0.001
Fitness value 0.999
Average population fitness is
0.474
Figure shows that many points
are clustered around the true
minimum
Some inferior points are also
there which are due to some
unsuccessful crossover events
Total number of function
evaluations required are
0.8*20*26 = 416
Illustrative example
Variation of population best function value with generation
number
Three different runs with different initial population
It can be observed that for all the runs one could quickly converge to a solution close
to the true optimum
Critical generation number is around 8 for the runs carried out.
Conclusions
Genetic algorithms are robust and very much applicable for process systems
research
For systems engineering, we need to depend on an NLP /LP solver for an MINLP
problem tried to solve using binary coded Gas.
The combinations of the three operators is wonderful. We first try to eliminate
maximum junk points, then allow reproduction on good points and further allow
possibility for local search.
GAs can be also applied for constrained optimization problems
Use penalty functions where constraints are coupled with the objective function
Real Coded GAs are also familiar
Instead of Binary strings use real numbers directly in the fitness evaluation and
perform reproduction, crossover and mutation on the real numbers only rather
than on the strings.
End of the day, what GA did is some thing better than what you
learnt in your earlier lectures. GA is not a determinstic but a
stochastic and probabilistic technique for the evolution of
optimal solutions from initial population. GAs are very robust
and can be applied for optimization of any engineering system let
it be Chemical, Mechanical, Electrical or Control systems.
THANK YOU VERY MUCH FOR YOUR ATTENTION. IF YOU
UNDERSTOOD EVERY SLIDE OF THIS PRESENTATION, YOU CAN
EASILY GO AHEAD IMPLEMENTING GA’S FOR SOLVING SOME
OPTIMIZATION PROBLEMS YOU ENCOUNTER IN YOUR
ENGINEERING EDUCATION AND RESEARCH.