blockedunlockpickup.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. from gym_minigrid.minigrid import Ball
  2. from gym_minigrid.roomgrid import RoomGrid
  3. from gym_minigrid.register import register
  4. class BlockedUnlockPickup(RoomGrid):
  5. """
  6. Unlock a door blocked by a ball, then pick up a box
  7. in another room
  8. """
  9. def __init__(self, seed=None):
  10. room_size = 6
  11. super().__init__(
  12. num_rows=1,
  13. num_cols=2,
  14. room_size=room_size,
  15. max_steps=16*room_size**2,
  16. seed=seed
  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, pos = self.add_door(0, 0, 0, locked=True)
  24. # Block the door with a ball
  25. color = self._rand_color()
  26. self.grid.set(pos[0]-1, pos[1], Ball(color))
  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 = "pick up the %s %s" % (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
  39. register(
  40. id='MiniGrid-BlockedUnlockPickup-v0',
  41. entry_point='gym_minigrid.envs:BlockedUnlockPickup'
  42. )