浏览代码

Added Playground-v0 environment for experiments

This environment has no explicit goal and no reward function.
Maxime Chevalier-Boisvert 7 年之前
父节点
当前提交
c4049b13ed

+ 1 - 0
gym_minigrid/envs/__init__.py

@@ -7,3 +7,4 @@ from gym_minigrid.envs.gotodoor import *
 from gym_minigrid.envs.putnear import *
 from gym_minigrid.envs.lockedroom import *
 from gym_minigrid.envs.fourroomqa import *
+from gym_minigrid.envs.playground_v0 import *

+ 0 - 1
gym_minigrid/envs/gotodoor.py

@@ -1,4 +1,3 @@
-from gym import spaces
 from gym_minigrid.minigrid import *
 from gym_minigrid.register import register
 

+ 2 - 14
gym_minigrid/envs/gotoobject.py

@@ -31,7 +31,6 @@ class GoToObjectEnv(MiniGridEnv):
 
         # Types and colors of objects we can generate
         types = ['key', 'ball', 'box']
-        colors = list(COLORS.keys())
 
         objs = []
         objPos = []
@@ -39,7 +38,7 @@ class GoToObjectEnv(MiniGridEnv):
         # Until we have generated all the objects
         while len(objs) < self.numObjs:
             objType = self._randElem(types)
-            objColor = self._randElem(colors)
+            objColor = self._randElem(COLOR_NAMES)
 
             # If this object already exists, try again
             if (objType, objColor) in objs:
@@ -52,18 +51,7 @@ class GoToObjectEnv(MiniGridEnv):
             elif objType == 'box':
                 obj = Box(objColor)
 
-            while True:
-                pos = (
-                    self._randInt(1, gridSz - 1),
-                    self._randInt(1, gridSz - 1)
-                )
-                if grid.get(*pos) != None:
-                    continue
-                if pos == self.startPos:
-                    continue
-                grid.set(*pos, obj)
-                break
-
+            pos = self.placeObj(grid, obj, self.startPos)
             objs.append((objType, objColor))
             objPos.append(pos)
 

+ 84 - 0
gym_minigrid/envs/playground_v0.py

@@ -0,0 +1,84 @@
+from gym_minigrid.minigrid import *
+from gym_minigrid.register import register
+
+class PlaygroundV0(MiniGridEnv):
+    """
+    Environment with multiple rooms and random objects.
+    This environment has no specific goals or rewards.
+    """
+
+    def __init__(self):
+        super().__init__(gridSize=19, maxSteps=100)
+        self.reward_range = (0, 1)
+
+    def _genGrid(self, width, height):
+        # Create the grid
+        grid = Grid(width, height)
+
+        # Generate the surrounding walls
+        grid.horzWall(0, 0)
+        grid.horzWall(0, height-1)
+        grid.vertWall(0, 0)
+        grid.vertWall(width-1, 0)
+
+        self.startPos = (width // 2, height // 2)
+        self.startDir = 0
+
+        roomW = width // 3
+        roomH = height // 3
+
+        # NOTE: if we want no room to have two doors of the same color,
+        # that adds difficulty. It's a graph coloring problem
+        # We could generate the door positions first, and do the coloring
+        # in a second pass through rejection sampling.
+
+        # For each row of rooms
+        for j in range(0, 3):
+
+            # For each column
+            for i in range(0, 3):
+                xL = i * roomW
+                yT = j * roomH
+                xR = xL + roomW
+                yB = yT + roomH
+
+                # Bottom wall and door
+                if i+1 < 3:
+                    grid.vertWall(xR, yT, roomH)
+                    pos = (xR, self._randInt(yT+1, yB-1))
+                    color = self._randElem(COLOR_NAMES)
+                    grid.set(*pos, Door(color))
+
+                # Bottom wall and door
+                if j+1 < 3:
+                    grid.horzWall(xL, yB, roomW)
+                    pos = (self._randInt(xL+1, xR-1), yB)
+                    color = self._randElem(COLOR_NAMES)
+                    grid.set(*pos, Door(color))
+
+        # Place random objects in the world
+        types = ['key', 'ball', 'box']
+        for i in range(0, 12):
+            objType = self._randElem(types)
+            objColor = self._randElem(COLOR_NAMES)
+            if objType == 'key':
+                obj = Key(objColor)
+            elif objType == 'ball':
+                obj = Ball(objColor)
+            elif objType == 'box':
+                obj = Box(objColor)
+            self.placeObj(grid, obj, self.startPos)
+
+        # No explicit mission in this environment
+        self.mission = ''
+
+        return grid
+
+    def step(self, action):
+        obs, reward, done, info = MiniGridEnv.step(self, action)
+        return obs, reward, done, info
+
+register(
+    id='MiniGrid-Playground-v0',
+    entry_point='gym_minigrid.envs:PlaygroundV0'
+)

+ 26 - 0
gym_minigrid/minigrid.py

@@ -330,6 +330,18 @@ class Grid:
         assert j >= 0 and j < self.height
         return self.grid[j * self.width + i]
 
+    def horzWall(self, x, y, length=None):
+        if length is None:
+            length = self.width - x
+        for i in range(0, length):
+            self.set(x + i, y, Wall())
+
+    def vertWall(self, x, y, length=None):
+        if length is None:
+            length = self.height - y
+        for j in range(0, length):
+            self.set(x, y + j, Wall())
+
     def rotateLeft(self):
         """
         Rotate the grid to the left (counter-clockwise)
@@ -604,6 +616,20 @@ class MiniGridEnv(gym.Env):
             self.np_random.randint(yLow, yHigh)
         )
 
+    def placeObj(self, grid, obj, excPos=None):
+        while True:
+            pos = (
+                self._randInt(0, grid.width),
+                self._randInt(0, grid.height)
+            )
+            if grid.get(*pos) != None:
+                continue
+            if pos == excPos:
+                continue
+            break
+        grid.set(*pos, obj)
+        return pos
+
     def _randElem(self, iterable):
         lst = list(iterable)
         idx = self._randInt(0, len(lst))

+ 2 - 0
gym_minigrid/rendering.py

@@ -68,6 +68,8 @@ class Window(QMainWindow):
             keyName = 'ALT'
         elif e.key() == Qt.Key_Control:
             keyName = 'CTRL'
+        elif e.key() == Qt.Key_Backspace:
+            keyName = 'BACKSPACE'
         elif e.key() == Qt.Key_Escape:
             keyName = 'ESCAPE'
 

+ 7 - 4
standalone.py

@@ -35,6 +35,13 @@ def main():
     renderer = env.render('human')
 
     def keyDownCb(keyName):
+        if keyName == 'BACKSPACE':
+            resetEnv()
+            return
+
+        if keyName == 'ESCAPE':
+            sys.exit(0)
+
         action = 0
         if keyName == 'LEFT':
             action = env.actions.left
@@ -46,10 +53,6 @@ def main():
             action = env.actions.toggle
         elif keyName == 'CTRL':
             action = env.actions.wait
-        elif keyName == 'RETURN':
-            resetEnv()
-        elif keyName == 'ESCAPE':
-            sys.exit(0)
         else:
             print("unknown key %s" % keyName)
             return