Giter Club home page Giter Club logo

gym-battlesnake's Introduction

Gym-Battlesnake

Gym-Battlesnake is a multi-agent reinforcement learning environment inspired by the annual Battlesnake event held in Victoria, BC each year, and conforming to the OpenAI Gym interface.

Alt Text

Features

  • Multi-threaded game implementation written in fast C++
  • Single agent training with multiple other agents as opponents
  • Render mode available to see your agents play

Installation

Prerequisites

Gym-Battlesnake has only been tested on Ubuntu 18.04. Install the dependencies using the command:

sudo apt-get update && sudo apt-get install cmake libopenmpi-dev python3-dev zlib1g-dev libsfml-dev

You will also need to install tensorflow or tensorflow-gpu (2.0 not tested), see https://www.tensorflow.org/install/pip.

Install using Pip

Clone this repository using the following command:

git clone https://github.com/ArthurFirmino/gym-battlesnake

Change into the directory and install using pip (consider setting up a Python virtual environment first):

cd gym-battlesnake
pip install -e .

Example

Single agent training:

from gym_battlesnake.gymbattlesnake import BattlesnakeEnv
from gym_battlesnake.custompolicy import CustomPolicy
from stable_baselines import PPO2

env = BattlesnakeEnv(n_threads=4, n_envs=16)

model = PPO2(CustomPolicy, env, verbose=1, learning_rate=1e-3)
model.learn(total_timesteps=100000)
model.save('ppo2_trainedmodel')

del model
model = PPO2.load('ppo2_trainedmodel')

obs = env.reset()
for _ in range(10000):
    action,_ = model.predict(obs)
    obs,_,_,_ = env.step(action)
    env.render()

Multi agent training:

from gym_battlesnake.gymbattlesnake import BattlesnakeEnv
from gym_battlesnake.custompolicy import CustomPolicy
from stable_baselines import PPO2

num_agents = 4
placeholder_env = BattlesnakeEnv(n_threads=4, n_envs=16)
models = [PPO2(CustomPolicy, placeholder_env, verbose=1, learning_rate=1e-3) for _ in range(num_agents)]
placeholder_env.close()

for _ in range(10):
    for model in models:
        env = BattlesnakeEnv(n_threads=4, n_envs=16, opponents=[ m for m in models if m is not model])
        model.set_env(env)
        model.learn(total_timesteps=100000)
        env.close()

model = models[0]
env = BattlesnakeEnv(n_threads=1, n_envs=1, opponents=[ m for m in models if m is not model])
obs = env.reset()
for _ in range(10000):
    action,_ = model.predict(obs)
    obs,_,_,_ = env.step(action)
    env.render()

Notes

  • See OpenAI documentation on gym for more details about its interface
  • See stable-baselines documentation for more details on their PPO2 implementation and other suitable algorithms
  • For multi-agent training tensorflow-gpu is recommended, as well as a large number of environments (~100) to maximize data transfer to the GPU.

Contributing

  1. Fork
  2. Clone and Setup
  3. Develop
  4. Pull Request

gym-battlesnake's People

Contributors

arthurfirmino 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

Watchers

 avatar  avatar

gym-battlesnake's Issues

Getting the direction of a predicted action

Hi,

action, _ = model.predict(obs) returns each env's actions from 0-3, and I would like to map the predicted action to it's corresponding direction name in Python, for example 0 might be Up, 1 might be Down, etc.

The problem is, the X and Y coordinates alternate pseudo-randomly based on a hash of the game ID and other info, so those action numbers don't correspond 1 to 1 with a given action name. It's different every time you run a prediction.

Is it possible to get the correct action mapping, or get the orientation from the observation, or something like that, so that I can map them to Up/Down/Left/Right labels?

Thanks in advance

can i customize reward and face of the snake?

basically the rendering tells me agent don't know the head of the snake is it so?
also, I would like to customize the reward system is there any place where rewards in the current env are defined?

I understand i might have to go into the C++ implementation and modify i guess.

Also, can you let tell me why the obs shape is : (N, 39, 39, 6) why 6?

anyway, thanks for a good starting point to play with.

According to the readme can not run

According to the readme can not run, as follows:

python examples/example_multiagent.py

Traceback (most recent call last): File "examples/example_multiagent.py", line 8, in <module> placeholder_env = BattlesnakeEnv(n_threads=4, n_envs=16) TypeError: Can't instantiate abstract class BattlesnakeEnv with abstract methods env_method, get_attr, set_attr

CMake Error during Setup

CMake Error at gym_battlesnake/cmake/Modules/FindSFML.cmake:355 (message):
      Could NOT find SFML (missing: SFML_GRAPHICS_LIBRARY SFML_WINDOW_LIBRARY
      SFML_SYSTEM_LIBRARY)

Got the above error when running setup.py.

Any ideas why this occurred or how I can fix?

Thanks for the help!

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.