Routing a fleet of delivery vehicles efficiently is a version of one of the most famous problems in computer science: the Travelling Salesman Problem (TSP). Given a depot and a set of customers, what order should they be visited in to minimise total distance travelled? For anything beyond a handful of stops, checking every possible order is computationally hopeless — 16 customers alone have over 650 billion distinct routes. Real logistics software instead uses metaheuristics that search intelligently without ever guaranteeing the perfect answer, and the genetic algorithm (GA) is one of the oldest and most intuitive of these.
This simulation evolves a population of candidate delivery routes generation by generation. Each generation, routes are scored by total distance, fitter (shorter) routes are more likely to be selected as parents, order crossover combines two parent routes into a valid child route, swap mutation nudges routes away from their current shape, and elitism guarantees the single best route survives untouched. Watch the best route drawn live on the map and the distance-over-generations chart trend downward as the population as a whole gets fitter — occasionally plateauing at a local optimum before a lucky mutation breaks through.
What is a genetic algorithm?
A genetic algorithm (GA) is a search heuristic inspired by natural selection. Instead of deriving a solution analytically, a GA maintains a population of candidate solutions — here, complete delivery routes — and repeatedly applies selection, crossover and mutation to breed new candidates. Fitter individuals (shorter routes) are more likely to pass their structure to the next generation. Over many generations the population's average quality rises even though no individual route was ever solved directly, because the search explores many regions of the solution space in parallel and keeps recombining whatever works.
What do crossover and mutation actually do here?
Each route is a permutation of customer stops, so ordinary crossover would produce invalid routes with repeated or missing customers. This simulation uses order crossover (OX): a contiguous slice of stops is copied from parent A at the same positions, and the remaining stops are filled in from parent B in the order they appear, skipping any stop already placed. This guarantees a valid permutation. Mutation is a swap mutation: with probability equal to the mutation rate, two randomly chosen stops in a route swap places, nudging the route away from its current shape without ever creating an invalid tour.
Why does elitism matter?
Selection, crossover and mutation are all stochastic, so a generation can by chance produce a population that is, on average, worse than the one before it — crossover can break up a good route and mutation can damage a near-optimal one. Elitism copies the single best route from the current generation directly into the next generation, completely unchanged. This guarantees the best distance found so far can never get worse from one generation to the next, which is why the "best distance" curve in the chart is always flat or falling, never rising.
This is a small vehicle-routing variant of the Travelling Salesman Problem (TSP): find the shortest closed tour starting and ending at a depot that visits every customer exactly once. TSP is NP-hard — the number of possible routes for N customers is (N−1)!/2, which for just 16 customers is over 650 billion. Exact algorithms (branch-and-bound, dynamic programming) can solve modest instances but scale poorly. Genetic algorithms, along with other metaheuristics like simulated annealing and ant colony optimisation, trade a guarantee of optimality for a route that is usually very good and found in a fraction of the time — exactly the trade-off real logistics software makes for fleets with dozens or hundreds of stops.
If the whole population converges toward routes that share the same basic structure, crossover between two similar parents mostly reproduces that same structure, and small swap mutations are rarely enough to escape a locally good but globally suboptimal loop — for example a route with one avoidable crossing edge. This is called premature convergence: diversity in the population collapses before the best possible tour is found. Raising the mutation rate, increasing population size, or using a fresh map to compare runs are ways to see this trade-off between exploration (diversity) and exploitation (refining what already works) play out.
Tournament selection picks a small random subset of the population (a tournament, here of size 3) and chooses the fittest member of that subset as a parent. It is simple, fast, and its selection pressure is easy to tune via the tournament size: a larger tournament makes it more likely the single best individual dominates reproduction (faster convergence, higher risk of premature convergence), while a smaller tournament keeps more diversity. This avoids some pitfalls of fitness-proportionate (roulette-wheel) selection, where one route with an unusually short distance can dominate the whole population immediately.
A larger population explores more of the route-permutation space per generation and is less likely to lose useful diversity to random drift, but each generation costs more distance evaluations. A higher mutation rate injects more randomness, helping escape local optima but also disrupting good routes more often, which can slow convergence or even worsen the population average temporarily (elitism protects the single best route regardless). In practice there is a sweet spot — roughly population sizes in the tens to low hundreds and mutation rates of a few percent per gene tend to converge fastest for problems of this size.