blockedunlockpickup.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. from gym_minigrid.minigrid import COLOR_NAMES, Ball, MissionSpace
  2. from gym_minigrid.roomgrid import RoomGrid
  3. class BlockedUnlockPickupEnv(RoomGrid):
  4. """
  5. ### Description
  6. The agent has to pick up a box which is placed in another room, behind a
  7. locked door. The door is also blocked by a ball which the agent has to move
  8. before it can unlock the door. Hence, the agent has to learn to move the
  9. ball, pick up the key, open the door and pick up the object in the other
  10. room. This environment can be solved without relying on language.
  11. ### Mission Space
  12. "pick up the {color} {type}"
  13. {color} is the color of the box. Can be "red", "green", "blue", "purple",
  14. "yellow" or "grey".
  15. {type} is the type of the object. Can be "box" or "key".
  16. ### Action Space
  17. | Num | Name | Action |
  18. |-----|--------------|-------------------|
  19. | 0 | left | Turn left |
  20. | 1 | right | Turn right |
  21. | 2 | forward | Move forward |
  22. | 3 | pickup | Pick up an object |
  23. | 4 | drop | Unused |
  24. | 5 | toggle | Unused |
  25. | 6 | done | Unused |
  26. ### Observation Encoding
  27. - Each tile is encoded as a 3 dimensional tuple:
  28. `(OBJECT_IDX, COLOR_IDX, STATE)`
  29. - `OBJECT_TO_IDX` and `COLOR_TO_IDX` mapping can be found in
  30. [gym_minigrid/minigrid.py](gym_minigrid/minigrid.py)
  31. - `STATE` refers to the door state with 0=open, 1=closed and 2=locked
  32. ### Rewards
  33. A reward of '1' is given for success, and '0' for failure.
  34. ### Termination
  35. The episode ends if any one of the following conditions is met:
  36. 1. The agent picks up the correct box.
  37. 2. Timeout (see `max_steps`).
  38. ### Registered Configurations
  39. - `MiniGrid-BlockedUnlockPickup-v0`
  40. """
  41. def __init__(self, **kwargs):
  42. room_size = 6
  43. mission_space = MissionSpace(
  44. mission_func=lambda color, type: f"pick up the {color} {type}",
  45. ordered_placeholders=[COLOR_NAMES, ["box", "key"]],
  46. )
  47. super().__init__(
  48. mission_space=mission_space,
  49. num_rows=1,
  50. num_cols=2,
  51. room_size=room_size,
  52. max_steps=16 * room_size**2,
  53. **kwargs,
  54. )
  55. def _gen_grid(self, width, height):
  56. super()._gen_grid(width, height)
  57. # Add a box to the room on the right
  58. obj, _ = self.add_object(1, 0, kind="box")
  59. # Make sure the two rooms are directly connected by a locked door
  60. door, pos = self.add_door(0, 0, 0, locked=True)
  61. # Block the door with a ball
  62. color = self._rand_color()
  63. self.grid.set(pos[0] - 1, pos[1], Ball(color))
  64. # Add a key to unlock the door
  65. self.add_object(0, 0, "key", door.color)
  66. self.place_agent(0, 0)
  67. self.obj = obj
  68. self.mission = f"pick up the {obj.color} {obj.type}"
  69. def step(self, action):
  70. obs, reward, terminated, truncated, info = super().step(action)
  71. if action == self.actions.pickup:
  72. if self.carrying and self.carrying == self.obj:
  73. reward = self._reward()
  74. terminated = True
  75. return obs, reward, terminated, truncated, info