Ivan Yeung โข 2024-05-06
๐๐ป๐๐๐ถ๐๐๐๐ฒ - ๐๐ผ๐บ๐ฝ๐๐๐ฒ๐ฟ ๐ฆ๐ฐ๐ถ๐ฒ๐ป๐ฐ๐ฒ ๐จ๐ป๐ถ๐๐ฒ๐ฟ๐๐ถ๐๐ ๐ผ๐ณ ๐ฆ๐ฎ๐ป ๐๐ถ๐ฒ๐ด๐ผ A deep neural network capable of winning the game "Snake". Using the genetic algorithm, hundreds of DNNs (deep neural networks) were created and initialized each generation with thousands of generations being trained. This article is retrieved from my github repository: https://github.com/theivanyeung/Snake-AI
Snake AI is still in training phase.ย parameters.txt
ย represents lastest model.
Using the genetic algorithm, hundreds of DNNs (deep neural networks) were created and initialized each generation with thousands of generations being trained.
In this repository, the development/training environment have been included where the models were trained, a testing environment where you can test out different models and how their compare on the gameboard, and also a game environment in which is playable for anyone to try.
To test the model
cd
ย into the ai-test folderpython model.py
ย orย python3 model.py
parameter.txt
Genetic Algorithm
- 1000 models per generation
- 20 models are choosen randomly each generation for repopulating
(Those with higher fitness scores have higher chances)
- 50/50 Simulated binary crossover, Single-point binary crossover
+ Variation (SBC): 15
- Gaussian mutation
+ Mutation rate: 0.05
+ Mean deviation of distribution: 0
+ Standard deviation of distribution: 0.1
Training board:
- Width: 25 blocks
- Height: 25 blocks
- Initial length: 3 blocks
Testing board:
- Width: 75 blocks
- Height: 75 blocks
- Initial length: 4 blocks
DNN Architecture
- 32 inputs
- hidden layer 1: 20 neurons
- hidden layer 2: 12 neurons
- 4 outputs
Inputs:
- Directions
- Up
- Down
- Right
- Left
- Up-right
- Up-left
- Down-right
- Down-left
For each direction
- Is 0 blocks away from the wall
- Is there part of snake
- Is there an apple
- Direction of snake
- Up
- Down
- Right
- Left
- Direction of snake tail
- Up
- Down
- Right
- Left
Ouputs:
+ Up
+ Down
+ Right
+ Left
Fitness function
f(steps, score) = steps + (2**score + score**2.1 * 500) - (score**1.2 * (0.25 * steps)**1.3)
You can also play the game by cd'ing into the playable-game folder and running either python game.py or python3 game.py.
Edit the game using these self variables:
self.board_width = 11
self.board_height = 11
self.y = 6
self.apple_x = 8
self.snake_head_x = 4
self.fps = 30
Hereโs a fun youtube video to watch:
Copyright ยฉ 2021 Govest, Inc. All rights reserved.