gotodoor.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. from minigrid.core.constants import COLOR_NAMES
  2. from minigrid.core.grid import Grid
  3. from minigrid.core.mission import MissionSpace
  4. from minigrid.core.world_object import Door
  5. from minigrid.minigrid_env import MiniGridEnv
  6. class GoToDoorEnv(MiniGridEnv):
  7. """
  8. ### Description
  9. This environment is a room with four doors, one on each wall. The agent
  10. receives a textual (mission) string as input, telling it which door to go
  11. to, (eg: "go to the red door"). It receives a positive reward for performing
  12. the `done` action next to the correct door, as indicated in the mission
  13. string.
  14. ### Mission Space
  15. "go to the {color} door"
  16. {color} is the color of the door. Can be "red", "green", "blue", "purple",
  17. "yellow" or "grey".
  18. ### Action Space
  19. | Num | Name | Action |
  20. |-----|--------------|----------------------|
  21. | 0 | left | Turn left |
  22. | 1 | right | Turn right |
  23. | 2 | forward | Move forward |
  24. | 3 | pickup | Unused |
  25. | 4 | drop | Unused |
  26. | 5 | toggle | Unused |
  27. | 6 | done | Done completing task |
  28. ### Observation Encoding
  29. - Each tile is encoded as a 3 dimensional tuple:
  30. `(OBJECT_IDX, COLOR_IDX, STATE)`
  31. - `OBJECT_TO_IDX` and `COLOR_TO_IDX` mapping can be found in
  32. [minigrid/minigrid.py](minigrid/minigrid.py)
  33. - `STATE` refers to the door state with 0=open, 1=closed and 2=locked
  34. ### Rewards
  35. A reward of '1' is given for success, and '0' for failure.
  36. ### Termination
  37. The episode ends if any one of the following conditions is met:
  38. 1. The agent stands next the correct door performing the `done` action.
  39. 2. Timeout (see `max_steps`).
  40. ### Registered Configurations
  41. - `MiniGrid-GoToDoor-5x5-v0`
  42. - `MiniGrid-GoToDoor-6x6-v0`
  43. - `MiniGrid-GoToDoor-8x8-v0`
  44. """
  45. def __init__(self, size=5, **kwargs):
  46. assert size >= 5
  47. self.size = size
  48. mission_space = MissionSpace(
  49. mission_func=lambda color: f"go to the {color} door",
  50. ordered_placeholders=[COLOR_NAMES],
  51. )
  52. super().__init__(
  53. mission_space=mission_space,
  54. width=size,
  55. height=size,
  56. max_steps=5 * size**2,
  57. # Set this to True for maximum speed
  58. see_through_walls=True,
  59. **kwargs,
  60. )
  61. def _gen_grid(self, width, height):
  62. # Create the grid
  63. self.grid = Grid(width, height)
  64. # Randomly vary the room width and height
  65. width = self._rand_int(5, width + 1)
  66. height = self._rand_int(5, height + 1)
  67. # Generate the surrounding walls
  68. self.grid.wall_rect(0, 0, width, height)
  69. # Generate the 4 doors at random positions
  70. doorPos = []
  71. doorPos.append((self._rand_int(2, width - 2), 0))
  72. doorPos.append((self._rand_int(2, width - 2), height - 1))
  73. doorPos.append((0, self._rand_int(2, height - 2)))
  74. doorPos.append((width - 1, self._rand_int(2, height - 2)))
  75. # Generate the door colors
  76. doorColors = []
  77. while len(doorColors) < len(doorPos):
  78. color = self._rand_elem(COLOR_NAMES)
  79. if color in doorColors:
  80. continue
  81. doorColors.append(color)
  82. # Place the doors in the grid
  83. for idx, pos in enumerate(doorPos):
  84. color = doorColors[idx]
  85. self.grid.set(*pos, Door(color))
  86. # Randomize the agent start position and orientation
  87. self.place_agent(size=(width, height))
  88. # Select a random target door
  89. doorIdx = self._rand_int(0, len(doorPos))
  90. self.target_pos = doorPos[doorIdx]
  91. self.target_color = doorColors[doorIdx]
  92. # Generate the mission string
  93. self.mission = "go to the %s door" % self.target_color
  94. def step(self, action):
  95. obs, reward, terminated, truncated, info = super().step(action)
  96. ax, ay = self.agent_pos
  97. tx, ty = self.target_pos
  98. # Don't let the agent open any of the doors
  99. if action == self.actions.toggle:
  100. terminated = True
  101. # Reward performing done action in front of the target door
  102. if action == self.actions.done:
  103. if (ax == tx and abs(ay - ty) == 1) or (ay == ty and abs(ax - tx) == 1):
  104. reward = self._reward()
  105. terminated = True
  106. return obs, reward, terminated, truncated, info