Visualizations
404 / 500

404 · Algorithms

Elastic Net TSP

A rubber band of neurons wriggles through a map of cities until it becomes a tour.

Durbin and Willshaw's elastic net (1987) is a closed ring with 2.5 neurons per city. Each city pulls on the ring through a Gaussian of width K, normalized so every city spends the same total pull, while tension keeps neighboring neurons together, and K shrinks by one percent every 25 iterations. The glowing field underneath is the landscape the band descends, the sum of every city's Gaussian at the current K: broad at first, so the band only feels the shape of the cloud, then sharpening until each city has a neuron sitting on it and the order of the neurons around the ring is a tour. A nearest neighbor tour improved by 2-opt and Or-opt runs alongside as a baseline, and the chart compares the two lengths, which usually land within a few percent of each other.

Try it. Click to add a city and drag a city to move it, even mid-solve, and the band reheats a little to adapt; right-click removes one. The buttons or keys pick new cities (R), cycle layouts such as clusters, spiral arms and rings (L), overlay the 2-opt tour (B) and pause (Space).

  • Elastic net
  • Self-organizing map
  • Deterministic annealing
  • 2-opt and Or-opt

View the source · one module, plus a small shared runtime for sizing, the animation loop and input

Build your own

Paste this into Claude Code, Codex or any coding agent to get a simple version running, then take it wherever you like.

Build an elastic net traveling salesman solver with JavaScript and the HTML canvas element. Put everything in a single index.html file with no libraries or build step, so I can open it directly in a browser.

Start simple:
- Make a canvas that fills the window, stays sharp on high-DPI screens (scale by devicePixelRatio), and has a dark background.
- Place about 100 random cities in a unit square, scaled to the screen.
- Make a closed ring of 2.5 times as many neurons, starting as a small circle at the center of the cities.
- Each iteration, for every city compute a weight for every neuron, exp(-d^2 / (2 K^2)) where d is their distance, and divide by the sum over neurons so each city's weights add up to 1. Move each neuron by 0.2 times the weighted sum of (city - neuron) over all cities, plus 2 K times (next neuron - 2 times this neuron + previous neuron) for the ring's tension.
- Start K at 0.2 and multiply it by 0.99 every 25 iterations. Run about 15 iterations per frame with requestAnimationFrame.
- Draw the ring as a closed line through the neurons and the cities as small bright dots.

Once that works, make it beautiful:
- Draw the ring as a glowing band whose color slowly shifts along its length, with a faint thread from each city to its nearest neuron.
- When K is small, read off the tour by sorting the cities by the index of their nearest neuron, and show its length.
- Let me click to add a city and drag cities while it solves.

Explain the key ideas in short code comments. When you're done, tell me how to open it and suggest three directions I could take it next, such as comparing against a 2-opt tour, drawing the summed Gaussian field under the band, or trying clustered and spiral city layouts.
PreviousCollatz CoralThousands of Collatz sequences grow backward from 1 into a swaying coral. NextMirageBent light over hot asphalt and cold sea: road puddles, floating cars, Fata Morgana.

Related visualizations

  • Self-Organizing QuiltMachine learning Kohonen maps sew a quilt from fabric dyes and drape a sheet over 3D point clouds.
  • One Line PortraitType and image Thousands of stipple dots become one unbroken pen line, untangled by 2-opt.
  • Spiking RasterMachine learning A thousand Izhikevich neurons learn by spike timing and ripple with travelling waves.
  • NEAT SpeciesMachine learning Networks grow their own topology to balance two poles, while species rise and fall.
  • t-SNE ConstellationsMachine learning 2,000 glyph images in 196 dimensions flow into constellations under live t-SNE.
  • Manifold UnrollMachine learning Isomap and LLE unroll a paper Swiss roll, and tear it when k is too large.
  • CPPN GardenMachine learning Breed images grown by tiny neural networks by picking the ones you like.
  • Lottery TicketMachine learning Prune a network to a sparse winning ticket that still learns, round by round.
  • Neuron GrowthNature A Golgi-stained circuit grows its dendrites and axons, wires up, then fires.

Use ← and → to move between demos. While the canvas has focus, keys go to the demo instead.

← More from Emergent Mind Labs