gotoobject.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. **kwargs
  13. ):
  14. self.numObjs = numObjs
  15. super().__init__(
  16. grid_size=size,
  17. max_steps=5*size**2,
  18. # Set this to True for maximum speed
  19. see_through_walls=True,
  20. **kwargs
  21. )
  22. def _gen_grid(self, width, height):
  23. self.grid = Grid(width, height)
  24. # Generate the surrounding walls
  25. self.grid.wall_rect(0, 0, width, height)
  26. # Types and colors of objects we can generate
  27. types = ['key', 'ball', 'box']
  28. objs = []
  29. objPos = []
  30. # Until we have generated all the objects
  31. while len(objs) < self.numObjs:
  32. objType = self._rand_elem(types)
  33. objColor = self._rand_elem(COLOR_NAMES)
  34. # If this object already exists, try again
  35. if (objType, objColor) in objs:
  36. continue
  37. if objType == 'key':
  38. obj = Key(objColor)
  39. elif objType == 'ball':
  40. obj = Ball(objColor)
  41. elif objType == 'box':
  42. obj = Box(objColor)
  43. pos = self.place_obj(obj)
  44. objs.append((objType, objColor))
  45. objPos.append(pos)
  46. # Randomize the agent start position and orientation
  47. self.place_agent()
  48. # Choose a random object to be picked up
  49. objIdx = self._rand_int(0, len(objs))
  50. self.targetType, self.target_color = objs[objIdx]
  51. self.target_pos = objPos[objIdx]
  52. descStr = '%s %s' % (self.target_color, self.targetType)
  53. self.mission = 'go to the %s' % descStr
  54. #print(self.mission)
  55. def step(self, action):
  56. obs, reward, done, info = MiniGridEnv.step(self, action)
  57. ax, ay = self.agent_pos
  58. tx, ty = self.target_pos
  59. # Toggle/pickup action terminates the episode
  60. if action == self.actions.toggle:
  61. done = True
  62. # Reward performing the done action next to the target object
  63. if action == self.actions.done:
  64. if abs(ax - tx) <= 1 and abs(ay - ty) <= 1:
  65. reward = self._reward()
  66. done = True
  67. return obs, reward, done, info
  68. class GotoEnv8x8N2(GoToObjectEnv):
  69. def __init__(self, **kwargs):
  70. super().__init__(size=8, numObjs=2, **kwargs)
  71. register(
  72. id='MiniGrid-GoToObject-6x6-N2-v0',
  73. entry_point='gym_minigrid.envs:GoToObjectEnv'
  74. )
  75. register(
  76. id='MiniGrid-GoToObject-8x8-N2-v0',
  77. entry_point='gym_minigrid.envs:GotoEnv8x8N2'
  78. )