multiroom.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. from gym_minigrid.minigrid import COLOR_NAMES, Door, Goal, Grid, MiniGridEnv, Wall
  2. class MultiRoom:
  3. def __init__(self, top, size, entryDoorPos, exitDoorPos):
  4. self.top = top
  5. self.size = size
  6. self.entryDoorPos = entryDoorPos
  7. self.exitDoorPos = exitDoorPos
  8. class MultiRoomEnv(MiniGridEnv):
  9. """
  10. Environment with multiple rooms (subgoals)
  11. """
  12. def __init__(self, minNumRooms, maxNumRooms, maxRoomSize=10, **kwargs):
  13. assert minNumRooms > 0
  14. assert maxNumRooms >= minNumRooms
  15. assert maxRoomSize >= 4
  16. self.minNumRooms = minNumRooms
  17. self.maxNumRooms = maxNumRooms
  18. self.maxRoomSize = maxRoomSize
  19. self.rooms = []
  20. super().__init__(grid_size=25, max_steps=self.maxNumRooms * 20, **kwargs)
  21. def _gen_grid(self, width, height):
  22. roomList = []
  23. # Choose a random number of rooms to generate
  24. numRooms = self._rand_int(self.minNumRooms, self.maxNumRooms + 1)
  25. while len(roomList) < numRooms:
  26. curRoomList = []
  27. entryDoorPos = (self._rand_int(0, width - 2), self._rand_int(0, width - 2))
  28. # Recursively place the rooms
  29. self._placeRoom(
  30. numRooms,
  31. roomList=curRoomList,
  32. minSz=4,
  33. maxSz=self.maxRoomSize,
  34. entryDoorWall=2,
  35. entryDoorPos=entryDoorPos,
  36. )
  37. if len(curRoomList) > len(roomList):
  38. roomList = curRoomList
  39. # Store the list of rooms in this environment
  40. assert len(roomList) > 0
  41. self.rooms = roomList
  42. # Create the grid
  43. self.grid = Grid(width, height)
  44. wall = Wall()
  45. prevDoorColor = None
  46. # For each room
  47. for idx, room in enumerate(roomList):
  48. topX, topY = room.top
  49. sizeX, sizeY = room.size
  50. # Draw the top and bottom walls
  51. for i in range(0, sizeX):
  52. self.grid.set(topX + i, topY, wall)
  53. self.grid.set(topX + i, topY + sizeY - 1, wall)
  54. # Draw the left and right walls
  55. for j in range(0, sizeY):
  56. self.grid.set(topX, topY + j, wall)
  57. self.grid.set(topX + sizeX - 1, topY + j, wall)
  58. # If this isn't the first room, place the entry door
  59. if idx > 0:
  60. # Pick a door color different from the previous one
  61. doorColors = set(COLOR_NAMES)
  62. if prevDoorColor:
  63. doorColors.remove(prevDoorColor)
  64. # Note: the use of sorting here guarantees determinism,
  65. # This is needed because Python's set is not deterministic
  66. doorColor = self._rand_elem(sorted(doorColors))
  67. entryDoor = Door(doorColor)
  68. self.grid.set(*room.entryDoorPos, entryDoor)
  69. prevDoorColor = doorColor
  70. prevRoom = roomList[idx - 1]
  71. prevRoom.exitDoorPos = room.entryDoorPos
  72. # Randomize the starting agent position and direction
  73. self.place_agent(roomList[0].top, roomList[0].size)
  74. # Place the final goal in the last room
  75. self.goal_pos = self.place_obj(Goal(), roomList[-1].top, roomList[-1].size)
  76. self.mission = "traverse the rooms to get to the goal"
  77. def _placeRoom(self, numLeft, roomList, minSz, maxSz, entryDoorWall, entryDoorPos):
  78. # Choose the room size randomly
  79. sizeX = self._rand_int(minSz, maxSz + 1)
  80. sizeY = self._rand_int(minSz, maxSz + 1)
  81. # The first room will be at the door position
  82. if len(roomList) == 0:
  83. topX, topY = entryDoorPos
  84. # Entry on the right
  85. elif entryDoorWall == 0:
  86. topX = entryDoorPos[0] - sizeX + 1
  87. y = entryDoorPos[1]
  88. topY = self._rand_int(y - sizeY + 2, y)
  89. # Entry wall on the south
  90. elif entryDoorWall == 1:
  91. x = entryDoorPos[0]
  92. topX = self._rand_int(x - sizeX + 2, x)
  93. topY = entryDoorPos[1] - sizeY + 1
  94. # Entry wall on the left
  95. elif entryDoorWall == 2:
  96. topX = entryDoorPos[0]
  97. y = entryDoorPos[1]
  98. topY = self._rand_int(y - sizeY + 2, y)
  99. # Entry wall on the top
  100. elif entryDoorWall == 3:
  101. x = entryDoorPos[0]
  102. topX = self._rand_int(x - sizeX + 2, x)
  103. topY = entryDoorPos[1]
  104. else:
  105. assert False, entryDoorWall
  106. # If the room is out of the grid, can't place a room here
  107. if topX < 0 or topY < 0:
  108. return False
  109. if topX + sizeX > self.width or topY + sizeY >= self.height:
  110. return False
  111. # If the room intersects with previous rooms, can't place it here
  112. for room in roomList[:-1]:
  113. nonOverlap = (
  114. topX + sizeX < room.top[0]
  115. or room.top[0] + room.size[0] <= topX
  116. or topY + sizeY < room.top[1]
  117. or room.top[1] + room.size[1] <= topY
  118. )
  119. if not nonOverlap:
  120. return False
  121. # Add this room to the list
  122. roomList.append(MultiRoom((topX, topY), (sizeX, sizeY), entryDoorPos, None))
  123. # If this was the last room, stop
  124. if numLeft == 1:
  125. return True
  126. # Try placing the next room
  127. for i in range(0, 8):
  128. # Pick which wall to place the out door on
  129. wallSet = {0, 1, 2, 3}
  130. wallSet.remove(entryDoorWall)
  131. exitDoorWall = self._rand_elem(sorted(wallSet))
  132. nextEntryWall = (exitDoorWall + 2) % 4
  133. # Pick the exit door position
  134. # Exit on right wall
  135. if exitDoorWall == 0:
  136. exitDoorPos = (topX + sizeX - 1, topY + self._rand_int(1, sizeY - 1))
  137. # Exit on south wall
  138. elif exitDoorWall == 1:
  139. exitDoorPos = (topX + self._rand_int(1, sizeX - 1), topY + sizeY - 1)
  140. # Exit on left wall
  141. elif exitDoorWall == 2:
  142. exitDoorPos = (topX, topY + self._rand_int(1, sizeY - 1))
  143. # Exit on north wall
  144. elif exitDoorWall == 3:
  145. exitDoorPos = (topX + self._rand_int(1, sizeX - 1), topY)
  146. else:
  147. assert False
  148. # Recursively create the other rooms
  149. success = self._placeRoom(
  150. numLeft - 1,
  151. roomList=roomList,
  152. minSz=minSz,
  153. maxSz=maxSz,
  154. entryDoorWall=nextEntryWall,
  155. entryDoorPos=exitDoorPos,
  156. )
  157. if success:
  158. break
  159. return True