playground_v1.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. from gym_minigrid.minigrid import *
  2. from gym_minigrid.register import register
  3. class Room:
  4. def __init__(self,
  5. top,
  6. size
  7. ):
  8. # Top-left corner and size (tuples)
  9. self.top = top
  10. self.size = size
  11. # List of door objects and door positions
  12. self.doors = []
  13. self.doorPos = []
  14. # Indicates if this room is locked
  15. self.locked = False
  16. # TODO: connectivity?
  17. # List of objects contained
  18. self.objs = []
  19. def randPos(self, env):
  20. topX, topY = self.top
  21. sizeX, sizeY = self.size
  22. return env._randPos(
  23. topX + 1, topX + sizeX - 1,
  24. topY + 1, topY + sizeY - 1
  25. )
  26. class PlaygroundV1(MiniGridEnv):
  27. """
  28. Environment with multiple rooms and random objects.
  29. This environment has no specific goals or rewards.
  30. """
  31. def __init__(
  32. self,
  33. roomSize=6,
  34. numCols=4
  35. ):
  36. assert roomSize > 0
  37. assert roomSize >= 4
  38. assert numCols > 0
  39. self.roomSize = roomSize
  40. self.numCols = numCols
  41. self.numRows = numCols
  42. gridSize = (roomSize - 1) * numCols + 1
  43. super().__init__(gridSize=gridSize, maxSteps=6*gridSize)
  44. self.reward_range = (0, 1)
  45. def _genGrid(self, width, height):
  46. # Create the grid
  47. self.grid = Grid(width, height)
  48. # Generate the surrounding walls
  49. self.grid.horzWall(0, 0)
  50. self.grid.horzWall(0, height-1)
  51. self.grid.vertWall(0, 0)
  52. self.grid.vertWall(width-1, 0)
  53. roomW = self.roomSize
  54. roomH = self.roomSize
  55. self.rooms = []
  56. # Generate the list of rooms
  57. for j in range(0, self.numRows):
  58. for i in range(0, self.numCols):
  59. room = Room(
  60. (i * (self.roomSize-1), j * (self.roomSize-1)),
  61. (self.roomSize, self.roomSize)
  62. )
  63. self.rooms.append(room)
  64. # TODO: generate walls
  65. # May want to add function to Grid class, wallRect(i, j, w, h, color)
  66. # Randomize the player start position and orientation
  67. self.placeAgent()
  68. # Place random objects in the world
  69. types = ['key', 'ball', 'box']
  70. for i in range(0, 12):
  71. objType = self._randElem(types)
  72. objColor = self._randElem(COLOR_NAMES)
  73. if objType == 'key':
  74. obj = Key(objColor)
  75. elif objType == 'ball':
  76. obj = Ball(objColor)
  77. elif objType == 'box':
  78. obj = Box(objColor)
  79. self.placeObj(obj)
  80. # TODO: curriculum generation
  81. self.mission = ''
  82. def step(self, action):
  83. obs, reward, done, info = super().step(action)
  84. return obs, reward, done, info
  85. register(
  86. id='MiniGrid-Playground-v1',
  87. entry_point='gym_minigrid.envs:PlaygroundV1'
  88. )