Giter Club home page Giter Club logo

snake-pygame-ai's Introduction

Snake

A snake game written in Python using the Pygame library, with implementations for different intelligent agents to play the game. Agents include a random agent, reflex agent, Q-learning agent, approximate Q-learning agent, and deep Q-learning agent.

Dependencies

Use Python 3.7 or above

matplotlib==3.5.1

numpy==1.22.3

pygame==2.1.2

torch==1.11.0

torchvision==0.12.0

Check requirements.txt for a full list of packages.

How to run

To play the game manually

In order to play the game as a human player, run python3 userAgent.py.

To run testing (located in test.py)

Flags:

-a {String}, --agent {String} (options: "random", "reflex", "exactq", "approxq", "dqn")

-n {Integer}, --num_runs {Integer}

-v, --verbose (Extra detail per test run)

-g, --graphics (Turn on graphics for testing)

-p, --plain (Show no score on graphics)

-j, --json (run settings from testSettings.json)

-f {Integer}, --framerate {Integer} (change the framerate of the game)

-ff, --fixed_food (Turn off random food spawning)

--load_dqn {Filename} (For dqn, specify which model to load)

--load_exactq {Filename} (For exact q agent, specify which model to load)

--load_approxq {Filename} (For appro q agent, specify which model to load)

Example Commands:

Test all agents with graphics for 5 runs each (random food spawning):

python3 test.py -a random reflex exactq approxq -n 5 -g

Note: DQNAgent is not included in the above list because it has a small chance of getting stuck in an infinite loop. The game will timeout after many steps, but this is boring to watch with graphics turned on.

Test Exact-Q & DQN agent with graphics for 5 runs each (fixed food spawning):

python3 test.py -a exactq dqn -n 5 -g -ff --load_dqn models/DQN_10000_fixed.pth

Observation: Note how differently these agents act with fixed food spawning.

Test DQN agent with graphics for 4 runs (random food spawn)

python3 test.py -a dqn -n 1 -g

Note: This agent may get stuck in a loop, and if it does, it will run until the game times out.

Test Random Agent for 1000 runs:

python3 test.py -a random -n 1000

To run training for a Q-learning agent (located in qLearningTrain.py)

Flags:

-a {String} (Agent to use)

-n {Integer} (Number of training episodes)

-t {Integer} (Number of test runs)

-g (Use graphics)

-v (Verbose output)

-l (Load checkpoint from pickle file)

-s (Save trained weights or qvalues)

-r (Random food spawn during testing)

--checkpoint {Filename} (Filename of checkpoint to load)

--save_filename {Filename} (Filename for saving weights)

Example Commands:

Train Exact-Q Agent for 5000 runs:

python3 qLearningTrain.py -a q -n 5000 -t 20 -s --save_filename exactq.pkl

Note: Weights will be saved to SRC/

Train Approximate-Q Agent for 5000 runs:

python3 qLearningTrain.py -a approxq -n 5000 -t 20 -s --save_filename approxq.pkl

Note: Weights will be saved to SRC/

To run training for a DQN agent (located in dqnTrain.py)

Flags:

-e {Integer}, --episodes {Integer} (number of episodes to train for)

-g, --graphics (show graphics)

-s, --save (save model)

-r, --random_food (spawn food randomly)

-d, --record_data (record run data as csv file)

-l, --load (load saved model)

Example Commands:

Train DQN Agent for 5000 runs with fixed food spawning:

python3 dqnTrain.py -e 5000 -s

Note: Model & stats files will be saved in models/

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.