Browse Source

Refactored to eliminate start_pos, simplify code.

Maxime Chevalier-Boisvert 5 years ago
parent
commit
13d4651e03

+ 2 - 2
gym_minigrid/envs/crossing.py

@@ -30,8 +30,8 @@ class CrossingEnv(MiniGridEnv):
         self.grid.wall_rect(0, 0, width, height)
 
         # Place the agent in the top-left corner
-        self.start_pos = (1, 1)
-        self.start_dir = 0
+        self.agent_pos = (1, 1)
+        self.agent_dir = 0
 
         # Place a goal square in the bottom-right corner
         self.grid.set(width - 2, height - 2, Goal())

+ 2 - 2
gym_minigrid/envs/distshift.py

@@ -44,8 +44,8 @@ class DistShiftEnv(MiniGridEnv):
 
         # Place the agent
         if self.agent_start_pos is not None:
-            self.start_pos = self.agent_start_pos
-            self.start_dir = self.agent_start_dir
+            self.agent_pos = self.agent_start_pos
+            self.agent_dir = self.agent_start_dir
         else:
             self.place_agent()
 

+ 1 - 1
gym_minigrid/envs/doorkey.py

@@ -28,7 +28,7 @@ class DoorKeyEnv(MiniGridEnv):
 
         # Place the agent at a random position and orientation
         # on the left side of the splitting wall
-        self.start_pos = self.place_agent(size=(splitIdx, height))
+        self.place_agent(size=(splitIdx, height))
 
         # Place a door in the wall
         doorIdx = self._rand_int(1, width-2)

+ 2 - 2
gym_minigrid/envs/dynamicobstacles.py

@@ -46,8 +46,8 @@ class DynamicObstaclesEnv(MiniGridEnv):
 
         # Place the agent
         if self.agent_start_pos is not None:
-            self.start_pos = self.agent_start_pos
-            self.start_dir = self.agent_start_dir
+            self.agent_pos = self.agent_start_pos
+            self.agent_dir = self.agent_start_dir
         else:
             self.place_agent()
 

+ 2 - 2
gym_minigrid/envs/empty.py

@@ -34,8 +34,8 @@ class EmptyEnv(MiniGridEnv):
 
         # Place the agent
         if self.agent_start_pos is not None:
-            self.start_pos = self.agent_start_pos
-            self.start_dir = self.agent_start_dir
+            self.agent_pos = self.agent_start_pos
+            self.agent_dir = self.agent_start_dir
         else:
             self.place_agent()
 

+ 2 - 2
gym_minigrid/envs/fourrooms.py

@@ -53,9 +53,9 @@ class FourRoomsEnv(MiniGridEnv):
 
         # Randomize the player start position and orientation
         if self._agent_default_pos is not None:
-            self.start_pos = self._agent_default_pos
+            self.agent_pos = self._agent_default_pos
             self.grid.set(*self._agent_default_pos, None)
-            self.start_dir = self._rand_int(0, 4)  # assuming random start direction
+            self.agent_dir = self._rand_int(0, 4)  # assuming random start direction
         else:
             self.place_agent()
 

+ 1 - 1
gym_minigrid/envs/lockedroom.py

@@ -102,7 +102,7 @@ class LockedRoom(MiniGridEnv):
         self.grid.set(*keyPos, Key(lockedRoom.color))
 
         # Randomize the player start position and orientation
-        self.start_pos = self.place_agent(
+        self.agent_pos = self.place_agent(
             top=(lWallIdx, 0),
             size=(rWallIdx-lWallIdx, height)
         )

+ 2 - 2
gym_minigrid/envs/memory.py

@@ -62,8 +62,8 @@ class MemoryEnv(MiniGridEnv):
             self.grid.set(hallway_end + 2, j, Wall())
 
         # Fix the player's start position and orientation
-        self.start_pos = (self._rand_int(1, hallway_end + 1), height // 2)
-        self.start_dir = 0
+        self.agent_pos = (self._rand_int(1, hallway_end + 1), height // 2)
+        self.agent_dir = 0
 
         # Place objects
         start_room_obj = self._rand_elem([Key, Ball])

+ 14 - 14
gym_minigrid/minigrid.py

@@ -709,9 +709,9 @@ class MiniGridEnv(gym.Env):
         self.max_steps = max_steps
         self.see_through_walls = see_through_walls
 
-        # Starting position and direction for the agent
-        self.start_pos = None
-        self.start_dir = None
+        # Current position and direction of the agent
+        self.agent_pos = None
+        self.agent_dir = None
 
         # Initialize the RNG
         self.seed(seed=seed)
@@ -720,23 +720,23 @@ class MiniGridEnv(gym.Env):
         self.reset()
 
     def reset(self):
+        # Current position and direction of the agent
+        self.agent_pos = None
+        self.agent_dir = None
+
         # Generate a new random grid at the start of each episode
         # To keep the same grid for each episode, call env.seed() with
         # the same seed before calling env.reset()
         self._gen_grid(self.width, self.height)
 
         # These fields should be defined by _gen_grid
-        assert self.start_pos is not None
-        assert self.start_dir is not None
+        assert self.agent_pos is not None
+        assert self.agent_dir is not None
 
         # Check that the agent doesn't overlap with an object
-        start_cell = self.grid.get(*self.start_pos)
+        start_cell = self.grid.get(*self.agent_pos)
         assert start_cell is None or start_cell.can_overlap()
 
-        # Place the agent in the starting position and direction
-        self.agent_pos = self.start_pos
-        self.agent_dir = self.start_dir
-
         # Item picked up, being carried, initially nothing
         self.carrying = None
 
@@ -934,7 +934,7 @@ class MiniGridEnv(gym.Env):
                 continue
 
             # Don't place the object where the agent is
-            if np.array_equal(pos, self.start_pos):
+            if np.array_equal(pos, self.agent_pos):
                 continue
 
             # Check if there is a filtering criterion
@@ -962,12 +962,12 @@ class MiniGridEnv(gym.Env):
         Set the agent's starting point at an empty position in the grid
         """
 
-        self.start_pos = None
+        self.agent_pos = None
         pos = self.place_obj(None, top, size, max_tries=max_tries)
-        self.start_pos = pos
+        self.agent_pos = pos
 
         if rand_dir:
-            self.start_dir = self._rand_int(0, 4)
+            self.agent_dir = self._rand_int(0, 4)
 
         return pos
 

+ 6 - 9
gym_minigrid/roomgrid.py

@@ -6,7 +6,7 @@ def reject_next_to(env, pos):
     the agent's starting point
     """
 
-    sx, sy = env.start_pos
+    sx, sy = env.agent_pos
     x, y = pos
     d = abs(sx - x) + abs(sy - y)
     return d < 2
@@ -162,11 +162,11 @@ class RoomGrid(MiniGridEnv):
                     room.door_pos[3] = room.neighbors[3].door_pos[1]
 
         # The agent starts in the middle, facing right
-        self.start_pos = (
+        self.agent_pos = (
             (self.num_cols // 2) * (self.room_size-1) + (self.room_size // 2),
             (self.num_rows // 2) * (self.room_size-1) + (self.room_size // 2)
         )
-        self.start_dir = 0
+        self.agent_dir = 0
 
     def place_in_room(self, i, j, obj):
         """
@@ -296,14 +296,11 @@ class RoomGrid(MiniGridEnv):
         # Find a position that is not right in front of an object
         while True:
             super().place_agent(room.top, room.size, rand_dir, max_tries=1000)
-            pos = self.start_pos
-            dir = DIR_TO_VEC[self.start_dir]
-            front_pos = pos + dir
-            front_cell = self.grid.get(*front_pos)
+            front_cell = self.grid.get(*self.front_pos)
             if front_cell is None or front_cell.type is 'wall':
                 break
 
-        return self.start_pos
+        return self.agent_pos
 
     def connect_all(self, door_colors=COLOR_NAMES, max_itrs=5000):
         """
@@ -311,7 +308,7 @@ class RoomGrid(MiniGridEnv):
         starting position
         """
 
-        start_room = self.room_from_pos(*self.start_pos)
+        start_room = self.room_from_pos(*self.agent_pos)
 
         added_doors = []