Package: gym-simplegrid
Simple Gridworld Environment for OpenAI GymSimpleGrid is a super simple gridworld environment forGymnasium. It is easy to use and customise and it is intended to offer an environment for quickly testing and prototyping different RL algorithms.It is also efficient, lightweight and has few dependencies (gymnasium, numpy, matplotlib).SimpleGrid involves navigating a grid from a Start (red tile) to a Goal (green tile) state without colliding with any Wall (black tiles) by walking over the Empty (white tiles) cells. The yellow circle denotes the agent's current position.InstallationTo install SimpleGrid, you can either use pippipinstallgym-simplegridor you can clone the repository and run an editable installationgitclonehttps://github.com/damat-le/gym-simplegrid.gitcdgym-simplegrid
pipinstall-e.CitationPlease use this bibtex if you want to cite this repository in your publications:@misc{gym_simplegrid,
  author ={Leo D'Amato},
  title ={Simple Gridworld Environment for OpenAI Gym},
  year ={2022},
  publisher ={GitHub},
  journal ={GitHub repository},
  howpublished ={\url{https://github.com/damat-le/gym-simplegrid}},}Getting StartedBasic usage options:importgymnasiumasgymimportgym_simplegrid# Load the default 8x8 mapenv=gym.make('SimpleGrid-8x8-v0',render_mode='human')# Load the default 4x4 mapenv=gym.make('SimpleGrid-4x4-v0',render_mode='human')# Load a custom mapobstacle_map=["10001000","10010000","00000001","01000001",]env=gym.make('SimpleGrid-v0',obstacle_map=obstacle_map,render_mode='human')# Use the options dict in the reset method# This initialises the agent in location (0,0) and the goal in location (7,7)env=gym.make('SimpleGrid-8x8-v0',render_mode='human')obs,info=env.reset(options={'start_loc':0,'goal_loc':63})Basic example with rendering:importgymnasiumasgymimportgym_simplegridenv=gym.make('SimpleGrid-8x8-v0',render_mode='human')obs,info=env.reset()done=env.unwrapped.donefor_inrange(50):ifdone:breakaction=env.action_space.sample()obs,reward,done,_,info=env.step(action)env.close()For an other example, take a look at theexample script.Environment DescriptionAction SpaceThe action space isgymnasium.spaces.Discrete(4). An action is aintnumber and represents a direction according to the following scheme:0: UP1: DOWN2: LEFT3: RIGHTObservation SpaceAssume to have an environment of size(nrow, ncol), then the observation space isgymnasium.spaces.Discrete(nrow * ncol). Hence, an observation is an integer from0tonrow * ncol - 1and represents the agent's current position. We can convert an observationsto a tuple(x,y)using the following formulae:x=s//ncol# integer divisiony=s%ncol# modulo operationFor example: letnrow=4,ncol=5and lets=11. Thenx=11//5=2andy=10%5=1.Viceversa, we can convert a tuple(x,y)to an observationsusing the following formulae:s=x*ncol+yFor example: letnrow=4,ncol=5and letx=2,y=1. Thens=2*5+1=11.Environment DynamicsIn the current implementation, the episodes terminates only when the agent reaches the goal state. In case the agent takes a non-valid action (e.g. it tries to walk over a wall or exit the grid), the agent stays in the same position and receives a negative reward.It is possible to subclass theSimpleGridEnvclass  and to override thestep()method to define custom dynamics (e.g. truncate the episode if the agent takes a non-valid action).RewardsCurrently, the reward map is defined in theget_reward()method of theSimpleGridEnvclass.For a given position(x,y), the default reward function is defined as follows:defget_reward(self,x:int,y:int)->float:"""Get the reward of a given cell."""ifnotself.is_in_bounds(x,y):# if the agent tries to exit the grid, it receives a negative rewardreturn-1.0elifnotself.is_free(x,y):# if the agent tries to walk over a wall, it receives a negative rewardreturn-1.0elif(x,y)==self.goal_xy:# if the agent reaches the goal, it receives a positive rewardreturn1.0else:# otherwise, it receives no rewardreturn0.0It is possible to subclass theSimpleGridEnvclass  and to override this method to define custom rewards.Notes on renderingThe default frame rate is 5 FPS. It is possible to change it through themetadatadictionary.To properly render the environment, remember that the point (x,y) in the desc matrix corresponds to the point (y,x) in the rendered matrix.
This is because the rendering code works in terms of width and height while the computation in the environment is done using x and y coordinates.
You don't have to worry about this unless you play with the environment's internals.