unlock.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. from gym_minigrid.minigrid import Ball
  2. from gym_minigrid.roomgrid import RoomGrid
  3. from gym_minigrid.register import register
  4. class Unlock(RoomGrid):
  5. """
  6. Unlock a door
  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. # 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.door = door
  25. self.mission = "open the door"
  26. def step(self, action):
  27. obs, reward, done, info = super().step(action)
  28. if action == self.actions.toggle:
  29. if self.door.is_open:
  30. reward = self._reward()
  31. done = True
  32. return obs, reward, done, info
  33. register(
  34. id='MiniGrid-Unlock-v0',
  35. entry_point='gym_minigrid.envs:Unlock'
  36. )