keycorridor.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. from typing import Optional
  2. from minigrid.core.constants import COLOR_NAMES
  3. from minigrid.core.mission import MissionSpace
  4. from minigrid.core.roomgrid import RoomGrid
  5. class KeyCorridorEnv(RoomGrid):
  6. """
  7. ![KeyCorridorS3R1](../_static/figures/KeyCorridorS3R1.png)<br />
  8. ![KeyCorridorS3R2](../_static/figures/KeyCorridorS3R2.png)<br />
  9. ![KeyCorridorS3R3](../_static/figures/KeyCorridorS3R3.png)<br />
  10. ![KeyCorridorS4R3](../_static/figures/KeyCorridorS4R3.png)<br />
  11. ![KeyCorridorS5R3](../_static/figures/KeyCorridorS5R3.png)<br />
  12. ![KeyCorridorS6R3](../_static/figures/KeyCorridorS6R3.png)
  13. ### Description
  14. This environment is similar to the locked room environment, but there are
  15. multiple registered environment configurations of increasing size,
  16. making it easier to use curriculum learning to train an agent to solve it.
  17. The agent has to pick up an object which is behind a locked door. The key is
  18. hidden in another room, and the agent has to explore the environment to find
  19. it. The mission string does not give the agent any clues as to where the
  20. key is placed. This environment can be solved without relying on language.
  21. ### Mission Space
  22. "pick up the {color} {obj_type}"
  23. {color} is the color of the object. Can be "red", "green", "blue", "purple",
  24. "yellow" or "grey".
  25. {type} is the type of the object. Can be "ball" or "key".
  26. ### Action Space
  27. | Num | Name | Action |
  28. |-----|--------------|-------------------|
  29. | 0 | left | Turn left |
  30. | 1 | right | Turn right |
  31. | 2 | forward | Move forward |
  32. | 3 | pickup | Pick up an object |
  33. | 4 | drop | Unused |
  34. | 5 | toggle | Unused |
  35. | 6 | done | Unused |
  36. ### Observation Encoding
  37. - Each tile is encoded as a 3 dimensional tuple:
  38. `(OBJECT_IDX, COLOR_IDX, STATE)`
  39. - `OBJECT_TO_IDX` and `COLOR_TO_IDX` mapping can be found in
  40. [minigrid/minigrid.py](minigrid/minigrid.py)
  41. - `STATE` refers to the door state with 0=open, 1=closed and 2=locked
  42. ### Rewards
  43. A reward of '1' is given for success, and '0' for failure.
  44. ### Termination
  45. The episode ends if any one of the following conditions is met:
  46. 1. The agent picks up the correct object.
  47. 2. Timeout (see `max_steps`).
  48. ### Registered Configurations
  49. S: room size.
  50. R: Number of rows.
  51. - `MiniGrid-KeyCorridorS3R1-v0`
  52. - `MiniGrid-KeyCorridorS3R2-v0`
  53. - `MiniGrid-KeyCorridorS3R3-v0`
  54. - `MiniGrid-KeyCorridorS4R3-v0`
  55. - `MiniGrid-KeyCorridorS5R3-v0`
  56. - `MiniGrid-KeyCorridorS6R3-v0`
  57. """
  58. def __init__(
  59. self,
  60. num_rows=3,
  61. obj_type="ball",
  62. room_size=6,
  63. max_steps: Optional[int] = None,
  64. **kwargs,
  65. ):
  66. self.obj_type = obj_type
  67. mission_space = MissionSpace(
  68. mission_func=lambda color: f"pick up the {color} {obj_type}",
  69. ordered_placeholders=[COLOR_NAMES],
  70. )
  71. if max_steps is None:
  72. max_steps = 30 * room_size**2
  73. super().__init__(
  74. mission_space=mission_space,
  75. room_size=room_size,
  76. num_rows=num_rows,
  77. max_steps=max_steps,
  78. **kwargs,
  79. )
  80. def _gen_grid(self, width, height):
  81. super()._gen_grid(width, height)
  82. # Connect the middle column rooms into a hallway
  83. for j in range(1, self.num_rows):
  84. self.remove_wall(1, j, 3)
  85. # Add a locked door on the bottom right
  86. # Add an object behind the locked door
  87. room_idx = self._rand_int(0, self.num_rows)
  88. door, _ = self.add_door(2, room_idx, 2, locked=True)
  89. obj, _ = self.add_object(2, room_idx, kind=self.obj_type)
  90. # Add a key in a random room on the left side
  91. self.add_object(0, self._rand_int(0, self.num_rows), "key", door.color)
  92. # Place the agent in the middle
  93. self.place_agent(1, self.num_rows // 2)
  94. # Make sure all rooms are accessible
  95. self.connect_all()
  96. self.obj = obj
  97. self.mission = f"pick up the {obj.color} {obj.type}"
  98. def step(self, action):
  99. obs, reward, terminated, truncated, info = super().step(action)
  100. if action == self.actions.pickup:
  101. if self.carrying and self.carrying == self.obj:
  102. reward = self._reward()
  103. terminated = True
  104. return obs, reward, terminated, truncated, info