multiroom.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. self.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. self.grid.set(topX + i, topY, wall)
  77. self.grid.set(topX + i, topY + sizeY - 1, wall)
  78. # Draw the left and right walls
  79. for j in range(0, sizeY):
  80. self.grid.set(topX, topY + j, wall)
  81. self.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(COLOR_NAMES)
  86. if prevDoorColor:
  87. doorColors.remove(prevDoorColor)
  88. # Note: the use of sorting here guarantees determinism,
  89. # This is needed because Python's set is not deterministic
  90. doorColor = self._randElem(sorted(doorColors))
  91. entryDoor = Door(doorColor)
  92. self.grid.set(*room.entryDoorPos, entryDoor)
  93. prevDoorColor = doorColor
  94. prevRoom = roomList[idx-1]
  95. prevRoom.exitDoorPos = room.entryDoorPos
  96. # Place the final goal
  97. while True:
  98. self.goalPos = (
  99. self._randInt(topX + 1, topX + sizeX - 1),
  100. self._randInt(topY + 1, topY + sizeY - 1)
  101. )
  102. # Make sure the goal doesn't overlap with the agent
  103. if self.goalPos != self.startPos:
  104. self.grid.set(*self.goalPos, Goal())
  105. break
  106. self.mission = 'traverse the rooms to get to the goal'
  107. def _placeRoom(
  108. self,
  109. numLeft,
  110. roomList,
  111. minSz,
  112. maxSz,
  113. entryDoorWall,
  114. entryDoorPos
  115. ):
  116. # Choose the room size randomly
  117. sizeX = self._randInt(minSz, maxSz+1)
  118. sizeY = self._randInt(minSz, maxSz+1)
  119. # The first room will be at the door position
  120. if len(roomList) == 0:
  121. topX, topY = entryDoorPos
  122. # Entry on the right
  123. elif entryDoorWall == 0:
  124. topX = entryDoorPos[0] - sizeX + 1
  125. y = entryDoorPos[1]
  126. topY = self._randInt(y - sizeY + 2, y)
  127. # Entry wall on the south
  128. elif entryDoorWall == 1:
  129. x = entryDoorPos[0]
  130. topX = self._randInt(x - sizeX + 2, x)
  131. topY = entryDoorPos[1] - sizeY + 1
  132. # Entry wall on the left
  133. elif entryDoorWall == 2:
  134. topX = entryDoorPos[0]
  135. y = entryDoorPos[1]
  136. topY = self._randInt(y - sizeY + 2, y)
  137. # Entry wall on the top
  138. elif entryDoorWall == 3:
  139. x = entryDoorPos[0]
  140. topX = self._randInt(x - sizeX + 2, x)
  141. topY = entryDoorPos[1]
  142. else:
  143. assert False, entryDoorWall
  144. # If the room is out of the grid, can't place a room here
  145. if topX < 0 or topY < 0:
  146. return False
  147. if topX + sizeX > self.gridSize or topY + sizeY >= self.gridSize:
  148. return False
  149. # If the room intersects with previous rooms, can't place it here
  150. for room in roomList[:-1]:
  151. nonOverlap = \
  152. topX + sizeX < room.top[0] or \
  153. room.top[0] + room.size[0] <= topX or \
  154. topY + sizeY < room.top[1] or \
  155. room.top[1] + room.size[1] <= topY
  156. if not nonOverlap:
  157. return False
  158. # Add this room to the list
  159. roomList.append(Room(
  160. (topX, topY),
  161. (sizeX, sizeY),
  162. entryDoorPos,
  163. None
  164. ))
  165. # If this was the last room, stop
  166. if numLeft == 1:
  167. return True
  168. # Try placing the next room
  169. for i in range(0, 8):
  170. # Pick which wall to place the out door on
  171. wallSet = set((0, 1, 2, 3))
  172. wallSet.remove(entryDoorWall)
  173. exitDoorWall = self._randElem(sorted(wallSet))
  174. nextEntryWall = (exitDoorWall + 2) % 4
  175. # Pick the exit door position
  176. # Exit on right wall
  177. if exitDoorWall == 0:
  178. exitDoorPos = (
  179. topX + sizeX - 1,
  180. topY + self._randInt(1, sizeY - 1)
  181. )
  182. # Exit on south wall
  183. elif exitDoorWall == 1:
  184. exitDoorPos = (
  185. topX + self._randInt(1, sizeX - 1),
  186. topY + sizeY - 1
  187. )
  188. # Exit on left wall
  189. elif exitDoorWall == 2:
  190. exitDoorPos = (
  191. topX,
  192. topY + self._randInt(1, sizeY - 1)
  193. )
  194. # Exit on north wall
  195. elif exitDoorWall == 3:
  196. exitDoorPos = (
  197. topX + self._randInt(1, sizeX - 1),
  198. topY
  199. )
  200. else:
  201. assert False
  202. # Recursively create the other rooms
  203. success = self._placeRoom(
  204. numLeft - 1,
  205. roomList=roomList,
  206. minSz=minSz,
  207. maxSz=maxSz,
  208. entryDoorWall=nextEntryWall,
  209. entryDoorPos=exitDoorPos
  210. )
  211. if success:
  212. break
  213. return True
  214. class MultiRoomEnvN2S4(MultiRoomEnv):
  215. def __init__(self):
  216. super().__init__(
  217. minNumRooms=2,
  218. maxNumRooms=2,
  219. maxRoomSize=4
  220. )
  221. class MultiRoomEnvN6(MultiRoomEnv):
  222. def __init__(self):
  223. super().__init__(
  224. minNumRooms=6,
  225. maxNumRooms=6
  226. )
  227. register(
  228. id='MiniGrid-MultiRoom-N2-S4-v0',
  229. entry_point='gym_minigrid.envs:MultiRoomEnvN2S4',
  230. reward_threshold=1000.0
  231. )
  232. register(
  233. id='MiniGrid-MultiRoom-N6-v0',
  234. entry_point='gym_minigrid.envs:MultiRoomEnvN6',
  235. reward_threshold=1000.0
  236. )