doorkey.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. from typing import Optional
  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. ![door-key-curriculum](../_static/figures/door-key-curriculum.gif)<br />
  9. ![door-key-env](../_static/figures/door-key-env.png)
  10. ### Description
  11. This environment has a key that the agent must pick up in order to unlock a
  12. goal and then get to the green goal square. This environment is difficult,
  13. because of the sparse reward, to solve using classical RL algorithms. It is
  14. useful to experiment with curiosity or curriculum learning.
  15. ### Mission Space
  16. "use the key to open the door and then get to the goal"
  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 reaches the goal.
  38. 2. Timeout (see `max_steps`).
  39. ### Registered Configurations
  40. - `MiniGrid-DoorKey-5x5-v0`
  41. - `MiniGrid-DoorKey-6x6-v0`
  42. - `MiniGrid-DoorKey-8x8-v0`
  43. - `MiniGrid-DoorKey-16x16-v0`
  44. """
  45. def __init__(self, size=8, max_steps: Optional[int] = None, **kwargs):
  46. if max_steps is None:
  47. max_steps = 10 * size**2
  48. mission_space = MissionSpace(
  49. mission_func=lambda: "use the key to open the door and then get to the goal"
  50. )
  51. super().__init__(
  52. mission_space=mission_space, grid_size=size, max_steps=max_steps, **kwargs
  53. )
  54. def _gen_grid(self, width, height):
  55. # Create an empty grid
  56. self.grid = Grid(width, height)
  57. # Generate the surrounding walls
  58. self.grid.wall_rect(0, 0, width, height)
  59. # Place a goal in the bottom-right corner
  60. self.put_obj(Goal(), width - 2, height - 2)
  61. # Create a vertical splitting wall
  62. splitIdx = self._rand_int(2, width - 2)
  63. self.grid.vert_wall(splitIdx, 0)
  64. # Place the agent at a random position and orientation
  65. # on the left side of the splitting wall
  66. self.place_agent(size=(splitIdx, height))
  67. # Place a door in the wall
  68. doorIdx = self._rand_int(1, width - 2)
  69. self.put_obj(Door("yellow", is_locked=True), splitIdx, doorIdx)
  70. # Place a yellow key on the left side
  71. self.place_obj(obj=Key("yellow"), top=(0, 0), size=(splitIdx, height))
  72. self.mission = "use the key to open the door and then get to the goal"