Solution representation
When solving the TSP, the cities are typically represented by numbers from 0 to n-1, and possible solutions will be sequences of these numbers. A problem with five cities, for example, can have solutions of the form [0, 1, 2, 3, 4], [2, 4, 3, 1, 0], and so on. Each solution can be evaluated by calculating and totaling the distances between each two subsequent cities, then adding the distance between the last city to the first one. Consequently, when applying the problem using the genetic algorithms approach, we can use a similar list of integers to serve as the chromosome.
The Python class described in the next subsection reads the contents of TSPLIB files and calculates the distances between each pair of cities. In ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access