doorkey.py 1.7 KB

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