unlock.py 2.5 KB

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