lockedroom.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. from gym_minigrid.minigrid import COLOR_NAMES, Door, Goal, Grid, Key, MiniGridEnv, Wall
  2. class LockedRoom:
  3. def __init__(self, top, size, doorPos):
  4. self.top = top
  5. self.size = size
  6. self.doorPos = doorPos
  7. self.color = None
  8. self.locked = False
  9. def rand_pos(self, env):
  10. topX, topY = self.top
  11. sizeX, sizeY = self.size
  12. return env._rand_pos(topX + 1, topX + sizeX - 1, topY + 1, topY + sizeY - 1)
  13. class LockedRoomEnv(MiniGridEnv):
  14. """
  15. Environment in which the agent is instructed to go to a given object
  16. named using an English text string
  17. """
  18. def __init__(self, size=19, **kwargs):
  19. super().__init__(grid_size=size, max_steps=10 * size, **kwargs)
  20. def _gen_grid(self, width, height):
  21. # Create the grid
  22. self.grid = Grid(width, height)
  23. # Generate the surrounding walls
  24. for i in range(0, width):
  25. self.grid.set(i, 0, Wall())
  26. self.grid.set(i, height - 1, Wall())
  27. for j in range(0, height):
  28. self.grid.set(0, j, Wall())
  29. self.grid.set(width - 1, j, Wall())
  30. # Hallway walls
  31. lWallIdx = width // 2 - 2
  32. rWallIdx = width // 2 + 2
  33. for j in range(0, height):
  34. self.grid.set(lWallIdx, j, Wall())
  35. self.grid.set(rWallIdx, j, Wall())
  36. self.rooms = []
  37. # Room splitting walls
  38. for n in range(0, 3):
  39. j = n * (height // 3)
  40. for i in range(0, lWallIdx):
  41. self.grid.set(i, j, Wall())
  42. for i in range(rWallIdx, width):
  43. self.grid.set(i, j, Wall())
  44. roomW = lWallIdx + 1
  45. roomH = height // 3 + 1
  46. self.rooms.append(LockedRoom((0, j), (roomW, roomH), (lWallIdx, j + 3)))
  47. self.rooms.append(
  48. LockedRoom((rWallIdx, j), (roomW, roomH), (rWallIdx, j + 3))
  49. )
  50. # Choose one random room to be locked
  51. lockedRoom = self._rand_elem(self.rooms)
  52. lockedRoom.locked = True
  53. goalPos = lockedRoom.rand_pos(self)
  54. self.grid.set(*goalPos, Goal())
  55. # Assign the door colors
  56. colors = set(COLOR_NAMES)
  57. for room in self.rooms:
  58. color = self._rand_elem(sorted(colors))
  59. colors.remove(color)
  60. room.color = color
  61. if room.locked:
  62. self.grid.set(*room.doorPos, Door(color, is_locked=True))
  63. else:
  64. self.grid.set(*room.doorPos, Door(color))
  65. # Select a random room to contain the key
  66. while True:
  67. keyRoom = self._rand_elem(self.rooms)
  68. if keyRoom != lockedRoom:
  69. break
  70. keyPos = keyRoom.rand_pos(self)
  71. self.grid.set(*keyPos, Key(lockedRoom.color))
  72. # Randomize the player start position and orientation
  73. self.agent_pos = self.place_agent(
  74. top=(lWallIdx, 0), size=(rWallIdx - lWallIdx, height)
  75. )
  76. # Generate the mission string
  77. self.mission = (
  78. "get the %s key from the %s room, "
  79. "unlock the %s door and "
  80. "go to the goal"
  81. ) % (lockedRoom.color, keyRoom.color, lockedRoom.color)
  82. def step(self, action):
  83. obs, reward, done, info = MiniGridEnv.step(self, action)
  84. return obs, reward, done, info