Skip to content
Multiplayer snake AI
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.
experiments cleaning Mar 30, 2018
LICENSE Add license Jul 3, 2018 Merge branch 'master' of Jul 17, 2018 cleaning Jul 3, 2018 cleaning Jul 3, 2018 clean interface, add agent object Jun 19, 2018 clean interface, add agent object Jun 19, 2018 cleaning Jul 3, 2018 merge evolution strategies Jun 12, 2018 - Multiplayer Snake AI

Game demo

This project provides

  • an interface for a multi-player snake game inpired by
  • implementations of various agents based on reinforcement learning (Q-learning, Policy Gradients) and game theory (adversarial search such as Minimax, Alpha-beta pruning ...)

The multiagent nature of this game provides endless opportunities to explore RL algorithms (eg. curriculum learning, how opponents during training episodes impact learned behaviors, ...) and is also a nice way of assessing the relative performance of each methods.
In addition, it is also possible to study how different reward functions or game rules will shape the agents' strategies: for example, is it better to grow as much as possible by eating candies or to try and kill other players to end the game as quickly as possible?

Read this blog post to get an overview of this project as well as some details on one of the reinforcement learning methods implemented (Q-learning with function approximation by neural networks).

Visualizing individual games

It is possible to run a single game with the GUI through the command

$ python [h]

If you do use the option h, this will add a 'human player': an agent you can control with the keyboard.

The config file

The config file lets you configure the different agents or the details of the experiments/simulations you would like to run. Here is an example configuration:

agent             = "RL"
filename          = "rl-pg-linear-r6-1000"
game_hp           = HP(grid_size = 20, max_iter = 3000, discount = 0.9)
rl_hp             = RlHp(rl_type = "policy_gradients", radius = 6, filter_actions = False, lambda_ = None, q_type = "linear")
depth             = lambda s,a : survivorDfunc(s, a , 2, 0.5)
evalFn            = greedyEvaluationFunction
opponents         = [SmartGreedyAgent, OpportunistAgent, searchAgent("alphabeta", depth, evalFn)]
num_trials        = 1000

Setting agent to RL or ES will add the corresponding agent to the opponents list (after training if necessary). Setting it to anything else will keep this list unchanged.

Running simulations

Once you filled the config file, you can easily run 500 simulations (without the GUI) to get some stats about how the AIs perform against each other:

$ python 500 [load] 

If you do use the load parameter, this will load pre-trained weights for the RL agents, otherwise it will first run some trial games to learn such weights. In the latter case, learned weights will be saved in the data/ folder with the name provided in the config file. For example, simple-ql-r6.p and simple-pg-r6.p contain the weights of RL agents trained respectively via Q-learning and Policy Gradients on 1,000 trials.
We recommend training agents against hard-coded strategies instead of search-based ones such as Minimax (at least at first) since it will be much faster.

Basic statistics will be printed in the terminal, but these (and more) will be saved in a file in experiments/ with the name set in the config file. Note that the snakes' id correspond to the strategy's index in the list opponents.

File structure

  • implements hard-coded strategies, especially useful to train RL agents or as baselines
  • implements adversarial strategies that expore trees of possible moves
  • provides the interface for RL-based algorithms
  • provides utilities to train and load RL agents
  • implements a simple Policy Gradients algorithm for reinforcement learning
  • implements Q-learning for reinforcement learning and supports both a simple linear model or neural nets
  • implements an Evolutionary Strategy algorithm
  • implements a FeatureExtractor to derive useful features from any state and used by RL agents
  •,,,, contain the general code for the game


MIT License

Have fun!

You can’t perform that action at this time.