A genetic algorithm that finds solutions for the Traveling Salesman Problem
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.


* The Traveling Salesman Problem

This project uses a Genetic Algorithm to solve the Traveling Salesman
Problem. The positions of the cities are given as coordinates on a 2D
plane. The script returns the cities in the order of one of the
shortest routes and can also write the route to an SVG image.

The travelling salesman problem (TSP) is an NP-hard problem in
combinatorial optimization studied in operations research and
theoretical computer science. Given a list of cities and their
pairwise distances, the task is to find the shortest possible route
that visits each city exactly once and returns to the origin city. It
is a special case of the travelling purchaser problem.

@<div class="source">[[http://en.wikipedia.org/wiki/Traveling_salesman_problem][Wikipedia]]@</div>

A set of 10 cities has 10!(factorial) = 3,628,800 possible routes. A
genetic algorithm can find an acceptable path in a small amount of
time by creating a pool of random routes and selecting the shortest
to create a new generation. Each generation random mutations are
introduced and pairs of routes are combined to create a new route.

* Usage

This runs the algorithm with an octagon:

python TravelingSalesman.py 26,2 62,2 87,26 87,62 62,87 26,87 2,62 2,26

You can also let it use random input:

python TravelingSalesman.py -r

Output a map to an image:

python TravelingSalesman.py -r -i map.svg