From 657c97fdf73939672c7ee6e6b7d7a40223af4191 Mon Sep 17 00:00:00 2001 From: Julius Miller Date: Thu, 26 Sep 2024 16:51:33 +0200 Subject: [PATCH] Docs and linter update --- examples/demo_pedestrian.py | 2 -- robot_sf/gym_env/reward.py | 5 +++++ robot_sf/nav/occupancy.py | 29 +++++++++++++++++++++++++++++ robot_sf/render/sim_view.py | 19 +++++++++---------- robot_sf/sim/simulator.py | 9 ++++++++- scripts/debug_ped_discrete.py | 3 --- 6 files changed, 51 insertions(+), 16 deletions(-) diff --git a/examples/demo_pedestrian.py b/examples/demo_pedestrian.py index 2760501..e23d27d 100644 --- a/examples/demo_pedestrian.py +++ b/examples/demo_pedestrian.py @@ -1,6 +1,4 @@ """Simulate the trained robot and a trained pedestrian.""" -import os -from pathlib import Path from stable_baselines3 import PPO import loguru diff --git a/robot_sf/gym_env/reward.py b/robot_sf/gym_env/reward.py index a4f1223..4e540c4 100644 --- a/robot_sf/gym_env/reward.py +++ b/robot_sf/gym_env/reward.py @@ -52,6 +52,10 @@ def simple_ped_reward(meta: dict, max_episode_step_discount: float=-0.1, Parameters: meta (dict): Metadata containing information about the pedestrian's current state. max_episode_step_discount (float): Discount factor for each step in the episode. + ped_coll_penalty (float): Penalty for colliding with a pedestrian. + obst_coll_penalty (float): Penalty for colliding with an obstacle. + robot_coll_reward (float): Reward for colliding with a robot. + robot_at_goal_penalty (float): Penalty if the robot reaches his goal. Returns: float: The calculated reward. @@ -76,6 +80,7 @@ def simple_ped_reward(meta: dict, max_episode_step_discount: float=-0.1, if meta["is_robot_collision"]: reward += robot_coll_reward + # If the robot has reached its goal, apply penalty if meta["is_robot_at_goal"]: reward += robot_at_goal_penalty diff --git a/robot_sf/nav/occupancy.py b/robot_sf/nav/occupancy.py index 860fb1d..6dc0c68 100644 --- a/robot_sf/nav/occupancy.py +++ b/robot_sf/nav/occupancy.py @@ -232,6 +232,35 @@ def is_in_bounds(self, world_x: float, world_y: float) -> bool: @dataclass class EgoPedContinuousOccupancy(ContinuousOccupancy): + """ + A class, which extends the continuous occupancy for the ego pedestrian. + + Attributes + ---------- + width : float + The width of the occupancy. + height : float + The height of the occupancy. + get_agent_coords : Callable[[], Vec2D] + A function to get the agent coordinates. + get_goal_coords : Callable[[], Vec2D] + A function to get the goal coordinates. + get_obstacle_coords : Callable[[], np.ndarray] + A function to get the obstacle coordinates. + get_pedestrian_coords : Callable[[], np.ndarray] + A function to get the pedestrian coordinates. + agent_radius : float, optional + The robot radius, by default 1.0. + ped_radius : float, optional + The pedestrian radius, by default 0.4. + goal_radius : float, optional + The goal radius, by default 1.0. + get_enemy_coords : Optional[Callable[[], Vec2D]] + The coordinates of the opposing agent. + enemy_radius : optional, float=1.0 + The radius of the opposing agent. + """ + get_enemy_coords: Optional[Callable[[], Vec2D]] = None enemy_radius: float=1.0 diff --git a/robot_sf/render/sim_view.py b/robot_sf/render/sim_view.py index 424dde9..217c527 100644 --- a/robot_sf/render/sim_view.py +++ b/robot_sf/render/sim_view.py @@ -234,7 +234,7 @@ def render(self, state: VisualizableSimState): # static objects if self.map_def.obstacles: self._draw_obstacles() - + # debugging objects # if self.map_def.robot_routes: # self._draw_robot_routes() @@ -296,7 +296,7 @@ def _draw_robot(self, pose: RobotPose): ROBOT_COLOR, self._scale_tuple(pose[0]), self.robot_radius * self.scaling) - + def _draw_ego_ped(self, pose: PedPose): # TODO: display robot with an image instead of a circle pygame.draw.circle( @@ -468,16 +468,16 @@ def _add_text(self, timestep: int, state: VisualizableSimState): for i, text in enumerate(text_lines): text_render = self.font.render(text, True, TEXT_COLOR) text_outline = self.font.render(text, True, TEXT_OUTLINE_COLOR) - + pos = (5, i * self.font.get_linesize() + 5) - + # Draw text outline for dx, dy in [(-1, -1), (-1, 1), (1, -1), (1, 1)]: text_surface.blit(text_outline, (pos[0] + dx, pos[1] + dy)) - + # Draw main text text_surface.blit(text_render, pos) - + self.screen.blit(text_surface, self._timestep_text_pos) def _add_help_text(self): @@ -496,7 +496,6 @@ def _add_help_text(self): # Determine max width of the text text_surface = self.font.render(text_lines[1], False, TEXT_COLOR) - width = text_surface.get_width() + 10 max_width = max(self.font.size(line)[0] for line in text_lines) text_height = len(text_lines) * self.font.get_linesize() @@ -506,13 +505,13 @@ def _add_help_text(self): for i, text in enumerate(text_lines): text_render = self.font.render(text, True, TEXT_COLOR) text_outline = self.font.render(text, True, TEXT_OUTLINE_COLOR) - + pos = (5, i * self.font.get_linesize() + 5) - + # Draw text outline for dx, dy in [(-1, -1), (-1, 1), (1, -1), (1, 1)]: text_surface.blit(text_outline, (pos[0] + dx, pos[1] + dy)) - + # Draw main text text_surface.blit(text_render, pos) diff --git a/robot_sf/sim/simulator.py b/robot_sf/sim/simulator.py index eb69c58..7cefbf6 100644 --- a/robot_sf/sim/simulator.py +++ b/robot_sf/sim/simulator.py @@ -177,6 +177,13 @@ def init_simulators( @dataclass class PedSimulator(Simulator): + """ + A pedestrian simulator, whic extends the base simulator. + + Args: + ego_ped (UnicycleDrivePedestrian): The ego pedestrian in the environment. + + """ ego_ped: UnicycleDrivePedestrian @property @@ -246,7 +253,7 @@ def get_proximity_point(self, fixed_point: Tuple[float, float], def is_obstacle_collision(self, x: float, y: float) -> bool: """ - TODO: copy from occupancy.py + TODO: this method is copied from occupancy.py """ if not (0 <= x <= self.map_def.width and 0 <= y <= self.map_def.height): return True diff --git a/scripts/debug_ped_discrete.py b/scripts/debug_ped_discrete.py index a2f8a86..513197d 100644 --- a/scripts/debug_ped_discrete.py +++ b/scripts/debug_ped_discrete.py @@ -1,7 +1,4 @@ """Simulate a hardcoded deterministic policy with four actions.""" -import os -from pathlib import Path -from math import pi from stable_baselines3 import PPO import loguru