obstructedmaze.py 7.0 KB

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