unlockpickup.py 3.2 KB

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