unlockpickup.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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, **kwargs):
  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. **kwargs
  17. )
  18. def _gen_grid(self, width, height):
  19. super()._gen_grid(width, height)
  20. # Add a box to the room on the right
  21. obj, _ = self.add_object(1, 0, kind="box")
  22. # Make sure the two rooms are directly connected by a locked door
  23. door, _ = self.add_door(0, 0, 0, locked=True)
  24. # Add a key to unlock the door
  25. self.add_object(0, 0, 'key', door.color)
  26. self.place_agent(0, 0)
  27. self.obj = obj
  28. self.mission = "pick up the %s %s" % (obj.color, obj.type)
  29. def step(self, action):
  30. obs, reward, done, info = super().step(action)
  31. if action == self.actions.pickup:
  32. if self.carrying and self.carrying == self.obj:
  33. reward = self._reward()
  34. done = True
  35. return obs, reward, done, info
  36. register(
  37. id='MiniGrid-UnlockPickup-v0',
  38. entry_point='gym_minigrid.envs:UnlockPickup'
  39. )