Skip to content

Deep Q-Learning agent that learns how to play the classic Snake game. Using reinforcement learning to maximize its score by optimizing the game's strategy.

License

Notifications You must be signed in to change notification settings

quochuy242/snake-deep-q-learning

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Deep Q-Learning for Snake Game

This repository contains a Deep Q-Learning agent that learns how to play the classic Snake game. The project involves training an agent using reinforcement learning to maximize its score by optimizing the game's strategy.

Table of Contents

Demo

Here’s a preview of the agent playing the Snake game:

Game Demo

via GIPHY

Results

After training the model over 200 games, the following results were obtained:

Score Chart

The chart above shows the agent's score over time, along with the average score per game.

Project Overview

The goal of this project is to apply deep reinforcement learning to teach an agent to play Snake. The agent is trained using a deep Q-network (DQN) that learns to optimize game performance over time by balancing exploration and exploitation.

Key Concepts:

  • Deep Q-Learning: The model uses a neural network to approximate Q-values for each action and update them based on rewards.
  • Exploration vs. Exploitation: The agent explores different strategies during training while gradually exploiting known strategies that yield better results.
  • Reward Structure: The reward function encourages the agent to eat food while penalizing collisions with walls or itself.

Features

  • Configurable environment for training the agent.
  • Dynamic adjustment of reward values and gamma (discount factor) for better learning.
  • Visualization of the training process and game performance.
  • Ability to save and load trained models for later use.

Installation

  1. Clone this repository:
git clone https://github.com/yourusername/snake-dqn.git
cd snake-dqn
  1. Install the required dependencies:
pip install -r requirements.txt

Usage

To train and watch what the agent do:

python main.py

You can modify hyperparameters such as the number of episodes, gamma, and learning rate in the utils/constant.py file.

References

License

This project is licensed under the MIT License. See the LICENSE file for details.

About

Deep Q-Learning agent that learns how to play the classic Snake game. Using reinforcement learning to maximize its score by optimizing the game's strategy.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages