A snake game where two AIs compete and improve
My snake AI started as a neural network I trained on a Raspberry Pi 5. Now it plays in the browser against a pathfinder, and both tune themselves in a league that keeps training on the visitor's device.
Results
37first DQN's best score, before it forgot
75league rounds trained offline so far
98%of the board the Pathfinder fills on average
The problem
My first DQN, trained on a Raspberry Pi 5, climbed from a score of 1 to 37 over about 2,200 games, then forgot it over the next several thousand. A plain pathfinder played better, but it never got any smarter.
What I did
- A Double DQN rebuilt from the Pi version, with a bigger replay memory and steadier exploration, after the first network forgot what it had learned.
- A Pathfinder that runs a breadth-first search to the food every move and checks it can still reach its own tail before eating.
- A league where an evolution strategy tunes the Pathfinder and population-based training tunes the DQN settings. Both play the same games and are scored for food collected and path efficiency.
- A judge adjusts the weight of score and efficiency within limits that keep both measures in use.
- Training that runs offline on 12 worker threads or in the visitor's browser, on the graphics card through WebGPU when that's faster.
What I took from it
The Pi network lost performance after its best run. The league keeps a champion and replaces it only after a challenger does better on the same games, which limits that kind of regression.