190 likes | 234 Views
Euclidean TSP EA. Pseudo Code. 1. 2. 4. 3. 5. What is a Euclidean TSP?. c ij 2 = (x i – x j ) 2 + (y i – y j ) 2. 1. 2. 4. 1. 1. 3. 2. 4. 5. 2. 3. 5. 5. 3. 1. 4. 4. 2. 3. 5. Individual Representation. Permutation of the cities
E N D
Euclidean TSP EA Pseudo Code
1 2 4 3 5 What is a Euclidean TSP? • cij2 = (xi – xj)2 + (yi – yj)2
1 2 4 1 1 3 2 4 5 2 3 5 5 3 1 4 4 2 3 5 Individual Representation • Permutation of the cities • What permutation represents this tour? • Solutions?
4 1 4 3 2 2 6 3 3 1 1 4 5 5 2 5 6 6 Permutation Creation • Create array • Number in-order • Loop i = 1 to N • j = random ≥ i • Swap(A[i], A[j]) [Skiena1997], Generating Permutations
Individual Datastructure • Generic solution is usually to have two data members • Genotype • Fitness • This way fitness is stored once calculated
Fitness Calculation • Sum up all the edge lengths • Requires many slow sqrt() calls
Program Requirements • Read from a parameter file • Execute one or more runs • Create a log file
Required Parameters • (world) TSP world • (logfile) Log file • (popsize) Population size • (offsize) Offspring per generation • (maxeval) Maximum evaluations • (runs) Number of runs • (seed) Random seed
Setup • Open configuration file • Parse values into variables • Read world file describing TSP world • Seed random number generator
Random Number Generator • Is a datastructure • Updates internal variables every time a random number is requested • Needs to be passed around by reference • Seed only once • Use the Mersenne Twister • http://www-personal.engin.umich.edu/~wagnerr/MersenneTwister.html
TSP World File Format • Line 1: “TSP” • Line 2: Number of cities • Lines 3+: x-coordinate y-coordinate • Two ascii integers per line separated by a space
EA Run • Pass in all configuration data • Make sure the random number generator is passed by reference • Allocate your data structures • population[popcount] • offspring[offcount] • Initialize population with random individuals • evalcount = popsize • While(evalcount < maxevals) • For # of children • Select two parents by two binary tournaments • Recombine using cut-and-crossfill • Mutate offspring • Evaluate child’s fitness (evalcount++) • Sort and elitist survival selection
Parent Selection • Create four random numbers a, b, c, d • Parent 1 • max_fitness(population[a], population[b]) • Parent 2 • max_fitness(population[c], population[d])
4 3 6 2 1 5 4 2 4 3 6 3 1 1 2 5 6 5 Recombination (“cut-and-crossfill”) Child [Eiben2003], p26
Recombination (“cut-and-crossfill”) • Used = boolean array of all false’s • XPoint = random from 1 to N • For (i = 1 to XPoint) • Child[i] = ParentA[i] • Used[Child[i]] = true • j = 0 • For (i = XPoint to N) • While(Used[ParentB[j]]) • j++ • Child[i] = ParentB[j]
Mutation • i = random from 1 to N • j = random from 1 to N • Swap(Child[i], Child[j]) 4 3 6 2 1 5 Child [Eiben2003], p26
Survival Selection • Combine populations • Sort • Crop
TSPView • Demo
References • [Eiben2003] A.E. Eiben and J.E. Smith. Introduction to Evolutionary Computing. Spring-Verlag, Berlin Heidelberg, 2003, ISBN 3-540-40184-9. • [Skiena1997] Steven S. Skiena. The Algorithm Design Manual. Springer-Verlag, New York, 1997.http://www2.toki.or.id/book/AlgDesignManual/