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