You are on page 1of 18

c

Ê   

      


      
  


Project Report on Computer Vision (CS 408)

By

Group No: 03


  : S. P. Marasinghe(S/06/070)

M. L. D. N. M. Abeyawardena(S/06/205)

P. M. R. L. Bandara(S/06/224)

R. V. I. Jayawardene(S/06/324)

M. M. A. V. Thilakarathne(S/06/447)

BSc SPECIAL IN COMPUTER STUDIES

Ê   !!" Ê"#"!"


#!$!%  Ê!% 

!& ' 

()*)+()**
u


 ,& "

c. Genetic Algorithms............................................................................................. 3-c0
c.‘ Introduction ...................................................................................................... 3
2.‘ Encoding........................................................................................................ 4-7
3.‘ Crossover ....................................................................................................... 7-9
4.‘ Mutation........................................................................................................... c0

2. Solving travelling Salesman Problem using genetic Algorithms ................... cc-c8


c.‘ Travelling Salesman Problem(TSP) .............................................................. cc
2.‘ Solving TSP using Genetic Algorithms.................................................. c2-c8
c.c.c.c.‘Chromosome encoding
c.c.c.2.‘Fitness function
c.c.c.3.‘Initial Population
c.c.c.4.‘Selection‘
c.c.c.5.‘Cross over
c.c.c.6.‘Mutation
c.c.c.7.‘Insertion
Ô

Chapter 1
*-
!" &
!.

!  

Genetic algorithms are an optimization technique based on natural evolution. They include the
survival of the fittest idea into a search algorithm which provides a method of searching which does not need
to explore every possible solution in the feasible region to obtain a good result. Genetic algorithms are based
on the natural process of evolution. In nature, the fittest individuals are most likely to survive and mate;
therefore the next generation should be fitter and healthier because they were bred from healthy parents. This
same idea is applied to a problem by first ·guessing· solutions and then combining the fittest solutions to
create a new generation of solutions which should be better than the previous generation. We also include a
random mutation element to account for the occasional ·mishap· in nature.
The genetic algorithm process consists of the following steps

‡‘ Encoding-Create solution chromosomes


‡‘ Evaluation-selection of parents from population based on fitness value
‡‘ Crossover-crossover the parents to form a new offspring with crossover probability
‡‘ Mutation-mutate new offspring at each locus with mutation probability
‡‘ Accepting-Place offspring in a new population
‡‘ Replace-Use new generated population to further run of algorithm
‡‘ Go to step 2 if not found the best solution
‡‘ Return the best solution.

Where
‡‘ Crossover probability-How often crossover will be performed
‡‘ Mutation probability/ How often parts of chromosome will be mutated
‡‘ Fitness value-objective value.

The process of GA and its parameters will be discussed in detailed in next chapters
J

*-‘  
We must encode solutions as feasible chromosomes to apply GA for any optimization
problem.The techniques for encoding solutions vary by problem and, involve a certain amount of
art.It is important for problem solution to select proper encoding. Encoding represents
transformation of solved problem to N-dimensional space of real (integer) numbers (mostly).
Various encodings are listed below.

½‘ , 0  
Mostly used encoding technique is the binary encoding.Chromosomes in binary encoding are
strings of 0 or c. In which each bit in the string represents a certain characteristics of the solution.
?‘ 1 /'2 
The knapsack problem is a problem in combinatorial optimization, Given a set of items,
each with a weight and a value, determine the number of each item to include in a collection so that
the total weight is less than or equal to a given limit and the total value is as large as possible. It
derives its name from the problem faced by someone who is constrained by a fixed-
size knapsack and must fill it with the most useful items.
Each bit represents whether the corresponding thing is in (c) or not in (0) the knapsack

Let W=c0

i c 2 3 4
Value(i)[v] c0 40 30 50
Weight(i)[w] 5 4 6 3

Solutions can be

chromosome A cc00

chromosome B 0cc0
Œ

½‘ $   


Direct value encoding can be used in problems where some complicated values such as
real numbers are used. Use of binary encoding for this type of problems would be very difficult.
In value encoding every chromosome is a string of some values. Values can be anything connected
to problem form numbers, real numbers or chars to some complicated objects as shown below.
?‘ 1 /  3    32
There is some neural network with given architecture. Find weights for inputs of
neurons to train the network for wanted output
Real values in chromosomes represent corresponding weights for inputs
Solutions can be 

chromosome A c,5,7,9

chromosome B 4,6,c0,2

?‘   1 /nouns per event, letters per first line of your document.

½‘      
Permutation encoding is useful when individual fitness depends on positions of genes in
chromosomes. In that case chromosomes A and B shown below are different and also have different
fitness. While in value encoding, chromosomes A and B (see examples for value encoding) are the
same, because the point of interest are values of individual genes, not their rank. Main difference
between value and permutation encoding is that there are no genes with the same value in
permutation encoding chromosomes.
Permutation encoding is only useful for ordering problems. Even for this problems for some
types of crossover and mutation corrections must be made to leave the chromosome consistent (i.e.
have real sequence in it).

?‘ 1 /    45


There are cities and given distances between them. Travelling salesman has to
visit all of them, but he does not to travel very much. Find a sequence of cities to
minimize travelled distance.
Chromosome says order of cities, in which salesman will visit them.
0

City c 2 3 4 5

c 6 3 c9 c9 20

2 c9 3 c2 4 c9

3 6 2c 3 c8 c0

4 20 c0 5 c0 c4

5 6 25 c2 c8 cc

Solutions can be

chromosomes A 3425c

chromosomes B 5c423

½‘    
GAs may also be used for program designing and construction. In that case chromosomes
genes represent programming language commands, mathematical operations and other components
of program. In tree encoding every chromosome is a tree of some objects, such as functions or
commands in programming language.
Tree encoding is good for evolving programs. Programing language LISP is often used to this,
because programs in it are represented in this form and can be easily parsed as a tree, so the
crossover and mutation can be done relatively easily.

?‘ 1 / Finding a function from given values


some input and output values are given. Task is to find a function, which will give the
best (closest to wanted) output to all inputs.

Chromosomes are functions represented in a tree.


›

Chromosome A

(+ x (/ 5 y))

(-‘ " 
Crossover is basic chromosomes operation used for creation of new individuals. Because of
its definition, new chromosomes are mixtures of their parent chromosomes. New solutions produced
in this way are supposed to be in search space somewhere "between" parent solutions.Many
crossover techniques exist for organisms which use different data structures to store themselves.

½‘  /   

A single crossover point on both parents' organism strings is selected. All data beyond that
point in either organism string is swapped between the two parent organisms. The resulting
organisms are the children

1 /

  c0cc0ccc0c

0ccc0c0cc0

"  c0cc0c0cc0

 0ccc0ccc0c
x

½‘ 3/   

Two-point crossover calls for two points to be selected on the parent organism strings.
Everything between the two points is swapped between the parent organisms, rendering two child
organisms

1 /

  c0cc0ccc0c

0ccc0c0cc0

"  c0cc0c0c0c

 0ccc0ccc0c

½‘ "   

Another crossover variant, the "cut and splice" approach, results in a change in length of the
children strings. The reason for this difference is that each parent string has a separate choice of
crossover point.

1 /

  c0cc0ccc0c

0ccc0c0cc0

"  c0cc0cc0

 0ccc0c0ccc0c
 

½‘ # " 

Bits are randomly copied from the first or from the second parent

1 /

  c*)0c0**

*c0***0c

" cc0ccccc

½‘    

Some arithmetic operation is performed to make a new offspring

1 /

  cc00c0cc

cc0ccccc    

" cc00c00c (AND)


c

Ô-‘   
Mutation is a genetic operator used to maintain genetic diversity from one generation of a
population of algorithm chromosomes to the next‘

½‘ w
A few cities are chosen and exchanged

½‘ 60  w

A few cities are randomly chosen and exchanged only if they improve solution (increase fitness)

½‘ 0  60  w

Cities are systematically chosen and exchanged only if they improve solution (increase fitness)

½‘   w

The same as "random, only improving", but before this is "normal random" mutation performed

½‘ 0    w

The same as "systematic, only improving", but before this is "normal random" mutation performed
cc

Chapter 2

&$!
 $&&!
 & ,& #!

!"
&
!.

*-‘     45


The travelling salesman problem (TSP) can be described as finding a tour of a given number
of cities, visiting each city exactly once and returning to the starting city where the length of this tour
is minimized.



In another words its goal is to find the shortest distance between N different cities that the
salesman travels.
The standard or symmetric travelling salesman problem can be stated mathematicallyas follows,
Given a weighted graph G = (V, E) where the weight cijon the edge betweennodes i and j is a non-
negative value, find the tour of all nodes that has the minimumtotal cost.
However, the solution to this problem is not trivial when the number of cities increases. For the
case of three cities, six permutations representing viable paths are possible. If we consider the case of
20 cities in which there are over 2.4 billion permutations. If we exhaustively searched through all the
possible path traversals at a rate of 500 million per second, it would take over c50 years to examine
them all. There is obviously a need for a method to comb through this massive search space more
efficiently. Problems can be solved using genetic algorithm (GA).
We will see how to apply the GA for TSP.
cu

(-‘   


   
As with any problem we attempt to solve, the first step is clearly defining the problem to be solved,
the required parameters for obtaining a solution, and the encoding to be used to represent a solution to the
problem. Using a genetic algorithm to obtain an optimal solution for the TSP requires that we identify the
relevant parameters and their encoding in terms of chromosomes, genes, and populations. A quick inspection
of the TSP reveals the pertinent parameters for encoding a representative solution to the problem, a list of
cities to be traversed in order and the distance between two arbitrary cities. Now let's see how the problem
issolved using genetic algorithm terminology.
*-‘ "   
A chromosome represents a single solution in the search space of the problem being
optimized. According to the article a chromosome is an ordered list of cities in which each city is
represented only once, and their order determines the total distance traveled. With chromosomes
being a collection of genes, a gene for the TSP is simply an object representing a city (its name
and  and coordinates). For simplicity they have the longitude and latitude measurements as
artesian  and coordinates, respectivelyIn this case they have used permutation encoding
technique which is commonly used in TSP.
 1 6 7 6,6"6Ê68      9    6
  7 :":Ê:,:: 80     4 6"6Ê6,65-;  
 41605  

(-‘   
The fitness of a chromosome is calculated by traversing the genes in order, summing the
distances between adjacent cities. The chromosome with the lowest fitness score is the fittest as
it represents the shortest path between the cities. The article provides the source code in java in
which the fitness is calculated‘
‘
i ‘‘

 ‘
‘
‘‘ ‘
 ‘  ‘   ‘
‘ ‘
 ‘
‘‘‘ ‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘
 ‘‘ ‘ 


‘
‘
 ‘
 ‘‘
‘‘‘‘‘
‘‘‘
‘
‘

 ‘‘‘
‘

‘‘‘‘‘‘ !‘""‘

‘‘‘‘‘‘‘‘‘
#$%&‘'‘‘#$%&‘ ‘
#$%&‘(‘‘ ‘
‘‘‘!‘)‘'‘
‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘(‘‘#$%&‘ ‘
 ‘
‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘(‘‘#$%&‘ "'‘
‘
‘
‘"‘ * '+,‘'-,‘‘
(+,‘(-‘
‘‘‘‘‘‘‘‘.‘
‘
‘‘‘
‘
‘/  ‘ ‘  ‘  ‘0‘0
‘
‘ ‘‘‘
‘1',‘'‘
 ‘1(,‘(‘‘ ‘% 
 ‘# 
‘
‘
‘
 ‘ ‘
 ‘ * 
 ‘1',‘
 ‘',‘

 ‘1(,‘
 ‘(‘
‘‘‘‘‘

    +$2  ‘‘1(‘)‘1'‘3‘1(‘)‘1'‘

    -$2  ‘‘(‘)‘'‘3‘(‘)‘'‘
‘
4  
0   +$2  ‘"‘   -$2  ‘,‘5‘
‘‘‘‘.‘


3.‘ !  
According to the article they have encoded the genetic algorithm for the TSP so that an
initial population of 500 random chromosomes is created and sorted, and the c00 fittest
chromosomes are the ones selected for the genetic algorithm starting point.

The article describes the advantage creating a large initial population, Can cover a greater amount
of the solution search space initially and then pare down the population to the fittest to focus the
search using relatively strong chromosomes. Otherwise, the computational power required to
perform a genetic algorithm on a population of 500 chromosomes is far greater than that
required for c00 chromosomes.

J‘    ‘
Next step is to select parents for reproduction. The goal of selection is to choose parents
that are capable of creating fitter children while maintaining genetic diversity. What this means is
that we want the genetic algorithm to pick not only the fittest to reproduce, but also occasionally
the less fit chromosomes to maintain variation in the population. If a few relatively strong
cJ

chromosomes are continually selected for reproduction, it's likely that the algorithm will
converge on these "super" chromosomes since it's their genetic material that's most likely to pass
on to the next generation. And it may be the case that the chromosomes represent a local
minimum in the cost function rather than the global minimum we're searching for. The
technique they have used in the selectParents() method in implementation code is referred to
astournament selection. A small group of chromosomes is randomly selected, in this case six,
and the two fittest are selected to reproduce. Keep the tournament size small results in a smaller
selection pressure, thus increasing genetic diversity.


‘i 
‘   ‘
‘
‘‘(‘6/ 


‘
7‘‘
 ‘ 


 6/ 


89‘ % 
 $!‘
‘‘‘‘‘
6/ 


89‘ ‘‘0‘6/ 


8(9‘
‘‘‘‘‘‘‘‘: ‘  
‘‘0‘: 
 $!‘
‘
‘
‘;‘ ‘ 
‘
‘0 ‘ ‘
 

 6 1‘‘&: 
 <  
6,‘‘

 
!‘)‘
 $!‘
‘
‘
‘‘ ‘ ‘ 


‘ ‘ ‘‘
 ‘!‘

‘‘‘‘ 6 1‘‘ ‘ 6 1‘"‘‘

 $!‘""‘
‘‘‘‘‘‘‘‘‘
  
‘)‘ 6 1,‘
 
 ‘
‘‘‘‘‘‘‘‘.‘
‘
‘
‘
‘ ‘)
 
‘‘‘

  
‘
‘
‘
‘‘ ‘‘0
‘
 89‘‘6/ 


‘  
 ‘
 8'9‘‘6/ 


‘  
 '‘
‘
‘
‘ ‘
‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘.‘

9-‘ " 
Once parents have been selected, the crossover operator combines the two parents
randomly to create two children chromosomes (uniform crossover). A crossover probability
(90%) is used to determine whether crossover will occur for two given parents.
Continuing with the TSP example, a typical crossover operation includes selecting a chunk
of a chromosome and iterating through the other parent chromosome to extract the genes that
weren't selected from the first parent in order to combine them in a new child.
As discussed earlier, the TSP is basically a permutation problem in which gene uniqueness
must be maintained. For other problems, such as permutation without the uniqueness constraint
or real-valued problems, crossover takes on a completely different appearance. For the
implementation they have used the TSP crossover described above.
C  ‘
‘
33‘%
‘ ‘

‘0 ‘ ‘ 


‘  ‘ ‘
‘ ‘‘ ‘
‘‘
‘0
‘  ‘ 


‘0  ‘‘ ‘2‘
‘‘‘ ‘
 


,‘‘
 ‘ ‘ ‘‘0 ‘‘ ‘
‘  ‘
 ‘
‘‘ ‘ 


‘

‘
‘
 6/ 


89‘

6/ 


 


,‘
 ‘


 ‘
‘
‘
‘‘‘‘
‘!‘‘ $!‘
6/ 


‘
‘‘ 
‘
6/ 


‘ ‘‘ 



‘
6/ 


89‘  ‘‘0‘6/ 


8(9‘

 ‘ 
&: 
 <  
*
 '‘
‘
‘
‘ 
‘ 

 ‘
‘‘‘‘


%
&: 
 <  
6',!)'‘
‘
‘
6&89‘  '
&,‘

%
)'‘
6&89‘  ' = &  ',‘
&,!)'‘
6&89‘  (‘‘ &,‘

%
‘)‘'‘
6&89‘  ( ‘‘= &  (,‘
&,‘‘
!‘)'‘
  89‘‘0‘#$%/ 


  ',‘  ' ‘
  8'9‘‘0‘#$%/ 


  (,‘  ( ‘
c0

‘
‘
‘  ‘
‘‘‘‘‘‘‘‘.‘
‘
‘
‘‘
‘

,‘  ‘ ‘ ‘ ‘ ‘
  89‘‘
‘
  8'9‘‘ ‘
‘
‘
‘  ‘
‘‘‘‘.‘
‘
0-‘   
Two genes are selected at random from the chromosome and swapped with mutation
probability (5%).(random mutation)
33‘4 ‘ ‘ 


‘‘ 
 ‘ ‘0
‘‘  ‘0 ‘ ‘‘
<‘ ‘ 
‘ ‘ ‘‘ 
 ‘3 ‘
 ‘
 ‘ 
 ‘ 
 ‘
‘‘‘‘‘

 ‘ 
‘‘&: 
 <  
*
 '‘
‘
‘
‘ 
‘  
 ‘
‘‘‘‘‘‘
‘!‘‘ $!‘
‘
‘
‘'‘‘&: 
 <  
6,‘!‘)‘'‘
‘(‘‘&: 
 <  
6,‘!‘)‘'‘
‘
‘
6&‘'‘‘6&‘ &'‘
6&‘(‘‘6&‘ &(‘
‘
‘
(,‘'‘
',‘(‘
‘‘‘‘‘‘‘‘.‘
‘‘‘‘.‘
-‘ !  
Next step is to insert the children into the population and begin the selection, crossover, and
mutation process again. To preserve the strongest chromosomes within the population, a
concept calledÔ t  that protects the  ( = c0 in this TSP example) best chromosomes is
introduced and replaces the rest of the population with the newly created children.
33‘4
‘
‘‘  ‘
‘ ‘
 
,‘‘ ‘ ‘‘

‘ ‘
‘ 
‘3 ‘
‘
‘

 ‘
 ‘6/ 


89‘  ,‘ ‘

‘‘‘‘‘


 
‘
‘
‘

‘‘‘‘ ‘‘ 
 
!‘""‘

 
,‘  8‘)‘ 9‘
‘
‘
‘‘‘‘.‘

     4 - -6 5     3      
      < -

 
cx

 "
½‘ Michael Lacy, An Introduction to Genetic Algorithms in JAVA, viewed date February c8th 20cc
O u

     O 0Ô  O 
½‘ Kylie Bryant, Arthur Benjamin, Genetic Algorithms and the Traveling Salesman Problem,
Department of Mathematics ,Harvey Mudd College, December 2000

½‘ Encoding,viewed date February c8th 20cc


O 
 

 
   
 O 
 O
½‘ Genetic Algorithms,viewed date February c8th 20cc
O  uΠ
    
  O 
½‘ Crossover and Mutation,viewed date February c8th 20cc
O 
 

 
   
 O  

  
O
½‘ Crossover (genetic algorithm),viewed date February c8th 20cc
O     
   

!"  !
 O #

You might also like