What you're watching
A cart slides along a rail. On top of it is a pole on a hinge, and on top of that pole, if you pick Two, another one. Left alone they fall over. A small neural network watches the cart's position and speed and each pole's angle and spin, and decides, 120 times a second, how hard to push the cart left or right. Balancing a broom on your hand is the same problem.
Each generation has a hundred or so networks, all trying from the same start. The bright cart is the one doing best right now; the faint lines are the others still standing. A try ends when a pole leans too far, the cart hits the end of the rail, or 20 seconds pass. The networks that lasted longest (and stayed near the middle) are the parents of the next generation: their weights are mixed and a few are nudged at random. Over the generations the pushes go from flailing to small, quick corrections.
Things to try
One pole first. One pole on a cart (the classic "cart-pole") is usually learned in a few generations. Two poles take far longer, because the top pole reacts to the bottom one: to move the tip left the cart first has to go right.
Give it a push. Tap the left or right half of the screen once it balances. A well-trained network catches it; one that only learned the exact start doesn't.
Swing up. Start hanging down. The cart has to rock back and forth to pump energy into the swing, then catch the pole at the top. With one pole this is learned in a few dozen generations; with two it's very hard.
Chaos. Three free double pendulums let go with the top arm a thousandth of a degree apart. For a few seconds they move as one, then they go completely different ways. That sensitivity to tiny differences is chaos, and it's why the network can't follow a fixed plan: it has to keep watching and correcting.
How it works
The physics is the real equations of motion for a cart with one or two hinged rods (light rods with a weight at each end), worked out with Lagrangian mechanics and stepped forward with the Runge-Kutta method. The cart weighs 1 kg, each weight 0.3 kg, each rod is 0.6 m long and the most the cart can be pushed with is 20 newtons. The network has one hidden layer of tanh neurons and one output, the push. In Balance a network scores a point a second it stays up, a little less the further it drifts from the middle; in Swing up it scores by how high the tip is, with a bonus for each moment it's really up. Your settings are saved in this browser; nothing is sent anywhere.