lockedroom.py 3.3 KB

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