浏览代码

Randomized agent position in playground environment

Maxime Chevalier-Boisvert 7 年之前
父节点
当前提交
e5f35ea056
共有 2 个文件被更改,包括 12 次插入3 次删除
  1. 3 3
      gym_minigrid/envs/playground_v0.py
  2. 9 0
      gym_minigrid/minigrid.py

+ 3 - 3
gym_minigrid/envs/playground_v0.py

@@ -21,9 +21,6 @@ class PlaygroundV0(MiniGridEnv):
         grid.vertWall(0, 0)
         grid.vertWall(width-1, 0)
 
-        self.startPos = (width // 2, height // 2)
-        self.startDir = 0
-
         roomW = width // 3
         roomH = height // 3
 
@@ -56,6 +53,9 @@ class PlaygroundV0(MiniGridEnv):
                     color = self._randElem(COLOR_NAMES)
                     grid.set(*pos, Door(color))
 
+        # Randomize the player start position and orientation
+        self.placeAgent(grid)
+
         # Place random objects in the world
         types = ['key', 'ball', 'box']
         for i in range(0, 12):

+ 9 - 0
gym_minigrid/minigrid.py

@@ -630,6 +630,15 @@ class MiniGridEnv(gym.Env):
         grid.set(*pos, obj)
         return pos
 
+    def placeAgent(self, grid, randDir=True):
+        pos = self.placeObj(grid, None)
+        self.startPos = pos
+
+        if randDir:
+            self.startDir = self._randInt(0, 4)
+
+        return pos
+
     def _randElem(self, iterable):
         lst = list(iterable)
         idx = self._randInt(0, len(lst))