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

3 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 SingleDoorEnv(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-SingleDoor-7x6-v0`
  39. """
  40. def __init__(self, width=7, height=6, max_steps: int | None = None, **kwargs):
  41. if max_steps is None:
  42. max_steps = 10 * (width * height)
  43. mission_space = MissionSpace(mission_func=self._gen_mission)
  44. super().__init__(
  45. mission_space=mission_space, width=width, height=height, max_steps=max_steps, **kwargs
  46. )
  47. @staticmethod
  48. def _gen_mission():
  49. return "use the key to open the door and then get to the goal"
  50. def _gen_grid(self, width, height):
  51. # Create an empty grid
  52. self.grid = Grid(width, height)
  53. # Generate the surrounding walls
  54. self.grid.wall_rect(0, 0, width, height)
  55. # Place a goal in the bottom-right corner
  56. self.put_obj(Goal(), width - 2, height - 2)
  57. # Create first vertical splitting wall
  58. # splitIdx = self._rand_int(2, width - 2)
  59. splitIdxOne = width // 2
  60. self.grid.vert_wall(splitIdxOne, 0)
  61. # Place the agent at a random position and orientation
  62. # on the left side of the splitting wall
  63. self.place_agent(size=(splitIdxOne, height))
  64. # Place a door in the wall
  65. doorIdx = height // 2
  66. self.put_obj(Door("yellow", is_locked=True), splitIdxOne, doorIdx)
  67. self.place_obj(obj=Key("yellow"), top=(0, 0), size=(splitIdxOne, height))
  68. # Place a yellow key on the left side
  69. self.mission = "use the key to open the door and then get to the goal"