putnear.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. from gym_minigrid.minigrid import COLOR_NAMES, Ball, Box, Grid, Key, MiniGridEnv
  2. class PutNearEnv(MiniGridEnv):
  3. """
  4. Environment in which the agent is instructed to place an object near
  5. another object through a natural language string.
  6. """
  7. def __init__(self, size=6, numObjs=2, **kwargs):
  8. self.numObjs = numObjs
  9. super().__init__(
  10. grid_size=size,
  11. max_steps=5 * size,
  12. # Set this to True for maximum speed
  13. see_through_walls=True,
  14. **kwargs
  15. )
  16. def _gen_grid(self, width, height):
  17. self.grid = Grid(width, height)
  18. # Generate the surrounding walls
  19. self.grid.horz_wall(0, 0)
  20. self.grid.horz_wall(0, height - 1)
  21. self.grid.vert_wall(0, 0)
  22. self.grid.vert_wall(width - 1, 0)
  23. # Types and colors of objects we can generate
  24. types = ["key", "ball", "box"]
  25. objs = []
  26. objPos = []
  27. def near_obj(env, p1):
  28. for p2 in objPos:
  29. dx = p1[0] - p2[0]
  30. dy = p1[1] - p2[1]
  31. if abs(dx) <= 1 and abs(dy) <= 1:
  32. return True
  33. return False
  34. # Until we have generated all the objects
  35. while len(objs) < self.numObjs:
  36. objType = self._rand_elem(types)
  37. objColor = self._rand_elem(COLOR_NAMES)
  38. # If this object already exists, try again
  39. if (objType, objColor) in objs:
  40. continue
  41. if objType == "key":
  42. obj = Key(objColor)
  43. elif objType == "ball":
  44. obj = Ball(objColor)
  45. elif objType == "box":
  46. obj = Box(objColor)
  47. else:
  48. raise ValueError(
  49. "{} object type given. Object type can only be of values key, ball and box.".format(
  50. objType
  51. )
  52. )
  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 {} {} near the {} {}".format(
  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 (
  84. self.carrying.type != self.move_type
  85. or self.carrying.color != self.moveColor
  86. ):
  87. done = True
  88. # If successfully dropping an object near the target
  89. if action == self.actions.drop and preCarrying:
  90. if self.grid.get(ox, oy) is preCarrying:
  91. if abs(ox - tx) <= 1 and abs(oy - ty) <= 1:
  92. reward = self._reward()
  93. done = True
  94. return obs, reward, done, info