I'm using a genetic algorithm (GA) to optimise a traveling salesman problem (TSP). My problem is how I calculate the fitness of an individual. Obviously solutions with shorter routes are fitter but how exactly do I assign a fitness value without knowing what the shortest possible route and longest possible route is to determine where my solution fits in that range?
3 Answers
Having fitness equals to path length is fine. Keep in mind that in genetic algorithms the fitness is only used for selecting individuals: consequently with usual selection procedures the scale does not matter, only the rank does.
Examples of implementation:
- http://www.codeproject.com/Articles/1403/Genetic-Algorithms-and-the-Traveling-Salesman-Prob
- http://khayyam.developpez.com/articles/algo/voyageur-de-commerce/genetique/ (use Google translate)
- http://www.lalena.com/ai/tsp/
- http://www.mathworks.com/matlabcentral/fileexchange/13680
More subtleties (2001 - Swarm Intelligence - Kennedy & Eberhart - page 249):
Pablo Moscato is a South American researcher who has pioneered the study of memetic algorithms (e.g., Moscato, 1989). He and Michael Norman, who is now in Scotland at the University of Edinburgh, began working together in the 1980s at Caltech. In a recent paper they describe the use of a memetic algorithm for optimization of a traveling salesman problem (TSP) (Moscato and Norman, 1992). Recall that the TSP requires finding the shortest path through a number of cities, passing through each one only once. The problem has a rich history in applied mathematics, as it is very hard to solve, especially when the number of cities is large. TSP is an NP-hard problem, which suggests that if a way is found to solve it, then a large number of other problems will also have been solved. Moscato and Norman use an algorithm with both cooperation and competition among agents in the population, and implement a hybrid version of simulated annealing for local search.
A population of individuals—these researchers usually use a population size of 16—searches the problem space, which is defined by permutations of the cities, called “tours.” The population is conceptualized as a ring, where each individual is attached to its two immediately adjacent neighbors, with whom it competes in the search; individuals are also connected to others on the far side of the ring, with whom they cooperate. Each individual in the population comprises a tour of the cities. Competition is seen as “challenge” and “battles” between pairs of individuals, where the tour lengths of an individual and its neighbor are compared and a probability threshold is set based on the difference. The difference between the tours’ lengths affects the steepness of the sshaped curve; when the difference is small or the temperature is cool, the probability distribution becomes nearly uniform, and when the difference in lengths between the two tours is great, the probability is increased that tour 1 will be deleted and replaced with a copy of tour 0.
Cooperation is used to let more successful individuals “mate” with one another, rather than with less-fit members of the population. The same rule that is used in deciding competitive interactions is used to assess the desirability of partners for crossover, which is implemented just as it is in GA. One individual “proposes” to another, and if the proposition is accepted, that is, if the stochastic decision favors their interaction, then the crossover operator is implemented. Thus the next generation is created.

- 1
- 1

- 77,520
- 72
- 342
- 501
-
Thanks for the help. I didn't make my solution's fitness exactly equal to the path's length because then the shorter the distance of the path the lower solution's fitness would have been. To resolve this I did: fitness = 1 / pathLength – Undefined Aug 03 '12 at 16:13
You could normalise all candidate solutions, such that the shortest path you've seen to date gets the fitness score 1.0 (or 10, or 42, or 3.14... whatever you like), and then scale all paths longer than this relatively. Same with the longest path - the longest path that you've observed is considered the worst possible score.
The trick comes with what you do when you find an even shorter path (given that you assigned some longer path the highest possible score, such as 1.0) - you have to then raise the ceiling on your normalisation function. Start assigning fitness 2.0, for example (or 1.1, or some other arbitrarily larger fitness score).

- 13,735
- 44
- 51
If your program is maximizing fitness values, you would want to maximize a fitness function
f = - Tour-Length
EDITED: I had added 1000000000000, an arbitrary number to the fitness, to make the fitness positive, On reading a few comments, I realize it is not necessary.
If your program is minimizing fitness values, you would want to minimize a fitness function
f = Tour-Length

- 913
- 3
- 11
- 15
-
1If the program is maximizing fitness value, choosing fitness equals to minus path length would avoid fixing an arbitrary tour length. – Franck Dernoncourt Jul 31 '12 at 23:32
-
I'm sorry, I don't quite understand why, choosing fitness equals to minus path length would avoid fixing an arbitrary tour length? – rohanag Aug 01 '12 at 06:28
-
if fitness equals to minus path length, then fitness increases when the path length decreases :) – Franck Dernoncourt Aug 01 '12 at 10:30
-
1@rohanag Fitness doesn't need to be a positive number, so you don't need to introduce the arbitrary path length 1000000000000 at all. – Chris Taylor Aug 01 '12 at 11:10
-
-
@FranckDernoncourt So , if the GA program/package maximizes fitness, fitness increases, path length decreases, that's what we want don't we? – rohanag Aug 01 '12 at 16:28
-
@ChrisTaylor , you're right, but that depends on different GA implementations too, Once I tried negative fitness values in NSGA-II and it didn't work as expected :) – rohanag Aug 01 '12 at 16:30
-
@mitch , if you implement proper crossover, initialization, and mutation operators specific for GA, then your path length won't converge to 0. – rohanag Aug 01 '12 at 16:34
-
@rohanag The arbritrary number that you put (your 100...000) can be anything, even 0, in that case you fall on the special case of Franck Dernoncourt. Maximizing a negative fitness is the same as minimizing a positive fitness. I don't see why NSGA-II would not work with negative fitnesses ... It works pretty well in every implementation that I know (DEAP, OpenBEAGLE, ECJ, EO, inspyred, etc.) – mitch Aug 01 '12 at 18:15
-
@mitch , I misunderstood your previous comment, I thought you said a negative path length fitness function, would cause the solution chromosome path length to be = 0, which I didn't understand. But now I see what you right :) – rohanag Aug 02 '12 at 10:55