Git Product home page Git Product logo

snake-ga's Introduction

Deep Reinforcement Learning

Project: Train AI to play Snake

UPDATE:

This project has been recently updated and improved:

  • It is now possible to optimize the Deep RL approach using Bayesian Optimization.
  • The code of Deep Reinforcement Learning was ported from Keras/TF to Pytorch. To see the old version of the code in Keras/TF, please refer to this repository: snake-ga-tf.

Introduction

The goal of this project is to develop an AI Bot able to learn how to play the popular game Snake from scratch. In order to do it, I implemented a Deep Reinforcement Learning algorithm. This approach consists in giving the system parameters related to its state, and a positive or negative reward based on its actions. No rules about the game are given, and initially the Bot has no information on what it needs to do. The goal for the system is to figure it out and elaborate a strategy to maximize the score - or the reward.
We are going to see how a Deep Q-Learning algorithm learns how to play Snake, scoring up to 50 points and showing a solid strategy after only 5 minutes of training.
Additionally, it is possible to run the Bayesian Optimization method to find the optimal parameters of the Deep neural network, as well as some parameters of the Deep RL approach.

Install

This project requires Python 3.6 with the pygame library installed, as well as Pytorch. If you encounter any error with torch=1.7.1, you might need to install Visual C++ 2015-2019 (or simply downgrade your pytorch version, it should be fine).
The full list of requirements is in requirements.txt.

git clone [email protected]:maurock/snake-ga.git

Run

To run and show the game, executes in the snake-ga folder:

python snakeClass.py

Arguments description:

  • --display - Type bool, default True, display or not game view
  • --speed - Type integer, default 50, game speed

The default configuration loads the file weights/weights.h5 and runs a test.

To train the agent, set in the file snakeClass.py:

  • params['train'] = True The parameters of the Deep neural network can be changed in snakeClass.py by modifying the dictionary params in the function define_parameters()

If you run snakeClass.py from the command line, you can set the arguments --display=False and --speed=0. This way, the game display is not shown and the training phase is faster.

Optimize Deep RL with Bayesian Optimization

To optimize the Deep neural network and additional parameters, run:

python snakeClass.py --bayesianopt=True

This method uses Bayesian optimization to optimize some parameters of Deep RL. The parameters and the features' search space can be modified in bayesOpt.py by editing the optim_params dictionary in optimize_RL.

For Mac users

It seems there is a OSX specific problem, since many users cannot see the game running. To fix this problem, in update_screen(), add this line.

def update_screen():
    pygame.display.update() <br>
    pygame.event.get() # <--- Add this line ###

snake-ga's People

Contributors

amelkikh avatar maurock avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

snake-ga's Issues

i get this error. what do i do? new to python and programming and just want to execute this cool program.

2020-11-26 23:16:55.536092: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN)to use the following CPU instructions in performance-critical operations: AVX2 FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2020-11-26 23:16:55.550165: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x7fdf6d52e130 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2020-11-26 23:16:55.550181: I tensorflow/compiler/xla/service/service.cc:176] StreamExecutor device (0): Host, Default Version
Traceback (most recent call last):
File "/Users/nahidahmed/Desktop/SnakeGame/snakeClass.py", line 309, in
run(args.display, args.speed, params)
File "/Users/nahidahmed/Desktop/SnakeGame/snakeClass.py", line 223, in run
agent = DQNAgent(params)
File "/Users/nahidahmed/Desktop/SnakeGame/DQN.py", line 29, in init
self.model = self.network()
File "/Users/nahidahmed/Desktop/SnakeGame/DQN.py", line 33, in network
model.add(Dense(output_dim=self.first_layer, activation='relu', input_dim=11))
TypeError: init() missing 1 required positional argument: 'units'

Snake body is not visible (speed = 50, display_option = True)

The Snake body is not visible even when setting the speed = 50 and display_option = True as it says in the README. Tried with both Python 3.7.2 and Python 3.6.8.

I know others have had this issue too, from reading the Medium article, comments section.

Python3.6.8/3.7.2 packages:
absl-py==0.7.0
astor==0.7.1
cycler==0.10.0
gast==0.2.2
grpcio==1.19.0
h5py==2.9.0
Keras==2.2.4
Keras-Applications==1.0.7
Keras-Preprocessing==1.0.9
kiwisolver==1.0.1
Markdown==3.0.1
matplotlib==3.0.2
mock==2.0.0
numpy==1.16.2
pandas==0.24.1
pbr==5.1.2
protobuf==3.6.1
pygame==1.9.4
pyparsing==2.3.1
python-dateutil==2.8.0
pytz==2018.9
PyYAML==3.13
scipy==1.2.1
seaborn==0.9.0
six==1.12.0
tensorboard==1.13.0
tensorflow==1.13.1
tensorflow-estimator==1.13.0
termcolor==1.1.0
Werkzeug==0.14.1

OS: MacOS Mojave

Tried to go back in the commit log, but seems to be the same issue for me, for all versions.

Game screen is not shown

When I run the game, it displays the window but the game does not show up the graphics. Just stay black.

I am on Mac

DQN.py:79:

E:\NGBH\Reinforcement Learning\snake-ga-master\DQN.py:79: UserWarning: Update your Dense call to the Keras 2 API: Dense(activation="relu", input_dim=11, units=120)
model.add(Dense(output_dim=120, activation='relu', input_dim=11))
WARNING:tensorflow:From C:\python64\lib\site-packages\tensorflow\python\framework\op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.
Instructions for updating:
Colocations handled automatically by placer.
WARNING:tensorflow:From C:\python64\lib\site-packages\keras\backend\tensorflow_backend.py:3445: calling dropout (from tensorflow.python.ops.nn_ops) with keep_prob is deprecated and will be removed in a future version.
Instructions for updating:
Please use rate instead of keep_prob. Rate should be set to rate = 1 - keep_prob.
E:\NGBH\Reinforcement Learning\snake-ga-master\DQN.py:81: UserWarning: Update your Dense call to the Keras 2 API: Dense(activation="relu", units=120)
model.add(Dense(output_dim=120, activation='relu'))
E:\NGBH\Reinforcement Learning\snake-ga-master\DQN.py:83: UserWarning: Update your Dense call to the Keras 2 API: Dense(activation="relu", units=120)
model.add(Dense(output_dim=120, activation='relu'))
E:\NGBH\Reinforcement Learning\snake-ga-master\DQN.py:85: UserWarning: Update your Dense call to the Keras 2 API: Dense(activation="softmax", units=3)
model.add(Dense(output_dim=3, activation='softmax'))
Traceback (most recent call last):
File "E:\NGBH\Reinforcement Learning\snake-ga-master\snakeClass.py", line 231, in
run()
File "E:\NGBH\Reinforcement Learning\snake-ga-master\snakeClass.py", line 181, in run
game = Game(440, 440)
File "E:\NGBH\Reinforcement Learning\snake-ga-master\snakeClass.py", line 23, in init
self.bg = pygame.image.load("img/background.png")
pygame.error: Couldn't open img/background.png
ss

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.