unlockpickup.py 2.9 KB

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