multiroom.py 7.3 KB

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