0

I am going through a past exam paper and I'm trying to understand the following question:

Assume you have N cities. It's possible to go from each city to any of the other cities. Assume that you have full information about the distances between cities in a tabulated form. The distance between the city number k and the city number l is given by d(k,l); so for example, the distance from the third city to the ninth city is given by d(3,9). Note that d(k,l)=d(l,k).

A travelling salesman needs to visit all N cities and wants to find the shortest route that connects all of the cities. Use a genetic algorithm to solve this problem.

Question: Define an appropriate fitness function for this problem and say whether high or low fitness is better.

Does anyone know what I need to do for this question? I'm really struggling with where to start and need some direction.

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
7389573987
  • 15
  • 3

1 Answers1

0

With the TSP you want to minimize the distance travelled. There are lots of different ways of travelling the n cities; N! / 2 to be exact.

So if you have N = 4, you want an array of the integers 1-4, each only occuring once. So some possible options would be:

[1,4,2,3]
[4,1,2,3]
[3,1,4,2]

You then evaluate the score by going from city i to i+1 in the list, computing the distance. Do this for every city i in the list (but the last) and you have the total distance; this is your score!

So for the examples above the scores would be:

// Please note that the integers 1-4 represent cities
score([1,4,2,3]) = d(1,4) + d(4,2) + d(2,3)
score([4,1,2,3]) = d(4,1) + d(1,2) + d(2,3)
score([3,1,4,2]) = d(3,1) + d(1,4) + d(4,2)

You want to minimize the distance, thus minimize the score.


You could do this by creating mutation functions that swap two cities in a list for example:
[1,4,2,3] -> [4,1,2,3]
[1,2,3,4] -> [1,3,2,4]

This video shows an excellent example of a Javascript implementation of optimizing the distance through a genetic algorithm.

Thomas Wagenaar
  • 6,489
  • 5
  • 30
  • 73