doorkey.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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__(gridSize=size, maxSteps=4 * size)
  9. def _genGrid(self, width, height):
  10. # Create an empty grid
  11. grid = Grid(width, height)
  12. # Place walls around the edges
  13. for i in range(0, width):
  14. grid.set(i, 0, Wall())
  15. grid.set(i, height - 1, Wall())
  16. for j in range(0, height):
  17. grid.set(0, j, Wall())
  18. grid.set(height - 1, j, Wall())
  19. # Place a goal in the bottom-right corner
  20. grid.set(width - 2, height - 2, Goal())
  21. # Create a vertical splitting wall
  22. splitIdx = self._randInt(2, width-2)
  23. for i in range(0, height):
  24. grid.set(splitIdx, i, Wall())
  25. # Place the agent at a random position and orientation
  26. self.startPos = self._randPos(
  27. 1, splitIdx,
  28. 1, height-1
  29. )
  30. self.startDir = self._randInt(0, 4)
  31. # Place a door in the wall
  32. doorIdx = self._randInt(1, width-2)
  33. grid.set(splitIdx, doorIdx, LockedDoor('yellow'))
  34. # Place a yellow key on the left side
  35. while True:
  36. pos = self._randPos(
  37. 1, splitIdx,
  38. 1, height-1
  39. )
  40. if pos == self.startPos:
  41. continue
  42. if grid.get(*pos) != None:
  43. continue
  44. grid.set(*pos, Key('yellow'))
  45. break
  46. return grid
  47. class DoorKeyEnv5x5(DoorKeyEnv):
  48. def __init__(self):
  49. super().__init__(size=5)
  50. class DoorKeyEnv6x6(DoorKeyEnv):
  51. def __init__(self):
  52. super().__init__(size=6)
  53. class DoorKeyEnv16x16(DoorKeyEnv):
  54. def __init__(self):
  55. super().__init__(size=16)
  56. register(
  57. id='MiniGrid-DoorKey-5x5-v0',
  58. entry_point='gym_minigrid.envs:DoorKeyEnv5x5'
  59. )
  60. register(
  61. id='MiniGrid-DoorKey-6x6-v0',
  62. entry_point='gym_minigrid.envs:DoorKeyEnv6x6'
  63. )
  64. register(
  65. id='MiniGrid-DoorKey-8x8-v0',
  66. entry_point='gym_minigrid.envs:DoorKeyEnv'
  67. )
  68. register(
  69. id='MiniGrid-DoorKey-16x16-v0',
  70. entry_point='gym_minigrid.envs:DoorKeyEnv16x16'
  71. )