unlockpickup.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. from gym_minigrid.minigrid import Ball
  2. from gym_minigrid.roomgrid import RoomGrid
  3. from gym_minigrid.register import register
  4. class UnlockPickup(RoomGrid):
  5. """
  6. Unlock a door, then pick up a box in another room
  7. """
  8. def __init__(self, seed=None):
  9. room_size = 6
  10. super().__init__(
  11. num_rows=1,
  12. num_cols=2,
  13. room_size=room_size,
  14. max_steps=8*room_size**2,
  15. seed=seed
  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, _ = self.add_door(0, 0, 0, locked=True)
  23. # Add a key to unlock the door
  24. self.add_object(0, 0, 'key', door.color)
  25. self.place_agent(0, 0)
  26. self.obj = obj
  27. self.mission = "pick up the %s %s" % (obj.color, obj.type)
  28. def step(self, action):
  29. obs, reward, done, info = super().step(action)
  30. if action == self.actions.pickup:
  31. if self.carrying and self.carrying == self.obj:
  32. reward = self._reward()
  33. done = True
  34. return obs, reward, done, info
  35. register(
  36. id='MiniGrid-UnlockPickup-v0',
  37. entry_point='gym_minigrid.envs:UnlockPickup'
  38. )