goto.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. """
  2. Copied and adapted from https://github.com/mila-iqia/babyai.
  3. Levels described in the Baby AI ICLR 2019 submission, with the `Go to` instruction.
  4. """
  5. from minigrid.envs.babyai.core.levelgen import LevelGen
  6. from minigrid.envs.babyai.core.roomgrid_level import RejectSampling, RoomGridLevel
  7. from minigrid.envs.babyai.core.verifier import GoToInstr, ObjDesc
  8. class GoToRedBallGrey(RoomGridLevel):
  9. """
  10. Go to the red ball, single room, with distractors.
  11. The distractors are all grey to reduce perceptual complexity.
  12. This level has distractors but doesn't make use of language.
  13. """
  14. def __init__(self, room_size=8, num_dists=7, **kwargs):
  15. self.num_dists = num_dists
  16. super().__init__(num_rows=1, num_cols=1, room_size=room_size, **kwargs)
  17. def gen_mission(self):
  18. self.place_agent()
  19. obj, _ = self.add_object(0, 0, "ball", "red")
  20. dists = self.add_distractors(num_distractors=self.num_dists, all_unique=False)
  21. for dist in dists:
  22. dist.color = "grey"
  23. # Make sure no unblocking is required
  24. self.check_objs_reachable()
  25. self.instrs = GoToInstr(ObjDesc(obj.type, obj.color))
  26. class GoToRedBall(RoomGridLevel):
  27. """
  28. Go to the red ball, single room, with distractors.
  29. This level has distractors but doesn't make use of language.
  30. """
  31. def __init__(self, room_size=8, num_dists=7, **kwargs):
  32. self.num_dists = num_dists
  33. super().__init__(num_rows=1, num_cols=1, room_size=room_size, **kwargs)
  34. def gen_mission(self):
  35. self.place_agent()
  36. obj, _ = self.add_object(0, 0, "ball", "red")
  37. self.add_distractors(num_distractors=self.num_dists, all_unique=False)
  38. # Make sure no unblocking is required
  39. self.check_objs_reachable()
  40. self.instrs = GoToInstr(ObjDesc(obj.type, obj.color))
  41. class GoToRedBallNoDists(GoToRedBall):
  42. """
  43. Go to the red ball. No distractors present.
  44. """
  45. def __init__(self, **kwargs):
  46. super().__init__(room_size=8, num_dists=0, **kwargs)
  47. class GoToObj(RoomGridLevel):
  48. """
  49. Go to an object, inside a single room with no doors, no distractors
  50. """
  51. def __init__(self, room_size=8, **kwargs):
  52. super().__init__(num_rows=1, num_cols=1, room_size=room_size, **kwargs)
  53. def gen_mission(self):
  54. self.place_agent()
  55. objs = self.add_distractors(num_distractors=1)
  56. obj = objs[0]
  57. self.instrs = GoToInstr(ObjDesc(obj.type, obj.color))
  58. class GoToLocal(RoomGridLevel):
  59. """
  60. Go to an object, inside a single room with no doors, no distractors
  61. """
  62. def __init__(self, room_size=8, num_dists=8, **kwargs):
  63. self.num_dists = num_dists
  64. super().__init__(num_rows=1, num_cols=1, room_size=room_size, **kwargs)
  65. def gen_mission(self):
  66. self.place_agent()
  67. objs = self.add_distractors(num_distractors=self.num_dists, all_unique=False)
  68. self.check_objs_reachable()
  69. obj = self._rand_elem(objs)
  70. self.instrs = GoToInstr(ObjDesc(obj.type, obj.color))
  71. class GoTo(RoomGridLevel):
  72. """
  73. Go to an object, the object may be in another room. Many distractors.
  74. """
  75. def __init__(
  76. self,
  77. room_size=8,
  78. num_rows=3,
  79. num_cols=3,
  80. num_dists=18,
  81. doors_open=False,
  82. **kwargs
  83. ):
  84. self.num_dists = num_dists
  85. self.doors_open = doors_open
  86. super().__init__(
  87. num_rows=num_rows, num_cols=num_cols, room_size=room_size, **kwargs
  88. )
  89. def gen_mission(self):
  90. self.place_agent()
  91. self.connect_all()
  92. objs = self.add_distractors(num_distractors=self.num_dists, all_unique=False)
  93. self.check_objs_reachable()
  94. obj = self._rand_elem(objs)
  95. self.instrs = GoToInstr(ObjDesc(obj.type, obj.color))
  96. # If requested, open all the doors
  97. if self.doors_open:
  98. self.open_all_doors()
  99. class GoToImpUnlock(RoomGridLevel):
  100. """
  101. Go to an object, which may be in a locked room.
  102. Competencies: Maze, GoTo, ImpUnlock
  103. No unblocking.
  104. """
  105. def gen_mission(self):
  106. # Add a locked door to a random room
  107. id = self._rand_int(0, self.num_cols)
  108. jd = self._rand_int(0, self.num_rows)
  109. door, pos = self.add_door(id, jd, locked=True)
  110. locked_room = self.get_room(id, jd)
  111. # Add the key to a different room
  112. while True:
  113. ik = self._rand_int(0, self.num_cols)
  114. jk = self._rand_int(0, self.num_rows)
  115. if ik is id and jk is jd:
  116. continue
  117. self.add_object(ik, jk, "key", door.color)
  118. break
  119. self.connect_all()
  120. # Add distractors to all but the locked room.
  121. # We do this to speed up the reachability test,
  122. # which otherwise will reject all levels with
  123. # objects in the locked room.
  124. for i in range(self.num_cols):
  125. for j in range(self.num_rows):
  126. if i is not id or j is not jd:
  127. self.add_distractors(i, j, num_distractors=2, all_unique=False)
  128. # The agent must be placed after all the object to respect constraints
  129. while True:
  130. self.place_agent()
  131. start_room = self.room_from_pos(*self.agent_pos)
  132. # Ensure that we are not placing the agent in the locked room
  133. if start_room is locked_room:
  134. continue
  135. break
  136. self.check_objs_reachable()
  137. # Add a single object to the locked room
  138. # The instruction requires going to an object matching that description
  139. (obj,) = self.add_distractors(id, jd, num_distractors=1, all_unique=False)
  140. self.instrs = GoToInstr(ObjDesc(obj.type, obj.color))
  141. class GoToSeq(LevelGen):
  142. """
  143. Sequencing of go-to-object commands.
  144. Competencies: Maze, GoTo, Seq
  145. No locked room.
  146. No locations.
  147. No unblocking.
  148. """
  149. def __init__(self, room_size=8, num_rows=3, num_cols=3, num_dists=18, **kwargs):
  150. super().__init__(
  151. room_size=room_size,
  152. num_rows=num_rows,
  153. num_cols=num_cols,
  154. num_dists=num_dists,
  155. action_kinds=["goto"],
  156. locked_room_prob=0,
  157. locations=False,
  158. unblocking=False,
  159. **kwargs
  160. )
  161. class GoToRedBlueBall(RoomGridLevel):
  162. """
  163. Go to the red ball or to the blue ball.
  164. There is exactly one red or blue ball, and some distractors.
  165. The distractors are guaranteed not to be red or blue balls.
  166. Language is not required to solve this level.
  167. """
  168. def __init__(self, room_size=8, num_dists=7, **kwargs):
  169. self.num_dists = num_dists
  170. super().__init__(num_rows=1, num_cols=1, room_size=room_size, **kwargs)
  171. def gen_mission(self):
  172. self.place_agent()
  173. dists = self.add_distractors(num_distractors=self.num_dists, all_unique=False)
  174. # Ensure there is only one red or blue ball
  175. for dist in dists:
  176. if dist.type == "ball" and (dist.color == "blue" or dist.color == "red"):
  177. raise RejectSampling("can only have one blue or red ball")
  178. color = self._rand_elem(["red", "blue"])
  179. obj, _ = self.add_object(0, 0, "ball", color)
  180. # Make sure no unblocking is required
  181. self.check_objs_reachable()
  182. self.instrs = GoToInstr(ObjDesc(obj.type, obj.color))
  183. class GoToDoor(RoomGridLevel):
  184. """
  185. Go to a door
  186. (of a given color, in the current room)
  187. No distractors, no language variation
  188. """
  189. def __init__(self, **kwargs):
  190. super().__init__(room_size=7, **kwargs)
  191. def gen_mission(self):
  192. objs = []
  193. for _ in range(4):
  194. door, _ = self.add_door(1, 1)
  195. objs.append(door)
  196. self.place_agent(1, 1)
  197. obj = self._rand_elem(objs)
  198. self.instrs = GoToInstr(ObjDesc("door", obj.color))
  199. class GoToObjDoor(RoomGridLevel):
  200. """
  201. Go to an object or door
  202. (of a given type and color, in the current room)
  203. """
  204. def __init__(self, **kwargs):
  205. super().__init__(room_size=8, **kwargs)
  206. def gen_mission(self):
  207. self.place_agent(1, 1)
  208. objs = self.add_distractors(1, 1, num_distractors=8, all_unique=False)
  209. for _ in range(4):
  210. door, _ = self.add_door(1, 1)
  211. objs.append(door)
  212. self.check_objs_reachable()
  213. obj = self._rand_elem(objs)
  214. self.instrs = GoToInstr(ObjDesc(obj.type, obj.color))