Genetic TSP

Watch evolution untangle the traveling salesman, one generation at a time.
Click on the canvas to add a city. Drag the "Cities" slider, or hit "Random cities".
Generation
0
Best distance
—
Cities
0
Population
0

How it works

Each individual is a full tour — a permutation of every city. Fitness is 1 / total distance, so shorter loops win. Every generation runs tournament selection (pick a few, keep the best), order crossover (OX) to splice a slice of one parent into the city order of another while keeping the permutation legal, then swap / reverse mutation — reversal is what unties the tangled crossings. The current champion is copied forward unchanged via elitism, so the best score never regresses. The small graph (top-right) tracks best distance over time.

Why it matters

TSP is NP-hard: the number of possible tours grows factorially, so brute force is hopeless past a handful of cities. Genetic algorithms don't guarantee the optimum, but they reliably converge to near-optimal solutions for routing, logistics, chip layout, and scheduling — a vivid demonstration of evolutionary search on combinatorial optimization.

← Gallery