obstructedmaze.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. from minigrid.core.constants import COLOR_NAMES, DIR_TO_VEC
  2. from minigrid.core.mission import MissionSpace
  3. from minigrid.core.roomgrid import RoomGrid
  4. from minigrid.core.world_object import Ball, Box, Key
  5. class ObstructedMazeEnv(RoomGrid):
  6. """
  7. ### Description
  8. The agent has to pick up a box which is placed in a corner of a 3x3 maze.
  9. The doors are locked, the keys are hidden in boxes and doors are obstructed
  10. by balls. This environment can be solved without relying on language.
  11. ### Mission Space
  12. "pick up the {COLOR_NAMES[0]} ball"
  13. ### Action Space
  14. | Num | Name | Action |
  15. |-----|--------------|---------------------------|
  16. | 0 | left | Turn left |
  17. | 1 | right | Turn right |
  18. | 2 | forward | Move forward |
  19. | 3 | pickup | Pick up an object |
  20. | 4 | drop | Unused |
  21. | 5 | toggle | Toggle/activate an object |
  22. | 6 | done | Unused |
  23. ### Observation Encoding
  24. - Each tile is encoded as a 3 dimensional tuple:
  25. `(OBJECT_IDX, COLOR_IDX, STATE)`
  26. - `OBJECT_TO_IDX` and `COLOR_TO_IDX` mapping can be found in
  27. [minigrid/minigrid.py](minigrid/minigrid.py)
  28. - `STATE` refers to the door state with 0=open, 1=closed and 2=locked
  29. ### Rewards
  30. A reward of '1' is given for success, and '0' for failure.
  31. ### Termination
  32. The episode ends if any one of the following conditions is met:
  33. 1. The agent picks up the blue ball.
  34. 2. Timeout (see `max_steps`).
  35. ### Registered Configurations
  36. "NDl" are the number of doors locked.
  37. "h" if the key is hidden in a box.
  38. "b" if the door is obstructed by a ball.
  39. "Q" number of quarters that will have doors and keys out of the 9 that the
  40. map already has.
  41. "Full" 3x3 maze with "h" and "b" options.
  42. - `MiniGrid-ObstructedMaze-1Dl-v0`
  43. - `MiniGrid-ObstructedMaze-1Dlh-v0`
  44. - `MiniGrid-ObstructedMaze-1Dlhb-v0`
  45. - `MiniGrid-ObstructedMaze-2Dl-v0`
  46. - `MiniGrid-ObstructedMaze-2Dlh-v0`
  47. - `MiniGrid-ObstructedMaze-2Dlhb-v0`
  48. - `MiniGrid-ObstructedMaze-1Q-v0`
  49. - `MiniGrid-ObstructedMaze-2Q-v0`
  50. - `MiniGrid-ObstructedMaze-Full-v0`
  51. """
  52. def __init__(self, num_rows, num_cols, num_rooms_visited, **kwargs):
  53. room_size = 6
  54. max_steps = 4 * num_rooms_visited * room_size**2
  55. mission_space = MissionSpace(
  56. mission_func=lambda: f"pick up the {COLOR_NAMES[0]} ball",
  57. )
  58. super().__init__(
  59. mission_space=mission_space,
  60. room_size=room_size,
  61. num_rows=num_rows,
  62. num_cols=num_cols,
  63. max_steps=max_steps,
  64. **kwargs,
  65. )
  66. self.obj = Ball() # initialize the obj attribute, that will be changed later on
  67. def _gen_grid(self, width, height):
  68. super()._gen_grid(width, height)
  69. # Define all possible colors for doors
  70. self.door_colors = self._rand_subset(COLOR_NAMES, len(COLOR_NAMES))
  71. # Define the color of the ball to pick up
  72. self.ball_to_find_color = COLOR_NAMES[0]
  73. # Define the color of the balls that obstruct doors
  74. self.blocking_ball_color = COLOR_NAMES[1]
  75. # Define the color of boxes in which keys are hidden
  76. self.box_color = COLOR_NAMES[2]
  77. self.mission = "pick up the %s ball" % self.ball_to_find_color
  78. def step(self, action):
  79. obs, reward, terminated, truncated, info = super().step(action)
  80. if action == self.actions.pickup:
  81. if self.carrying and self.carrying == self.obj:
  82. reward = self._reward()
  83. terminated = True
  84. return obs, reward, terminated, truncated, info
  85. def add_door(
  86. self,
  87. i,
  88. j,
  89. door_idx=0,
  90. color=None,
  91. locked=False,
  92. key_in_box=False,
  93. blocked=False,
  94. ):
  95. """
  96. Add a door. If the door must be locked, it also adds the key.
  97. If the key must be hidden, it is put in a box. If the door must
  98. be obstructed, it adds a ball in front of the door.
  99. """
  100. door, door_pos = super().add_door(i, j, door_idx, color, locked=locked)
  101. if blocked:
  102. vec = DIR_TO_VEC[door_idx]
  103. blocking_ball = Ball(self.blocking_ball_color) if blocked else None
  104. self.grid.set(door_pos[0] - vec[0], door_pos[1] - vec[1], blocking_ball)
  105. if locked:
  106. obj = Key(door.color)
  107. if key_in_box:
  108. box = Box(self.box_color)
  109. box.contains = obj
  110. obj = box
  111. self.place_in_room(i, j, obj)
  112. return door, door_pos
  113. class ObstructedMaze_1Dlhb(ObstructedMazeEnv):
  114. """
  115. A blue ball is hidden in a 2x1 maze. A locked door separates
  116. rooms. Doors are obstructed by a ball and keys are hidden in boxes.
  117. """
  118. def __init__(self, key_in_box=True, blocked=True, **kwargs):
  119. self.key_in_box = key_in_box
  120. self.blocked = blocked
  121. super().__init__(num_rows=1, num_cols=2, num_rooms_visited=2, **kwargs)
  122. def _gen_grid(self, width, height):
  123. super()._gen_grid(width, height)
  124. self.add_door(
  125. 0,
  126. 0,
  127. door_idx=0,
  128. color=self.door_colors[0],
  129. locked=True,
  130. key_in_box=self.key_in_box,
  131. blocked=self.blocked,
  132. )
  133. self.obj, _ = self.add_object(1, 0, "ball", color=self.ball_to_find_color)
  134. self.place_agent(0, 0)
  135. class ObstructedMaze_Full(ObstructedMazeEnv):
  136. """
  137. A blue ball is hidden in one of the 4 corners of a 3x3 maze. Doors
  138. are locked, doors are obstructed by a ball and keys are hidden in
  139. boxes.
  140. """
  141. def __init__(
  142. self,
  143. agent_room=(1, 1),
  144. key_in_box=True,
  145. blocked=True,
  146. num_quarters=4,
  147. num_rooms_visited=25,
  148. **kwargs,
  149. ):
  150. self.agent_room = agent_room
  151. self.key_in_box = key_in_box
  152. self.blocked = blocked
  153. self.num_quarters = num_quarters
  154. super().__init__(
  155. num_rows=3, num_cols=3, num_rooms_visited=num_rooms_visited, **kwargs
  156. )
  157. def _gen_grid(self, width, height):
  158. super()._gen_grid(width, height)
  159. middle_room = (1, 1)
  160. # Define positions of "side rooms" i.e. rooms that are neither
  161. # corners nor the center.
  162. side_rooms = [(2, 1), (1, 2), (0, 1), (1, 0)][: self.num_quarters]
  163. for i in range(len(side_rooms)):
  164. side_room = side_rooms[i]
  165. # Add a door between the center room and the side room
  166. self.add_door(
  167. *middle_room, door_idx=i, color=self.door_colors[i], locked=False
  168. )
  169. for k in [-1, 1]:
  170. # Add a door to each side of the side room
  171. self.add_door(
  172. *side_room,
  173. locked=True,
  174. door_idx=(i + k) % 4,
  175. color=self.door_colors[(i + k) % len(self.door_colors)],
  176. key_in_box=self.key_in_box,
  177. blocked=self.blocked,
  178. )
  179. corners = [(2, 0), (2, 2), (0, 2), (0, 0)][: self.num_quarters]
  180. ball_room = self._rand_elem(corners)
  181. self.obj, _ = self.add_object(
  182. ball_room[0], ball_room[1], "ball", color=self.ball_to_find_color
  183. )
  184. self.place_agent(*self.agent_room)
  185. class ObstructedMaze_2Dl(ObstructedMaze_Full):
  186. def __init__(self, **kwargs):
  187. super().__init__((2, 1), False, False, 1, 4, **kwargs)
  188. class ObstructedMaze_2Dlh(ObstructedMaze_Full):
  189. def __init__(self, **kwargs):
  190. super().__init__((2, 1), True, False, 1, 4, **kwargs)
  191. class ObstructedMaze_2Dlhb(ObstructedMaze_Full):
  192. def __init__(self, **kwargs):
  193. super().__init__((2, 1), True, True, 1, 4, **kwargs)