浏览代码

PEP8 changes

saleml 2 年之前
父节点
当前提交
1ce76678e1

+ 1 - 2
gym_minigrid/envs/fetch.py

@@ -100,5 +100,4 @@ register(
     numObjs=2,
     numObjs=2,
 )
 )
 
 
-register(id="MiniGrid-Fetch-8x8-N3-v0",
-         entry_point="gym_minigrid.envs.fetch:FetchEnv")
+register(id="MiniGrid-Fetch-8x8-N3-v0", entry_point="gym_minigrid.envs.fetch:FetchEnv")

+ 1 - 2
gym_minigrid/envs/lockedroom.py

@@ -56,8 +56,7 @@ class LockedRoomEnv(MiniGridEnv):
 
 
             roomW = lWallIdx + 1
             roomW = lWallIdx + 1
             roomH = height // 3 + 1
             roomH = height // 3 + 1
-            self.rooms.append(LockedRoom(
-                (0, j), (roomW, roomH), (lWallIdx, j + 3)))
+            self.rooms.append(LockedRoom((0, j), (roomW, roomH), (lWallIdx, j + 3)))
             self.rooms.append(
             self.rooms.append(
                 LockedRoom((rWallIdx, j), (roomW, roomH), (rWallIdx, j + 3))
                 LockedRoom((rWallIdx, j), (roomW, roomH), (rWallIdx, j + 3))
             )
             )

+ 6 - 12
gym_minigrid/envs/multiroom.py

@@ -37,8 +37,7 @@ class MultiRoomEnv(MiniGridEnv):
         while len(roomList) < numRooms:
         while len(roomList) < numRooms:
             curRoomList = []
             curRoomList = []
 
 
-            entryDoorPos = (self._rand_int(0, width - 2),
-                            self._rand_int(0, width - 2))
+            entryDoorPos = (self._rand_int(0, width - 2), self._rand_int(0, width - 2))
 
 
             # Recursively place the rooms
             # Recursively place the rooms
             self._placeRoom(
             self._placeRoom(
@@ -90,8 +89,7 @@ class MultiRoomEnv(MiniGridEnv):
                 doorColor = self._rand_elem(sorted(doorColors))
                 doorColor = self._rand_elem(sorted(doorColors))
 
 
                 entryDoor = Door(doorColor)
                 entryDoor = Door(doorColor)
-                self.grid.set(
-                    room.entryDoorPos[0], room.entryDoorPos[1], entryDoor)
+                self.grid.set(room.entryDoorPos[0], room.entryDoorPos[1], entryDoor)
                 prevDoorColor = doorColor
                 prevDoorColor = doorColor
 
 
                 prevRoom = roomList[idx - 1]
                 prevRoom = roomList[idx - 1]
@@ -101,8 +99,7 @@ class MultiRoomEnv(MiniGridEnv):
         self.place_agent(roomList[0].top, roomList[0].size)
         self.place_agent(roomList[0].top, roomList[0].size)
 
 
         # Place the final goal in the last room
         # Place the final goal in the last room
-        self.goal_pos = self.place_obj(
-            Goal(), roomList[-1].top, roomList[-1].size)
+        self.goal_pos = self.place_obj(Goal(), roomList[-1].top, roomList[-1].size)
 
 
         self.mission = "traverse the rooms to get to the goal"
         self.mission = "traverse the rooms to get to the goal"
 
 
@@ -156,8 +153,7 @@ class MultiRoomEnv(MiniGridEnv):
                 return False
                 return False
 
 
         # Add this room to the list
         # Add this room to the list
-        roomList.append(
-            MultiRoom((topX, topY), (sizeX, sizeY), entryDoorPos, None))
+        roomList.append(MultiRoom((topX, topY), (sizeX, sizeY), entryDoorPos, None))
 
 
         # If this was the last room, stop
         # If this was the last room, stop
         if numLeft == 1:
         if numLeft == 1:
@@ -175,12 +171,10 @@ class MultiRoomEnv(MiniGridEnv):
             # Pick the exit door position
             # Pick the exit door position
             # Exit on right wall
             # Exit on right wall
             if exitDoorWall == 0:
             if exitDoorWall == 0:
-                exitDoorPos = (topX + sizeX - 1, topY +
-                               self._rand_int(1, sizeY - 1))
+                exitDoorPos = (topX + sizeX - 1, topY + self._rand_int(1, sizeY - 1))
             # Exit on south wall
             # Exit on south wall
             elif exitDoorWall == 1:
             elif exitDoorWall == 1:
-                exitDoorPos = (
-                    topX + self._rand_int(1, sizeX - 1), topY + sizeY - 1)
+                exitDoorPos = (topX + self._rand_int(1, sizeX - 1), topY + sizeY - 1)
             # Exit on left wall
             # Exit on left wall
             elif exitDoorWall == 2:
             elif exitDoorWall == 2:
                 exitDoorPos = (topX, topY + self._rand_int(1, sizeY - 1))
                 exitDoorPos = (topX, topY + self._rand_int(1, sizeY - 1))

+ 4 - 5
gym_minigrid/envs/obstructedmaze.py

@@ -67,8 +67,7 @@ class ObstructedMazeEnv(RoomGrid):
         if blocked:
         if blocked:
             vec = DIR_TO_VEC[door_idx]
             vec = DIR_TO_VEC[door_idx]
             blocking_ball = Ball(self.blocking_ball_color) if blocked else None
             blocking_ball = Ball(self.blocking_ball_color) if blocked else None
-            self.grid.set(door_pos[0] - vec[0],
-                          door_pos[1] - vec[1], blocking_ball)
+            self.grid.set(door_pos[0] - vec[0], door_pos[1] - vec[1], blocking_ball)
 
 
         if locked:
         if locked:
             obj = Key(door.color)
             obj = Key(door.color)
@@ -106,8 +105,7 @@ class ObstructedMaze_1Dlhb(ObstructedMazeEnv):
             blocked=self.blocked,
             blocked=self.blocked,
         )
         )
 
 
-        self.obj, _ = self.add_object(
-            1, 0, "ball", color=self.ball_to_find_color)
+        self.obj, _ = self.add_object(1, 0, "ball", color=self.ball_to_find_color)
         self.place_agent(0, 0)
         self.place_agent(0, 0)
 
 
 
 
@@ -166,7 +164,8 @@ class ObstructedMaze_Full(ObstructedMazeEnv):
         ball_room = self._rand_elem(corners)
         ball_room = self._rand_elem(corners)
 
 
         self.obj, _ = self.add_object(
         self.obj, _ = self.add_object(
-            ball_room[0], ball_room[1], "ball", color=self.ball_to_find_color)
+            ball_room[0], ball_room[1], "ball", color=self.ball_to_find_color
+        )
         self.place_agent(*self.agent_room)
         self.place_agent(*self.agent_room)
 
 
 
 

+ 1 - 2
gym_minigrid/envs/unlock.py

@@ -41,5 +41,4 @@ class UnlockEnv(RoomGrid):
         return obs, reward, terminated, truncated, info
         return obs, reward, terminated, truncated, info
 
 
 
 
-register(id="MiniGrid-Unlock-v0",
-         entry_point="gym_minigrid.envs.unlock:UnlockEnv")
+register(id="MiniGrid-Unlock-v0", entry_point="gym_minigrid.envs.unlock:UnlockEnv")

+ 15 - 26
gym_minigrid/minigrid.py

@@ -1,12 +1,13 @@
 import hashlib
 import hashlib
 import math
 import math
 import string
 import string
+from abc import abstractmethod
 from enum import IntEnum
 from enum import IntEnum
 
 
 import gym
 import gym
 import numpy as np
 import numpy as np
 from gym import spaces
 from gym import spaces
-from abc import abstractmethod
+
 # Size in pixels of a tile in the full-scale human view
 # Size in pixels of a tile in the full-scale human view
 from gym_minigrid.rendering import (
 from gym_minigrid.rendering import (
     downsample,
     downsample,
@@ -34,8 +35,7 @@ COLORS = {
 COLOR_NAMES = sorted(list(COLORS.keys()))
 COLOR_NAMES = sorted(list(COLORS.keys()))
 
 
 # Used to map colors to integers
 # Used to map colors to integers
-COLOR_TO_IDX = {"red": 0, "green": 1, "blue": 2,
-                "purple": 3, "yellow": 4, "grey": 5}
+COLOR_TO_IDX = {"red": 0, "green": 1, "blue": 2, "purple": 3, "yellow": 4, "grey": 5}
 
 
 IDX_TO_COLOR = dict(zip(COLOR_TO_IDX.values(), COLOR_TO_IDX.keys()))
 IDX_TO_COLOR = dict(zip(COLOR_TO_IDX.values(), COLOR_TO_IDX.keys()))
 
 
@@ -203,14 +203,10 @@ class Lava(WorldObj):
         for i in range(3):
         for i in range(3):
             ylo = 0.3 + 0.2 * i
             ylo = 0.3 + 0.2 * i
             yhi = 0.4 + 0.2 * i
             yhi = 0.4 + 0.2 * i
-            fill_coords(img, point_in_line(
-                0.1, ylo, 0.3, yhi, r=0.03), (0, 0, 0))
-            fill_coords(img, point_in_line(
-                0.3, yhi, 0.5, ylo, r=0.03), (0, 0, 0))
-            fill_coords(img, point_in_line(
-                0.5, ylo, 0.7, yhi, r=0.03), (0, 0, 0))
-            fill_coords(img, point_in_line(
-                0.7, yhi, 0.9, ylo, r=0.03), (0, 0, 0))
+            fill_coords(img, point_in_line(0.1, ylo, 0.3, yhi, r=0.03), (0, 0, 0))
+            fill_coords(img, point_in_line(0.3, yhi, 0.5, ylo, r=0.03), (0, 0, 0))
+            fill_coords(img, point_in_line(0.5, ylo, 0.7, yhi, r=0.03), (0, 0, 0))
+            fill_coords(img, point_in_line(0.7, yhi, 0.9, ylo, r=0.03), (0, 0, 0))
 
 
 
 
 class Wall(WorldObj):
 class Wall(WorldObj):
@@ -273,8 +269,7 @@ class Door(WorldObj):
         # Door frame and door
         # Door frame and door
         if self.is_locked:
         if self.is_locked:
             fill_coords(img, point_in_rect(0.00, 1.00, 0.00, 1.00), c)
             fill_coords(img, point_in_rect(0.00, 1.00, 0.00, 1.00), c)
-            fill_coords(img, point_in_rect(
-                0.06, 0.94, 0.06, 0.94), 0.45 * np.array(c))
+            fill_coords(img, point_in_rect(0.06, 0.94, 0.06, 0.94), 0.45 * np.array(c))
 
 
             # Draw key slot
             # Draw key slot
             fill_coords(img, point_in_rect(0.52, 0.75, 0.50, 0.56), c)
             fill_coords(img, point_in_rect(0.52, 0.75, 0.50, 0.56), c)
@@ -491,8 +486,7 @@ class Grid:
             )
             )
 
 
             # Rotate the agent based on its direction
             # Rotate the agent based on its direction
-            tri_fn = rotate_fn(tri_fn, cx=0.5, cy=0.5,
-                               theta=0.5 * math.pi * agent_dir)
+            tri_fn = rotate_fn(tri_fn, cx=0.5, cy=0.5, theta=0.5 * math.pi * agent_dir)
             fill_coords(img, tri_fn, (255, 0, 0))
             fill_coords(img, tri_fn, (255, 0, 0))
 
 
         # Highlight the cell if needed
         # Highlight the cell if needed
@@ -515,8 +509,7 @@ class Grid:
         """
         """
 
 
         if highlight_mask is None:
         if highlight_mask is None:
-            highlight_mask = np.zeros(
-                shape=(self.width, self.height), dtype=bool)
+            highlight_mask = np.zeros(shape=(self.width, self.height), dtype=bool)
 
 
         # Compute the total grid size
         # Compute the total grid size
         width_px = self.width * tile_size
         width_px = self.width * tile_size
@@ -768,8 +761,7 @@ class MiniGridEnv(gym.Env):
         """
         """
         sample_hash = hashlib.sha256()
         sample_hash = hashlib.sha256()
 
 
-        to_encode = [self.grid.encode().tolist(), self.agent_pos,
-                     self.agent_dir]
+        to_encode = [self.grid.encode().tolist(), self.agent_pos, self.agent_dir]
         for item in to_encode:
         for item in to_encode:
             sample_hash.update(str(item).encode("utf8"))
             sample_hash.update(str(item).encode("utf8"))
 
 
@@ -936,10 +928,8 @@ class MiniGridEnv(gym.Env):
 
 
             pos = np.array(
             pos = np.array(
                 (
                 (
-                    self._rand_int(top[0], min(
-                        top[0] + size[0], self.grid.width)),
-                    self._rand_int(top[1], min(
-                        top[1] + size[1], self.grid.height)),
+                    self._rand_int(top[0], min(top[0] + size[0], self.grid.width)),
+                    self._rand_int(top[1], min(top[1] + size[1], self.grid.height)),
                 )
                 )
             )
             )
 
 
@@ -1173,7 +1163,7 @@ class MiniGridEnv(gym.Env):
             pass
             pass
 
 
         else:
         else:
-            raise ValueError('Unknown action: {}'.format(action))
+            raise ValueError(f"Unknown action: {action}")
 
 
         if self.step_count >= self.max_steps:
         if self.step_count >= self.max_steps:
             truncated = True
             truncated = True
@@ -1233,8 +1223,7 @@ class MiniGridEnv(gym.Env):
         # - an image (partially observable view of the environment)
         # - an image (partially observable view of the environment)
         # - the agent's direction/orientation (acting as a compass)
         # - the agent's direction/orientation (acting as a compass)
         # - a textual mission string (instructions for the agent)
         # - a textual mission string (instructions for the agent)
-        obs = {"image": image, "direction": self.agent_dir,
-               "mission": self.mission}
+        obs = {"image": image, "direction": self.agent_dir, "mission": self.mission}
 
 
         return obs
         return obs
 
 

+ 5 - 6
run_tests.py

@@ -133,11 +133,9 @@ for env_idx, env_name in enumerate(env_list):
     env.close()
     env.close()
 
 
     # Test the wrappers return proper observation spaces.
     # Test the wrappers return proper observation spaces.
-    wrappers = [RGBImgObsWrapper,
-                RGBImgPartialObsWrapper, OneHotPartialObsWrapper]
+    wrappers = [RGBImgObsWrapper, RGBImgPartialObsWrapper, OneHotPartialObsWrapper]
     for wrapper in wrappers:
     for wrapper in wrappers:
-        env = wrapper(
-            gym.make(env_name, render_mode="rgb_array", new_step_api=True))
+        env = wrapper(gym.make(env_name, render_mode="rgb_array", new_step_api=True))
         obs_space, wrapper_name = env.observation_space, wrapper.__name__
         obs_space, wrapper_name = env.observation_space, wrapper.__name__
         assert isinstance(
         assert isinstance(
             obs_space, spaces.Dict
             obs_space, spaces.Dict
@@ -186,8 +184,9 @@ wrappers = [
 ]
 ]
 for wrapper in wrappers:
 for wrapper in wrappers:
     env1 = wrapper(EmptyEnvWithExtraObs(render_mode="rgb_array"))
     env1 = wrapper(EmptyEnvWithExtraObs(render_mode="rgb_array"))
-    env2 = wrapper(gym.make("MiniGrid-Empty-5x5-v0",
-                   render_mode="rgb_array", new_step_api=True))
+    env2 = wrapper(
+        gym.make("MiniGrid-Empty-5x5-v0", render_mode="rgb_array", new_step_api=True)
+    )
 
 
     obs1 = env1.reset(seed=0)
     obs1 = env1.reset(seed=0)
     obs2 = env2.reset(seed=0)
     obs2 = env2.reset(seed=0)