fetch.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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__(
  15. grid_size=size,
  16. max_steps=5*size**2,
  17. # Set this to True for maximum speed
  18. see_through_walls=True
  19. )
  20. def _gen_grid(self, width, height):
  21. self.grid = Grid(width, height)
  22. # Generate the surrounding walls
  23. self.grid.horz_wall(0, 0)
  24. self.grid.horz_wall(0, height-1)
  25. self.grid.vert_wall(0, 0)
  26. self.grid.vert_wall(width-1, 0)
  27. types = ['key', 'ball']
  28. objs = []
  29. # For each object to be generated
  30. while len(objs) < self.numObjs:
  31. objType = self._rand_elem(types)
  32. objColor = self._rand_elem(COLOR_NAMES)
  33. if objType == 'key':
  34. obj = Key(objColor)
  35. elif objType == 'ball':
  36. obj = Ball(objColor)
  37. self.place_obj(obj)
  38. objs.append(obj)
  39. # Randomize the player start position and orientation
  40. self.place_agent()
  41. # Choose a random object to be picked up
  42. target = objs[self._rand_int(0, len(objs))]
  43. self.targetType = target.type
  44. self.targetColor = target.color
  45. descStr = '%s %s' % (self.targetColor, self.targetType)
  46. # Generate the mission string
  47. idx = self._rand_int(0, 5)
  48. if idx == 0:
  49. self.mission = 'get a %s' % descStr
  50. elif idx == 1:
  51. self.mission = 'go get a %s' % descStr
  52. elif idx == 2:
  53. self.mission = 'fetch a %s' % descStr
  54. elif idx == 3:
  55. self.mission = 'go fetch a %s' % descStr
  56. elif idx == 4:
  57. self.mission = 'you must fetch a %s' % descStr
  58. assert hasattr(self, 'mission')
  59. def step(self, action):
  60. obs, reward, done, info = MiniGridEnv.step(self, action)
  61. if self.carrying:
  62. if self.carrying.color == self.targetColor and \
  63. self.carrying.type == self.targetType:
  64. reward = self._reward()
  65. done = True
  66. else:
  67. reward = 0
  68. done = True
  69. return obs, reward, done, info
  70. class FetchEnv5x5N2(FetchEnv):
  71. def __init__(self):
  72. super().__init__(size=5, numObjs=2)
  73. class FetchEnv6x6N2(FetchEnv):
  74. def __init__(self):
  75. super().__init__(size=6, numObjs=2)
  76. register(
  77. id='MiniGrid-Fetch-5x5-N2-v0',
  78. entry_point='gym_minigrid.envs:FetchEnv5x5N2'
  79. )
  80. register(
  81. id='MiniGrid-Fetch-6x6-N2-v0',
  82. entry_point='gym_minigrid.envs:FetchEnv6x6N2'
  83. )
  84. register(
  85. id='MiniGrid-Fetch-8x8-N3-v0',
  86. entry_point='gym_minigrid.envs:FetchEnv'
  87. )