Browse Source

Fixed bug signaled in #73

Maxime Chevalier-Boisvert 5 years ago
parent
commit
50f396a9bf

+ 2 - 2
gym_minigrid/envs/crossing.py

@@ -34,7 +34,7 @@ class CrossingEnv(MiniGridEnv):
         self.agent_dir = 0
 
         # Place a goal square in the bottom-right corner
-        self.grid.set(width - 2, height - 2, Goal())
+        self.put_obj(Goal(), width - 2, height - 2)
 
         # Place obstacles (lava or walls)
         v, h = object(), object()  # singleton `vertical` and `horizontal` objects
@@ -51,7 +51,7 @@ class CrossingEnv(MiniGridEnv):
             itt.product(rivers_v, range(1, height - 1)),
         )
         for i, j in obstacle_pos:
-            self.grid.set(i, j, self.obstacle_type())
+            self.put_obj(self.obstacle_type(), i, j)
 
         # Sample path to goal
         path = [h] * len(rivers_v) + [v] * len(rivers_h)

+ 1 - 1
gym_minigrid/envs/distshift.py

@@ -35,7 +35,7 @@ class DistShiftEnv(MiniGridEnv):
         self.grid.wall_rect(0, 0, width, height)
 
         # Place a goal square in the bottom-right corner
-        self.grid.set(*self.goal_pos, Goal())
+        self.put_obj(Goal(), *self.goal_pos)
 
         # Place the lava rows
         for i in range(self.width - 6):

+ 2 - 2
gym_minigrid/envs/doorkey.py

@@ -20,7 +20,7 @@ class DoorKeyEnv(MiniGridEnv):
         self.grid.wall_rect(0, 0, width, height)
 
         # Place a goal in the bottom-right corner
-        self.grid.set(width - 2, height - 2, Goal())
+        self.put_obj(Goal(), width - 2, height - 2)
 
         # Create a vertical splitting wall
         splitIdx = self._rand_int(2, width-2)
@@ -32,7 +32,7 @@ class DoorKeyEnv(MiniGridEnv):
 
         # Place a door in the wall
         doorIdx = self._rand_int(1, width-2)
-        self.grid.set(splitIdx, doorIdx, Door('yellow', is_locked=True))
+        self.put_obj(Door('yellow', is_locked=True), splitIdx, doorIdx)
 
         # Place a yellow key on the left side
         self.place_obj(

+ 1 - 1
gym_minigrid/envs/empty.py

@@ -30,7 +30,7 @@ class EmptyEnv(MiniGridEnv):
         self.grid.wall_rect(0, 0, width, height)
 
         # Place a goal square in the bottom-right corner
-        self.grid.set(width - 2, height - 2, Goal())
+        self.put_obj(Goal(), width - 2, height - 2)
 
         # Place the agent
         if self.agent_start_pos is not None:

+ 1 - 2
gym_minigrid/envs/fourrooms.py

@@ -61,7 +61,7 @@ class FourRoomsEnv(MiniGridEnv):
 
         if self._goal_default_pos is not None:
             goal = Goal()
-            self.grid.set(*self._goal_default_pos, goal)
+            self.put_obj(goal, *self._goal_default_pos)
             goal.init_pos, goal.cur_pos = self._goal_default_pos
         else:
             self.place_obj(Goal())
@@ -72,7 +72,6 @@ class FourRoomsEnv(MiniGridEnv):
         obs, reward, done, info = MiniGridEnv.step(self, action)
         return obs, reward, done, info
 
-
 register(
     id='MiniGrid-FourRooms-v0',
     entry_point='gym_minigrid.envs:FourRoomsEnv'

+ 1 - 1
gym_minigrid/envs/lavagap.py

@@ -32,7 +32,7 @@ class LavaGapEnv(MiniGridEnv):
 
         # Place a goal square in the bottom-right corner
         self.goal_pos = np.array((width - 2, height - 2))
-        self.grid.set(*self.goal_pos, Goal())
+        self.put_obj(Goal(), *self.goal_pos)
 
         # Generate and store random gap position
         self.gap_pos = np.array((

+ 9 - 0
gym_minigrid/minigrid.py

@@ -958,6 +958,15 @@ class MiniGridEnv(gym.Env):
 
         return pos
 
+    def put_obj(self, obj, i, j):
+        """
+        Put an object at a specific position in the grid
+        """
+
+        self.grid.set(i, j, obj)
+        obj.init_pos = (i, j)
+        obj.cur_pos = (i, j)
+
     def place_agent(
         self,
         top=None,