multiroom.py 7.6 KB

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