Neural Snakes

Snakes with small neural networks for brains, learning to play by evolution. A 2017 high-school project, rebuilt for the browser.

Generation 1 Tick 0 / 200 0 of 0 alive

The brain

The followed snake's brain at this moment, one block per layer, drawn the way the maths reads: y = f(W·x). The strip along the top is x, the values entering the layer; its last cell is a constant 1 for the bias. The square is the weight matrix W: each row is one neuron's weights, lined up under the inputs they multiply. The strip down the right is y, what each neuron outputs. It becomes the top strip of the next layer. Amber is positive, blue is negative. In the first layer's top strip, blue cells are walls or bodies and amber cells are food.

Parameters

Each setting says when it takes effect. Your settings are remembered in this browser.

Save and load

This population

Download every snake's brain, with the settings it needs, as a JSON file.

The 2017 snakes

The original program saved three generations of evolved snakes. These load the real files from this repository and switch the settings to match them.

How it works

Seeing

Each tick, a snake looks at a small window of cells in front of it, turned to face the way it's going: 5 rows by 5 columns in the original. Every cell becomes one input number: −1 for a wall or a body, 1 for food, 0 for empty. The optional food compass adds two more inputs that point at the nearest food.

Thinking

The inputs go through a multilayer perceptron. Each neuron multiplies every input by a weight, adds them up with a bias, and passes the sum through an activation function. A whole layer is a matrix of weights times a vector of inputs. The original's saved snakes have 25 inputs, two hidden layers of 100 neurons and one output: 12,801 weights each.

Steering

In the original, the single output is split into thirds: below 0.33 turns left, above 0.67 turns right, and anything in between goes straight. The alternative uses three outputs, one per choice, and takes the largest.

Evolving

Nobody trains these brains with examples. After each generation the snakes are ranked by how long they grew, then by how many different cells they visited, then by how long they lived. The best are paired up, and each pair has children: copies, mutated copies, mixes of the two parents' weights, and some brand-new random snakes. Over hundreds of generations, brains that happen to steer well take over.

Two quirks of the 2017 code are kept as options: its activation computed 1 / (1 + eˣ), a mirrored sigmoid, and every mutant had all of its weights nudged. The original C# project is still in the repository, in Atestat_Iova_Rares_AI.