blockedunlockpickup.py 1.4 KB

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