playground_v0.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. from gym_minigrid.minigrid import *
  2. from gym_minigrid.register import register
  3. class PlaygroundV0(MiniGridEnv):
  4. """
  5. Environment with multiple rooms and random objects.
  6. This environment has no specific goals or rewards.
  7. """
  8. def __init__(self):
  9. super().__init__(gridSize=19, maxSteps=100)
  10. self.reward_range = (0, 1)
  11. def _genGrid(self, width, height):
  12. # Create the grid
  13. self.grid = Grid(width, height)
  14. # Generate the surrounding walls
  15. self.grid.horzWall(0, 0)
  16. self.grid.horzWall(0, height-1)
  17. self.grid.vertWall(0, 0)
  18. self.grid.vertWall(width-1, 0)
  19. roomW = width // 3
  20. roomH = height // 3
  21. # For each row of rooms
  22. for j in range(0, 3):
  23. # For each column
  24. for i in range(0, 3):
  25. xL = i * roomW
  26. yT = j * roomH
  27. xR = xL + roomW
  28. yB = yT + roomH
  29. # Bottom wall and door
  30. if i+1 < 3:
  31. self.grid.vertWall(xR, yT, roomH)
  32. pos = (xR, self._randInt(yT+1, yB-1))
  33. color = self._randElem(COLOR_NAMES)
  34. self.grid.set(*pos, Door(color))
  35. # Bottom wall and door
  36. if j+1 < 3:
  37. self.grid.horzWall(xL, yB, roomW)
  38. pos = (self._randInt(xL+1, xR-1), yB)
  39. color = self._randElem(COLOR_NAMES)
  40. self.grid.set(*pos, Door(color))
  41. # Randomize the player start position and orientation
  42. self.placeAgent()
  43. # Place random objects in the world
  44. types = ['key', 'ball', 'box']
  45. for i in range(0, 12):
  46. objType = self._randElem(types)
  47. objColor = self._randElem(COLOR_NAMES)
  48. if objType == 'key':
  49. obj = Key(objColor)
  50. elif objType == 'ball':
  51. obj = Ball(objColor)
  52. elif objType == 'box':
  53. obj = Box(objColor)
  54. self.placeObj(obj)
  55. # No explicit mission in this environment
  56. self.mission = ''
  57. def step(self, action):
  58. obs, reward, done, info = MiniGridEnv.step(self, action)
  59. return obs, reward, done, info
  60. register(
  61. id='MiniGrid-Playground-v0',
  62. entry_point='gym_minigrid.envs:PlaygroundV0'
  63. )