GithubHelp home page GithubHelp logo

bhairavmehta95 / slitherin-gym Goto Github PK

View Code? Open in Web Editor NEW
12.0 1.0 3.0 47 KB

Slither-in Inspired Snake Environment for OpenAI Gym (Part of Requests for Research 2.0)

Python 86.65% Jupyter Notebook 13.35%
reinforcement-learning openai-gym gym snake-game multiagent-reinforcement-learning requests-for-research pygame snake-environment slitherin-gym

slitherin-gym's Introduction

slitherin-gym

Slither-in Inspired Snake Environment for OpenAI Gym (Part of Requests for Research 2.0)

Environment Description

  • observation_space - A tuple of size: (n_agents, resolution, resolution), where n_agents is the number of agents in the game (as initialized) and resolution is a number calculated by taking the screen's height and dividing it by the spacing, both of which can be specified in the environment's constructor.
  • action_space - A n_agents-sized array that takes in 4 discrete actions [0, 1, 2, 3], which corresponds to the directions that each agent should move at the next timestep.

The agents and environment abide by the same rules and dynamics of the classic Snake game (i.e can't go backwards, die if they hit themselves, etc.). A more detailed description can be found here.

Each observation per agent is a matrix that corresponds to the agent's place in the grid.

  • If the agent is dead, each square fills up with a -1.
  • If the agent is occupying that cell, the value of that cell will be 1.
  • If another agent is occupying that cell, the value of that cell will be 2.
  • Likewise, a fruit will be 3.
  • A wall (which surrounds the outside of the grid, and is lethal to any agent who touches it) will have a value of -1.
  • Any empty or non-occupied cell will take the value of 0.

slitherin-gym's People

Contributors

bhairavmehta95 avatar

Stargazers

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

Watchers

 avatar

slitherin-gym's Issues

Register slitherin-gym environment

I think it would be nice to register your custom environment and
use it like below code for portability.

import gym
import slitherinenv

env = gym.make('SlitherinEnv-v0')
env.__init__(num_agents=1)
env.reset()

for i_episode in range(20):
    observation = env.reset()
    for t in range(100):
        env.render()
        #print(observation)
        action = env.action_space.sample()
        observation, reward, done, info = env.step(action)
        if done:
            print("Episode finished after {} timesteps".format(t+1))
            break

Issue in slitherinenv.py

Hello, writer.

I tried to insert output of slitherin env into MLP policy network and found some issue from that trial.

First is that reset function do not return observation.

def reset(self):
        #print("def reset(self):")
        for i, p in enumerate(self.agents):
            self.killed[i] = False

            x = np.random.randint(1, self.max_spawn_idx - 1) * self.spacing
            y = np.random.randint(1, self.max_spawn_idx - 1) * self.spacing
            direction = np.random.randint(0, 1) # TODO: Fix vertical spawning

            p._reset(x, y, direction)

        for f in range(self.num_fruits):
            self.fruits[f] = self._generate_goal()

        self.active_agents = self.num_agents
        new_obs = []

        '''
        for i in range(self.num_agents):
            ob = self._generate_obs(i)
            new_obs.append(ob)   		
  
        return deepcopy(new_obs)
        '''

Second is that format of observation_space and action_space is not same.

       self.observation_space = spaces.Box(low=-1, high=3, 
       #                                    shape=(self.num_agents, self.window_dimension // self.spacing, self.window_dimension // self.spacing))

       '''
        self.observation_space = spaces.Tuple(
            [spaces.Discrete((self.window_dimension // self.spacing) * (self.window_dimension // self.spacing)) for i in range(self.num_agents)]
        )
       '''

        self.action_space = spaces.Tuple(
            [spaces.Discrete(4) for i in range(self.num_agents)]
        )

Also, I am little confused with Box and Discrete class of gym. Is it possible to insert 2D Box value into placeholder of tensorflow?
Should flatten function be used for observation value?

Thank you for your work.

Collision control

Great work, looks very pretty!

Here are some issues I spotted:

  • In the beginning of each game, snakes might start on top of each other
  • Sometimes snakes start while overlapping the boundary
  • The collision of the head of a snake with higher index (e.g. snake 3) with the tail of a snake with lower index (e.g. snake 1) is not detected.

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.