You are on page 1of 83

Genetic Algorithms

Genetic Algorithms

An
An
algorithm
algorithm
is a set of instructions that is
is a set of instructions that is
repeated to solve a problem.
repeated to solve a problem.

A
A
genetic algorithm
genetic algorithm
conceptually follows
conceptually follows
steps inspired by the biological processes of
steps inspired by the biological processes of
evolution.
evolution.

Genetic Algorithms follow the idea of
Genetic Algorithms follow the idea of
SURVIVAL OF THE FITTEST
SURVIVAL OF THE FITTEST
- Better and
- Better and
better solutions evolve from previous
better solutions evolve from previous
generations until a near optimal solution is
generations until a near optimal solution is
obtained.
obtained.
Genetic Algorithms
Genetic Algorithms

Also known as
Also known as
evolutionary algorithms
evolutionary algorithms
,
,
genetic algorithms demonstrate self
genetic algorithms demonstrate self
organization and adaptation similar to the
organization and adaptation similar to the
way that the fittest biological organism
way that the fittest biological organism
survive and reproduce.
survive and reproduce.

A genetic algorithm is an iterative procedure
A genetic algorithm is an iterative procedure
that represents its candidate solutions as
that represents its candidate solutions as
strings of genes called chromosomes.
strings of genes called chromosomes.

Generally applied to spaces which are too
large

Genetic Algorithms
Genetic Algorithms

Genetic Algorithms are often used to improve
Genetic Algorithms are often used to improve
the performance of other AI methods such as
the performance of other AI methods such as
epert systems or neural networks.
epert systems or neural networks.

!he method learns by producing offspring
!he method learns by producing offspring
that are better and better as measured by a
that are better and better as measured by a
fitness function, which is a measure of the
fitness function, which is a measure of the
ob"ective to be obtained #maimum or
ob"ective to be obtained #maimum or
minimum$.
minimum$.
Simple GA
Simple GA

initiali!e pop"lation#
e$al"ate pop"lation#
while Termination%riteria&otSatis'ied

select parents 'or reprod"ction#
per'orm crosso$er and m"tation#
repair()#
e$al"ate pop"lation#
*
*
Every loop called
generation
%oncepts
%oncepts

Population&set of individuals each representing
a possible solution to a given problem.

Gene&a solution to problem represented as a
set of parameters ,these parameters known as
genes.

Chromosome&genes "oined together to form a
string of values called chromosome.

Fitness score(value)&every chromosome has
fitness score can be inferred from the
chromosome itself by using fitness function.
Recombination
'tochastic operators
'tochastic operators

Selection
Selection
replicates the most successful
replicates the most successful
solutions found in a population at a rate
solutions found in a population at a rate
proportional to their relative (uality
proportional to their relative (uality

Recombination
Recombination
(%rosso$er)
(%rosso$er)
decomposes two
decomposes two
distinct solutions and then randomly mies
distinct solutions and then randomly mies
their parts to form novel solutions
their parts to form novel solutions

Mutation
Mutation
randomly perturbs a candidate
randomly perturbs a candidate
solution
solution
'imulated )volution
'imulated )volution

*e need the following
*e need the following

+epresentation of an individual
+epresentation of an individual

,itness ,unction
,itness ,unction

+eproduction -ethod
+eproduction -ethod

'election %riteria
'election %riteria
+epresenting an Individual
+epresenting an Individual

An individual is data structure
An individual is data structure
representing the .genetic structure/ of a
representing the .genetic structure/ of a
possible solution.
possible solution.

Genetic structure consists of an
Genetic structure consists of an
alphabet #usually 0,1$
alphabet #usually 0,1$
Binary )ncoding
Binary )ncoding

-ost %ommon 2 string of bits, 0 or 1. -ost %ommon 2 string of bits, 0 or 1.
%hrom& A 3 1 0 11 0 0 1 0 1 1 %hrom& A 3 1 0 11 0 0 1 0 1 1
%hrom& B 3 1 1 1 1 1 1 0 0 0 0 %hrom& B 3 1 1 1 1 1 1 0 0 0 0

Gives you many possibilities Gives you many possibilities

)ample 4roblem& 5napsack problem )ample 4roblem& 5napsack problem

!he problem& there are things with given value and !he problem& there are things with given value and
size. !he knapsack has given capacity. 'elect things size. !he knapsack has given capacity. 'elect things
to maimize the values. to maimize the values.

)ncoding& )ach bit says, if the corresponding thing is )ncoding& )ach bit says, if the corresponding thing is
in the knapsack in the knapsack
4ermutation )ncoding
4ermutation )ncoding

6sed in .ordering problems/
6sed in .ordering problems/

)very chromosome is a string of numbers,
)very chromosome is a string of numbers,
which represents number is a se(uence.
which represents number is a se(uence.
%hrom A& 1 7 8 9 : ; < = > %hrom A& 1 7 8 9 : ; < = >
%hrom B& > 7 < < 9 8 1 ; = %hrom B& > 7 < < 9 8 1 ; =

)ample& !ravelling salesman problem
)ample& !ravelling salesman problem

!he problem& cities that must be visited.
!he problem& cities that must be visited.

)ncoding says order of cities in which
)ncoding says order of cities in which
salesman willl visit.
salesman willl visit.
Another )ample
Another )ample

!o find optimal (uantity of three ma"or
!o find optimal (uantity of three ma"or
ingredients #sugar, milk, sesame oil$
ingredients #sugar, milk, sesame oil$
denoting ounces.
denoting ounces.

6se an alphabet of 1-= denoting ounces..
6se an alphabet of 1-= denoting ounces..

'olutions might be 1-1-1, 9-1-;, 8-8-1.
'olutions might be 1-1-1, 9-1-;, 8-8-1.
?alue )ncoding
?alue )ncoding

6sed for complicated values #real numbers$
6sed for complicated values #real numbers$
and when binary coding would be difficult
and when binary coding would be difficult

)ach chromosome is a string of some values.
)ach chromosome is a string of some values.
%hrom A& 1.9898 7.89;8 0.;77: %hrom A& 1.9898 7.89;8 0.;77:
%hrom B& abcd"eif"dhdier"fd %hrom B& abcd"eif"dhdier"fd
%hrom %& #back$, #back$, #right$, #forward$, #left$ %hrom %& #back$, #back$, #right$, #forward$, #left$
)ample& ,inding weights for neural nets.
)ample& ,inding weights for neural nets.
!he problem& find weights for network
!he problem& find weights for network
)ncoding& +eal values that represent weights
)ncoding& +eal values that represent weights
+ule base system
+ule base system

Given a rule #if color3red and size3small and
Given a rule #if color3red and size3small and
shape3round then ob"ect3apple.
shape3round then ob"ect3apple.

Assume that each feature has finite set of
Assume that each feature has finite set of
values #e.g., size 3 small,large$
values #e.g., size 3 small,large$

+epresent the value as a substring of length
+epresent the value as a substring of length
e(ul to the number of possible values. ,or
e(ul to the number of possible values. ,or
eample, small 3 10, large 3 01.
eample, small 3 10, large 3 01.

!he entire rule would be 100 10 01 0100 2
!he entire rule would be 100 10 01 0100 2
set of rules concatenating the values
set of rules concatenating the values
together.
together.
How o''spring are prod"ced
How o''spring are prod"ced
+ Reprod"ction
+ Reprod"ction

Reproduction
Reproduction
- !hrough
- !hrough
reprod"ction
reprod"ction
,
,
genetic algorithms produce new
genetic algorithms produce new
generations of improved solutions by
generations of improved solutions by
selecting parents with higher fitness
selecting parents with higher fitness
ratings or by giving such parents a
ratings or by giving such parents a
greater probability of being contributors
greater probability of being contributors
and by using random selection
and by using random selection
How o''spring are prod"ced
How o''spring are prod"ced

(Recombination)
(Recombination)
Crossover
Crossover
- -any genetic
- -any genetic
algorithms use strings of binary symbols for
algorithms use strings of binary symbols for
chromosomes, as in our 5napsack eample,
chromosomes, as in our 5napsack eample,
to represent solutions.
to represent solutions.
%rosso$er
%rosso$er
means
means
choosing a random position in the string #say,
choosing a random position in the string #say,
after 9 digits$ and echanging the segments
after 9 digits$ and echanging the segments
either to the right or to the left of this point
either to the right or to the left of this point
with another string partitioned similarly to
with another string partitioned similarly to
produce two new off spring.
produce two new off spring.
%rosso$er E,ample
%rosso$er E,ample

4arent A 011011
4arent A 011011

4arent B 101100
4arent B 101100

.
.
-ate the parents by splitting each number
-ate the parents by splitting each number
as shown between the second and third
as shown between the second and third
digits #position is randomly selected$
digits #position is randomly selected$

01@1011
01@1011
10@1100
10@1100
%rossover )ample
%rossover )ample

Aow combine the first digits of A with the last digits
Aow combine the first digits of A with the last digits
of B, and the first digits of B with the last digits of A
of B, and the first digits of B with the last digits of A

!his gives you two new offspring
!his gives you two new offspring

011100 011100

101011 101011

If these new solutions, or offspring, are better
If these new solutions, or offspring, are better
solutions than the parent solutions, the system will
solutions than the parent solutions, the system will
keep these as more optimal solutions and they will
keep these as more optimal solutions and they will
become parents. !his is repeated until some
become parents. !his is repeated until some
condition #for eample number of populations or
condition #for eample number of populations or
improvement of the best solution$ is satisfied.
improvement of the best solution$ is satisfied.
How o''spring are prod"ced
How o''spring are prod"ced

Mutation
Mutation
-
-
-"tation
-"tation
is an arbitrary
is an arbitrary
change in a situation. 'ometimes it is
change in a situation. 'ometimes it is
used to prevent the algorithm from
used to prevent the algorithm from
getting stuck. !he procedure changes a
getting stuck. !he procedure changes a
1 to a 0 to a 1 instead of duplicating
1 to a 0 to a 1 instead of duplicating
them. !his change occurs with a very
them. !his change occurs with a very
low probability #say 1 in 1000$
low probability #say 1 in 1000$
1 0 1 0 1 1 1
1 1 0 0 0 1 1
Parent 1
Parent 2
1 0 1 0 0 1 1
1 1 0 0 1 1 0
Child 1
Child 2
Mutation
Genetic Algorithm Operators
Genetic Algorithm Operators
-"tation and %rosso$er
-"tation and %rosso$er
%rossover Bperators
%rossover Bperators


'ingle 'ingle­ ­point crossover& point crossover&

4arent A& 4arent A& . / / . /0 . . . / . . / / . /0 . . . / .

4arent B& 0 1 0 1 1 C1 0 1 1 0 4arent B& 0 1 0 1 1 C1 0 1 1 0

%hild AB& %hild AB& . / / . / . / / . / 1 0 1 1 0 1 0 1 1 0

%hild BA& 0 1 0 1 1 %hild BA& 0 1 0 1 1 . . . / . . . . / .

!wo !wo­ ­point crossover& point crossover&

4arent A& 4arent A& . / /0. / . .0 . / . . / /0. / . .0 . / .

4arent B& 0 1 0 4arent B& 0 1 0@ @ 1 1 1 0 1 1 1 0@ @ 1 1 0 1 1 0

%hild AB& %hild AB& . / / . . / / . 1 1 0 1 1 0 . / . . / .

%hild BA& 0 1 0 1 %hild BA& 0 1 0 1 / . . / . . 1 1 0 1 1 0
6niform %rossover and
6niform %rossover and
- A random mask is generated - A random mask is generated
- !he mask determines which bits are copied from one - !he mask determines which bits are copied from one
parent and which from the other parent parent and which from the other parent
- Bit density in mask determines how much material is - Bit density in mask determines how much material is
taken from the other parent. taken from the other parent.
)amples
)amples

-utation&
-utation&
!he recipe eample&
!he recipe eample&
1-9-8 may be changed to 1-8-8 or 8-9-8,
1-9-8 may be changed to 1-8-8 or 8-9-8,
giving two new offspring. Dow oftenE Dow
giving two new offspring. Dow oftenE Dow
many digits changeE Dow bigE
many digits changeE Dow bigE
#parameters to ad"ust$
#parameters to ad"ust$
-ore eamples&
-ore eamples&

%rossover
%rossover
+ecipe &
+ecipe &


4arents 1-8-8 F 8-9-8. %rossover point
4arents 1-8-8 F 8-9-8. %rossover point
after the first digit. Generate two
after the first digit. Generate two
offspring& 8-8-8 and 1-9-8.
offspring& 8-8-8 and 1-9-8.
%an have one or two point crossover.
%an have one or two point crossover.
%rossover 2 4ermutation
%rossover 2 4ermutation
)ncoding
)ncoding
Single point crosso$er Single point crosso$er - one crossover point is selected, till this - one crossover point is selected, till this
point the permutation is copied from the first parent, then the point the permutation is copied from the first parent, then the
second parent is scanned and if the number is not yet in the second parent is scanned and if the number is not yet in the
offspring it is added offspring it is added
# #. 1 2 3 40 . 1 2 3 40 : < > =$ G # : < > =$ G #3 4 2 5 60 3 4 2 5 60 = < 9 1$ 3 # = < 9 1$ 3 #. 1 2 3 4 . 1 2 3 4 : > = <$ : > = <$
-"tation -"tation
Order changing Order changing - two numbers are selected and echanged - two numbers are selected and echanged
#1 #1 1 1 8 ; 7 : 8 ; 7 : 6 6 = <$ 3H #1 = <$ 3H #1 6 6 8 ; 7 : 8 ; 7 : 1 1 = =
<$ <$
%rossover 2 ?alue )ncoding
%rossover 2 ?alue )ncoding
%rosso$er
%rosso$er

All crossovers from
All crossovers from
7inary encoding
7inary encoding
can be
can be
used
used
-"tation
-"tation

Adding Adding a small number #for real value encoding$ - to a small number #for real value encoding$ - to
selected values is added #or subtracted$ a small selected values is added #or subtracted$ a small
number number

#
#1.9= 7.:> 1.9= 7.:> 1865 1865 38.. 38.. 7.77$ 3H #1.9= 7.:> 7.77$ 3H #1.9= 7.:> 1892 1892 3811 3811 7.77$ 7.77$
'election %riteria
'election %riteria

,itness proportionate selection, rank
,itness proportionate selection, rank
selection methods.
selection methods.

,itness proportionate 2 each individual, ,itness proportionate 2 each individual, I, I, has the has the
probability fitness(I)/sum_over_all_individual_j probability fitness(I)/sum_over_all_individual_j
itness(j itness(j$, where $, where itness(I) itness(I) is the fitness function is the fitness function
value for individual value for individual I! I!

+ank selection 2 sorts individual by fitness and the +ank selection 2 sorts individual by fitness and the
probability that an individual will be selected is probability that an individual will be selected is
proportional to its rank in this sorted list. proportional to its rank in this sorted list.
,itness ,unction
,itness ,unction

+epresents a rank of the .representation/
+epresents a rank of the .representation/

It is usually a real number.
It is usually a real number.

!he function usually has a value between 0
!he function usually has a value between 0
and 1 and is monotonically increasing.
and 1 and is monotonically increasing.

Bne can have a sub"ective "udgment #e.g. 1-7
Bne can have a sub"ective "udgment #e.g. 1-7
for recipe 9-1-;.$
for recipe 9-1-;.$

'imilarly the length of the route in the
'imilarly the length of the route in the
traveling salesperson problem is a good
traveling salesperson problem is a good
measure, because the shorter the route, the
measure, because the shorter the route, the
better the solution.
better the solution.
O"tline o' the :asic Genetic Algorithm
O"tline o' the :asic Genetic Algorithm


;
;
Start<
Start<
Generate random population of
Generate random population of
n
n
chromosomes #suitable solutions for
chromosomes #suitable solutions for
the problem$
the problem$

;Fitness<
;Fitness<
)valuate the fitness
)valuate the fitness
f(")
f(")
of
of
each chromosome
each chromosome
"
"
in the population
in the population

;&ew pop"lation<
;&ew pop"lation<
%reate a new
%reate a new
population by repeating following
population by repeating following
steps until the new population is
steps until the new population is
complete
complete

O"tline o' the :asic Genetic Algorithm
O"tline o' the :asic Genetic Algorithm
38 38
;
;
Selection<
Selection<
'elect two parent chromosomes
'elect two parent chromosomes
from a population according to their fitness #the
from a population according to their fitness #the
better fitness, the bigger chance to be selected$
better fitness, the bigger chance to be selected$
!he idea is to choose the better parents.
!he idea is to choose the better parents.
48 48
;%rosso$er<
;%rosso$er<
*ith a crossover probability cross
*ith a crossover probability cross
over the parents to form a new offspring
over the parents to form a new offspring
#children$. If no crossover was performed,
#children$. If no crossover was performed,
offspring is an eact copy of parents.
offspring is an eact copy of parents.
58 58
;-"tation<
;-"tation<
*ith a mutation probability mutate
*ith a mutation probability mutate
new offspring at each locus #position in
new offspring at each locus #position in
chromosome$.
chromosome$.
O"tline o' the :asic Genetic Algorithm
O"tline o' the :asic Genetic Algorithm
98 98
;
;
Accepting<
Accepting<
4lace new offspring in a new
4lace new offspring in a new
population
population
68 68
;Replace<
;Replace<
6se new generated population
6se new generated population
for a further run of algorithm
for a further run of algorithm
=8 =8
;Test<
;Test<
If the end condition is satisfied,
If the end condition is satisfied,
stop
stop
,
,
and return the best solution in current
and return the best solution in current
population
population
./8 ./8
;Loop<
;Loop<
Go to step
Go to step
1
1


Flow >iagram o' the Genetic
Flow >iagram o' the Genetic
Algorithm ?rocess
Algorithm ?rocess
Describe
Problem
Generate
Initial
Solutions
Test: is initial
solution good enough?
Stop
Select parents
to reproduce
ppl! crosso"er process
and create a set o# o##spring
ppl! random mutation
Step 1
Step 2
Step $
Step %
Step &
'es
(o
%omponents of a GA
%omponents of a GA


A problem definition as input, and
A problem definition as input, and

)ncoding principles #gene, chromosome$
)ncoding principles #gene, chromosome$

Initialization procedure #creation$
Initialization procedure #creation$

'election of parents #reproduction$
'election of parents #reproduction$

Genetic operators #mutation, recombination$
Genetic operators #mutation, recombination$

)valuation function #environment$
)valuation function #environment$

!ermination condition
!ermination condition
+epresentation #encoding$
+epresentation #encoding$
4ossible individualIs encoding
4ossible individualIs encoding

Bit strings #0101 ... 1100$ Bit strings #0101 ... 1100$

+eal numbers #;8.9 -88.1 ... 0.0 >=.9$ +eal numbers #;8.9 -88.1 ... 0.0 >=.9$

4ermutations of element #)11 )8 )< ... )1 )17$ 4ermutations of element #)11 )8 )< ... )1 )17$

Jists of rules #+1 +9 +8 ... +99 +98$ Jists of rules #+1 +9 +8 ... +99 +98$

4rogram elements #genetic programming$ 4rogram elements #genetic programming$

... any data structure ... ... any data structure ...
+epresentation #cont$
+epresentation #cont$
*hen choosing an encoding method rely on the
following key ideas

6se a data structure as close as possible to the
6se a data structure as close as possible to the
natural representation
natural representation

*rite appropriate genetic operators as needed
*rite appropriate genetic operators as needed

If possible, ensure that all genotypes correspond
If possible, ensure that all genotypes correspond
to feasible solutions
to feasible solutions

If possible, ensure that genetic operators
If possible, ensure that genetic operators
preserve feasibility
preserve feasibility
Initialization
Initialization
'tart with a population of randomly
generated individuals, or use
- A previously saved population
- A set of solutions provided by
a human epert
- A set of solutions provided by
another heuristic algorithm
'election
'election
,itness 4roportionate 'election
,itness 4roportionate 'election

Kerived by Dolland as the optimal trade-off
Kerived by Dolland as the optimal trade-off
between eploration and eploitation
between eploration and eploitation
Krawbacks
Krawbacks

Kifferent selection for
Kifferent selection for
f
f
1 1
(x)
(x)
and
and
f
f
2 2
(x) = f
(x) = f
1 1
(x) + c
(x) + c

#uperindividuals
#uperindividuals
cause convergence #that may
cause convergence #that may
be premature$
be premature$
Jinear +anking 'election
Jinear +anking 'election
Based on sorting of individuals by decreasing fitness
!he probability to be etracted for the ith individual
in the ranking is defined as
2 1 ,
1
1
) 1 ( 2
1
) ( ≤ ≤








− − = β β β
n
i
n
i p
where β can be interpreted as
the epected sampling rate of
the best individual
!ournament 'election
!ournament 'election
Tournament Selection:
– randomly select two individuals and the one
with the highest rank goes on and reproduces
– cares only about the one with the higher rank,
not the spread between the two fitness scores
– puts an upper and lower bound on the chances
that any individual to reproduce for the next
generation eual to! )2s – 2r + 1* / s
2
" s is the si#e of the population
" r is the rank of the $winning$ individual
– can be generali#ed to select best of n individuals
+ecombination #%rossover$
+ecombination #%rossover$
@ )nables the evolutionary process
to move toward promising
regions of the search space
@ -atches good parents% sub-solutions
to construct better offspring
-utation
-utation
?"rpose@
?"rpose@
to simulate the effect of errors that
to simulate the effect of errors that
happen with low probability during duplication
happen with low probability during duplication
Res"lts@
Res"lts@
- -ovement in the search space
- -ovement in the search space
- +estoration of lost information to the population
- +estoration of lost information to the population
)valuation #fitness function$
)valuation #fitness function$

'olution is only as good as the evaluation
'olution is only as good as the evaluation
functionL choosing a good one is often the
functionL choosing a good one is often the
hardest part
hardest part

'imilar-encoded solutions should have a
'imilar-encoded solutions should have a
similar fitness
similar fitness
!ermination condition
!ermination condition
)amples&
)amples&

A pre-determined number of generations or time
A pre-determined number of generations or time
has elapsed
has elapsed

A satisfactory solution has been achieved
A satisfactory solution has been achieved

Ao improvement in solution (uality has taken
Ao improvement in solution (uality has taken
place for a pre-determined number of generations
place for a pre-determined number of generations
)ample&
)ample&
the -AMBA) problem
the -AMBA) problem
'uppose we want to maimize the number of
ones in a string of l binary digits
Is it a trivial problemE
It may seem so because we know the answer in
advance
Dowever, we can think of it as maimizing the
number of correct answers, each encoded by 1,
to l yesNno difficult (uestionsO
)ample #cont$
)ample #cont$

An individual is encoded #naturally$ as a
An individual is encoded #naturally$ as a
string of
string of
l
l
binary digits
binary digits

!he fitness
!he fitness
f
f
of a candidate solution to the
of a candidate solution to the
-AMBA) problem is the number of ones in
-AMBA) problem is the number of ones in
its genetic code
its genetic code

*e start with a population of
*e start with a population of
n
n
random
random
strings. 'uppose that
strings. 'uppose that
l
l
3 10 and
3 10 and
n
n
3 :
3 :
)ample #initialization$
)ample #initialization$
*e toss a fair coin :0 times and get the
following initial population&
s
1
& 1111'1'1'1 f (s
1
) & (
s
2
& '111'''1'1 f (s
2
) & )
s
*
& 111'11'1'1 f (s
*
) & (
s
+
& '1'''1''11 f (s
+
) & +
s
)
& 111'1111'1 f (s
)
) & ,
s
-
& '1''11'''' f (s
-
) & *
)ample #selection1$
)ample #selection1$
Aet we apply fitness proportionate selection with the
roulette wheel method&
2
1
n
*
.rea is
/roportional
to fitness
value
Individual i will have a
probability to be chosen

i
i f
i f
) (
) (
4
*e repeat the etraction
as many times as the
number of individuals we
need to have the same
parent population size
#: in our case$
)ample #selection9$
)ample #selection9$
'uppose that, after performing selection, we get
the following population&
s
1
0 & 1111'1'1'1 (s
1
)
s
2
0 & 111'11'1'1 (s
*
)
s
*
0 & 111'1111'1 (s
)
)
s
+
0 & '111'''1'1 (s
2
)
s
)
0 & '1'''1''11 (s
+
)
s
-
0 & 111'1111'1 (s
)
)
)ample #crossover1$
)ample #crossover1$
Aet we mate strings for crossover. ,or each
couple we decide according to crossover
probability #for instance 0.:$ whether to actually
perform crossover or not
'uppose that we decide to actually perform
crossover only for couples #s
1
O, s
9
O$ and #s
7
O, s
:
O$.
,or each couple, we randomly etract a
crossover point, for instance 9 for the first and 7
for the second
)ample #crossover9$
)ample #crossover9$
s
1
0 & 1111'1'1'1
s
2
0 & 111'11'1'1
s
)
0 & '1'''1''11
s
-
0 & 111'1111'1
Before crossover&
After crossover&
s
1
00 & 111'11'1'1
s
2
00 & 1111'1'1'1
s
)
00 & '1'''111'1
s
-
00 & 111'11''11
)ample #mutation1$
)ample #mutation1$
!he 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&
s
1
00 & 111'11'1'1
s
2
00 & 1111'1'1'1
s
*
00 & 111'1111'1
s
+
00 & '111'''1'1
s
)
00 & '1'''111'1
s
-
00 & 111'11''11
)ample #mutation9$
)ample #mutation9$
After applying mutation&
s
1
000 & 111'1''1'1 f (s
1
000 ) & -
s
2
000 & 111111'1'' f (s
2
000 ) & (
s
*
000 & 111'1'1111 f (s
*
000 ) & ,
s
+
000 & '111'''1'1 f (s
+
000 ) & )
s
)
000 & '1'''111'1 f (s
)
000 ) & )
s
-
000 & 111'11'''1 f (s
-
000 ) & -
)ample #end$
)ample #end$
In one generation, the total population fitness
changed from 8; to 8<, thus improved by P=Q
At this point, we go through the same process
all over again, until a stopping criterion is met
E,ample @
E,ample @

S"ppose a Genetic Algorithm "ses chromosomes o' the 'orm ,Aa7cde'gh S"ppose a Genetic Algorithm "ses chromosomes o' the 'orm ,Aa7cde'gh
with a 'i,ed length o' eight genes 8 Each gene can 7e any digit 7etween / with a 'i,ed length o' eight genes 8 Each gene can 7e any digit 7etween /
and = 8 Let the 'itness o' indi$id"al , 7e calc"lated as @ and = 8 Let the 'itness o' indi$id"al , 7e calc"lated as @
'(,) A(aB7)+(cBd)B(eB')+ ( gBh) '(,) A(aB7)+(cBd)B(eB')+ ( gBh)
And let the initial pop"lation consist o' 'o"r indi$id"als ,.C 888 C,3 with the And let the initial pop"lation consist o' 'o"r indi$id"als ,.C 888 C,3 with the
'ollowing chromosomes @ 'ollowing chromosomes @
D. A 5 4 3 . 2 4 2 1 D. A 5 4 3 . 2 4 2 1
F(,.) A(5B4)+(3B.)B(2B4)+(2B1) A = F(,.) A(5B4)+(3B.)B(2B4)+(2B1) A =
D1 A 6 9 . 1 5 5 / . D1 A 6 9 . 1 5 5 / .
F(,1) A (6B9)+(.B1)B(5B5)+(/B.) A 12 F(,1) A (6B9)+(.B1)B(5B5)+(/B.) A 12
D2 A 1 2 = 1 . 1 6 4 D2 A 1 2 = 1 . 1 6 4
F(,2) A (1B2)+(=B1)B(.B1)+(6B4) A +.5 F(,2) A (1B2)+(=B1)B(.B1)+(6B4) A +.5
D3A 3 . 6 4 1 / = 3 D3A 3 . 6 4 1 / = 3
F(,3) A (3B.)+(6B4)B(1B/)+(=B3) A +.= F(,3) A (3B.)+(6B4)B(1B/)+(=B3) A +.=
The arrangement is ( ass"me ma,imi!ation ) The arrangement is ( ass"me ma,imi!ation )
D1 D1 ,. ,. ,2 ,2 ,3 ,3
( the 'ittest indi$id"al ) ( the 'ittest indi$id"al ) ( least 'it indi$id"al ) ( least 'it indi$id"al )
4ut the calculations in table
4ut the calculations in table
for simplicity
for simplicity
Indi$id"als Indi$id"als
String String
Representation Representation
Fitness Fitness
Arrangement Ass"me Arrangement Ass"me
ma,imi!ation ma,imi!ation
M1 M1
:7;18789 :7;18789 = = M9#fittest individual$ M9#fittest individual$
M9 M9
><19::01 ><19::01 98 98 M1#second fittest individual$ M1#second fittest individual$
M8 M8
98=919>7 98=919>7 -1: -1: M8 #third fittest individual$ M8 #third fittest individual$
M; M;
;1>790=; ;1>790=; -1= -1= M; #least fit individual$ M; #least fit individual$
'o Average fitness &-0.<7 Best & 98 *orst & -1=
Average fitness 3 # =G98G -1: G -1=$N ; 3-0.<7
E E D1 A 6 D1 A 6 9 9 . . 1 1 5 5 5 5 / / . .
D. A 5 D. A 5 4 4 3 3 . . 2 2 4 4 2 2 1 1
O''spring . A 6 O''spring . A 6 9 9 . . 1 1 2 2 4 4 2 2 1 1
O''spring 1 A 5 O''spring 1 A 5 4 4 3 3 . . 5 5 5 5 / / . .
D. A 5 D. A 5 4 4 3 3 . . 2 2 4 4 2 2 1 1
D2 A 1 D2 A 1 2 2 = = 1 1 . . 1 1 6 6 4 4
O''spring 2 A 5 O''spring 2 A 5 4 4 = = 1 1 . . 1 1 2 2 1 1
O''spring 3 A 1 O''spring 3 A 1 2 2 3 3 . . 2 2 4 4 6 6 4 4
Middle crossover
crossover crossover
M9 3 > M9 3 > < < 1 1 9 9 : : : : 0 0 1 1
M8 3 9 M8 3 9 8 8 = = 9 9 1 1 9 9 > > 7 7
Bffspring 7 3 > Bffspring 7 3 > 8 8 1 1 9 9 : : : : > > 1 1
Bffspring : 3 9 Bffspring : 3 9 < < 1 1 9 9 : : 9 9 > > 1 1
Bffspring 1 3 > Bffspring 1 3 > < < 1 1 9 9 8 8 7 7 8 8 9 9
F (O''spring .) A(6B9)+(.B1)B(2B4)+(2B1) A .4 F (O''spring .) A(6B9)+(.B1)B(2B4)+(2B1) A .4
Bffspring 9 3 : Bffspring 9 3 : 7 7 ; ; 1 1 : : : : 0 0 1 1
F (O''spring 1) A(5B4)+(3B.)B(5B5)+(/B.) A .9 F (O''spring 1) A(5B4)+(3B.)B(5B5)+(/B.) A .9
Bffspring 8 3 : Bffspring 8 3 : 7 7 = = 9 9 1 1 9 9 8 8 9 9
F (O''spring 2) A(5B4)+(=B1)B(.B1)+(2B1) A +1 F (O''spring 2) A(5B4)+(=B1)B(.B1)+(2B1) A +1
Bffspring ; 3 9 Bffspring ; 3 9 8 8 ; ; 1 1 8 8 7 7 > > 7 7
F (O''spring 3) A(1B2)+(3B.)B(2B4)+(6B4) A +4 F (O''spring 3) A(1B2)+(3B.)B(2B4)+(6B4) A +4
Bffspring 7 3 > Bffspring 7 3 > 8 8 1 1 9 9 : : : : > > 1 1
F (O''spring 4) A(6B2)+(.B1)B(5B5)+(6B.) A .. F (O''spring 4) A(6B2)+(.B1)B(5B5)+(6B.) A ..
Bffspring : 3 9 Bffspring : 3 9 < < 1 1 9 9 : : 9 9 > > 1 1
F (O''spring 5) A(1B9)+(.B1)B(5B1)+(6B.) A 4 F (O''spring 5) A(1B9)+(.B1)B(5B1)+(6B.) A 4
Indi$id"als Indi$id"als String Representation String Representation Fitness Fitness
Bffspring 1 Bffspring 1 ><198789 ><198789 17 17
Bffspring Bffspring 9 9 :7;1::01 :7;1::01 1< 1<
Bffspring Bffspring 8 8 :7=91989 :7=91989 -9 -9
Bffspring ; Bffspring ; 98;187>7 98;187>7 -7 -7
Bffspring 7 Bffspring 7 ><=91901 ><=91901 11 11
Bffspring : Bffspring : 98=9::01 98=9::01 7 7
4ut the calculation in table for simplicity
A$erage 'itness @ 58622 :est @ .9 Forst @ +4
'o that , the overall fitness is improved , since the average
is better and worst is improved .
Average fitness 3 #17G1<G -7 G -9 G 11G7 $N : 3 :.>888
)ample& !he 5napsack
)ample& !he 5napsack
4roblem
4roblem

Rou are going on an overnight hike and have
Rou are going on an overnight hike and have
a number of items that you could take along.
a number of items that you could take along.
)ach item has a weight #in pounds$ and a
)ach item has a weight #in pounds$ and a
benefit or value to you on the hike#for
benefit or value to you on the hike#for
measurements sake letIs say, in 6' dollars$,
measurements sake letIs say, in 6' dollars$,
and you can take one of each item at most.
and you can take one of each item at most.
!here is a capacity limit on the weight you
!here is a capacity limit on the weight you
can carry #constraint$. !his problem only
can carry #constraint$. !his problem only
illustrates one constraint, but in reality there
illustrates one constraint, but in reality there
could be many constraints including volume,
could be many constraints including volume,
time, etc.
time, etc.

Item: Item: 1 2 $ % & + , 1 2 $ % & + ,

-ene#it: -ene#it: & . $ 2 , / % & . $ 2 , / %

0eight: 0eight: , . % , . % 10 % + % 10 % + %

1napsac2 holds a ma3imum o# 22 pounds 1napsac2 holds a ma3imum o# 22 pounds

4ill it to get the ma3imum bene#it 4ill it to get the ma3imum bene#it

Solutions ta2e the #orm o# a string o# 15s and 05s Solutions ta2e the #orm o# a string o# 15s and 05s

Solutions: lso 2no6n as strings o# genes called Solutions: lso 2no6n as strings o# genes called
Chromosomes Chromosomes

1. 0101010 1. 0101010

9. 1100100 9. 1100100

8. 0100111 8. 0100111
GA E,ample@ The GnapsacH
GA E,ample@ The GnapsacH
?ro7lem
?ro7lem
E,ample@ The GnapsacH
E,ample@ The GnapsacH
?ro7lem
?ro7lem

*e represent a solution as a string of seven
*e represent a solution as a string of seven
1s and 0s and the fitness function as the total
1s and 0s and the fitness function as the total
benefit, which is the sum of the gene values
benefit, which is the sum of the gene values
in a string solution times their representative
in a string solution times their representative
benefit coefficient.
benefit coefficient.

!he method generates a set of random
!he method generates a set of random
solutions #initial parents$, uses total benefit as
solutions #initial parents$, uses total benefit as
the fitness function and selects the parents
the fitness function and selects the parents
randomly to create generations of offspring
randomly to create generations of offspring
by crossover and mutation.
by crossover and mutation.
5napsack )ample
5napsack )ample

!ypically, a string of 1s and 0s can
!ypically, a string of 1s and 0s can
represent a solution.
represent a solution.

4ossible solutions generated by the
4ossible solutions generated by the
system using
system using
Reproduction, Crossover
Reproduction, Crossover
,
,
or
or
Mutations
Mutations

1. 0101010
1. 0101010

9. 1100100
9. 1100100

8. 0100111
8. 0100111
5napsack )ample
5napsack )ample
'olution 1
'olution 1

Benefit > G 9 G = 3 1=
Benefit > G 9 G = 3 1=

*eight > G 10 G : 3 9;
*eight > G 10 G : 3 9;
Item
Item
1
1
9
9
8
8
;
;
7
7
:
:
<
<
Sol"tion
Sol"tion
/
/
.
.
/
/
.
.
/
/
.
.
/
/
Benefit
Benefit
7
7
>
>
8
8
9
9
<
<
=
=
;
;
*eight
*eight
<
<
>
>
;
;
10
10
;
;
:
:
;
;
5napsack )ample
5napsack )ample
'olution 9
'olution 9

Benefit 7 G > G < 3 90
Benefit 7 G > G < 3 90

*eight < G > G ; 3 1=
*eight < G > G ; 3 1=
Item
Item
1
1
9
9
8
8
;
;
7
7
:
:
<
<
Sol"tion
Sol"tion
.
.
.
.
/
/
/
/
.
.
/
/
/
/
Benefit
Benefit
7
7
>
>
8
8
9
9
<
<
=
=
;
;
*eight
*eight
<
<
>
>
;
;
10
10
;
;
:
:
;
;
5napsack )ample
5napsack )ample
'olution 8
'olution 8

Benefit > G < G = G ; 3 9>
Benefit > G < G = G ; 3 9>

*eight > G ; G : G ; 3 99
*eight > G ; G : G ; 3 99
Item
Item
1
1
9
9
8
8
;
;
7
7
:
:
<
<
Sol"tion
Sol"tion
/
/
.
.
/
/
/
/
.
.
.
.
.
.
Benefit
Benefit
7
7
>
>
8
8
9
9
<
<
=
=
;
;
*eight
*eight
<
<
>
>
;
;
10
10
;
;
:
:
;
;
5napsack )ample
5napsack )ample

'olution 8 is clearly the best solution and has
'olution 8 is clearly the best solution and has
met our conditions, therefore, item number 9,
met our conditions, therefore, item number 9,
7, :, and < will be taken on the hiking trip. *e
7, :, and < will be taken on the hiking trip. *e
will be able to get the most benefit out of
will be able to get the most benefit out of
these items while still having weight e(ual to
these items while still having weight e(ual to
99 pounds.
99 pounds.

!his is a simple eample illustrating a genetic
!his is a simple eample illustrating a genetic
algorithm approach.
algorithm approach.
5napsack 4roblem
5napsack 4roblem
!o understand GA must work with the following
problem&
(Knap Sack Problem)

!hief wants to steal gold store.

!hief has a bag#the bag can hold a specific weight$.

)very piece of gold has a specific weight and price.

!hief wants to steal gold with high price but the
weight must e(ual or less than the weight that bag
can hold it.

If we gave every gold piece a specific number 1,9,8,
S,n#suggest n3> in this eample$.
. 1 2 3 4 5 9 6
weight 4 2 ./ 5 4 4 3 3
price .// 2/ 14/ .4/ 4/ 94 4/ 4/
.+Encoding (representation)
(geneCchromosome)
%hromosome co"ld 7e@

Bit strings #101101010100$.

+eal numbers #;8.1,;7.9,::.8,11.0$.

4ermutation of elements #)11 )8 )< S )1 )17$.

Integer Aumbers #11,19,7;,=>,:97,1$.

Any data structures.

In knap sack problem can represent any solution as
chromosome by using bit string of length >.
)&- 1 1 0 1 0 0 0 1
> < : 7 ; 8 9 1
1#gene$&this piece taken, 0#gene$&this piece untaken.
1+Initiali!e pop"lation

Implementers speci'y pop"lation si!e 8

To initiali!e pop"lation create chromosomes
randomly and store them in list o' length the
pop"lation si!e8

In o"r pro7lem lets taHe pop"lation si!e 5
chromosomes8

Fe can initiali!e pop"lation a 'ollowing@
. I././///.I
1 I/...//..I
2 I....////I
3 I//..//..I
4 I/./././.I
5 I////....I
2+E$al"ation o' pop"lation8

Giving every chromosome in population 'itness
$al"e by using 'itness '"nction.

Fitness '"nctions will differ according to the problem
and encoding techni(ue.

Fitness '"nction returns a single numerical#'itness$
which refle the "tility or the a7ility of the individual
which that chromosome represents.

,itness function can calculate & strength, weight,
width, maimum load,cost,construction time or
combination of all these.

,itness value well be stored with chromosome.
chromosome 1itness function 1itness value
-
In our eample we will make fitness function as the sum
of price of all gold pieces.
-!o complete our eample must apply fitness function
on all chromosomes.
. I././///.I 114
1 I/...//..I 2/4
2 I....////I 114
3 I//..//..I
114
4 I/./././.I 34/
5 I////....I 42/
Chromosome #itness "alue
. 1 2 3 4 5 9 6
weight 4 2 ./ 5 4 4 3 3
price 4/ 4/ 94 4/ .4/ 14/ 2/ .//
3+Selection o' new parents(reproduction)

Individuals are selected from population randomly or
by using any selection method to improve the
population itself.

Good individuals will probably be selected several
times in a generation ,poor ones may not be at all.


Methods of selection
Random ,est, !ournament, Roulette "heel,
!runcation, Rank, #$ponential, olt%man, Stead&
state, 'nteractive and binar& tournament
selection(

In our eample we will use Tr"ncation selection with
parameter 8.

search best 8 chromosomes and then select :
chromosomes randomly from these three
chromosomes and store them as new population to
be used in the net step.
chromosome 'itnes $al"e
. I././///.I 114
1 I/...//..I 2/4
2 I....////I 114
3 I//..//..I 114
4 I/./././.I 34/
5 I////....I 42/
chromosome 'itnes $al"e
. I/...//..I 2/4
1 I/./././.I 34/
2 I////....I 42/
2earch best *
chromosomes
chromosome 'itnes $al"e
. I/./././.I 34/
1 I////....I 42/
2 I////....I 42/
3 I////....I 42/
4 I/...//..I 2/4
5 I/./././.I 34/
3ew population
4ld population
4+%rosso$er

%rossover is performed with probability Pcross
#crossover probability or crossover rate $ between two
selected individuals, called parents, by echanging
parts of their genes to form two new individuals called
offsprin)s(

!he simplest method know as sin)le point
crossover.

'ingle point crossover take 9 individual and cut their
chromosome strings at randomly chosen position, to
produce 9 head segments and 9 tail segments .!he
tail segments are then swapped over to produce 9
new full length chromosomes.

Pcross #crossover probability or crossover rate $ is
typically between 0.: and 1.0

!here is also -"lti+?ointC Uni'ormC :it Sim"latedC
?ro7lem %entered and speciali!ed crosso$er
techni(ues.
chromosome 'itnes $al"e
. I/./././.I 34/
1 I////....I 42/
2 I////....I 42/
3 I////....I 42/
4 I/...//..I 2/4
5 I/./././.I 34/
we will do it 5ust
for first two
parents6
/cross&'6-
2elect two parent(1,2)
7enerate random number between '6'­16'('6*)
'6*8&'6-(yes) apply crossover
generate random number between 1­,(*)
old ' 1 ' 1 ' 1 0 1 ' ' ' ' 1 1 1 1
new ' 1 ' 1 ' 1 1 1 ' ' ' ' 1 1 0 1 s6ap tails
9o this for each pair in population6
5+-"tation

Applied to each child individually after crossover .

It alters some of genes in chromosome with small
probability .

-ust specify ?m"t#mutation probability that is
relatively small$ therefore a few number of
chromosomes will be mutated.

In our eample&
suppose ?m"t 30.9
generate number between 0-1 #0.01$
0.01T30.9#yes$ apply mutation.
Generate number between 1->#:$
0 1 / 1 0 1 0 1 3H 0 1 . 1 0 1 0 1
Ko this for each chromosome in population.
!ermination %riteria
!ermination %riteria
There e,ist three termination condition
type@
.+!ime@in secondsC in minutes and may be in
hours according to the problem that you have
it.
1+*umber of )enerations@ in hundreds, in
thousands may be in millions according to
the problem you have it.
2+conver)ence@ when =7Q of populations
have the same fitness value we can say the
convergence started to appear and the user
can stop its genetic program to take the
result.
!he 5napsack 4roblem
!he 5napsack 4roblem

!he knapsack
!he knapsack
problem, though
problem, though
simple, has
simple, has
many important
many important
applications
applications
including
including
determining
determining
what items to
what items to
take on a space
take on a space
ship mission.
ship mission.
Genetic Algorithms
Genetic Algorithms

Genetic Algorithms are a type of machine
Genetic Algorithms are a type of machine
learning for representing and solving comple
learning for representing and solving comple
problems.
problems.

!hey provide a set of efficient, domain-
!hey provide a set of efficient, domain-
independent search heuristics for a broad
independent search heuristics for a broad
spectrum of applications.
spectrum of applications.

A genetic algorithm interprets information that
A genetic algorithm interprets information that
enables it to re"ect inferior solutions and
enables it to re"ect inferior solutions and
accumulate good ones, and thus it learns
accumulate good ones, and thus it learns
about its universe.
about its universe.
Genetic Algorithm Application
Genetic Algorithm Application
Areas
Areas

D!namic process control D!namic process control

Induction o# rule optimi7ation Induction o# rule optimi7ation

Disco"ering ne6 connecti"it! topologies Disco"ering ne6 connecti"it! topologies

Simulating biological models o# beha"ior and e"olution Simulating biological models o# beha"ior and e"olution

Comple3 design o# engineering structures Comple3 design o# engineering structures

Pattern recognition Pattern recognition

Scheduling Scheduling

Transportation Transportation

8a!out and circuit design 8a!out and circuit design

Telecommunication Telecommunication

Graph9based problems Graph9based problems
:"siness Applications
:"siness Applications

Schedule ssembl! lines at :ol"o Truc2 (orth Schedule ssembl! lines at :ol"o Truc2 (orth
merica merica

Channel % Tele"ision );ngland* to schedule Channel % Tele"ision );ngland* to schedule
commercials commercials

Dri"er scheduling in a public transportation s!stem Dri"er scheduling in a public transportation s!stem

<obshop scheduling <obshop scheduling

ssignment o# destinations to sources ssignment o# destinations to sources

Trading stoc2s Trading stoc2s

Producti"it! in 6his2!9ma2ing is increased Producti"it! in 6his2!9ma2ing is increased

=#ten genetic algorithm h!brids 6ith other I methods =#ten genetic algorithm h!brids 6ith other I methods