empty.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. from gym_minigrid.minigrid import *
  2. from gym_minigrid.register import register
  3. class EmptyEnv(MiniGridEnv):
  4. """
  5. Empty grid environment, no obstacles, sparse reward
  6. """
  7. def __init__(
  8. self,
  9. size=8,
  10. agent_start_pos=(1,1),
  11. agent_start_dir=0,
  12. ):
  13. self.agent_start_pos = agent_start_pos
  14. self.agent_start_dir = agent_start_dir
  15. super().__init__(
  16. grid_size=size,
  17. max_steps=4*size*size,
  18. # Set this to True for maximum speed
  19. see_through_walls=True
  20. )
  21. def _gen_grid(self, width, height):
  22. # Create an empty grid
  23. self.grid = Grid(width, height)
  24. # Generate the surrounding walls
  25. self.grid.wall_rect(0, 0, width, height)
  26. # Place a goal square in the bottom-right corner
  27. self.put_obj(Goal(), width - 2, height - 2)
  28. # Place the agent
  29. if self.agent_start_pos is not None:
  30. self.agent_pos = self.agent_start_pos
  31. self.agent_dir = self.agent_start_dir
  32. else:
  33. self.place_agent()
  34. self.mission = "get to the green goal square"
  35. class EmptyEnv5x5(EmptyEnv):
  36. def __init__(self, **kwargs):
  37. super().__init__(size=5, **kwargs)
  38. class EmptyRandomEnv5x5(EmptyEnv):
  39. def __init__(self):
  40. super().__init__(size=5, agent_start_pos=None)
  41. class EmptyEnv6x6(EmptyEnv):
  42. def __init__(self, **kwargs):
  43. super().__init__(size=6, **kwargs)
  44. class EmptyRandomEnv6x6(EmptyEnv):
  45. def __init__(self):
  46. super().__init__(size=6, agent_start_pos=None)
  47. class EmptyEnv16x16(EmptyEnv):
  48. def __init__(self, **kwargs):
  49. super().__init__(size=16, **kwargs)
  50. register(
  51. id='MiniGrid-Empty-5x5-v0',
  52. entry_point='gym_minigrid.envs:EmptyEnv5x5'
  53. )
  54. register(
  55. id='MiniGrid-Empty-Random-5x5-v0',
  56. entry_point='gym_minigrid.envs:EmptyRandomEnv5x5'
  57. )
  58. register(
  59. id='MiniGrid-Empty-6x6-v0',
  60. entry_point='gym_minigrid.envs:EmptyEnv6x6'
  61. )
  62. register(
  63. id='MiniGrid-Empty-Random-6x6-v0',
  64. entry_point='gym_minigrid.envs:EmptyRandomEnv6x6'
  65. )
  66. register(
  67. id='MiniGrid-Empty-8x8-v0',
  68. entry_point='gym_minigrid.envs:EmptyEnv'
  69. )
  70. register(
  71. id='MiniGrid-Empty-16x16-v0',
  72. entry_point='gym_minigrid.envs:EmptyEnv16x16'
  73. )