multiroom.py 6.9 KB

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