playground_v0.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. grid = Grid(width, height)
  14. # Generate the surrounding walls
  15. grid.horzWall(0, 0)
  16. grid.horzWall(0, height-1)
  17. grid.vertWall(0, 0)
  18. grid.vertWall(width-1, 0)
  19. self.startPos = (width // 2, height // 2)
  20. self.startDir = 0
  21. roomW = width // 3
  22. roomH = height // 3
  23. # NOTE: if we want no room to have two doors of the same color,
  24. # that adds difficulty. It's a graph coloring problem
  25. # We could generate the door positions first, and do the coloring
  26. # in a second pass through rejection sampling.
  27. # For each row of rooms
  28. for j in range(0, 3):
  29. # For each column
  30. for i in range(0, 3):
  31. xL = i * roomW
  32. yT = j * roomH
  33. xR = xL + roomW
  34. yB = yT + roomH
  35. # Bottom wall and door
  36. if i+1 < 3:
  37. grid.vertWall(xR, yT, roomH)
  38. pos = (xR, self._randInt(yT+1, yB-1))
  39. color = self._randElem(COLOR_NAMES)
  40. grid.set(*pos, Door(color))
  41. # Bottom wall and door
  42. if j+1 < 3:
  43. grid.horzWall(xL, yB, roomW)
  44. pos = (self._randInt(xL+1, xR-1), yB)
  45. color = self._randElem(COLOR_NAMES)
  46. grid.set(*pos, Door(color))
  47. # Place random objects in the world
  48. types = ['key', 'ball', 'box']
  49. for i in range(0, 12):
  50. objType = self._randElem(types)
  51. objColor = self._randElem(COLOR_NAMES)
  52. if objType == 'key':
  53. obj = Key(objColor)
  54. elif objType == 'ball':
  55. obj = Ball(objColor)
  56. elif objType == 'box':
  57. obj = Box(objColor)
  58. self.placeObj(grid, obj, self.startPos)
  59. # No explicit mission in this environment
  60. self.mission = ''
  61. return grid
  62. def step(self, action):
  63. obs, reward, done, info = MiniGridEnv.step(self, action)
  64. return obs, reward, done, info
  65. register(
  66. id='MiniGrid-Playground-v0',
  67. entry_point='gym_minigrid.envs:PlaygroundV0'
  68. )