multiroom.py 6.9 KB

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