unlockpickup.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. from gym_minigrid.minigrid import COLOR_NAMES, MissionSpace
  2. from gym_minigrid.roomgrid import RoomGrid
  3. class UnlockPickupEnv(RoomGrid):
  4. """
  5. Unlock a door, then pick up a box in another room
  6. """
  7. def __init__(self, **kwargs):
  8. room_size = 6
  9. mission_space = MissionSpace(
  10. mission_func=lambda color: f"pick up the {color} box",
  11. ordered_placeholders=[COLOR_NAMES],
  12. )
  13. super().__init__(
  14. mission_space=mission_space,
  15. num_rows=1,
  16. num_cols=2,
  17. room_size=room_size,
  18. max_steps=8 * room_size**2,
  19. **kwargs,
  20. )
  21. def _gen_grid(self, width, height):
  22. super()._gen_grid(width, height)
  23. # Add a box to the room on the right
  24. obj, _ = self.add_object(1, 0, kind="box")
  25. # Make sure the two rooms are directly connected by a locked door
  26. door, _ = self.add_door(0, 0, 0, locked=True)
  27. # Add a key to unlock the door
  28. self.add_object(0, 0, "key", door.color)
  29. self.place_agent(0, 0)
  30. self.obj = obj
  31. self.mission = f"pick up the {obj.color} {obj.type}"
  32. def step(self, action):
  33. obs, reward, done, info = super().step(action)
  34. if action == self.actions.pickup:
  35. if self.carrying and self.carrying == self.obj:
  36. reward = self._reward()
  37. done = True
  38. return obs, reward, done, info