obstructedmaze.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. from gym_minigrid.minigrid import COLOR_NAMES, DIR_TO_VEC, Ball, Box, Key, MissionSpace
  2. from gym_minigrid.roomgrid import RoomGrid
  3. class ObstructedMazeEnv(RoomGrid):
  4. """
  5. A blue ball is hidden in the maze. Doors may be locked,
  6. doors may be obstructed by a ball and keys may be hidden in boxes.
  7. """
  8. def __init__(self, num_rows, num_cols, num_rooms_visited, **kwargs):
  9. room_size = 6
  10. max_steps = 4 * num_rooms_visited * room_size**2
  11. mission_space = MissionSpace(
  12. mission_func=lambda: f"pick up the {COLOR_NAMES[0]} ball",
  13. )
  14. super().__init__(
  15. mission_space=mission_space,
  16. room_size=room_size,
  17. num_rows=num_rows,
  18. num_cols=num_cols,
  19. max_steps=max_steps,
  20. **kwargs,
  21. )
  22. self.obj = Ball() # initialize the obj attribute, that will be changed later on
  23. def _gen_grid(self, width, height):
  24. super()._gen_grid(width, height)
  25. # Define all possible colors for doors
  26. self.door_colors = self._rand_subset(COLOR_NAMES, len(COLOR_NAMES))
  27. # Define the color of the ball to pick up
  28. self.ball_to_find_color = COLOR_NAMES[0]
  29. # Define the color of the balls that obstruct doors
  30. self.blocking_ball_color = COLOR_NAMES[1]
  31. # Define the color of boxes in which keys are hidden
  32. self.box_color = COLOR_NAMES[2]
  33. self.mission = "pick up the %s ball" % self.ball_to_find_color
  34. def step(self, action):
  35. obs, reward, terminated, truncated, info = super().step(action)
  36. if action == self.actions.pickup:
  37. if self.carrying and self.carrying == self.obj:
  38. reward = self._reward()
  39. terminated = True
  40. return obs, reward, terminated, truncated, info
  41. def add_door(
  42. self,
  43. i,
  44. j,
  45. door_idx=0,
  46. color=None,
  47. locked=False,
  48. key_in_box=False,
  49. blocked=False,
  50. ):
  51. """
  52. Add a door. If the door must be locked, it also adds the key.
  53. If the key must be hidden, it is put in a box. If the door must
  54. be obstructed, it adds a ball in front of the door.
  55. """
  56. door, door_pos = super().add_door(i, j, door_idx, color, locked=locked)
  57. if blocked:
  58. vec = DIR_TO_VEC[door_idx]
  59. blocking_ball = Ball(self.blocking_ball_color) if blocked else None
  60. self.grid.set(door_pos[0] - vec[0], door_pos[1] - vec[1], blocking_ball)
  61. if locked:
  62. obj = Key(door.color)
  63. if key_in_box:
  64. box = Box(self.box_color)
  65. box.contains = obj
  66. obj = box
  67. self.place_in_room(i, j, obj)
  68. return door, door_pos
  69. class ObstructedMaze_1Dlhb(ObstructedMazeEnv):
  70. """
  71. A blue ball is hidden in a 2x1 maze. A locked door separates
  72. rooms. Doors are obstructed by a ball and keys are hidden in boxes.
  73. """
  74. def __init__(self, key_in_box=True, blocked=True, **kwargs):
  75. self.key_in_box = key_in_box
  76. self.blocked = blocked
  77. super().__init__(num_rows=1, num_cols=2, num_rooms_visited=2, **kwargs)
  78. def _gen_grid(self, width, height):
  79. super()._gen_grid(width, height)
  80. self.add_door(
  81. 0,
  82. 0,
  83. door_idx=0,
  84. color=self.door_colors[0],
  85. locked=True,
  86. key_in_box=self.key_in_box,
  87. blocked=self.blocked,
  88. )
  89. self.obj, _ = self.add_object(1, 0, "ball", color=self.ball_to_find_color)
  90. self.place_agent(0, 0)
  91. class ObstructedMaze_Full(ObstructedMazeEnv):
  92. """
  93. A blue ball is hidden in one of the 4 corners of a 3x3 maze. Doors
  94. are locked, doors are obstructed by a ball and keys are hidden in
  95. boxes.
  96. """
  97. def __init__(
  98. self,
  99. agent_room=(1, 1),
  100. key_in_box=True,
  101. blocked=True,
  102. num_quarters=4,
  103. num_rooms_visited=25,
  104. **kwargs,
  105. ):
  106. self.agent_room = agent_room
  107. self.key_in_box = key_in_box
  108. self.blocked = blocked
  109. self.num_quarters = num_quarters
  110. super().__init__(
  111. num_rows=3, num_cols=3, num_rooms_visited=num_rooms_visited, **kwargs
  112. )
  113. def _gen_grid(self, width, height):
  114. super()._gen_grid(width, height)
  115. middle_room = (1, 1)
  116. # Define positions of "side rooms" i.e. rooms that are neither
  117. # corners nor the center.
  118. side_rooms = [(2, 1), (1, 2), (0, 1), (1, 0)][: self.num_quarters]
  119. for i in range(len(side_rooms)):
  120. side_room = side_rooms[i]
  121. # Add a door between the center room and the side room
  122. self.add_door(
  123. *middle_room, door_idx=i, color=self.door_colors[i], locked=False
  124. )
  125. for k in [-1, 1]:
  126. # Add a door to each side of the side room
  127. self.add_door(
  128. *side_room,
  129. locked=True,
  130. door_idx=(i + k) % 4,
  131. color=self.door_colors[(i + k) % len(self.door_colors)],
  132. key_in_box=self.key_in_box,
  133. blocked=self.blocked,
  134. )
  135. corners = [(2, 0), (2, 2), (0, 2), (0, 0)][: self.num_quarters]
  136. ball_room = self._rand_elem(corners)
  137. self.obj, _ = self.add_object(
  138. ball_room[0], ball_room[1], "ball", color=self.ball_to_find_color
  139. )
  140. self.place_agent(*self.agent_room)
  141. class ObstructedMaze_2Dl(ObstructedMaze_Full):
  142. def __init__(self, **kwargs):
  143. super().__init__((2, 1), False, False, 1, 4, **kwargs)
  144. class ObstructedMaze_2Dlh(ObstructedMaze_Full):
  145. def __init__(self, **kwargs):
  146. super().__init__((2, 1), True, False, 1, 4, **kwargs)
  147. class ObstructedMaze_2Dlhb(ObstructedMaze_Full):
  148. def __init__(self, **kwargs):
  149. super().__init__((2, 1), True, True, 1, 4, **kwargs)