Neural Network to play a snake game – Towards Data Science part 1

in #neural-network7 years ago

something about machine learning, neural networks, and TensorFlow but there is no problem otherwise.

And finally, obviously, there are better approaches to write a logic for a snake game but let’s pretend that it is a real task and we need to solve it this way.

Part 0: The game

Firstly we need to write a game itself. It will have a 20x20 field, a snake of 3 pieces at the start, one randomly generated apple at each moment in time and API to use with our network. You can find a code of the game here.

Now let’s start with a neural network.

Part 1: Survive

Features

To make the snake “smart” we need to give some knowledge to it — we need to create **features **to teach it. Always try to choose features which will be most useful. If you add not enough features, a network will not get enough information to be good. From the other side, if there are too many features. it will be hard for a network to decide which are more important and learning will be longer.

At the first step, we will learn the snake how to survive and will not think about apples. To choose a right direction it should know if there are any obstacles around it. Considering these obstacles and suggested direction the network will decide is it a good action or not.

So on the input of our neural network we will give an array of 4 numbers:

  • Is there an obstacle to the left of the snake (1 — yes, 0 — no)
  • Is there an obstacle in front of the snake (1 — yes, 0 — no)
  • Is there an obstacle to the right of the snake (1 — yes, 0 — no)
  • Suggested direction (-1 — left, 0 — forward, 1 — right)

And as the output we want to receive a decision. 1 — we should go in the selected direction, 0 — we should choose another one.
To be continued.
UPVOTE PLS!
website: https://towardsdatascience.com/