simple_envs.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. from gym_minigrid.minigrid import *
  2. from gym_minigrid.register import register
  3. class EmptyEnv(MiniGridEnv):
  4. """
  5. Empty grid environment, no obstacles, sparse reward
  6. """
  7. def __init__(self, size=8):
  8. super().__init__(gridSize=size, maxSteps=3 * size)
  9. class EmptyEnv6x6(EmptyEnv):
  10. def __init__(self):
  11. super().__init__(size=6)
  12. class EmptyEnv16x16(EmptyEnv):
  13. def __init__(self):
  14. super().__init__(size=16)
  15. register(
  16. id='MiniGrid-Empty-6x6-v0',
  17. entry_point='gym_minigrid.envs:EmptyEnv6x6'
  18. )
  19. register(
  20. id='MiniGrid-Empty-8x8-v0',
  21. entry_point='gym_minigrid.envs:EmptyEnv'
  22. )
  23. register(
  24. id='MiniGrid-Empty-16x16-v0',
  25. entry_point='gym_minigrid.envs:EmptyEnv16x16'
  26. )
  27. class DoorKeyEnv(MiniGridEnv):
  28. """
  29. Environment with a door and key, sparse reward
  30. """
  31. def __init__(self, size=8):
  32. super().__init__(gridSize=size, maxSteps=4 * size)
  33. def _genGrid(self, width, height):
  34. grid = super()._genGrid(width, height)
  35. assert width == height
  36. gridSz = width
  37. # Create a vertical splitting wall
  38. splitIdx = self._randInt(2, gridSz-2)
  39. for i in range(0, gridSz):
  40. grid.set(splitIdx, i, Wall())
  41. # Place the agent at a random position and orientation
  42. self.startPos = (
  43. self._randInt(1, splitIdx),
  44. self._randInt(1, gridSz-1)
  45. )
  46. self.startDir = self._randInt(0, 4)
  47. # Place a door in the wall
  48. doorIdx = self._randInt(1, gridSz-2)
  49. grid.set(splitIdx, doorIdx, LockedDoor('yellow'))
  50. # Place a yellow key on the left side
  51. while True:
  52. pos = (
  53. self._randInt(1, splitIdx),
  54. self._randInt(1, gridSz-1)
  55. )
  56. if pos == self.startPos:
  57. continue
  58. if grid.get(*pos) != None:
  59. continue
  60. grid.set(*pos, Key('yellow'))
  61. break
  62. return grid
  63. class DoorKeyEnv5x5(DoorKeyEnv):
  64. def __init__(self):
  65. super().__init__(size=5)
  66. class DoorKeyEnv6x6(DoorKeyEnv):
  67. def __init__(self):
  68. super().__init__(size=6)
  69. class DoorKeyEnv16x16(DoorKeyEnv):
  70. def __init__(self):
  71. super().__init__(size=16)
  72. register(
  73. id='MiniGrid-DoorKey-5x5-v0',
  74. entry_point='gym_minigrid.envs:DoorKeyEnv5x5'
  75. )
  76. register(
  77. id='MiniGrid-DoorKey-6x6-v0',
  78. entry_point='gym_minigrid.envs:DoorKeyEnv6x6'
  79. )
  80. register(
  81. id='MiniGrid-DoorKey-8x8-v0',
  82. entry_point='gym_minigrid.envs:DoorKeyEnv'
  83. )
  84. register(
  85. id='MiniGrid-DoorKey-16x16-v0',
  86. entry_point='gym_minigrid.envs:DoorKeyEnv16x16'
  87. )