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.

100 lines
3.3 KiB

2 months ago
  1. from __future__ import annotations
  2. from minigrid.core.grid import Grid
  3. from minigrid.core.mission import MissionSpace
  4. from minigrid.core.world_object import Door, Goal, Key
  5. from minigrid.minigrid_env import MiniGridEnv
  6. class DoorKeyEnv(MiniGridEnv):
  7. """
  8. ## Description
  9. This environment has a key that the agent must pick up in order to unlock a
  10. goal and then get to the green goal square. This environment is difficult,
  11. because of the sparse reward, to solve using classical RL algorithms. It is
  12. useful to experiment with curiosity or curriculum learning.
  13. ## Mission Space
  14. "use the key to open the door and then get to the goal"
  15. ## Action Space
  16. | Num | Name | Action |
  17. |-----|--------------|---------------------------|
  18. | 0 | left | Turn left |
  19. | 1 | right | Turn right |
  20. | 2 | forward | Move forward |
  21. | 3 | pickup | Pick up an object |
  22. | 4 | drop | Unused |
  23. | 5 | toggle | Toggle/activate an object |
  24. | 6 | done | Unused |
  25. ## Observation Encoding
  26. - Each tile is encoded as a 3 dimensional tuple:
  27. `(OBJECT_IDX, COLOR_IDX, STATE)`
  28. - `OBJECT_TO_IDX` and `COLOR_TO_IDX` mapping can be found in
  29. [minigrid/minigrid.py](minigrid/minigrid.py)
  30. - `STATE` refers to the door state with 0=open, 1=closed and 2=locked
  31. ## Rewards
  32. A reward of '1 - 0.9 * (step_count / max_steps)' is given for success, and '0' for failure.
  33. ## Termination
  34. The episode ends if any one of the following conditions is met:
  35. 1. The agent reaches the goal.
  36. 2. Timeout (see `max_steps`).
  37. ## Registered Configurations
  38. - `MiniGrid-DoorKey-5x5-v0`
  39. - `MiniGrid-DoorKey-6x6-v0`
  40. - `MiniGrid-DoorKey-8x8-v0`
  41. - `MiniGrid-DoorKey-16x16-v0`
  42. """
  43. def __init__(self, size=8, max_steps: int | None = None, **kwargs):
  44. if max_steps is None:
  45. max_steps = 10 * size**2
  46. mission_space = MissionSpace(mission_func=self._gen_mission)
  47. super().__init__(
  48. mission_space=mission_space, grid_size=size, max_steps=max_steps, **kwargs
  49. )
  50. @staticmethod
  51. def _gen_mission():
  52. return "use the key to open the door and then get to the goal"
  53. def _gen_grid(self, width, height):
  54. # Create an empty grid
  55. self.grid = Grid(width, height)
  56. # Generate the surrounding walls
  57. self.grid.wall_rect(0, 0, width, height)
  58. # Place a goal in the bottom-right corner
  59. self.put_obj(Goal(), width - 2, height - 2)
  60. # Create a vertical splitting wall
  61. splitIdx = self._rand_int(2, width - 2)
  62. self.grid.vert_wall(splitIdx, 0)
  63. # Place the agent at a random position and orientation
  64. # on the left side of the splitting wall
  65. self.place_agent(size=(splitIdx, height))
  66. # Place a door in the wall
  67. doorIdx = self._rand_int(1, width - 2)
  68. self.put_obj(Door("yellow", is_locked=True), splitIdx, doorIdx)
  69. # Place a yellow key on the left side
  70. self.place_obj(obj=Key("yellow"), top=(0, 0), size=(splitIdx, height))
  71. self.mission = "use the key to open the door and then get to the goal"