doorkey.py 3.1 KB

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