playground.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. from gym_minigrid.minigrid import COLOR_NAMES, Ball, Box, Door, Grid, Key, MiniGridEnv
  2. class PlaygroundEnv(MiniGridEnv):
  3. """
  4. Environment with multiple rooms and random objects.
  5. This environment has no specific goals or rewards.
  6. """
  7. def __init__(self, **kwargs):
  8. super().__init__(grid_size=19, max_steps=100, **kwargs)
  9. def _gen_grid(self, width, height):
  10. # Create the grid
  11. self.grid = Grid(width, height)
  12. # Generate the surrounding walls
  13. self.grid.horz_wall(0, 0)
  14. self.grid.horz_wall(0, height - 1)
  15. self.grid.vert_wall(0, 0)
  16. self.grid.vert_wall(width - 1, 0)
  17. roomW = width // 3
  18. roomH = height // 3
  19. # For each row of rooms
  20. for j in range(0, 3):
  21. # For each column
  22. for i in range(0, 3):
  23. xL = i * roomW
  24. yT = j * roomH
  25. xR = xL + roomW
  26. yB = yT + roomH
  27. # Bottom wall and door
  28. if i + 1 < 3:
  29. self.grid.vert_wall(xR, yT, roomH)
  30. pos = (xR, self._rand_int(yT + 1, yB - 1))
  31. color = self._rand_elem(COLOR_NAMES)
  32. self.grid.set(*pos, Door(color))
  33. # Bottom wall and door
  34. if j + 1 < 3:
  35. self.grid.horz_wall(xL, yB, roomW)
  36. pos = (self._rand_int(xL + 1, xR - 1), yB)
  37. color = self._rand_elem(COLOR_NAMES)
  38. self.grid.set(*pos, Door(color))
  39. # Randomize the player start position and orientation
  40. self.place_agent()
  41. # Place random objects in the world
  42. types = ["key", "ball", "box"]
  43. for i in range(0, 12):
  44. objType = self._rand_elem(types)
  45. objColor = self._rand_elem(COLOR_NAMES)
  46. if objType == "key":
  47. obj = Key(objColor)
  48. elif objType == "ball":
  49. obj = Ball(objColor)
  50. elif objType == "box":
  51. obj = Box(objColor)
  52. else:
  53. raise ValueError(
  54. "{} object type given. Object type can only be of values key, ball and box.".format(
  55. objType
  56. )
  57. )
  58. self.place_obj(obj)
  59. # No explicit mission in this environment
  60. self.mission = ""
  61. def step(self, action):
  62. obs, reward, done, info = super().step(action)
  63. return obs, reward, done, info