unlock.py 1006 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. from gym_minigrid.roomgrid import RoomGrid
  2. class UnlockEnv(RoomGrid):
  3. """
  4. Unlock a door
  5. """
  6. def __init__(self, **kwargs):
  7. room_size = 6
  8. super().__init__(
  9. num_rows=1,
  10. num_cols=2,
  11. room_size=room_size,
  12. max_steps=8 * room_size**2,
  13. **kwargs
  14. )
  15. def _gen_grid(self, width, height):
  16. super()._gen_grid(width, height)
  17. # Make sure the two rooms are directly connected by a locked door
  18. door, _ = self.add_door(0, 0, 0, locked=True)
  19. # Add a key to unlock the door
  20. self.add_object(0, 0, "key", door.color)
  21. self.place_agent(0, 0)
  22. self.door = door
  23. self.mission = "open the door"
  24. def step(self, action):
  25. obs, reward, done, info = super().step(action)
  26. if action == self.actions.toggle:
  27. if self.door.is_open:
  28. reward = self._reward()
  29. done = True
  30. return obs, reward, done, info