Applying Deep Learning on Snake Game
PyGame supports up to python3.7; therefore, this project will stay at Python3.7. Below guide are tested in MacOS 10.14.6 only.
- 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
- 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 ../
- Install Project
python3.7 ~/.poetry/bin/poetry install
- Run Game
python3.7 ~/.poetry/bin/poetry run game
- 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
- Run Game with Model
python3.7 ~/.poetry/bin/poetry run test_model
DEMO: Apply model with 100 rounds in 200 steps
DEMO: Apply model with 1000 rounds in 2000 steps
- Run Game with Genetic Algorithm (without traning data)
python3.7 ~/.poetry/bin/poetry run test_genetic
DEMO: Apply Genetic Algorithm (2001st generation)
DEMO: Apply Genetic Algorithm with 50 obstables (2001st generation)
python3.7 ~/.poetry/bin/poetry env remove 3.7