lockedroom.py 3.5 KB

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