unlockpickup.py 1.2 KB

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