fetch.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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: "{} {} {}".format(
  27. syntax, color, type
  28. ),
  29. ordered_placeholders=[MISSION_SYNTAX, COLOR_NAMES, self.obj_types],
  30. )
  31. super().__init__(
  32. mission_space=mission_space,
  33. width=size,
  34. height=size,
  35. max_steps=5 * size**2,
  36. # Set this to True for maximum speed
  37. see_through_walls=True,
  38. **kwargs,
  39. )
  40. def _gen_grid(self, width, height):
  41. self.grid = Grid(width, height)
  42. # Generate the surrounding walls
  43. self.grid.horz_wall(0, 0)
  44. self.grid.horz_wall(0, height - 1)
  45. self.grid.vert_wall(0, 0)
  46. self.grid.vert_wall(width - 1, 0)
  47. objs = []
  48. # For each object to be generated
  49. while len(objs) < self.numObjs:
  50. objType = self._rand_elem(self.obj_types)
  51. objColor = self._rand_elem(COLOR_NAMES)
  52. if objType == "key":
  53. obj = Key(objColor)
  54. elif objType == "ball":
  55. obj = Ball(objColor)
  56. else:
  57. raise ValueError(
  58. "{} object type given. Object type can only be of values key and ball.".format(
  59. objType
  60. )
  61. )
  62. self.place_obj(obj)
  63. objs.append(obj)
  64. # Randomize the player start position and orientation
  65. self.place_agent()
  66. # Choose a random object to be picked up
  67. target = objs[self._rand_int(0, len(objs))]
  68. self.targetType = target.type
  69. self.targetColor = target.color
  70. descStr = f"{self.targetColor} {self.targetType}"
  71. # Generate the mission string
  72. idx = self._rand_int(0, 5)
  73. if idx == 0:
  74. self.mission = "get a %s" % descStr
  75. elif idx == 1:
  76. self.mission = "go get a %s" % descStr
  77. elif idx == 2:
  78. self.mission = "fetch a %s" % descStr
  79. elif idx == 3:
  80. self.mission = "go fetch a %s" % descStr
  81. elif idx == 4:
  82. self.mission = "you must fetch a %s" % descStr
  83. assert hasattr(self, "mission")
  84. def step(self, action):
  85. obs, reward, done, info = MiniGridEnv.step(self, action)
  86. if self.carrying:
  87. if (
  88. self.carrying.color == self.targetColor
  89. and self.carrying.type == self.targetType
  90. ):
  91. reward = self._reward()
  92. done = True
  93. else:
  94. reward = 0
  95. done = True
  96. return obs, reward, done, info