gotoobject.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. from gym_minigrid.minigrid import COLOR_NAMES, Ball, Box, Grid, Key, MiniGridEnv
  2. class GoToObjectEnv(MiniGridEnv):
  3. """
  4. Environment in which the agent is instructed to go to a given object
  5. named using an English text 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**2,
  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.wall_rect(0, 0, width, height)
  20. # Types and colors of objects we can generate
  21. types = ["key", "ball", "box"]
  22. objs = []
  23. objPos = []
  24. # Until we have generated all the objects
  25. while len(objs) < self.numObjs:
  26. objType = self._rand_elem(types)
  27. objColor = self._rand_elem(COLOR_NAMES)
  28. # If this object already exists, try again
  29. if (objType, objColor) in objs:
  30. continue
  31. if objType == "key":
  32. obj = Key(objColor)
  33. elif objType == "ball":
  34. obj = Ball(objColor)
  35. elif objType == "box":
  36. obj = Box(objColor)
  37. else:
  38. raise ValueError(
  39. "{} object type given. Object type can only be of values key, ball and box.".format(
  40. objType
  41. )
  42. )
  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 = f"{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 = super().step(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