fetch.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. from gym_minigrid.minigrid import (
  2. COLOR_NAMES,
  3. Ball,
  4. Grid,
  5. Key,
  6. MiniGridEnv,
  7. MissionSpace,
  8. )
  9. class FetchEnv(MiniGridEnv):
  10. """
  11. Environment in which the agent has to fetch a random object
  12. named using English text strings
  13. """
  14. def __init__(self, size=8, numObjs=3, **kwargs):
  15. self.numObjs = numObjs
  16. self.obj_types = ["key", "ball"]
  17. MISSION_SYNTAX = [
  18. "get a",
  19. "go get a",
  20. "fetch a",
  21. "go fetch a",
  22. "you must fetch a",
  23. ]
  24. self.size = size
  25. mission_space = MissionSpace(
  26. mission_func=lambda syntax, color, type: f"{syntax} {color} {type}",
  27. ordered_placeholders=[MISSION_SYNTAX, COLOR_NAMES, self.obj_types],
  28. )
  29. super().__init__(
  30. mission_space=mission_space,
  31. width=size,
  32. height=size,
  33. max_steps=5 * size**2,
  34. # Set this to True for maximum speed
  35. see_through_walls=True,
  36. **kwargs,
  37. )
  38. def _gen_grid(self, width, height):
  39. self.grid = Grid(width, height)
  40. # Generate the surrounding walls
  41. self.grid.horz_wall(0, 0)
  42. self.grid.horz_wall(0, height - 1)
  43. self.grid.vert_wall(0, 0)
  44. self.grid.vert_wall(width - 1, 0)
  45. objs = []
  46. # For each object to be generated
  47. while len(objs) < self.numObjs:
  48. objType = self._rand_elem(self.obj_types)
  49. objColor = self._rand_elem(COLOR_NAMES)
  50. if objType == "key":
  51. obj = Key(objColor)
  52. elif objType == "ball":
  53. obj = Ball(objColor)
  54. else:
  55. raise ValueError(
  56. "{} object type given. Object type can only be of values key and ball.".format(
  57. objType
  58. )
  59. )
  60. self.place_obj(obj)
  61. objs.append(obj)
  62. # Randomize the player start position and orientation
  63. self.place_agent()
  64. # Choose a random object to be picked up
  65. target = objs[self._rand_int(0, len(objs))]
  66. self.targetType = target.type
  67. self.targetColor = target.color
  68. descStr = f"{self.targetColor} {self.targetType}"
  69. # Generate the mission string
  70. idx = self._rand_int(0, 5)
  71. if idx == 0:
  72. self.mission = "get a %s" % descStr
  73. elif idx == 1:
  74. self.mission = "go get a %s" % descStr
  75. elif idx == 2:
  76. self.mission = "fetch a %s" % descStr
  77. elif idx == 3:
  78. self.mission = "go fetch a %s" % descStr
  79. elif idx == 4:
  80. self.mission = "you must fetch a %s" % descStr
  81. assert hasattr(self, "mission")
  82. def step(self, action):
  83. obs, reward, terminated, truncated, info = super().step(action)
  84. if self.carrying:
  85. if (
  86. self.carrying.color == self.targetColor
  87. and self.carrying.type == self.targetType
  88. ):
  89. reward = self._reward()
  90. terminated = True
  91. else:
  92. reward = 0
  93. terminated = True
  94. return obs, reward, terminated, truncated, info