AI Learns to Win at Snake using GA and NN
Chris Wilkerson
Unknown
- 0 Collaborators
Using a genetic algorithm and neural network, an AI is able to learn to solve a 10x10 grid and use that knowledge to tackle a 50x50 grid. ...learn more
Project status: Published/In Market
Overview / Usage
The problem is simple. Can an AI learn to play Snake?
Generally when people think of a neural network they think of supervised learning. With supervised learning you have some training data that consists of inputs and outputs. You can use those inputs and outputs to train the weights of a neural network to create a model. But what if you don't have training data? In this project I look at how to use evolutionary traits from a genetic algorithm to learn the weights of a neural network.
Methodology / Approach
First it's important to think of what tools you want to use. For this I wanted to use a neural network that took in information in all directions and output which direction to go (up, down, left or right). Knowing that I need to take in information of the surroundings made me think of what was important. Really the only things the AI can see are a wall, the apple, or a part of itself. Because we want equal weights for each portion, we can use one-hot encoding for what it sees. Playing a game of Snake myself made me realize I pay attention to which direction I'm moving in. This lead me to create an additional one-hot encoding for the direction the tail of the snake is moving in.
Since this is a neural network, we need a way to train the weights. By using a population of snakes, we can evolve the population to slowly learn to play the game. The main parts of a genetic algorithm used for this are: selection, crossover, and mutation. Because there is no backpropagation, there isn't really a need for a framework like TensorFlow. Instead I chose to use numpy to hold the weights and PyQt to handle graphics.
Technologies Used
Python3.6+, numpy, and PyQt are all that are needed.