doorkey.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. from gym_minigrid.minigrid import Door, Goal, Grid, Key, MiniGridEnv
  2. from gym_minigrid.register import register
  3. class DoorKeyEnv(MiniGridEnv):
  4. """
  5. Environment with a door and key, sparse reward
  6. """
  7. def __init__(self, size=8, **kwargs):
  8. if "max_steps" not in kwargs:
  9. kwargs["max_steps"] = 10 * size * size
  10. super().__init__(grid_size=size, **kwargs)
  11. def _gen_grid(self, width, height):
  12. # Create an empty grid
  13. self.grid = Grid(width, height)
  14. # Generate the surrounding walls
  15. self.grid.wall_rect(0, 0, width, height)
  16. # Place a goal in the bottom-right corner
  17. self.put_obj(Goal(), width - 2, height - 2)
  18. # Create a vertical splitting wall
  19. splitIdx = self._rand_int(2, width - 2)
  20. self.grid.vert_wall(splitIdx, 0)
  21. # Place the agent at a random position and orientation
  22. # on the left side of the splitting wall
  23. self.place_agent(size=(splitIdx, height))
  24. # Place a door in the wall
  25. doorIdx = self._rand_int(1, width - 2)
  26. self.put_obj(Door("yellow", is_locked=True), splitIdx, doorIdx)
  27. # Place a yellow key on the left side
  28. self.place_obj(obj=Key("yellow"), top=(0, 0), size=(splitIdx, height))
  29. self.mission = "use the key to open the door and then get to the goal"
  30. register(
  31. id="MiniGrid-DoorKey-5x5-v0",
  32. entry_point="gym_minigrid.envs.doorkey:DoorKeyEnv",
  33. size=5,
  34. )
  35. register(
  36. id="MiniGrid-DoorKey-6x6-v0",
  37. entry_point="gym_minigrid.envs.doorkey:DoorKeyEnv",
  38. size=6,
  39. )
  40. register(
  41. id="MiniGrid-DoorKey-8x8-v0",
  42. entry_point="gym_minigrid.envs.doorkey:DoorKeyEnv",
  43. size=8,
  44. )
  45. register(
  46. id="MiniGrid-DoorKey-16x16-v0",
  47. entry_point="gym_minigrid.envs.doorkey:DoorKeyEnv",
  48. size=16,
  49. )