blockedunlockpickup.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. from typing import Optional
  2. from minigrid.core.constants import COLOR_NAMES
  3. from minigrid.core.mission import MissionSpace
  4. from minigrid.core.roomgrid import RoomGrid
  5. from minigrid.core.world_object import Ball
  6. class BlockedUnlockPickupEnv(RoomGrid):
  7. """
  8. <p>
  9. <img src="https://raw.githubusercontent.com/Farama-Foundation/Minigrid/master/figures/BlockedUnlockPickup.png" alt="BlockedUnlockPickup" width="200px"/>
  10. </p>
  11. ### Description
  12. The agent has to pick up a box which is placed in another room, behind a
  13. locked door. The door is also blocked by a ball which the agent has to move
  14. before it can unlock the door. Hence, the agent has to learn to move the
  15. ball, pick up the key, open the door and pick up the object in the other
  16. room. This environment can be solved without relying on language.
  17. ### Mission Space
  18. "pick up the {color} {type}"
  19. {color} is the color of the box. Can be "red", "green", "blue", "purple",
  20. "yellow" or "grey".
  21. {type} is the type of the object. Can be "box" or "key".
  22. ### Action Space
  23. | Num | Name | Action |
  24. |-----|--------------|-------------------|
  25. | 0 | left | Turn left |
  26. | 1 | right | Turn right |
  27. | 2 | forward | Move forward |
  28. | 3 | pickup | Pick up an object |
  29. | 4 | drop | Unused |
  30. | 5 | toggle | Unused |
  31. | 6 | done | Unused |
  32. ### Observation Encoding
  33. - Each tile is encoded as a 3 dimensional tuple:
  34. `(OBJECT_IDX, COLOR_IDX, STATE)`
  35. - `OBJECT_TO_IDX` and `COLOR_TO_IDX` mapping can be found in
  36. [minigrid/minigrid.py](minigrid/minigrid.py)
  37. - `STATE` refers to the door state with 0=open, 1=closed and 2=locked
  38. ### Rewards
  39. A reward of '1' is given for success, and '0' for failure.
  40. ### Termination
  41. The episode ends if any one of the following conditions is met:
  42. 1. The agent picks up the correct box.
  43. 2. Timeout (see `max_steps`).
  44. ### Registered Configurations
  45. - `MiniGrid-BlockedUnlockPickup-v0`
  46. """
  47. def __init__(self, max_steps: Optional[int] = None, **kwargs):
  48. mission_space = MissionSpace(
  49. mission_func=self._gen_mission,
  50. ordered_placeholders=[COLOR_NAMES, ["box", "key"]],
  51. )
  52. room_size = 6
  53. if max_steps is None:
  54. max_steps = 16 * room_size**2
  55. super().__init__(
  56. mission_space=mission_space,
  57. num_rows=1,
  58. num_cols=2,
  59. room_size=room_size,
  60. max_steps=max_steps,
  61. **kwargs,
  62. )
  63. @staticmethod
  64. def _gen_mission(color: str, obj_type: str):
  65. return f"pick up the {color} {obj_type}"
  66. def _gen_grid(self, width, height):
  67. super()._gen_grid(width, height)
  68. # Add a box to the room on the right
  69. obj, _ = self.add_object(1, 0, kind="box")
  70. # Make sure the two rooms are directly connected by a locked door
  71. door, pos = self.add_door(0, 0, 0, locked=True)
  72. # Block the door with a ball
  73. color = self._rand_color()
  74. self.grid.set(pos[0] - 1, pos[1], Ball(color))
  75. # Add a key to unlock the door
  76. self.add_object(0, 0, "key", door.color)
  77. self.place_agent(0, 0)
  78. self.obj = obj
  79. self.mission = f"pick up the {obj.color} {obj.type}"
  80. def step(self, action):
  81. obs, reward, terminated, truncated, info = super().step(action)
  82. if action == self.actions.pickup:
  83. if self.carrying and self.carrying == self.obj:
  84. reward = self._reward()
  85. terminated = True
  86. return obs, reward, terminated, truncated, info