doorkey.py 2.0 KB

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