lockedroom.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. ):
  30. size = 19
  31. super().__init__(grid_size=size, max_steps=10*size)
  32. self.observation_space = spaces.Dict({
  33. 'image': self.observation_space
  34. })
  35. def _gen_grid(self, width, height):
  36. # Create the grid
  37. self.grid = Grid(width, height)
  38. # Generate the surrounding walls
  39. for i in range(0, width):
  40. self.grid.set(i, 0, Wall())
  41. self.grid.set(i, height-1, Wall())
  42. for j in range(0, height):
  43. self.grid.set(0, j, Wall())
  44. self.grid.set(width-1, j, Wall())
  45. # Hallway walls
  46. lWallIdx = width // 2 - 2
  47. rWallIdx = width // 2 + 2
  48. for j in range(0, height):
  49. self.grid.set(lWallIdx, j, Wall())
  50. self.grid.set(rWallIdx, j, Wall())
  51. self.rooms = []
  52. # Room splitting walls
  53. for n in range(0, 3):
  54. j = n * (height // 3)
  55. for i in range(0, lWallIdx):
  56. self.grid.set(i, j, Wall())
  57. for i in range(rWallIdx, width):
  58. self.grid.set(i, j, Wall())
  59. roomW = lWallIdx + 1
  60. roomH = height // 3 + 1
  61. self.rooms.append(Room(
  62. (0, j),
  63. (roomW, roomH),
  64. (lWallIdx, j + 3)
  65. ))
  66. self.rooms.append(Room(
  67. (rWallIdx, j),
  68. (roomW, roomH),
  69. (rWallIdx, j + 3)
  70. ))
  71. # Choose one random room to be locked
  72. lockedRoom = self._rand_elem(self.rooms)
  73. lockedRoom.locked = True
  74. goalPos = lockedRoom.rand_pos(self)
  75. self.grid.set(*goalPos, Goal())
  76. # Assign the door colors
  77. colors = set(COLOR_NAMES)
  78. for room in self.rooms:
  79. color = self._rand_elem(sorted(colors))
  80. colors.remove(color)
  81. room.color = color
  82. if room.locked:
  83. self.grid.set(*room.doorPos, LockedDoor(color))
  84. else:
  85. self.grid.set(*room.doorPos, Door(color))
  86. # Select a random room to contain the key
  87. while True:
  88. keyRoom = self._rand_elem(self.rooms)
  89. if keyRoom != lockedRoom:
  90. break
  91. keyPos = keyRoom.rand_pos(self)
  92. self.grid.set(*keyPos, Key(lockedRoom.color))
  93. # Randomize the player start position and orientation
  94. self.start_pos = self.place_agent(
  95. top=(lWallIdx, 0),
  96. size=(rWallIdx-lWallIdx, height)
  97. )
  98. # Generate the mission string
  99. self.mission = (
  100. 'get the %s key from the %s room, '
  101. 'then use it to unlock the %s door '
  102. 'so you can get to the goal'
  103. ) % (lockedRoom.color, keyRoom.color, lockedRoom.color)
  104. def step(self, action):
  105. obs, reward, done, info = MiniGridEnv.step(self, action)
  106. return obs, reward, done, info
  107. register(
  108. id='MiniGrid-LockedRoom-v0',
  109. entry_point='gym_minigrid.envs:LockedRoom'
  110. )