The “Snake” animation

Oct 28, 2018 | General

What’s up with the little serpents at and how they learn autonomously with Artificial Neural Networks

Designing our website, we wanted to have a cool visual on the landing page. But what could it be?

We straight out rejected a generic “particles” animation that literally 90% of the blockchain-related websites use. We wanted to be original and to express in the best way possible how ScyNet evolves.

This is how we came up with the idea to use genetically evolving Neural Networks. And visualize it with snakes learning to avoid obstacles and eat the “food” pieces, scattered over the screen.

Before we explain the mechanics behind the snakes’ decisions, here’s a quick heads-up on the concepts we use — Artificial Neural Networks and Genetic Algorithms. If you’re familiar with them, you can skip straight after.

What is an Artificial Neural Network?

An artificial neural network is a framework, used widely in machine learning algorithms which in general attempts to mimic how our brain works. In most cases, it consists of an input layer, a hidden layer(s), and an output layer or neurons. The input layer is where the data to our problem goes in. This data is then processed by the hidden layers and the final answer is generated in the output layer. Neural networks are really useful for finding patterns in problems which are generally hard for a programmer to solve by hand.

What is a Genetic Algorithm?

Genetic algorithms are inspired by Charles Darwin’s theory of natural evolution. They reflect the process of natural selection where the fittest individuals of a population are selected for reproduction.

If you want to know more about the principles of how neural networks and genetic algorithms work, which is not in the scope of this article, I recommend the video series on Neural Networks from the Youtube channel “3Blue1Brown” and the Genetic Algorithms series from the Coding Train.

So, how does our snake animation work?

In our animation, you will find a population of 15 serpents. Each snake’s DNA is represented by a neural network.

Input and decisions

Every neural network takes input via 50 sensors at the tip of its snake’s head, which are used to detect information about its surroundings. Sensors detect the distance to the nearest object, and tell whether it’s:

  1. An enemy — the wall or another serpent or

2. A delicious snack

This information is then used as data for the input layer of the snake’s neural net, which in turn returns a single value between 0 and 1. If the value is between 0 and 0.45, the snake will turn left. And if the value is between 0.55 and 1, the snake will turn right. Otherwise, it will move forward. The angle of the maneuver depends on how close the output value is to the borderlines. For example, a 0.1 will result in much sharper left turn than a 0.44.

Measuring performance

The metric which tells us how the snake is performing is its fitness function. Its value depends on how many times a snake has died and how much food it has eaten. Deaths lower the fitness and reaching a meal increases it, although the negative effect of death is stronger than the positive feedback of eating. Meaning our snakes tend to prioritize survival.

Evolution and mutations

After a certain time-length has passed, which we call Generation length, seven of our best-performing snakes are chosen to create the next generation of 15 snakes. Each new snake is created by applying a cross-over of the genes of two randomly picked snakes from the former seven. Therefore, parts of the parent neural networks’ parameters are randomly mixed in together, and give us a new “offspring” neural network which combines the characteristics of its parents.

In order to also create new genomes, which behave in new ways, not shown in their parents, we apply a mutation to each new offspring. This simply means that some of its genes are randomly modified. The probability of a mutation is a function of performance. The better the fitness, the lower the mutation chance.

Special thanks to the Github user Elyx0, whose project snakeneuralnetworkjs, helped us a lot in the making of this animation. We have made a few modifications, though. For example, our selection process is a little simpler than his Round-Robin tournaments where serpents fight one-on-one. Also, we have added incentive in the face of enticing snacks, randomly generated across the screen.

Currently, the evolution of snakes takes place only in the visitor’s browser. In the future, however, we plan on creating a back-end implementation. When somebody finds a better genome in their browser, this new generation would be sent and stored on the server. This means that snakes would evolve collectively throughout all website visitors. And it’s curious to see how the new ‘supersmart’ serpents would behave.

Check out the source code of our snake animation here.

What about ScyNet?

We chose this animation, as we saw it as a good way to illustrate how ScyNet autonomously learns to self-improve. If you want to learn more about ScyNet and how it works, here’s a quick introductory read.

And feel welcome to join us on Discord, where we discuss decentralized AI and share updates on how the ecosystem is evolving.