Переглянути джерело

Minor refactoring for environment creation process

Maxime Chevalier-Boisvert 7 роки тому
батько
коміт
5fd284ff88
3 змінених файлів з 39 додано та 30 видалено
  1. 18 8
      gym_minigrid/envs/doorkey.py
  2. 18 1
      gym_minigrid/envs/empty.py
  3. 3 21
      gym_minigrid/minigrid.py

+ 18 - 8
gym_minigrid/envs/doorkey.py

@@ -10,31 +10,41 @@ class DoorKeyEnv(MiniGridEnv):
         super().__init__(gridSize=size, maxSteps=4 * size)
 
     def _genGrid(self, width, height):
-        grid = super()._genGrid(width, height)
-        assert width == height
-        gridSz = width
+        # Create an empty grid
+        grid = Grid(width, height)
+
+        # Place walls around the edges
+        for i in range(0, width):
+            grid.set(i, 0, Wall())
+            grid.set(i, height - 1, Wall())
+        for j in range(0, height):
+            grid.set(0, j, Wall())
+            grid.set(height - 1, j, Wall())
+
+        # Place a goal in the bottom-right corner
+        grid.set(width - 2, height - 2, Goal())
 
         # Create a vertical splitting wall
-        splitIdx = self._randInt(2, gridSz-2)
-        for i in range(0, gridSz):
+        splitIdx = self._randInt(2, width-2)
+        for i in range(0, height):
             grid.set(splitIdx, i, Wall())
 
         # Place the agent at a random position and orientation
         self.startPos = self._randPos(
             1, splitIdx,
-            1, gridSz-1
+            1, height-1
         )
         self.startDir = self._randInt(0, 4)
 
         # Place a door in the wall
-        doorIdx = self._randInt(1, gridSz-2)
+        doorIdx = self._randInt(1, width-2)
         grid.set(splitIdx, doorIdx, LockedDoor('yellow'))
 
         # Place a yellow key on the left side
         while True:
             pos = self._randPos(
                 1, splitIdx,
-                1, gridSz-1
+                1, height-1
             )
             if pos == self.startPos:
                 continue

+ 18 - 1
gym_minigrid/envs/empty.py

@@ -7,7 +7,24 @@ class EmptyEnv(MiniGridEnv):
     """
 
     def __init__(self, size=8):
-        super().__init__(gridSize=size, maxSteps=3 * size)
+        super().__init__(gridSize=size, maxSteps=3*size)
+
+    def _genGrid(self, width, height):
+        # Create an empty grid
+        grid = Grid(width, height)
+
+        # Place walls around the edges
+        for i in range(0, width):
+            grid.set(i, 0, Wall())
+            grid.set(i, height - 1, Wall())
+        for j in range(0, height):
+            grid.set(0, j, Wall())
+            grid.set(height - 1, j, Wall())
+
+        # Place a goal in the bottom-right corner
+        grid.set(width - 2, height - 2, Goal())
+
+        return grid
 
 class EmptyEnv6x6(EmptyEnv):
     def __init__(self):

+ 3 - 21
gym_minigrid/minigrid.py

@@ -541,30 +541,12 @@ class MiniGridEnv(gym.Env):
         self.reset()
 
     def _genGrid(self, width, height):
-        """
-        Generate a new grid
-        """
-
-        # Initialize the grid
-        grid = Grid(width, height)
-
-        # Place walls around the edges
-        for i in range(0, width):
-            grid.set(i, 0, Wall())
-            grid.set(i, height - 1, Wall())
-        for j in range(0, height):
-            grid.set(0, j, Wall())
-            grid.set(height - 1, j, Wall())
-
-        # Place a goal in the bottom-left corner
-        grid.set(width - 2, height - 2, Goal())
-
-        return grid
+        assert False, "_genGrid needs to be implemented by each environment"
 
     def _reset(self):
         # Generate a new random grid at the start of each episode
-        # To prevent this behavior, call env.seed with the same
-        # seed before env.reset
+        # To keep the same grid for each episode, call env.seed() with
+        # the same seed before calling env.reset()
         self.grid = self._genGrid(self.gridSize, self.gridSize)
 
         # Place the agent in the starting position and direction