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