fetch.py 3.6 KB

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