keycorridor.py 3.9 KB

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