keycorridor.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. from gym_minigrid.minigrid import COLOR_NAMES, MissionSpace
  2. from gym_minigrid.roomgrid import RoomGrid
  3. class KeyCorridorEnv(RoomGrid):
  4. """
  5. A ball is behind a locked door, the key is placed in a
  6. random room.
  7. """
  8. def __init__(self, num_rows=3, obj_type="ball", room_size=6, **kwargs):
  9. self.obj_type = obj_type
  10. mission_space = MissionSpace(
  11. mission_func=lambda color: f"pick up the {color} {obj_type}",
  12. ordered_placeholders=[COLOR_NAMES],
  13. )
  14. super().__init__(
  15. mission_space=mission_space,
  16. room_size=room_size,
  17. num_rows=num_rows,
  18. max_steps=30 * room_size**2,
  19. **kwargs,
  20. )
  21. def _gen_grid(self, width, height):
  22. super()._gen_grid(width, height)
  23. # Connect the middle column rooms into a hallway
  24. for j in range(1, self.num_rows):
  25. self.remove_wall(1, j, 3)
  26. # Add a locked door on the bottom right
  27. # Add an object behind the locked door
  28. room_idx = self._rand_int(0, self.num_rows)
  29. door, _ = self.add_door(2, room_idx, 2, locked=True)
  30. obj, _ = self.add_object(2, room_idx, kind=self.obj_type)
  31. # Add a key in a random room on the left side
  32. self.add_object(0, self._rand_int(0, self.num_rows), "key", door.color)
  33. # Place the agent in the middle
  34. self.place_agent(1, self.num_rows // 2)
  35. # Make sure all rooms are accessible
  36. self.connect_all()
  37. self.obj = obj
  38. self.mission = f"pick up the {obj.color} {obj.type}"
  39. def step(self, action):
  40. obs, reward, terminated, truncated, info = super().step(action)
  41. if action == self.actions.pickup:
  42. if self.carrying and self.carrying == self.obj:
  43. reward = self._reward()
  44. terminated = True
  45. return obs, reward, terminated, truncated, info