🚚 Delivery Route Optimizer — Simulated Annealing Live
Watch simulated annealing anneal a delivery fleet's routes across a city map, escaping local minima with controlled random jumps as total distance drops toward the optimum.
About this simulation
Delivery routing is one of the oldest hard problems in operations research: given a depot and a set of stops, find the shortest closed tour that visits every stop exactly once — the traveling salesman problem. This simulation implements a genuine simulated annealing optimizer over that problem. A real 2-opt neighbourhood (segment reversal), a real geometric cooling schedule, and the real Metropolis acceptance rule all run continuously in the browser, and you watch the current tour and the best-so-far tour redraw live on the city map as total distance falls.
🔬 What it shows
Every animation frame proposes several random 2-opt moves: two positions in the tour are chosen and the segment between them is reversed, which is equivalent to swapping two edges for two others. The exact change in tour length (Δ) is computed from just the four affected edge lengths. If Δ < 0 the move is always kept; otherwise it is accepted with probability e^(−Δ/T). Temperature T decays every iteration as T ← α·T, so early on the tour bounces around and even gets longer sometimes, and later it settles into smooth, monotonic improvement.
🎮 How to use
Drag the delivery stops slider (8–40) or click "New random map" to generate a fresh city layout. The cooling rate α controls how slowly temperature falls — values near 0.9999 explore far more before committing, values near 0.985 behave almost like pure greedy 2-opt. Initial temperature sets how aggressively early moves are accepted. Steps-per-frame controls playback speed. Restart re-shuffles the tour and resets the schedule on the same map; Pause freezes the anneal so you can inspect the current state.
💡 Did you know?
Simulated annealing takes its name — and its acceptance rule — directly from metallurgy: heating a metal and cooling it slowly lets its atoms find a low-energy, low-defect crystal lattice, while cooling it too fast "freezes in" a disordered, higher-energy structure. Kirkpatrick, Gelatt and Vecchi applied exactly this physical analogy to combinatorial optimization in 1983, and route optimization — the traveling salesman problem — was one of their original test cases.
Frequently asked questions
What is simulated annealing and why is it used for route optimization?
Simulated annealing is a probabilistic optimization technique inspired by the metallurgical process of heating a metal and slowly cooling it so its atoms settle into a low-energy crystalline structure. Applied to the traveling salesman / vehicle routing problem, the "energy" is the total tour distance. At high temperature the algorithm accepts many worsening moves, letting it explore widely and jump out of poor local arrangements; as temperature falls it becomes increasingly greedy, refining the tour until it converges near a short route. It is popular for routing because the search space of possible stop orderings is factorial in size, far too large to search exhaustively, yet 2-opt neighbourhoods combined with annealing reliably find tours within a few percent of optimal.
What is a 2-opt move and why reverse a segment?
A 2-opt move removes two edges from the tour and reconnects the four endpoints the only other way that keeps a single closed loop, which is equivalent to reversing the order of the stops between the two cut points. It is the simplest local-search move that can uncross a tour: whenever two route segments cross on the map, exactly one 2-opt move straightens them out and shortens the total distance. Because only two edges change, the change in tour length (the delta) can be computed by comparing just those two old and two new edge lengths, without re-summing the entire route.
What is the Metropolis acceptance criterion?
After a candidate move's cost delta is computed, the algorithm always accepts moves that shorten the tour (Δ < 0). For moves that lengthen it, it accepts with probability e^(−Δ/T), where T is the current temperature. This means a large worsening move is rarely accepted, but small worsening moves are still fairly likely early on when T is high. As T decays toward zero, e^(−Δ/T) collapses toward zero for any positive Δ, so the algorithm effectively becomes pure greedy descent — hill-climbing is disallowed and only improving moves survive.
How does the cooling schedule affect the result?
This simulation uses geometric cooling: T is multiplied by a cooling rate α (close to but below 1) after every proposed move, so T decays exponentially with iteration count. A cooling rate very close to 1 (e.g. 0.9995) cools slowly, giving the search many iterations at higher temperatures to explore broadly before it commits to refining a solution — this typically finds shorter tours but takes longer to settle. A lower cooling rate (e.g. 0.985) cools fast and behaves almost like greedy 2-opt local search, converging quickly but more likely to get stuck in a mediocre local minimum.
Why does the tour length sometimes get worse before it gets better?
That is the entire point of annealing: at high temperature the Metropolis criterion deliberately accepts some length-increasing moves. A tour can look locally optimal (no single 2-opt move improves it) while still being far from the shortest possible tour — this is a local minimum. By occasionally accepting a worse move, the search can climb out of that local minimum's basin and later fall into a different, shorter one. Watching the distance chart, you'll typically see it fall quickly at first, occasionally spike upward while T is still high, and then settle into a smooth monotonic decline as T approaches zero.
How is this related to real-world delivery route planning?
Real logistics companies solve vehicle routing problems (VRPs) with hundreds or thousands of stops, multiple vehicles, time windows and capacity limits — an NP-hard combinatorial problem where exact solutions are computationally infeasible past a few dozen stops. Metaheuristics like simulated annealing, along with genetic algorithms, ant colony optimization, and tabu search, are industry-standard tools for finding very good (though not provably optimal) routes in seconds to minutes. This simulation models the single-vehicle core of that problem — the classic traveling salesman problem — which is the same combinatorial engine at the heart of production routing software.
A real 2-opt neighbourhood, a real geometric cooling schedule, and the real Metropolis acceptance criterion run every frame — the current tour and the best-found tour both redraw live as total distance and temperature evolve.
3D · Three.js / WebGL renderer · 60 FPS target · runs fully client-side, no install