Skip to content

Brady-Simon/PySnake

Repository files navigation

AI In Action

Fancy Snake UI

PySnake

An AI-enabled Snake that allows you to train a PyTorch neural network and see the trained network play the game in a Tkinter application. This project was made by Brady Simon and Gerald Arenas as a way to learn more about neural networks and how representing the same information differently can cause different outcomes. The window can be customized to look basic or have fancier colors and gradients.

Basic Snake UI Fancy Snake UI

There are three different neural network files:

  1. NeuralNetwork.py
  2. VisualNeuralNetwork.py
  3. GeneticSnakeAI.py

Once a model is trained (or loaded from one of the stored versions), you can set it as a snake's controller and have it play the game through a SnakeWindow instance.

General Information

A Board object stores all of the marks on the board. The SnakeBoard manages the Snake instances on the board and updates the board. Snakes have a SnakeControllable instance called controller that defines which direction a snake should move given a SnakeBoard and the snake's name.

SnakeWindow can play the given SnakeBoard until the game ends, at which point a reset function can continuously play the game. Each neural network implements SnakeControllable so that SnakeBoard can automatically update its desired direction when requested.

SnakeWindow.py

This is a Tkinter window that plays the given SnakeBoard instance. It allows either AI-controlled snakes or human-controlled snakes, as well as other customizations like gradients, toggling gridlines, and a snake health bar.

NeuralNetwork.py

A simple starter neural network that attempts to train a snake neural network based off recorded games played by people. This model only has a few inputs to keep the model simple:

  1. Safe directions (up, down, left, right around snake's head, binary)
  2. Length of snake (segments / 100)
  3. Directions to fruit (estimated by a number between 0 and 1)

Total Input: 5-length Tensor.

Total Output: 4-length Tensor (direction to choose).

VisualNeuralNetwork.py

A larger version of NeuralNetwork that uses more inputs in an attempt to achieve a higher score. This model has several inputs, but trains the same way as NeuralNetwork:

  1. Current snake direction (up, down, left, right, 0/1)
  2. Directions to point (up, down, left, right, up/right, down/left, etc, binary)
  3. Safe directions (up, down, left, right around snake's head, binary)
  4. Cardinal vision around the snake (8 directions, 3 values each: fruit in sight, snake in sight, distance to wall)

Total Input: 36-length Tensor.

Total Output: 4-length Tensor (direction to choose).

GeneticSnakeAI.py

Functions identically to VisualNeuralNetwork but instead is trained through a genetic algorithm. The GeneticTrainer class is given a PyTorch state dictionary and its accompanying model and parameters. The GeneticTrainer creates the initial population by randomizing the original state dictionary and then simulates each snake in the population. The snakes with a higher than average fitness live onto the next generation and crossover their genes with each other, filling in the remaining population positions.

This cycle continues until one of the exit conditions are met:

  • The number of generations have been met.
  • A desired cutoff fitness has been reached.
  • Training time has exceeded a given timeout.

This training process takes considerably longer than the previous networks, but yields greater results due to overcoming human limitations in training data. Further improvements could build upon the inputs to the network (either by adding more or reworking the vision system), but a general solution was desired as opposed to passing in the entire SnakeBoard instance and restricting the model to a certain board layout.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages