Browse Source

Added env.agentSees(x, y) method

Maxime Chevalier-Boisvert 7 years ago
parent
commit
dcc9732274
2 changed files with 34 additions and 1 deletions
  1. 9 1
      gym_minigrid/minigrid.py
  2. 25 0
      run_tests.py

+ 9 - 1
gym_minigrid/minigrid.py

@@ -639,13 +639,21 @@ class MiniGridEnv(gym.Env):
             topX = self.agentPos[0] - AGENT_VIEW_SIZE // 2
             topY = self.agentPos[1] - AGENT_VIEW_SIZE + 1
         else:
-            assert False
+            assert False, "invalid agent direction"
 
         botX = topX + AGENT_VIEW_SIZE
         botY = topY + AGENT_VIEW_SIZE
 
         return (topX, topY, botX, botY)
 
+    def agentSees(self, x, y):
+        """
+        Check if a grid position is visible to the agent
+        """
+
+        topX, topY, botX, botY = self.getViewExts()
+        return (x >= topX and x < botX and y >= topY and y < botY)
+
     def step(self, action):
         self.stepCount += 1
 

+ 25 - 0
run_tests.py

@@ -12,6 +12,8 @@ from gym_minigrid.envs import DoorKeyEnv
 # Test importing wrappers
 from gym_minigrid.wrappers import *
 
+##############################################################################
+
 print('%d environments registered' % len(envSet))
 
 for envName in sorted(envSet):
@@ -59,3 +61,26 @@ for envName in sorted(envSet):
         env.render('rgb_array')
 
     env.close()
+
+##############################################################################
+
+# Test the env.agentSees() function
+env = gym.make('MiniGrid-Empty-6x6-v0')
+goalPos = (env.grid.width - 2, env.grid.height - 2)
+
+def goalInObs(obs):
+    grid = Grid.decode(obs)
+    for j in range(0, grid.height):
+        for i in range(0, grid.width):
+            cell = grid.get(i, j)
+            if cell and cell.color == 'green':
+                return True
+    return False
+
+env.reset()
+for i in range(0, 200):
+    action = random.randint(0, env.action_space.n - 1)
+    obs, reward, done, info = env.step(action)
+    assert env.agentSees(*goalPos) == goalInObs(obs)
+    if done:
+        env.reset()