Skip to content

Commit

Permalink
Merge pull request #65 from ll7/autopep8/2024-09-27
Browse files Browse the repository at this point in the history
Autopep8/2024-09-27
  • Loading branch information
ll7 authored Sep 27, 2024
2 parents fd3bb1d + e3bddb4 commit d0d0717
Show file tree
Hide file tree
Showing 69 changed files with 639 additions and 478 deletions.
13 changes: 8 additions & 5 deletions SLURM/log_gpu_cpu_usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from robot_sf.feature_extractor import DynamicsExtractor
from robot_sf.tb_logging import DrivingMetricsCallback


class LogResourceUsageCallback(BaseCallback):
"""Custom callback to log CPU and GPU usage to TensorBoard."""

Expand All @@ -32,6 +33,7 @@ def _on_step(self) -> bool:

return True


def training(
n_envs: int = os.cpu_count(),
ped_densities: list[float] = None,
Expand All @@ -46,6 +48,7 @@ def training(
logger.info(f"Number of CPUs: {n_envs}")
if ped_densities is None:
ped_densities = [0.01, 0.02, 0.04, 0.08]

def make_env():
config = EnvSettings()
config.sim_config.ped_density_by_difficulty = ped_densities
Expand All @@ -60,29 +63,29 @@ def make_env():
env,
tensorboard_log="./logs/ppo_logs/",
policy_kwargs=policy_kwargs
)
)
save_model_callback = CheckpointCallback(
500_000 // n_envs,
"./model/backup",
"ppo_model"
)
)
collect_metrics_callback = DrivingMetricsCallback(n_envs)
combined_callback = CallbackList(
[save_model_callback, collect_metrics_callback, LogResourceUsageCallback()]
)
)

logger.info("Start learning")

model.learn(
total_timesteps=1_000_000,
progress_bar=True,
callback=combined_callback
)

)

logger.info("Save model")
model.save("./model/ppo_model")


if __name__ == '__main__':
logger.info(f"Python path: {sys.executable}")
logger.info(f"Python version: {sys.version}")
Expand Down
1 change: 0 additions & 1 deletion SLURM/slurm_PPO_robot_sf.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ def training():
ped_densities = [0.01, 0.02, 0.04, 0.08]
difficulty = 2


def make_env():
config = EnvSettings()
config.sim_config.ped_density_by_difficulty = ped_densities
Expand Down
94 changes: 47 additions & 47 deletions examples/demo_defensive.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from robot_sf.sim.sim_config import SimulationSettings
from robot_sf.robot.differential_drive import DifferentialDriveSettings


def run_simulation():
"""
Run a simulation using a pre-trained PPO model in a robot environment.
Expand Down Expand Up @@ -63,53 +64,52 @@ def obs_adapter(orig_obs):


def prepare_gym_spaces():
obs_low = np.array([ 0., 0., 0., 0. , 0., 0.,
0., 0., 0., 0. , 0., 0.,
0., 0., 0., 0. , 0., 0.,
0., 0., 0., 0. , 0., 0.,
0., 0., 0., 0. , 0., 0.,
0., 0., 0., 0. , 0., 0.,
0., 0., 0., 0. , 0., 0.,
0., 0., 0., 0. , 0., 0.,
0., 0., 0., 0. , 0., 0.,
0., 0., 0., 0. , 0., 0.,
0., 0., 0., 0. , 0., 0.,
0., 0., 0., 0. , 0., 0.,
0., 0., 0., 0. , 0., 0.,
0., 0., 0., 0. , 0., 0.,
0., 0., 0., 0. , 0., 0.,
0., 0., 0., 0. , 0., 0.,
0., 0., 0., 0. , 0., 0.,
0., 0., 0., 0. , 0., 0.,
0., 0., 0., 0. , 0., 0.,
0., 0., 0., 0. , 0., 0.,
0., 0., 0., 0. , 0., 0.,
0., 0., 0., 0. , 0., 0.,
0., 0., 0., 0. , 0., 0.,
0., 0., 0., 0. , 0., 0.,
0., 0., 0., 0. , 0., 0.,
0., 0., 0., 0. , 0., 0.,
0., 0., 0., 0. , 0., 0.,
0., 0., 0., 0. , 0., 0.,
0., 0., 0., 0. , 0., 0.,
0., 0., 0., 0. , 0., 0.,
0., 0., 0., 0. , 0., 0.,
0., 0., 0., 0. , 0., 0.,
0., 0., 0., 0. , 0., 0.,
0., 0., 0., 0. , 0., 0.,
0., 0., 0., 0. , 0., 0.,
0., 0., 0., 0. , 0., 0.,
0., 0., 0., 0. , 0., 0.,
0., 0., 0., 0. , 0., 0.,
0., 0., 0., 0. , 0., 0.,
0., 0., 0., 0. , 0., 0.,
0., 0., 0., 0. , 0., 0.,
0., 0., 0., 0. , 0., 0.,
0., 0., 0., 0. , 0., 0.,
0., 0., 0., 0. , 0., 0.,
0., 0., 0., 0. , 0., 0.,
0., 0., 0., -0.5, 0., -3.14159265])

obs_low = np.array([0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0.,
0., 0., 0., -0.5, 0., -3.14159265])

obs_high = np.array([1.00000000e+01, 1.00000000e+01, 1.00000000e+01, 1.00000000e+01,
1.00000000e+01, 1.00000000e+01, 1.00000000e+01, 1.00000000e+01,
Expand Down
8 changes: 5 additions & 3 deletions examples/demo_pedestrian.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

logger = loguru.logger


def make_env(map_name: str, robot_model: str):
ped_densities = [0.01, 0.02, 0.04, 0.08]
difficulty = 2
Expand All @@ -21,9 +22,9 @@ def make_env(map_name: str, robot_model: str):
env_config = PedEnvSettings(
map_pool=MapDefinitionPool(map_defs={"my_map": map_definition}),
sim_config=SimulationSettings(difficulty=difficulty,
ped_density_by_difficulty=ped_densities),
ped_density_by_difficulty=ped_densities),
robot_config=BicycleDriveSettings(radius=0.5, max_accel=3.0, allow_backwards=True)
)
)
return PedestrianEnv(env_config, robot_model=robot_model, debug=True, recording_enabled=False)


Expand All @@ -38,14 +39,15 @@ def run(filename: str, map_name: str, robot_model: str):

for _ in range(10000):
action, _ = model.predict(obs, deterministic=True)
obs, _, done, _ , _ = env.step(action)
obs, _, done, _, _ = env.step(action)
env.render()

if done:
obs, _ = env.reset()
env.render()
env.exit()


if __name__ == '__main__':
SVG_MAP = "maps/svg_maps/debug_06.svg"
PED_MODEL = "./model_ped/ppo_ped_02.zip"
Expand Down
9 changes: 4 additions & 5 deletions examples/ego_ped_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,17 @@
from robot_sf.nav.svg_map_parser import convert_map




logger.info("Simulate the trained robot and a pedestrian with a random policy.")


def test_simulation(map_definition: MapDefinition):

logger.info("Creating the environment.")
env_config = PedEnvSettings(
map_pool=MapDefinitionPool(map_defs={"my_map": map_definition}),
sim_config=SimulationSettings(difficulty=0, ped_density_by_difficulty=[0.02]),
robot_config=BicycleDriveSettings(radius=0.5, max_accel=3.0, allow_backwards=True)
)
)

robot_model = PPO.load("./model/run_043", env=None)

Expand All @@ -36,7 +35,7 @@ def test_simulation(map_definition: MapDefinition):
logger.info("Simulating the random policy.")
for _ in range(10000):
action_ped = env.action_space.sample()
obs, _, done, _ , _= env.step(action_ped)
obs, _, done, _, _ = env.step(action_ped)
env.render()

if done:
Expand All @@ -46,6 +45,7 @@ def test_simulation(map_definition: MapDefinition):
env.reset()
env.exit()


def get_file():
"""Get the latest recorded file."""

Expand All @@ -54,7 +54,6 @@ def get_file():
return Path('recordings', filename)



def main():

map_def = convert_map("maps/svg_maps/debug_03.svg")
Expand Down
9 changes: 5 additions & 4 deletions examples/simulate_with_svg_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@
from robot_sf.robot.differential_drive import DifferentialDriveSettings




logger.info("Simulate a random policy with a map defined in SVG format.")


def test_simulation(map_definition: MapDefinition):
"""Test the simulation with a random policy."""

Expand Down Expand Up @@ -54,12 +53,13 @@ def obs_adapter(orig_obs):
env.render()
env.exit()


def main():
"""Simulate a random policy with a map defined in SVG format."""
logger.info("Simulating a random policy with the map.")

#svg_file = "maps/svg_maps/02_simple_maps.svg"
#svg_file = "maps/svg_maps/03_mid_object.svg"
# svg_file = "maps/svg_maps/02_simple_maps.svg"
# svg_file = "maps/svg_maps/03_mid_object.svg"
svg_file = "maps/svg_maps/04_small_mid_object.svg"

logger.info("Converting SVG map to MapDefinition object.")
Expand All @@ -72,5 +72,6 @@ def main():

logger.info("MapDefinition object created.")


if __name__ == "__main__":
main()
6 changes: 3 additions & 3 deletions examples/view_recording.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@
from robot_sf.render.playback_recording import load_states_and_visualize




logger.info("Simulate a random policy with a map defined in SVG format.")


def test_simulation(map_definition: MapDefinition):
"""Test the simulation with a random policy."""

Expand All @@ -38,6 +37,7 @@ def test_simulation(map_definition: MapDefinition):
env.reset() # Save the recording
env.exit()


def convert_map(svg_file: str):
"""Create MapDefinition from svg file."""

Expand All @@ -47,6 +47,7 @@ def convert_map(svg_file: str):
converter = SvgMapConverter(svg_file)
return converter.map_definition


def get_file():
"""Get the latest recorded file."""

Expand All @@ -55,7 +56,6 @@ def get_file():
return Path('recordings', filename)



def main():
"""Simulate a random policy with a map defined in SVG format and view the recording."""

Expand Down
Loading

0 comments on commit d0d0717

Please sign in to comment.