Maxime Chevalier-Boisvert 7 лет назад
Родитель
Сommit
3c52268b00

+ 1 - 4
gym_minigrid/envs/doorkey.py

@@ -14,10 +14,7 @@ class DoorKeyEnv(MiniGridEnv):
         self.grid = Grid(width, height)
 
         # Generate the surrounding walls
-        self.grid.horzWall(0, 0)
-        self.grid.horzWall(0, height-1)
-        self.grid.vertWall(0, 0)
-        self.grid.vertWall(width-1, 0)
+        self.grid.wallRect(0, 0, width, height)
 
         # Place a goal in the bottom-right corner
         self.grid.set(width - 2, height - 2, Goal())

+ 1 - 4
gym_minigrid/envs/gotodoor.py

@@ -24,10 +24,7 @@ class GoToDoorEnv(MiniGridEnv):
         height = self._randInt(5, height+1)
 
         # Generate the surrounding walls
-        self.grid.horzWall(0, 0, width)
-        self.grid.horzWall(0, height-1, width)
-        self.grid.vertWall(0, 0, height)
-        self.grid.vertWall(width-1, 0, height)
+        self.grid.wallRect(0, 0, width, height)
 
         # Generate the 4 doors at random positions
         doorPos = []

+ 1 - 4
gym_minigrid/envs/gotoobject.py

@@ -20,10 +20,7 @@ class GoToObjectEnv(MiniGridEnv):
         self.grid = Grid(width, height)
 
         # Generate the surrounding walls
-        self.grid.horzWall(0, 0)
-        self.grid.horzWall(0, height-1)
-        self.grid.vertWall(0, 0)
-        self.grid.vertWall(width-1, 0)
+        self.grid.wallRect(0, 0, width, height)
 
         # Types and colors of objects we can generate
         types = ['key', 'ball', 'box']

+ 30 - 15
gym_minigrid/envs/roomgrid.py

@@ -17,7 +17,8 @@ class Room:
         # Indicates if this room is locked
         self.locked = False
 
-        # TODO: connectivity?
+        # Set of rooms this is connected to
+        self.neighbors = set()
 
         # List of objects contained
         self.objs = []
@@ -57,43 +58,57 @@ class RoomGrid(MiniGridEnv):
 
         self.reward_range = (0, 1)
 
+    def getRoom(self, x, y):
+        """Get the room a given position maps to"""
+
+        assert x >= 0
+        assert y >= 0
+
+        i = x // self.roomSize
+        j = y // self.roomSize
+
+        assert i < self.numCols
+        assert j < self.numRows
+
+        return self.roomGrid[j][i]
+
     def _genGrid(self, width, height):
         # Create the grid
         self.grid = Grid(width, height)
 
-        # Generate the surrounding walls
-        self.grid.horzWall(0, 0)
-        self.grid.horzWall(0, height-1)
-        self.grid.vertWall(0, 0)
-        self.grid.vertWall(width-1, 0)
-
-        roomW = self.roomSize
-        roomH = self.roomSize
-
+        self.roomGrid = []
         self.rooms = []
 
-        # Generate the list of rooms
+        # For each row of rooms
         for j in range(0, self.numRows):
+            row = []
+
+            # For each column of rooms
             for i in range(0, self.numCols):
                 room = Room(
                     (i * (self.roomSize-1), j * (self.roomSize-1)),
                     (self.roomSize, self.roomSize)
                 )
+
+                row.append(room)
                 self.rooms.append(room)
 
-        # TODO: generate walls
-        # May want to add function to Grid class, wallRect(i, j, w, h, color)
+                # Generate the walls for this room
+                self.grid.wallRect(*room.top, *room.size)
 
+            self.roomGrid.append(row)
 
+        # Randomize the player start position and orientation
+        self.placeAgent()
 
+        # Find which room the agent was placed in
+        startRoom = self.getRoom(*self.startPos)
 
 
 
 
 
 
-        # Randomize the player start position and orientation
-        self.placeAgent()
 
         # TODO: respect maxObsPerRoom
 

+ 6 - 0
gym_minigrid/minigrid.py

@@ -342,6 +342,12 @@ class Grid:
         for j in range(0, length):
             self.set(x, y + j, Wall())
 
+    def wallRect(self, x, y, w, h):
+        self.horzWall(x, y, w)
+        self.horzWall(x, y+h-1, w)
+        self.vertWall(x, y, h)
+        self.vertWall(x+w-1, y, h)
+
     def rotateLeft(self):
         """
         Rotate the grid to the left (counter-clockwise)