Click empty space to add a city, drag a city to move it, right-click or double-click to remove.
The science
The travelling salesman problem asks for the shortest closed tour L = Σ |cπ(i) − cπ(i+1)| through N cities. There are (N−1)!/2 distinct tours, so exact enumeration is impossible beyond a few dozen cities; the best exact algorithm (Held-Karp dynamic programming) costs O(N²2N).
A genetic algorithm keeps a population of tours. Selection: tournament of size k (the shortest of k random tours becomes a parent). Crossover: order crossover (OX) copies a random slice of parent A and fills the rest in parent B's order, so every child is a valid permutation. Mutation: 2-opt reverses a random segment (it removes two edges and reconnects them), the swap operator exchanges two cities. Elitism keeps the best tours unchanged, so the best length never increases.
Fitness is simply the tour length. For a regular N-gon of circumradius R the optimum is known exactly, L* = 2NR sin(π/N), and for an even grid with spacing s the optimum is N s; both are used as independent checks.
Honest simplifications: a GA gives no optimality guarantee, and for pure Euclidean TSP specialised solvers (Lin-Kernighan, Concorde) are far better. The population here is small, the random seed is not fixed, and "nearest-neighbour" is a greedy heuristic, typically 20-25% above optimum on random uniform instances.