gotoobject.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. from gym_minigrid.minigrid import *
  2. from gym_minigrid.register import register
  3. class GoToObjectEnv(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=6,
  11. numObjs=2
  12. ):
  13. self.numObjs = numObjs
  14. super().__init__(gridSize=size, maxSteps=5*size)
  15. self.reward_range = (-1000, 1000)
  16. def _genGrid(self, width, height):
  17. assert width == height
  18. gridSz = width
  19. # Create a grid surrounded by walls
  20. grid = Grid(width, height)
  21. for i in range(0, width):
  22. grid.set(i, 0, Wall())
  23. grid.set(i, height-1, Wall())
  24. for j in range(0, height):
  25. grid.set(0, j, Wall())
  26. grid.set(width-1, j, Wall())
  27. # Types and colors of objects we can generate
  28. types = ['key', 'ball', 'box']
  29. colors = list(COLORS.keys())
  30. objs = []
  31. objPos = []
  32. # Until we have generated all the objects
  33. while len(objs) < self.numObjs:
  34. objType = self._randElem(types)
  35. objColor = self._randElem(colors)
  36. # If this object already exists, try again
  37. if (objType, objColor) in objs:
  38. continue
  39. if objType == 'key':
  40. obj = Key(objColor)
  41. elif objType == 'ball':
  42. obj = Ball(objColor)
  43. elif objType == 'box':
  44. obj = Box(objColor)
  45. while True:
  46. pos = (
  47. self._randInt(1, gridSz - 1),
  48. self._randInt(1, gridSz - 1)
  49. )
  50. if pos != self.startPos:
  51. grid.set(*pos, obj)
  52. break
  53. objs.append((objType, objColor))
  54. objPos.append(pos)
  55. # Choose a random object to be picked up
  56. objIdx = self._randInt(0, len(objs))
  57. self.targetType, self.targetColor = objs[objIdx]
  58. self.targetPos = objPos[objIdx]
  59. descStr = '%s %s' % (self.targetColor, self.targetType)
  60. """
  61. # Generate the mission string
  62. idx = self._randInt(0, 5)
  63. if idx == 0:
  64. self.mission = 'get a %s' % descStr
  65. elif idx == 1:
  66. self.mission = 'go get a %s' % descStr
  67. elif idx == 2:
  68. self.mission = 'fetch a %s' % descStr
  69. elif idx == 3:
  70. self.mission = 'go fetch a %s' % descStr
  71. elif idx == 4:
  72. self.mission = 'you must fetch a %s' % descStr
  73. assert hasattr(self, 'mission')
  74. """
  75. self.mission = 'go to the %s' % descStr
  76. #print(self.mission)
  77. return grid
  78. def _observation(self, obs):
  79. """
  80. Encode observations
  81. """
  82. obs = {
  83. 'image': obs,
  84. 'mission': self.mission,
  85. 'advice' : ''
  86. }
  87. return obs
  88. def _reset(self):
  89. obs = MiniGridEnv._reset(self)
  90. return self._observation(obs)
  91. def _step(self, action):
  92. obs, reward, done, info = MiniGridEnv._step(self, action)
  93. ax, ay = self.agentPos
  94. tx, ty = self.targetPos
  95. # Reward being next to the object
  96. # Double reward waiting next to the object
  97. if abs(ax - tx) <= 1 and abs(ay - ty) <= 1:
  98. if action == self.actions.wait:
  99. reward = 2
  100. else:
  101. reward = 1
  102. obs = self._observation(obs)
  103. return obs, reward, done, info
  104. class GotoEnv8x8N2(GoToObjectEnv):
  105. def __init__(self):
  106. super().__init__(size=8, numObjs=2)
  107. register(
  108. id='MiniGrid-GoToObject-6x6-N2-v0',
  109. entry_point='gym_minigrid.envs:GoToObjectEnv'
  110. )
  111. register(
  112. id='MiniGrid-GoToObject-8x8-N2-v0',
  113. entry_point='gym_minigrid.envs:GotoEnv8x8N2'
  114. )