doorkey.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. from gym_minigrid.minigrid import Door, Goal, Grid, Key, MiniGridEnv, MissionSpace
  2. class DoorKeyEnv(MiniGridEnv):
  3. """
  4. ### Description
  5. This environment has a key that the agent must pick up in order to unlock a
  6. goal and then get to the green goal square. This environment is difficult,
  7. because of the sparse reward, to solve using classical RL algorithms. It is
  8. useful to experiment with curiosity or curriculum learning.
  9. ### Mission Space
  10. "use the key to open the door and then get to the goal"
  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 | Pick up an object |
  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. [gym_minigrid/minigrid.py](gym_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' 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 reaches the goal.
  32. 2. Timeout (see `max_steps`).
  33. ### Registered Configurations
  34. - `MiniGrid-DoorKey-5x5-v0`
  35. - `MiniGrid-DoorKey-6x6-v0`
  36. - `MiniGrid-DoorKey-8x8-v0`
  37. - `MiniGrid-DoorKey-16x16-v0`
  38. """
  39. def __init__(self, size=8, **kwargs):
  40. if "max_steps" not in kwargs:
  41. kwargs["max_steps"] = 10 * size * size
  42. mission_space = MissionSpace(
  43. mission_func=lambda: "use the key to open the door and then get to the goal"
  44. )
  45. super().__init__(mission_space=mission_space, grid_size=size, **kwargs)
  46. def _gen_grid(self, width, height):
  47. # Create an empty grid
  48. self.grid = Grid(width, height)
  49. # Generate the surrounding walls
  50. self.grid.wall_rect(0, 0, width, height)
  51. # Place a goal in the bottom-right corner
  52. self.put_obj(Goal(), width - 2, height - 2)
  53. # Create a vertical splitting wall
  54. splitIdx = self._rand_int(2, width - 2)
  55. self.grid.vert_wall(splitIdx, 0)
  56. # Place the agent at a random position and orientation
  57. # on the left side of the splitting wall
  58. self.place_agent(size=(splitIdx, height))
  59. # Place a door in the wall
  60. doorIdx = self._rand_int(1, width - 2)
  61. self.put_obj(Door("yellow", is_locked=True), splitIdx, doorIdx)
  62. # Place a yellow key on the left side
  63. self.place_obj(obj=Key("yellow"), top=(0, 0), size=(splitIdx, height))
  64. self.mission = "use the key to open the door and then get to the goal"