unlock.py 2.4 KB

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