The source code and dockerfile for the GSW2024 AI Lab.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

97 lines
2.7 KiB

3 months ago
  1. from __future__ import annotations
  2. from minigrid.core.mission import MissionSpace
  3. from minigrid.core.roomgrid import RoomGrid
  4. class UnlockEnv(RoomGrid):
  5. """
  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 - 0.9 * (step_count / max_steps)' 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, max_steps: int | None = None, **kwargs):
  37. room_size = 6
  38. mission_space = MissionSpace(mission_func=self._gen_mission)
  39. if max_steps is None:
  40. max_steps = 8 * room_size**2
  41. super().__init__(
  42. mission_space=mission_space,
  43. num_rows=1,
  44. num_cols=2,
  45. room_size=room_size,
  46. max_steps=max_steps,
  47. **kwargs,
  48. )
  49. @staticmethod
  50. def _gen_mission():
  51. return "open the door"
  52. def _gen_grid(self, width, height):
  53. super()._gen_grid(width, height)
  54. # Make sure the two rooms are directly connected by a locked door
  55. door, _ = self.add_door(0, 0, 0, locked=True)
  56. # Add a key to unlock the door
  57. self.add_object(0, 0, "key", door.color)
  58. self.place_agent(0, 0)
  59. self.door = door
  60. self.mission = "open the door"
  61. def step(self, action):
  62. obs, reward, terminated, truncated, info = super().step(action)
  63. if action == self.actions.toggle:
  64. if self.door.is_open:
  65. reward = self._reward()
  66. terminated = True
  67. return obs, reward, terminated, truncated, info