doorkey.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. class DoorKeyEnv5x5(DoorKeyEnv):
  31. def __init__(self, **kwargs):
  32. super().__init__(size=5, **kwargs)
  33. class DoorKeyEnv6x6(DoorKeyEnv):
  34. def __init__(self, **kwargs):
  35. super().__init__(size=6, **kwargs)
  36. class DoorKeyEnv16x16(DoorKeyEnv):
  37. def __init__(self, **kwargs):
  38. super().__init__(size=16, **kwargs)
  39. register(id="MiniGrid-DoorKey-5x5-v0", entry_point="gym_minigrid.envs:DoorKeyEnv5x5")
  40. register(id="MiniGrid-DoorKey-6x6-v0", entry_point="gym_minigrid.envs:DoorKeyEnv6x6")
  41. register(id="MiniGrid-DoorKey-8x8-v0", entry_point="gym_minigrid.envs:DoorKeyEnv")
  42. register(
  43. id="MiniGrid-DoorKey-16x16-v0", entry_point="gym_minigrid.envs:DoorKeyEnv16x16"
  44. )