lockedroom.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. from gym import spaces
  2. from gym_minigrid.minigrid import *
  3. from gym_minigrid.register import register
  4. class Room:
  5. def __init__(self,
  6. top,
  7. size,
  8. doorPos
  9. ):
  10. self.top = top
  11. self.size = size
  12. self.doorPos = doorPos
  13. self.color = None
  14. self.locked = False
  15. def randPos(self, env):
  16. topX, topY = self.top
  17. sizeX, sizeY = self.size
  18. return env._randPos(
  19. topX + 1, topX + sizeX - 1,
  20. topY + 1, topY + sizeY - 1
  21. )
  22. class LockedRoom(MiniGridEnv):
  23. """
  24. Environment in which the agent is instructed to go to a given object
  25. named using an English text string
  26. """
  27. def __init__(
  28. self
  29. ):
  30. size = 19
  31. super().__init__(gridSize=size, maxSteps=10*size)
  32. self.observation_space = spaces.Dict({
  33. 'image': self.observation_space
  34. })
  35. def _genGrid(self, width, height):
  36. # Create the grid
  37. self.grid = Grid(width, height)
  38. # Generate the surrounding walls
  39. for i in range(0, width):
  40. self.grid.set(i, 0, Wall())
  41. self.grid.set(i, height-1, Wall())
  42. for j in range(0, height):
  43. self.grid.set(0, j, Wall())
  44. self.grid.set(width-1, j, Wall())
  45. # Hallway walls
  46. lWallIdx = width // 2 - 2
  47. rWallIdx = width // 2 + 2
  48. for j in range(0, height):
  49. self.grid.set(lWallIdx, j, Wall())
  50. self.grid.set(rWallIdx, j, Wall())
  51. self.rooms = []
  52. # Room splitting walls
  53. for n in range(0, 3):
  54. j = n * (height // 3)
  55. for i in range(0, lWallIdx):
  56. self.grid.set(i, j, Wall())
  57. for i in range(rWallIdx, width):
  58. self.grid.set(i, j, Wall())
  59. roomW = lWallIdx + 1
  60. roomH = height // 3 + 1
  61. self.rooms.append(Room(
  62. (0, j),
  63. (roomW, roomH),
  64. (lWallIdx, j + 3)
  65. ))
  66. self.rooms.append(Room(
  67. (rWallIdx, j),
  68. (roomW, roomH),
  69. (rWallIdx, j + 3)
  70. ))
  71. # Choose one random room to be locked
  72. lockedRoom = self._randElem(self.rooms)
  73. lockedRoom.locked = True
  74. goalPos = lockedRoom.randPos(self)
  75. self.grid.set(*goalPos, Goal())
  76. # Assign the door colors
  77. colors = set(COLOR_NAMES)
  78. for room in self.rooms:
  79. color = self._randElem(sorted(colors))
  80. colors.remove(color)
  81. room.color = color
  82. if room.locked:
  83. self.grid.set(*room.doorPos, LockedDoor(color))
  84. else:
  85. self.grid.set(*room.doorPos, Door(color))
  86. # Select a random room to contain the key
  87. while True:
  88. keyRoom = self._randElem(self.rooms)
  89. if keyRoom != lockedRoom:
  90. break
  91. keyPos = keyRoom.randPos(self)
  92. self.grid.set(*keyPos, Key(lockedRoom.color))
  93. # Randomize the player start position and orientation
  94. self.startPos = self._randPos(
  95. lWallIdx + 1, rWallIdx,
  96. 1, height-1
  97. )
  98. self.startDir = self._randInt(0, 4)
  99. # Generate the mission string
  100. self.mission = (
  101. 'get the %s key from the %s room, '
  102. 'then use it to unlock the %s door '
  103. 'so you can get to the goal'
  104. ) % (lockedRoom.color, keyRoom.color, lockedRoom.color)
  105. def step(self, action):
  106. obs, reward, done, info = MiniGridEnv.step(self, action)
  107. return obs, reward, done, info
  108. register(
  109. id='MiniGrid-LockedRoom-v0',
  110. entry_point='gym_minigrid.envs:LockedRoom'
  111. )