Skip to content

note35/Snake-DeepLearning

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Snake-DeepLearning

Applying Deep Learning on Snake Game

Installation

PyGame supports up to python3.7; therefore, this project will stay at Python3.7. Below guide are tested in MacOS 10.14.6 only.

  1. Install Poetry
curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python3.7

You can find your python path (Virtualenv -> Path) by running below command python3.7 ~/.poetry/bin/poetry env info.

Virtualenv
Python:         3.7.4
Implementation: CPython
Path:           /Users/xxx/Library/Caches/pypoetry/virtualenvs/snake-deeplearning-LtzCkVy--py3.7
Valid:          True

System
Platform: darwin
OS:       posix
Python:   /usr/local/Cellar/python/3.7.4_1/Frameworks/Python.framework/Versions/3.7
  1. Install PyGame

Since PyGame is problematic for MacOS if you install it through regular approach, we need to comile it from source code. Below commands follow the guide from StackOverflow.

brew install sdl2 sdl2_gfx sdl2_image sdl2_mixer sdl2_net sdl2_ttf
git clone -b 1.9.6 https://github.com/pygame/pygame.git --single-branch
cd pygame
<Virtualenv python3.7 PATH>/bin/python3.7 setup.py -config -auto -sdl2
<Virtualenv python3.7 PATH>/bin/python3.7 setup.py install
cd ../
  1. Install Project
python3.7 ~/.poetry/bin/poetry install
  1. Run Game
python3.7 ~/.poetry/bin/poetry run game
  1. Generate Training Model
python3.7 ~/.poetry/bin/poetry run generate_model
  • Run 100 games with 2000 steps will get about 95% accurate result.
  • This training model has one defect, it will hit self while head is surrending by body but apple appears in the other side of body. (Therefore, the model based on this training data will hit similar error.)
  • This training model doesn't support additional obstacles in the map. One TODO work is to support this use case.

DEMO: Generate model in 2000 steps

generate_model_2000steps.gif

  1. Run Game with Model
python3.7 ~/.poetry/bin/poetry run test_model

DEMO: Apply model with 100 rounds in 200 steps

test_model_100_200

DEMO: Apply model with 1000 rounds in 2000 steps

test_model_1000_2000.gif

  1. Run Game with Genetic Algorithm (without traning data)
python3.7 ~/.poetry/bin/poetry run test_genetic

DEMO: Apply Genetic Algorithm (2001st generation)

test_genetic_2000steps.gif

DEMO: Apply Genetic Algorithm with 50 obstables (2001st generation)

test_genetic_obstacles.gif

Cleanup

python3.7 ~/.poetry/bin/poetry env remove 3.7

Reference

About

Applying Deep Learning on Snake Game

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages