keycorridor.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. from gym_minigrid.minigrid import COLOR_NAMES, MissionSpace
  2. from gym_minigrid.roomgrid import RoomGrid
  3. class KeyCorridorEnv(RoomGrid):
  4. """
  5. ### Description
  6. This environment is similar to the locked room environment, but there are
  7. multiple registered environment configurations of increasing size,
  8. making it easier to use curriculum learning to train an agent to solve it.
  9. The agent has to pick up an object which is behind a locked door. The key is
  10. hidden in another room, and the agent has to explore the environment to find
  11. it. The mission string does not give the agent any clues as to where the
  12. key is placed. This environment can be solved without relying on language.
  13. ### Mission Space
  14. "pick up the {color} {obj_type}"
  15. {color} is the color of the object. Can be "red", "green", "blue", "purple",
  16. "yellow" or "grey".
  17. {type} is the type of the object. Can be "ball" or "key".
  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 | Pick up an object |
  25. | 4 | drop | Unused |
  26. | 5 | toggle | Unused |
  27. | 6 | done | Unused |
  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. [gym_minigrid/minigrid.py](gym_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 picks up the correct object.
  39. 2. Timeout (see `max_steps`).
  40. ### Registered Configurations
  41. S: room size.
  42. R: Number of rows.
  43. - `MiniGrid-KeyCorridorS3R1-v0`
  44. - `MiniGrid-KeyCorridorS3R2-v0`
  45. - `MiniGrid-KeyCorridorS3R3-v0`
  46. - `MiniGrid-KeyCorridorS4R3-v0`
  47. - `MiniGrid-KeyCorridorS5R3-v0`
  48. - `MiniGrid-KeyCorridorS6R3-v0`
  49. """
  50. def __init__(self, num_rows=3, obj_type="ball", room_size=6, **kwargs):
  51. self.obj_type = obj_type
  52. mission_space = MissionSpace(
  53. mission_func=lambda color: f"pick up the {color} {obj_type}",
  54. ordered_placeholders=[COLOR_NAMES],
  55. )
  56. super().__init__(
  57. mission_space=mission_space,
  58. room_size=room_size,
  59. num_rows=num_rows,
  60. max_steps=30 * room_size**2,
  61. **kwargs,
  62. )
  63. def _gen_grid(self, width, height):
  64. super()._gen_grid(width, height)
  65. # Connect the middle column rooms into a hallway
  66. for j in range(1, self.num_rows):
  67. self.remove_wall(1, j, 3)
  68. # Add a locked door on the bottom right
  69. # Add an object behind the locked door
  70. room_idx = self._rand_int(0, self.num_rows)
  71. door, _ = self.add_door(2, room_idx, 2, locked=True)
  72. obj, _ = self.add_object(2, room_idx, kind=self.obj_type)
  73. # Add a key in a random room on the left side
  74. self.add_object(0, self._rand_int(0, self.num_rows), "key", door.color)
  75. # Place the agent in the middle
  76. self.place_agent(1, self.num_rows // 2)
  77. # Make sure all rooms are accessible
  78. self.connect_all()
  79. self.obj = obj
  80. self.mission = f"pick up the {obj.color} {obj.type}"
  81. def step(self, action):
  82. obs, reward, terminated, truncated, info = super().step(action)
  83. if action == self.actions.pickup:
  84. if self.carrying and self.carrying == self.obj:
  85. reward = self._reward()
  86. terminated = True
  87. return obs, reward, terminated, truncated, info