putnear.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. from gym_minigrid.minigrid import *
  2. from gym_minigrid.register import register
  3. class PutNearEnv(MiniGridEnv):
  4. """
  5. Environment in which the agent is instructed to place an object near
  6. another object through a natural language string.
  7. """
  8. def __init__(
  9. self,
  10. size=6,
  11. numObjs=2
  12. ):
  13. self.numObjs = numObjs
  14. super().__init__(
  15. grid_size=size,
  16. max_steps=5*size,
  17. # Set this to True for maximum speed
  18. see_through_walls=True
  19. )
  20. self.reward_range = (0, 1)
  21. def _gen_grid(self, width, height):
  22. self.grid = Grid(width, height)
  23. # Generate the surrounding walls
  24. self.grid.horz_wall(0, 0)
  25. self.grid.horz_wall(0, height-1)
  26. self.grid.vert_wall(0, 0)
  27. self.grid.vert_wall(width-1, 0)
  28. # Types and colors of objects we can generate
  29. types = ['key', 'ball', 'box']
  30. objs = []
  31. objPos = []
  32. def near_obj(env, p1):
  33. for p2 in objPos:
  34. dx = p1[0] - p2[0]
  35. dy = p1[1] - p2[1]
  36. if abs(dx) <= 1 and abs(dy) <= 1:
  37. return True
  38. return False
  39. # Until we have generated all the objects
  40. while len(objs) < self.numObjs:
  41. objType = self._rand_elem(types)
  42. objColor = self._rand_elem(COLOR_NAMES)
  43. # If this object already exists, try again
  44. if (objType, objColor) in objs:
  45. continue
  46. if objType == 'key':
  47. obj = Key(objColor)
  48. elif objType == 'ball':
  49. obj = Ball(objColor)
  50. elif objType == 'box':
  51. obj = Box(objColor)
  52. pos = self.place_obj(obj, reject_fn=near_obj)
  53. objs.append((objType, objColor))
  54. objPos.append(pos)
  55. # Randomize the agent start position and orientation
  56. self.place_agent()
  57. # Choose a random object to be moved
  58. objIdx = self._rand_int(0, len(objs))
  59. self.move_type, self.moveColor = objs[objIdx]
  60. self.move_pos = objPos[objIdx]
  61. # Choose a target object (to put the first object next to)
  62. while True:
  63. targetIdx = self._rand_int(0, len(objs))
  64. if targetIdx != objIdx:
  65. break
  66. self.target_type, self.target_color = objs[targetIdx]
  67. self.target_pos = objPos[targetIdx]
  68. self.mission = 'put the %s %s near the %s %s' % (
  69. self.moveColor,
  70. self.move_type,
  71. self.target_color,
  72. self.target_type
  73. )
  74. def step(self, action):
  75. preCarrying = self.carrying
  76. obs, reward, done, info = super().step(action)
  77. u, v = self.get_dir_vec()
  78. ox, oy = (self.agent_pos[0] + u, self.agent_pos[1] + v)
  79. tx, ty = self.target_pos
  80. # If we picked up the wrong object, terminate the episode
  81. if action == self.actions.pickup and self.carrying:
  82. if self.carrying.type != self.move_type or self.carrying.color != self.moveColor:
  83. done = True
  84. # If successfully dropping an object near the target
  85. if action == self.actions.drop and preCarrying:
  86. if self.grid.get(ox, oy) is preCarrying:
  87. if abs(ox - tx) <= 1 and abs(oy - ty) <= 1:
  88. reward = 1
  89. done = True
  90. return obs, reward, done, info
  91. class PutNear8x8N3(PutNearEnv):
  92. def __init__(self):
  93. super().__init__(size=8, numObjs=3)
  94. register(
  95. id='MiniGrid-PutNear-6x6-N2-v0',
  96. entry_point='gym_minigrid.envs:PutNearEnv'
  97. )
  98. register(
  99. id='MiniGrid-PutNear-8x8-N3-v0',
  100. entry_point='gym_minigrid.envs:PutNear8x8N3'
  101. )