keycorridor.py 4.2 KB

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