memory.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. from gym_minigrid.minigrid import *
  2. from gym_minigrid.register import register
  3. class MemoryEnv(MiniGridEnv):
  4. """
  5. This environment is a memory test. The agent starts in a small room
  6. where it sees an object. It then has to go through a narrow hallway
  7. which ends in a split. At each end of the split there is an object,
  8. one of which is the same as the object in the starting room. The
  9. agent has to remember the initial object, and go to the matching
  10. object at split.
  11. """
  12. def __init__(
  13. self,
  14. seed=None,
  15. size=8,
  16. random_length=False,
  17. ):
  18. self.random_length = random_length
  19. super().__init__(
  20. seed=seed,
  21. grid_size=size,
  22. max_steps=5*size**2,
  23. # Set this to True for maximum speed
  24. see_through_walls=False,
  25. )
  26. def _gen_grid(self, width, height):
  27. self.grid = Grid(width, height)
  28. # Generate the surrounding walls
  29. self.grid.horz_wall(0, 0)
  30. self.grid.horz_wall(0, height-1)
  31. self.grid.vert_wall(0, 0)
  32. self.grid.vert_wall(width - 1, 0)
  33. assert height % 2 == 1
  34. upper_room_wall = height // 2 - 2
  35. lower_room_wall = height // 2 + 2
  36. if self.random_length:
  37. hallway_end = self._rand_int(4, width - 2)
  38. else:
  39. hallway_end = width - 3
  40. # Start room
  41. for i in range(1, 5):
  42. self.grid.set(i, upper_room_wall, Wall())
  43. self.grid.set(i, lower_room_wall, Wall())
  44. self.grid.set(4, upper_room_wall + 1, Wall())
  45. self.grid.set(4, lower_room_wall - 1, Wall())
  46. # Horizontal hallway
  47. for i in range(5, hallway_end):
  48. self.grid.set(i, upper_room_wall + 1, Wall())
  49. self.grid.set(i, lower_room_wall - 1, Wall())
  50. # Vertical hallway
  51. for j in range(0, height):
  52. if j != height // 2:
  53. self.grid.set(hallway_end, j, Wall())
  54. self.grid.set(hallway_end + 2, j, Wall())
  55. # Fix the player's start position and orientation
  56. self.agent_pos = (self._rand_int(1, hallway_end + 1), height // 2)
  57. self.agent_dir = 0
  58. # Place objects
  59. start_room_obj = self._rand_elem([Key, Ball])
  60. self.grid.set(1, height // 2 - 1, start_room_obj('green'))
  61. other_objs = self._rand_elem([[Ball, Key], [Key, Ball]])
  62. pos0 = (hallway_end + 1, height // 2 - 2)
  63. pos1 = (hallway_end + 1, height // 2 + 2)
  64. self.grid.set(*pos0, other_objs[0]('green'))
  65. self.grid.set(*pos1, other_objs[1]('green'))
  66. # Choose the target objects
  67. if start_room_obj == other_objs[0]:
  68. self.success_pos = (pos0[0], pos0[1] + 1)
  69. self.failure_pos = (pos1[0], pos1[1] - 1)
  70. else:
  71. self.success_pos = (pos1[0], pos1[1] - 1)
  72. self.failure_pos = (pos0[0], pos0[1] + 1)
  73. self.mission = 'go to the matching object at the end of the hallway'
  74. def step(self, action):
  75. if action == MiniGridEnv.Actions.pickup:
  76. action = MiniGridEnv.Actions.toggle
  77. obs, reward, done, info = MiniGridEnv.step(self, action)
  78. if tuple(self.agent_pos) == self.success_pos:
  79. reward = self._reward()
  80. done = True
  81. if tuple(self.agent_pos) == self.failure_pos:
  82. reward = 0
  83. done = True
  84. return obs, reward, done, info
  85. register(
  86. id='MiniGrid-MemoryS17Random-v0',
  87. entry_point='gym_minigrid.envs.memory:MemoryEnv',
  88. size=17, random_length=True
  89. )
  90. register(
  91. id='MiniGrid-MemoryS13Random-v0',
  92. entry_point='gym_minigrid.envs.memory:MemoryEnv',
  93. size=13, random_length=True
  94. )
  95. register(
  96. id='MiniGrid-MemoryS13-v0',
  97. entry_point='gym_minigrid.envs.memory:MemoryEnv',
  98. size=13
  99. )
  100. register(
  101. id='MiniGrid-MemoryS11-v0',
  102. entry_point='gym_minigrid.envs.memory:MemoryEnv',
  103. size=11
  104. )
  105. register(
  106. id='MiniGrid-MemoryS9-v0',
  107. entry_point='gym_minigrid.envs.memory:MemoryEnv',
  108. size=9
  109. )
  110. register(
  111. id='MiniGrid-MemoryS7-v0',
  112. entry_point='gym_minigrid.envs.memory:MemoryEnv',
  113. size=7
  114. )