gotodoor.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. from gym_minigrid.minigrid import *
  2. from gym_minigrid.register import register
  3. class GoToDoorEnv(MiniGridEnv):
  4. """
  5. Environment in which the agent is instructed to go to a given object
  6. named using an English text string
  7. """
  8. def __init__(
  9. self,
  10. size=5
  11. ):
  12. assert size >= 5
  13. super().__init__(gridSize=size, maxSteps=10*size)
  14. self.reward_range = (0, 1)
  15. def _genGrid(self, width, height):
  16. # Create the grid
  17. self.grid = Grid(width, height)
  18. # Randomly vary the room width and height
  19. width = self._randInt(5, width+1)
  20. height = self._randInt(5, height+1)
  21. # Generate the surrounding walls
  22. self.grid.wallRect(0, 0, width, height)
  23. # Generate the 4 doors at random positions
  24. doorPos = []
  25. doorPos.append((self._randInt(2, width-2), 0))
  26. doorPos.append((self._randInt(2, width-2), height-1))
  27. doorPos.append((0, self._randInt(2, height-2)))
  28. doorPos.append((width-1, self._randInt(2, height-2)))
  29. # Generate the door colors
  30. doorColors = []
  31. while len(doorColors) < len(doorPos):
  32. color = self._randElem(COLOR_NAMES)
  33. if color in doorColors:
  34. continue
  35. doorColors.append(color)
  36. # Place the doors in the grid
  37. for idx, pos in enumerate(doorPos):
  38. color = doorColors[idx]
  39. self.grid.set(*pos, Door(color))
  40. # Select a random target door
  41. doorIdx = self._randInt(0, len(doorPos))
  42. self.targetPos = doorPos[doorIdx]
  43. self.targetColor = doorColors[doorIdx]
  44. # Generate the mission string
  45. self.mission = 'go to the %s door' % self.targetColor
  46. def step(self, action):
  47. obs, reward, done, info = MiniGridEnv.step(self, action)
  48. ax, ay = self.agentPos
  49. tx, ty = self.targetPos
  50. # Don't let the agent open any of the doors
  51. if action == self.actions.toggle:
  52. done = True
  53. # Reward waiting in front of the target door
  54. if action == self.actions.wait:
  55. if (ax == tx and abs(ay - ty) == 1) or (ay == ty and abs(ax - tx) == 1):
  56. reward = 1
  57. done = True
  58. return obs, reward, done, info
  59. class GoToDoor8x8Env(GoToDoorEnv):
  60. def __init__(self):
  61. super().__init__(size=8)
  62. class GoToDoor6x6Env(GoToDoorEnv):
  63. def __init__(self):
  64. super().__init__(size=6)
  65. register(
  66. id='MiniGrid-GoToDoor-5x5-v0',
  67. entry_point='gym_minigrid.envs:GoToDoorEnv'
  68. )
  69. register(
  70. id='MiniGrid-GoToDoor-6x6-v0',
  71. entry_point='gym_minigrid.envs:GoToDoor6x6Env'
  72. )
  73. register(
  74. id='MiniGrid-GoToDoor-8x8-v0',
  75. entry_point='gym_minigrid.envs:GoToDoor8x8Env'
  76. )