Pārlūkot izejas kodu

Split toggle into pickup/drop/toggle actions

This change is made to allow eventual support for having the
agent carry more than one object at once.
Maxime Chevalier-Boisvert 7 gadi atpakaļ
vecāks
revīzija
63289b521c
4 mainītis faili ar 49 papildinājumiem un 22 dzēšanām
  1. 3 1
      README.md
  2. 34 21
      gym_minigrid/minigrid.py
  3. 4 0
      gym_minigrid/rendering.py
  4. 8 0
      standalone.py

+ 3 - 1
README.md

@@ -95,7 +95,9 @@ Actions in the basic environment:
 - Turn left
 - Turn right
 - Move forward
-- Toggle (pick up or interact with objects)
+- Pick up an object
+- Drop the object being carried
+- Toggle (interact with objects)
 - Wait (noop, do nothing)
 
 By default, sparse rewards for reaching a goal square are provided, but you can

+ 34 - 21
gym_minigrid/minigrid.py

@@ -537,10 +537,16 @@ class MiniGridEnv(gym.Env):
         left = 0
         right = 1
         forward = 2
-        # Toggle/pick up/activate object
-        toggle = 3
+
+        # Pick up an object
+        pickup = 3
+        # Drop an object
+        drop = 4
+        # Toggle/activate an object
+        toggle = 5
+
         # Wait/stay put/do nothing
-        wait = 4
+        wait = 6
 
     def __init__(self, gridSize=16, maxSteps=100):
         # Action enumeration for this environment
@@ -745,6 +751,13 @@ class MiniGridEnv(gym.Env):
         reward = 0
         done = False
 
+        # Get the position in front of the agent
+        u, v = self.getDirVec()
+        fwdPos = (self.agentPos[0] + u, self.agentPos[1] + v)
+
+        # Get the contents of the cell in front of the agent
+        fwdCell = self.grid.get(*fwdPos)
+
         # Rotate left
         if action == self.actions.left:
             self.agentDir -= 1
@@ -757,30 +770,30 @@ class MiniGridEnv(gym.Env):
 
         # Move forward
         elif action == self.actions.forward:
-            u, v = self.getDirVec()
-            newPos = (self.agentPos[0] + u, self.agentPos[1] + v)
-            targetCell = self.grid.get(newPos[0], newPos[1])
-            if targetCell == None or targetCell.canOverlap():
-                self.agentPos = newPos
-            if targetCell != None and targetCell.type == 'goal':
+            if fwdCell == None or fwdCell.canOverlap():
+                self.agentPos = fwdPos
+            if fwdCell != None and fwdCell.type == 'goal':
                 done = True
                 reward = 1000 - self.stepCount
 
-        # Pick up or trigger/activate an item
-        elif action == self.actions.toggle:
-            u, v = self.getDirVec()
-            objPos = (self.agentPos[0] + u, self.agentPos[1] + v)
-            cell = self.grid.get(*objPos)
-            if cell and cell.canPickup():
+        # Pick up an object
+        elif action == self.actions.pickup:
+            if fwdCell and fwdCell.canPickup():
                 if self.carrying is None:
-                    self.carrying = cell
-                    self.grid.set(*objPos, None)
-            elif cell:
-                cell.toggle(self, objPos)
-            elif self.carrying:
-                self.grid.set(*objPos, self.carrying)
+                    self.carrying = fwdCell
+                    self.grid.set(*fwdPos, None)
+
+        # Drop an object
+        elif action == self.actions.drop:
+            if not fwdCell and self.carrying:
+                self.grid.set(*fwdPos, self.carrying)
                 self.carrying = None
 
+        # Toggle/activate an object
+        elif action == self.actions.toggle:
+            if fwdCell:
+                fwdCell.toggle(self, fwdPos)
+
         # Wait/do nothing
         elif action == self.actions.wait:
             pass

+ 4 - 0
gym_minigrid/rendering.py

@@ -68,6 +68,10 @@ class Window(QMainWindow):
             keyName = 'ALT'
         elif e.key() == Qt.Key_Control:
             keyName = 'CTRL'
+        elif e.key() == Qt.Key_PageUp:
+            keyName = 'PAGE_UP'
+        elif e.key() == Qt.Key_PageDown:
+            keyName = 'PAGE_DOWN'
         elif e.key() == Qt.Key_Backspace:
             keyName = 'BACKSPACE'
         elif e.key() == Qt.Key_Escape:

+ 8 - 0
standalone.py

@@ -43,16 +43,24 @@ def main():
             sys.exit(0)
 
         action = 0
+
         if keyName == 'LEFT':
             action = env.actions.left
         elif keyName == 'RIGHT':
             action = env.actions.right
         elif keyName == 'UP':
             action = env.actions.forward
+
         elif keyName == 'SPACE':
             action = env.actions.toggle
+        elif keyName == 'PAGE_UP':
+            action = env.actions.pickup
+        elif keyName == 'PAGE_DOWN':
+            action = env.actions.drop
+
         elif keyName == 'CTRL':
             action = env.actions.wait
+
         else:
             print("unknown key %s" % keyName)
             return