multiroom.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. grid_size=25,
  32. max_steps=self.maxNumRooms * 20
  33. )
  34. def _gen_grid(self, width, height):
  35. roomList = []
  36. # Choose a random number of rooms to generate
  37. numRooms = self._rand_int(self.minNumRooms, self.maxNumRooms+1)
  38. while len(roomList) < numRooms:
  39. curRoomList = []
  40. entryDoorPos = (
  41. self._rand_int(0, width - 2),
  42. self._rand_int(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. # Create the grid
  59. self.grid = Grid(width, height)
  60. wall = Wall()
  61. prevDoorColor = None
  62. # For each room
  63. for idx, room in enumerate(roomList):
  64. topX, topY = room.top
  65. sizeX, sizeY = room.size
  66. # Draw the top and bottom walls
  67. for i in range(0, sizeX):
  68. self.grid.set(topX + i, topY, wall)
  69. self.grid.set(topX + i, topY + sizeY - 1, wall)
  70. # Draw the left and right walls
  71. for j in range(0, sizeY):
  72. self.grid.set(topX, topY + j, wall)
  73. self.grid.set(topX + sizeX - 1, topY + j, wall)
  74. # If this isn't the first room, place the entry door
  75. if idx > 0:
  76. # Pick a door color different from the previous one
  77. doorColors = set(COLOR_NAMES)
  78. if prevDoorColor:
  79. doorColors.remove(prevDoorColor)
  80. # Note: the use of sorting here guarantees determinism,
  81. # This is needed because Python's set is not deterministic
  82. doorColor = self._rand_elem(sorted(doorColors))
  83. entryDoor = Door(doorColor)
  84. self.grid.set(*room.entryDoorPos, entryDoor)
  85. prevDoorColor = doorColor
  86. prevRoom = roomList[idx-1]
  87. prevRoom.exitDoorPos = room.entryDoorPos
  88. # Randomize the starting agent position and direction
  89. self.place_agent(roomList[0].top, roomList[0].size)
  90. # Place the final goal in the last room
  91. self.goal_pos = self.place_obj(Goal(), roomList[-1].top, roomList[-1].size)
  92. self.mission = 'traverse the rooms to get to the goal'
  93. def _placeRoom(
  94. self,
  95. numLeft,
  96. roomList,
  97. minSz,
  98. maxSz,
  99. entryDoorWall,
  100. entryDoorPos
  101. ):
  102. # Choose the room size randomly
  103. sizeX = self._rand_int(minSz, maxSz+1)
  104. sizeY = self._rand_int(minSz, maxSz+1)
  105. # The first room will be at the door position
  106. if len(roomList) == 0:
  107. topX, topY = entryDoorPos
  108. # Entry on the right
  109. elif entryDoorWall == 0:
  110. topX = entryDoorPos[0] - sizeX + 1
  111. y = entryDoorPos[1]
  112. topY = self._rand_int(y - sizeY + 2, y)
  113. # Entry wall on the south
  114. elif entryDoorWall == 1:
  115. x = entryDoorPos[0]
  116. topX = self._rand_int(x - sizeX + 2, x)
  117. topY = entryDoorPos[1] - sizeY + 1
  118. # Entry wall on the left
  119. elif entryDoorWall == 2:
  120. topX = entryDoorPos[0]
  121. y = entryDoorPos[1]
  122. topY = self._rand_int(y - sizeY + 2, y)
  123. # Entry wall on the top
  124. elif entryDoorWall == 3:
  125. x = entryDoorPos[0]
  126. topX = self._rand_int(x - sizeX + 2, x)
  127. topY = entryDoorPos[1]
  128. else:
  129. assert False, entryDoorWall
  130. # If the room is out of the grid, can't place a room here
  131. if topX < 0 or topY < 0:
  132. return False
  133. if topX + sizeX > self.width or topY + sizeY >= self.height:
  134. return False
  135. # If the room intersects with previous rooms, can't place it here
  136. for room in roomList[:-1]:
  137. nonOverlap = \
  138. topX + sizeX < room.top[0] or \
  139. room.top[0] + room.size[0] <= topX or \
  140. topY + sizeY < room.top[1] or \
  141. room.top[1] + room.size[1] <= topY
  142. if not nonOverlap:
  143. return False
  144. # Add this room to the list
  145. roomList.append(Room(
  146. (topX, topY),
  147. (sizeX, sizeY),
  148. entryDoorPos,
  149. None
  150. ))
  151. # If this was the last room, stop
  152. if numLeft == 1:
  153. return True
  154. # Try placing the next room
  155. for i in range(0, 8):
  156. # Pick which wall to place the out door on
  157. wallSet = set((0, 1, 2, 3))
  158. wallSet.remove(entryDoorWall)
  159. exitDoorWall = self._rand_elem(sorted(wallSet))
  160. nextEntryWall = (exitDoorWall + 2) % 4
  161. # Pick the exit door position
  162. # Exit on right wall
  163. if exitDoorWall == 0:
  164. exitDoorPos = (
  165. topX + sizeX - 1,
  166. topY + self._rand_int(1, sizeY - 1)
  167. )
  168. # Exit on south wall
  169. elif exitDoorWall == 1:
  170. exitDoorPos = (
  171. topX + self._rand_int(1, sizeX - 1),
  172. topY + sizeY - 1
  173. )
  174. # Exit on left wall
  175. elif exitDoorWall == 2:
  176. exitDoorPos = (
  177. topX,
  178. topY + self._rand_int(1, sizeY - 1)
  179. )
  180. # Exit on north wall
  181. elif exitDoorWall == 3:
  182. exitDoorPos = (
  183. topX + self._rand_int(1, sizeX - 1),
  184. topY
  185. )
  186. else:
  187. assert False
  188. # Recursively create the other rooms
  189. success = self._placeRoom(
  190. numLeft - 1,
  191. roomList=roomList,
  192. minSz=minSz,
  193. maxSz=maxSz,
  194. entryDoorWall=nextEntryWall,
  195. entryDoorPos=exitDoorPos
  196. )
  197. if success:
  198. break
  199. return True
  200. class MultiRoomEnvN2S4(MultiRoomEnv):
  201. def __init__(self):
  202. super().__init__(
  203. minNumRooms=2,
  204. maxNumRooms=2,
  205. maxRoomSize=4
  206. )
  207. class MultiRoomEnvN4S5(MultiRoomEnv):
  208. def __init__(self):
  209. super().__init__(
  210. minNumRooms=4,
  211. maxNumRooms=4,
  212. maxRoomSize=5
  213. )
  214. class MultiRoomEnvN6(MultiRoomEnv):
  215. def __init__(self):
  216. super().__init__(
  217. minNumRooms=6,
  218. maxNumRooms=6
  219. )
  220. register(
  221. id='MiniGrid-MultiRoom-N2-S4-v0',
  222. entry_point='gym_minigrid.envs:MultiRoomEnvN2S4'
  223. )
  224. register(
  225. id='MiniGrid-MultiRoom-N4-S5-v0',
  226. entry_point='gym_minigrid.envs:MultiRoomEnvN4S5'
  227. )
  228. register(
  229. id='MiniGrid-MultiRoom-N6-v0',
  230. entry_point='gym_minigrid.envs:MultiRoomEnvN6'
  231. )