fetch.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. from gym_minigrid.minigrid import *
  2. from gym_minigrid.register import register
  3. class FetchEnv(MiniGridEnv):
  4. """
  5. Environment in which the agent has to fetch a random object
  6. named using English text strings
  7. """
  8. def __init__(
  9. self,
  10. size=8,
  11. numObjs=3
  12. ):
  13. self.numObjs = numObjs
  14. super().__init__(gridSize=size, maxSteps=5*size)
  15. self.observation_space = spaces.Dict({
  16. 'image': self.observation_space
  17. })
  18. self.reward_range = (-1000, 1000)
  19. def _genGrid(self, width, height):
  20. assert width == height
  21. gridSz = width
  22. # Create a grid surrounded by walls
  23. grid = Grid(width, height)
  24. for i in range(0, width):
  25. grid.set(i, 0, Wall())
  26. grid.set(i, height-1, Wall())
  27. for j in range(0, height):
  28. grid.set(0, j, Wall())
  29. grid.set(width-1, j, Wall())
  30. types = ['key', 'ball']
  31. colors = list(COLORS.keys())
  32. objs = []
  33. # For each object to be generated
  34. while len(objs) < self.numObjs:
  35. objType = self._randElem(types)
  36. objColor = self._randElem(colors)
  37. if objType == 'key':
  38. obj = Key(objColor)
  39. elif objType == 'ball':
  40. obj = Ball(objColor)
  41. while True:
  42. pos = (
  43. self._randInt(1, gridSz - 1),
  44. self._randInt(1, gridSz - 1)
  45. )
  46. if pos != self.startPos:
  47. grid.set(*pos, obj)
  48. break
  49. objs.append(obj)
  50. # Choose a random object to be picked up
  51. target = objs[self._randInt(0, len(objs))]
  52. self.targetType = target.type
  53. self.targetColor = target.color
  54. descStr = '%s %s' % (self.targetColor, self.targetType)
  55. # Generate the mission string
  56. idx = self._randInt(0, 5)
  57. if idx == 0:
  58. self.mission = 'get a %s' % descStr
  59. elif idx == 1:
  60. self.mission = 'go get a %s' % descStr
  61. elif idx == 2:
  62. self.mission = 'fetch a %s' % descStr
  63. elif idx == 3:
  64. self.mission = 'go fetch a %s' % descStr
  65. elif idx == 4:
  66. self.mission = 'you must fetch a %s' % descStr
  67. assert hasattr(self, 'mission')
  68. return grid
  69. def _observation(self, obs):
  70. """
  71. Encode observations
  72. """
  73. obs = {
  74. 'image': obs,
  75. 'mission': self.mission
  76. }
  77. return obs
  78. def _reset(self):
  79. obs = MiniGridEnv._reset(self)
  80. return self._observation(obs)
  81. def _step(self, action):
  82. obs, reward, done, info = MiniGridEnv._step(self, action)
  83. if self.carrying:
  84. if self.carrying.color == self.targetColor and \
  85. self.carrying.type == self.targetType:
  86. reward = 1000 - self.stepCount
  87. done = True
  88. else:
  89. reward = -1000
  90. done = True
  91. obs = self._observation(obs)
  92. return obs, reward, done, info
  93. class FetchEnv5x5N2(FetchEnv):
  94. def __init__(self):
  95. super().__init__(size=5, numObjs=2)
  96. register(
  97. id='MiniGrid-Fetch-5x5-N2-v0',
  98. entry_point='gym_minigrid.envs:FetchEnv5x5N2'
  99. )
  100. register(
  101. id='MiniGrid-Fetch-8x8-N3-v0',
  102. entry_point='gym_minigrid.envs:FetchEnv'
  103. )