pickup.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. """
  2. Copied and adapted from https://github.com/mila-iqia/babyai.
  3. Levels described in the Baby AI ICLR 2019 submission, with the `Pick up` 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 ObjDesc, PickupInstr
  8. class Pickup(RoomGridLevel):
  9. """
  10. Pick up an object, the object may be in another room.
  11. """
  12. def gen_mission(self):
  13. self.place_agent()
  14. self.connect_all()
  15. objs = self.add_distractors(num_distractors=18, all_unique=False)
  16. self.check_objs_reachable()
  17. obj = self._rand_elem(objs)
  18. self.instrs = PickupInstr(ObjDesc(obj.type, obj.color))
  19. class UnblockPickup(RoomGridLevel):
  20. """
  21. Pick up an object, the object may be in another room. The path may
  22. be blocked by one or more obstructors.
  23. """
  24. def gen_mission(self):
  25. self.place_agent()
  26. self.connect_all()
  27. objs = self.add_distractors(num_distractors=20, all_unique=False)
  28. # Ensure that at least one object is not reachable without unblocking
  29. # Note: the selected object will still be reachable most of the time
  30. if self.check_objs_reachable(raise_exc=False):
  31. raise RejectSampling("all objects reachable")
  32. obj = self._rand_elem(objs)
  33. self.instrs = PickupInstr(ObjDesc(obj.type, obj.color))
  34. class PickupLoc(LevelGen):
  35. """
  36. Pick up an object which may be described using its location. This is a
  37. single room environment.
  38. Competencies: PickUp, Loc. No unblocking.
  39. """
  40. def __init__(self, **kwargs):
  41. # We add many distractors to increase the probability
  42. # of ambiguous locations within the same room
  43. super().__init__(
  44. action_kinds=["pickup"],
  45. instr_kinds=["action"],
  46. num_rows=1,
  47. num_cols=1,
  48. num_dists=8,
  49. locked_room_prob=0,
  50. locations=True,
  51. unblocking=False,
  52. **kwargs
  53. )
  54. class PickupDist(RoomGridLevel):
  55. """
  56. Pick up an object
  57. The object to pick up is given by its type only, or
  58. by its color, or by its type and color.
  59. (in the current room, with distractors)
  60. """
  61. def __init__(self, debug=False, **kwargs):
  62. self.debug = debug
  63. super().__init__(num_rows=1, num_cols=1, room_size=7, **kwargs)
  64. def gen_mission(self):
  65. # Add 5 random objects in the room
  66. objs = self.add_distractors(num_distractors=5)
  67. self.place_agent(0, 0)
  68. obj = self._rand_elem(objs)
  69. type = obj.type
  70. color = obj.color
  71. select_by = self._rand_elem(["type", "color", "both"])
  72. if select_by == "color":
  73. type = None
  74. elif select_by == "type":
  75. color = None
  76. self.instrs = PickupInstr(ObjDesc(type, color), strict=self.debug)
  77. class PickupAbove(RoomGridLevel):
  78. """
  79. Pick up an object (in the room above)
  80. This task requires to use the compass to be solved effectively.
  81. """
  82. def __init__(self, **kwargs):
  83. room_size = 6
  84. super().__init__(room_size=room_size, max_steps=8 * room_size**2, **kwargs)
  85. def gen_mission(self):
  86. # Add a random object to the top-middle room
  87. obj, pos = self.add_object(1, 0)
  88. # Make sure the two rooms are directly connected
  89. self.add_door(1, 1, 3, locked=False)
  90. self.place_agent(1, 1)
  91. self.connect_all()
  92. self.instrs = PickupInstr(ObjDesc(obj.type, obj.color))