multiroom.py 7.3 KB

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