Snake
A snake learns to hunt apples around a grid without boxing itself in. The genetic algorithm evolves a tiny neural network from wandering in circles to eating seventy apples a game.
Go to the live page Open the Snake demoThe Snake demo is the classic grid game. A snake moves one cell at a time around a walled board, eats the food, and grows a little longer with every bite. The genetic algorithm evolves a small neural network to steer it. The first snakes mostly wander until the game stops them for making no progress. Within a cycle or two the best snake hunts down apple after apple, grows long, and keeps clear of its own body for the whole game.
It lives alongside the other live-agent games (Flappy Bird, the Self-Driving Car, Dino Runner, Breakout, Pong, Gem Collector and Frog Crossing) under the same Live Agent demo. Use the tabs at the top of the page to switch between them.
The Problem¶
The board is 15 by 15 cells. The snake starts three cells long in the middle, facing right, and one piece of food sits on a random free cell. Hitting a wall or the snake's own body ends the run.
Steering is relative to the way the snake is facing: turn left, go straight, or turn right. That means the snake can never reverse into itself in a single move. For each of its three moves the snake senses three things: whether the move is dangerous (it hits a wall or the body, or leads into a dead end where the body would close it in), whether it brings the snake closer to the apple along a path around its body, and how much open space it leads into. One small network, three inputs, one hidden layer of eight neurons and one output, scores each move from its three readings, and the best-scoring move wins. Because the same network judges every direction, a lesson like "avoid danger" only has to be learned once.
Fitness rewards each piece of food with a big bonus, gives a little for every step toward the food and takes a little away for every step away from it. Each step also costs a tiny amount, so circling has a price. A crash costs as much as about ten apples, so a snake that takes risks for one more apple loses out to one that stays safe. A run also ends after 100 moves without eating, or after 960 moves in total (the same two minutes you watch, so every snake is tested for as long as it plays on screen), so a snake that loops forever can't pile up points. When that happens the board says so ("No progress" or "Time's up"), so you can tell it apart from a crash.
How a Run Looks¶
Click Start. The current best network plays a game live, with its score for each of the three moves shown each step. The chart on the right tracks the best score for each cycle. The food appears in a new order each cycle, so a good score means the snake learned to hunt, not to repeat one route.
The first generations wander aimlessly. Within a cycle or two the best snake heads for each apple in turn, steers around its own body, and keeps going until the two minutes are up. In our tests (24 runs, 10 cycles each), 238 of the 240 games we watched lasted the full two minutes, with about 70 apples each.
Playing It Yourself¶
Switch the mode toggle from Agent to You, or press M, to take over. Steer with the arrow keys, or on a touch screen or with a mouse, flick on the game in the direction you want to go. Each short flick turns the snake straight away, and you can chain several turns in one drag without lifting your thumb. Directions are absolute, and pointing straight behind the snake does nothing. Quick turns are queued, so pressing up then left in a hurry makes both turns on consecutive steps. The joystick appears wherever you touch or click the game, so you can play with your thumb in whichever spot is comfortable, and it stays there for next time. When the game ends, tap the game (or press Space, or Play again) to start a new round straight away. With no key pressed the snake keeps going straight. The move limits only apply to the agent, so in You mode you can take as long as you like. Press P to pause or resume and Esc to reset. Switch back to Agent at any time and the evolved network picks up from where you left off.
Settings Worth Trying¶
Open Settings to tune the genetic algorithm for the next run. These only affect Agent mode:
- Population size. More snakes per generation means more variety to select from, at the cost of a slower cycle. The default is 30.
- Generations per cycle. How much evolving happens between each live game you watch, from 1 to 15. The default is 2, so you can see it improve from cycle to cycle; raise it for a stronger snake sooner.
- Mutation rate. How much the networks are randomly perturbed each generation. A little keeps the search exploring; too much stops it from settling on a good policy.
What the Achievement Rewards¶
Signed-in users unlock the Snake charmer achievement the first time an evolved snake eats at least eight pieces of food in one of the games you watch in Agent mode. Your own games in You mode don't count toward it.
Snake records and shares like the other demos. See Sharing and recording for how to capture a clip of a long snake.
For more on how the demos catalog works, see What the Demos Do.