playground.py 2.7 KB

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