You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
3.1 KiB

3 months ago
  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 UnlockPickupEnv(RoomGrid):
  6. """
  7. ## Description
  8. The agent has to pick up a box which is placed in another room, behind a
  9. locked door. This environment can be solved without relying on language.
  10. ## Mission Space
  11. "pick up the {color} box"
  12. {color} is the color of the box. Can be "red", "green", "blue", "purple",
  13. "yellow" or "grey".
  14. ## Action Space
  15. | Num | Name | Action |
  16. |-----|--------------|---------------------------|
  17. | 0 | left | Turn left |
  18. | 1 | right | Turn right |
  19. | 2 | forward | Move forward |
  20. | 3 | pickup | Pick up an object |
  21. | 4 | drop | Unused |
  22. | 5 | toggle | Toggle/activate an object |
  23. | 6 | done | Unused |
  24. ## Observation Encoding
  25. - Each tile is encoded as a 3 dimensional tuple:
  26. `(OBJECT_IDX, COLOR_IDX, STATE)`
  27. - `OBJECT_TO_IDX` and `COLOR_TO_IDX` mapping can be found in
  28. [minigrid/minigrid.py](minigrid/minigrid.py)
  29. - `STATE` refers to the door state with 0=open, 1=closed and 2=locked
  30. ## Rewards
  31. A reward of '1 - 0.9 * (step_count / max_steps)' is given for success, and '0' for failure.
  32. ## Termination
  33. The episode ends if any one of the following conditions is met:
  34. 1. The agent picks up the correct box.
  35. 2. Timeout (see `max_steps`).
  36. ## Registered Configurations
  37. - `MiniGrid-Unlock-v0`
  38. """
  39. def __init__(self, max_steps: int | None = None, **kwargs):
  40. room_size = 6
  41. mission_space = MissionSpace(
  42. mission_func=self._gen_mission,
  43. ordered_placeholders=[COLOR_NAMES],
  44. )
  45. if max_steps is None:
  46. max_steps = 8 * room_size**2
  47. super().__init__(
  48. mission_space=mission_space,
  49. num_rows=1,
  50. num_cols=2,
  51. room_size=room_size,
  52. max_steps=max_steps,
  53. **kwargs,
  54. )
  55. @staticmethod
  56. def _gen_mission(color: str):
  57. return f"pick up the {color} box"
  58. def _gen_grid(self, width, height):
  59. super()._gen_grid(width, height)
  60. # Add a box to the room on the right
  61. obj, _ = self.add_object(1, 0, kind="box")
  62. # Make sure the two rooms are directly connected by a locked door
  63. door, _ = self.add_door(0, 0, 0, locked=True)
  64. # Add a key to unlock the door
  65. self.add_object(0, 0, "key", door.color)
  66. self.place_agent(0, 0)
  67. self.obj = obj
  68. self.mission = f"pick up the {obj.color} {obj.type}"
  69. def step(self, action):
  70. obs, reward, terminated, truncated, info = super().step(action)
  71. if action == self.actions.pickup:
  72. if self.carrying and self.carrying == self.obj:
  73. reward = self._reward()
  74. terminated = True
  75. return obs, reward, terminated, truncated, info