blockedunlockpickup.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. from minigrid.core.constants import COLOR_NAMES
  2. from minigrid.core.mission import MissionSpace
  3. from minigrid.core.roomgrid import RoomGrid
  4. from minigrid.core.world_object import Ball
  5. class BlockedUnlockPickupEnv(RoomGrid):
  6. """
  7. ### Description
  8. The agent has to pick up a box which is placed in another room, behind a
  9. locked door. The door is also blocked by a ball which the agent has to move
  10. before it can unlock the door. Hence, the agent has to learn to move the
  11. ball, pick up the key, open the door and pick up the object in the other
  12. room. This environment can be solved without relying on language.
  13. ### Mission Space
  14. "pick up the {color} {type}"
  15. {color} is the color of the box. Can be "red", "green", "blue", "purple",
  16. "yellow" or "grey".
  17. {type} is the type of the object. Can be "box" 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. [minigrid/minigrid.py](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 box.
  39. 2. Timeout (see `max_steps`).
  40. ### Registered Configurations
  41. - `MiniGrid-BlockedUnlockPickup-v0`
  42. """
  43. def __init__(self, **kwargs):
  44. room_size = 6
  45. mission_space = MissionSpace(
  46. mission_func=lambda color, type: f"pick up the {color} {type}",
  47. ordered_placeholders=[COLOR_NAMES, ["box", "key"]],
  48. )
  49. super().__init__(
  50. mission_space=mission_space,
  51. num_rows=1,
  52. num_cols=2,
  53. room_size=room_size,
  54. max_steps=16 * room_size**2,
  55. **kwargs,
  56. )
  57. def _gen_grid(self, width, height):
  58. super()._gen_grid(width, height)
  59. # Add a box to the room on the right
  60. obj, _ = self.add_object(1, 0, kind="box")
  61. # Make sure the two rooms are directly connected by a locked door
  62. door, pos = self.add_door(0, 0, 0, locked=True)
  63. # Block the door with a ball
  64. color = self._rand_color()
  65. self.grid.set(pos[0] - 1, pos[1], Ball(color))
  66. # Add a key to unlock the door
  67. self.add_object(0, 0, "key", door.color)
  68. self.place_agent(0, 0)
  69. self.obj = obj
  70. self.mission = f"pick up the {obj.color} {obj.type}"
  71. def step(self, action):
  72. obs, reward, terminated, truncated, info = super().step(action)
  73. if action == self.actions.pickup:
  74. if self.carrying and self.carrying == self.obj:
  75. reward = self._reward()
  76. terminated = True
  77. return obs, reward, terminated, truncated, info