keycorridor.py 4.3 KB

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