From b3b4951d23cbd16e6a2ca6b7e2626ef1a79bc1af Mon Sep 17 00:00:00 2001 From: cbampeta Date: Sat, 22 Nov 2025 02:24:17 +0100 Subject: [PATCH 1/6] Feat: Change the pipe organization --- .../controllerWorldSupervisor.py | 10 ++--- .../launch_train_multiprocessing.py | 37 ++++++++++--------- src/Simulateur/worlds/.piste0.wbproj | 2 +- src/Simulateur/worlds/.piste1.wbproj | 2 +- src/Simulateur/worlds/.piste2.wbproj | 2 +- 5 files changed, 27 insertions(+), 26 deletions(-) diff --git a/src/Simulateur/controllers/controllerWorldSupervisor/controllerWorldSupervisor.py b/src/Simulateur/controllers/controllerWorldSupervisor/controllerWorldSupervisor.py index 272fa25f..4b3fdadf 100644 --- a/src/Simulateur/controllers/controllerWorldSupervisor/controllerWorldSupervisor.py +++ b/src/Simulateur/controllers/controllerWorldSupervisor/controllerWorldSupervisor.py @@ -112,11 +112,11 @@ def __init__(self, vehicle_rank: int): self.receiver.setChannel(2 * self.vehicle_rank + 1) - log(f"CLIENT{simulation_rank}/{vehicle_rank} : begins init") - log(f"CLIENT{simulation_rank}/{vehicle_rank} : {simulation_rank}toserver.pipe") - self.fifo_w = open(f"/tmp/autotech/{simulation_rank}toserver.pipe", "wb") - log(f"CLIENT{simulation_rank}/{vehicle_rank} : serverto{simulation_rank}.pipe") - self.fifo_r = open(f"/tmp/autotech/serverto{simulation_rank}.pipe", "rb") + log(f"CLIENT{simulation_rank}/{vehicle_rank}_{vehicle_rank} : begins init") + log(f"CLIENT{simulation_rank}/{vehicle_rank}_{vehicle_rank} : {simulation_rank}_{vehicle_rank}toserver.pipe") + self.fifo_w = open(f"/tmp/autotech/{simulation_rank}_{vehicle_rank}toserver.pipe", "wb") + log(f"CLIENT{simulation_rank}/{vehicle_rank} : serverto{simulation_rank}_{vehicle_rank}.pipe") + self.fifo_r = open(f"/tmp/autotech/serverto{simulation_rank}_{vehicle_rank}.pipe", "rb") # Last data received from the car self.last_data = np.zeros(n_sensors + lidar_horizontal_resolution + camera_horizontal_resolution, dtype=np.float32) diff --git a/src/Simulateur/launch_train_multiprocessing.py b/src/Simulateur/launch_train_multiprocessing.py index f0381c65..23fee8e6 100644 --- a/src/Simulateur/launch_train_multiprocessing.py +++ b/src/Simulateur/launch_train_multiprocessing.py @@ -36,9 +36,10 @@ class WebotsSimulationGymEnvironment(gym.Env): supervisor: the supervisor of the simulation """ - def __init__(self, simulation_rank: int): + def __init__(self, simulation_rank: int, vehicle_rank: int): super().__init__() self.simulation_rank = simulation_rank + self.vehicle_rank = vehicle_rank # this is only true if lidar_horizontal_resolution = camera_horizontal_resolution box_min = np.zeros([2, context_size, lidar_horizontal_resolution], dtype=np.float32) @@ -50,21 +51,21 @@ def __init__(self, simulation_rank: int): if not os.path.exists("/tmp/autotech"): os.mkdir("/tmp/autotech") - log(f"SERVER{simulation_rank} : {simulation_rank=}") + log(f"SERVER{simulation_rank}_{vehicle_rank} : {simulation_rank}_{vehicle_rank}") - os.mkfifo(f"/tmp/autotech/{simulation_rank}toserver.pipe") - os.mkfifo(f"/tmp/autotech/serverto{simulation_rank}.pipe") + os.mkfifo(f"/tmp/autotech/{simulation_rank}_{vehicle_rank}toserver.pipe") + os.mkfifo(f"/tmp/autotech/serverto{simulation_rank}_{vehicle_rank}.pipe") # --mode=fast --minimize --no-rendering --batch --stdout os.system(f""" webots {__file__.rsplit('/', 1)[0]}/worlds/piste{simulation_rank % n_map}.wbt --mode=fast --minimize --no-rendering --batch --stdout & - echo $! {simulation_rank} >>/tmp/autotech/simulationranks + echo $! {simulation_rank}_{vehicle_rank} >>/tmp/autotech/simulationranks """) - log(f"SERVER{simulation_rank} : {simulation_rank}toserver.pipe") - self.fifo_r = open(f"/tmp/autotech/{simulation_rank}toserver.pipe", "rb") - log(f"SERVER{simulation_rank} : serverto{simulation_rank}.pipe") - self.fifo_w = open(f"/tmp/autotech/serverto{simulation_rank}.pipe", "wb") - log(f"SERVER{simulation_rank} : fifo opened :D and init done") + log(f"SERVER{simulation_rank}_{vehicle_rank} : {simulation_rank}_{vehicle_rank}toserver.pipe") + self.fifo_r = open(f"/tmp/autotech/{simulation_rank}_{vehicle_rank}toserver.pipe", "rb") + log(f"SERVER{simulation_rank}_{vehicle_rank} : serverto{simulation_rank}_{vehicle_rank}.pipe") + self.fifo_w = open(f"/tmp/autotech/serverto{simulation_rank}_{vehicle_rank}.pipe", "wb") + log(f"SERVER{simulation_rank}_{vehicle_rank} : fifo opened :D and init done") log("-------------------------------------------------------------------") def reset(self, seed=0): @@ -77,19 +78,19 @@ def reset(self, seed=0): return obs, info def step(self, action): - log(f"SERVER{self.simulation_rank} : sending {action=}") + log(f"SERVER{self.simulation_rank}_{self.vehicle_rank} : sending {action=}") self.fifo_w.write(action.tobytes()) self.fifo_w.flush() # communication with the supervisor cur_state = np.frombuffer(self.fifo_r.read(np.dtype(np.float32).itemsize * (n_sensors + lidar_horizontal_resolution + camera_horizontal_resolution)), dtype=np.float32) - log(f"SERVER{self.simulation_rank} : received {cur_state=}") + log(f"SERVER{self.simulation_rank}_{self.vehicle_rank} : received {cur_state=}") reward = np.frombuffer(self.fifo_r.read(np.dtype(np.float32).itemsize), dtype=np.float32)[0] # scalar - log(f"SERVER{self.simulation_rank} : received {reward=}") + log(f"SERVER{self.simulation_rank}_{self.vehicle_rank} : received {reward=}") done = np.frombuffer(self.fifo_r.read(np.dtype(np.bool).itemsize), dtype=np.bool)[0] # scalar - log(f"SERVER{self.simulation_rank} : received {done=}") + log(f"SERVER{self.simulation_rank}_{self.vehicle_rank} : received {done=}") truncated = np.frombuffer(self.fifo_r.read(np.dtype(np.bool).itemsize), dtype=np.bool)[0] # scalar - log(f"SERVER{self.simulation_rank} : received {truncated=}") + log(f"SERVER{self.simulation_rank}_{self.vehicle_rank} : received {truncated=}") info = {} cur_state = np.nan_to_num(cur_state[n_sensors:], nan=0., posinf=30.) @@ -119,11 +120,11 @@ def step(self, action): if B_DEBUG: print("Webots started", file=open("/tmp/autotech/logs", "w")) - def make_env(rank: int): + def make_env(rank: int, rank_v: int): log(f"CAREFUL !!! created an SERVER env with {rank=}") - return WebotsSimulationGymEnvironment(rank) + return WebotsSimulationGymEnvironment(rank, rank_v) - envs = SubprocVecEnv([lambda rank=rank : make_env(rank) for rank in range(n_simulations)]) + envs = SubprocVecEnv([lambda rank=rank, rank_v =rank_v : make_env(rank, rank_v) for rank_v in range(n_vehicles) for rank in range(n_simulations)]) ExtractorClass = TemporalResNetExtractor diff --git a/src/Simulateur/worlds/.piste0.wbproj b/src/Simulateur/worlds/.piste0.wbproj index 2e84c967..adb170bc 100644 --- a/src/Simulateur/worlds/.piste0.wbproj +++ b/src/Simulateur/worlds/.piste0.wbproj @@ -1,5 +1,5 @@ Webots Project File version R2025a -perspectives: 000000ff00000000fd00000002000000010000011c00000177fc0200000001fb0000001400540065007800740045006400690074006f00720000000016000001770000004500ffffff00000003000006c000000216fc0100000001fb0000001a0043006f006e0073006f006c00650041006c006c0041006c006c0100000000000006c00000008700ffffff000006c0000001c400000001000000020000000100000008fc00000000 +perspectives: 000000ff00000000fd00000002000000010000011c00000177fc0200000001fb0000001400540065007800740045006400690074006f00720000000016000001770000003f00ffffff00000003000004f400000039fc0100000001fb0000001a0043006f006e0073006f006c00650041006c006c0041006c006c0100000000000004f40000006900ffffff000004f40000017c00000001000000020000000100000008fc00000000 simulationViewPerspectives: 000000ff0000000100000002000001000000017d0100000002010000000100 sceneTreePerspectives: 000000ff00000001000000030000001f0000013e000000fa0100000002010000000200 maximizedDockId: -1 diff --git a/src/Simulateur/worlds/.piste1.wbproj b/src/Simulateur/worlds/.piste1.wbproj index 91e2a61f..d86aec8c 100644 --- a/src/Simulateur/worlds/.piste1.wbproj +++ b/src/Simulateur/worlds/.piste1.wbproj @@ -1,5 +1,5 @@ Webots Project File version R2025a -perspectives: 000000ff00000000fd0000000200000001000000870000028afc0200000001fb0000001400540065007800740045006400690074006f007201000000000000028a0000004500ffffff00000003000006c000000150fc0100000002fb0000001a0043006f006e0073006f006c00650041006c006c0041006c006c0100000000000006c00000008700fffffffb0000001a0043006f006e0073006f006c00650041006c006c0041006c006c010000000000000a000000000000000000000006370000028a00000001000000020000000100000008fc00000000 +perspectives: 000000ff00000000fd0000000200000001000000870000017cfc0200000001fb0000001400540065007800740045006400690074006f007201000000130000017c0000003f00ffffff00000003000004f400000039fc0100000002fb0000001a0043006f006e0073006f006c00650041006c006c0041006c006c0100000000000004f40000006900fffffffb0000001a0043006f006e0073006f006c00650041006c006c0041006c006c010000000000000a0000000000000000000000046b0000017c00000001000000020000000100000008fc00000000 simulationViewPerspectives: 000000ff00000001000000020000012b000005a50100000002010000000100 sceneTreePerspectives: 000000ff00000001000000030000001e00000364000000fa0100000002010000000200 minimizedPerspectives: 000000ff00000000fd0000000200000001000000750000017bfc0200000001fb0000001400540065007800740045006400690074006f007201000000160000017b0000003f00ffffff000000030000039b00000039fc0100000002fb0000001a0043006f006e0073006f006c00650041006c006c0041006c006c01000000000000039b0000006900fffffffb0000001a0043006f006e0073006f006c00650041006c006c0041006c006c010000000000000a000000000000000000000003240000017b00000001000000020000000100000008fc00000000 diff --git a/src/Simulateur/worlds/.piste2.wbproj b/src/Simulateur/worlds/.piste2.wbproj index 3568e5a3..b49a3c5e 100644 --- a/src/Simulateur/worlds/.piste2.wbproj +++ b/src/Simulateur/worlds/.piste2.wbproj @@ -1,5 +1,5 @@ Webots Project File version R2025a -perspectives: 000000ff00000000fd0000000200000001000001910000036ffc0200000001fb0000001400540065007800740045006400690074006f007200000000160000036f0000003f00ffffff000000030000039b00000039fc0100000002fb0000001a0043006f006e0073006f006c00650041006c006c0041006c006c01000000000000039b0000006900fffffffb0000001a0043006f006e0073006f006c00650041006c006c0041006c006c01000000000000078000000000000000000000039b0000036a00000001000000020000000100000008fc00000000 +perspectives: 000000ff00000000fd0000000200000001000001910000036ffc0200000001fb0000001400540065007800740045006400690074006f007200000000160000036f0000003f00ffffff000000030000078000000039fc0100000002fb0000001a0043006f006e0073006f006c00650041006c006c0041006c006c0100000000000007800000006900fffffffb0000001a0043006f006e0073006f006c00650041006c006c0041006c006c0100000000000007800000000000000000000007800000038c00000001000000020000000100000008fc00000000 simulationViewPerspectives: 000000ff00000001000000020000014d000006310100000002010000000100 sceneTreePerspectives: 000000ff00000001000000030000001d0000013c000000fa0100000002010000000200 maximizedDockId: -1 From 05ce4113dd674660c2ffc989811fb756371efd2c Mon Sep 17 00:00:00 2001 From: cbampeta Date: Sat, 22 Nov 2025 03:15:08 +0100 Subject: [PATCH 2/6] fix : fix some issues after the merge between nikopol and arthur --- scripts/launch_train_multiprocessing.py | 10 +++++----- src/Simulateur/WebotsSimulationGymEnvironment.py | 3 ++- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/scripts/launch_train_multiprocessing.py b/scripts/launch_train_multiprocessing.py index b80eeb7e..d198b4f7 100644 --- a/scripts/launch_train_multiprocessing.py +++ b/scripts/launch_train_multiprocessing.py @@ -33,11 +33,11 @@ def log(s: str): if B_DEBUG: print("Webots started", file=open("/tmp/autotech/logs", "w")) - def make_env(rank: int): - log(f"CAREFUL !!! created an SERVER env with {rank=}") - return WebotsSimulationGymEnvironment(rank) + def make_env(simulation_rank: int, vehicle_rank: int): + log(f"CAREFUL !!! created an SERVER env with {simulation_rank}_{vehicle_rank}") + return WebotsSimulationGymEnvironment(simulation_rank, vehicle_rank) - envs = SubprocVecEnv([lambda rank=rank : make_env(rank) for rank in range(n_simulations)]) + envs = SubprocVecEnv([lambda simulation_rank=simulation_rank, vehicle_rank=vehicle_rank : make_env(simulation_rank, vehicle_rank) for vehicle_rank in range(n_vehicles) for simulation_rank in range(n_simulations)]) ExtractorClass = TemporalResNetExtractor @@ -124,7 +124,7 @@ def make_env(rank: int): test_onnx(model) if B_DEBUG: - model.learn(total_timesteps=500_000, callback=DynamicActionPlotDistributionCallback()) + model.learn(total_timesteps=500_000, callback=DynamicActionPlotDistributionCallback()) else: model.learn(total_timesteps=500_000) diff --git a/src/Simulateur/WebotsSimulationGymEnvironment.py b/src/Simulateur/WebotsSimulationGymEnvironment.py index 7ff93fa2..87db88ad 100644 --- a/src/Simulateur/WebotsSimulationGymEnvironment.py +++ b/src/Simulateur/WebotsSimulationGymEnvironment.py @@ -9,7 +9,6 @@ def log(s: str): if B_DEBUG: print(s, file=open("/tmp/autotech/logs", "a")) - class WebotsSimulationGymEnvironment(gym.Env): """ One environment for each vehicle @@ -92,3 +91,5 @@ def step(self, action): # if self.simulation_rank == 0: # print(f"{(obs[0] == 0).mean():.3f} {(obs[1] == 0).mean():.3f}") return obs, reward, done, truncated, info + + From ad403a8091a9bbddf38eac3cc6ddb28a5a79f68c Mon Sep 17 00:00:00 2001 From: cbampeta Date: Sat, 22 Nov 2025 06:59:21 +0100 Subject: [PATCH 3/6] AlmostFeat : replacing emitter / receiver by fifo. Not working --- scripts/launch_train_multiprocessing.py | 5 +- .../WebotsSimulationGymEnvironment.py | 19 +++--- src/Simulateur/config.py | 8 +-- .../controllerVehicleDriver.py | 61 +++++++++++++---- .../controllerWorldSupervisor.py | 66 ++++++++----------- 5 files changed, 93 insertions(+), 66 deletions(-) diff --git a/scripts/launch_train_multiprocessing.py b/scripts/launch_train_multiprocessing.py index d198b4f7..ae5c20e4 100644 --- a/scripts/launch_train_multiprocessing.py +++ b/scripts/launch_train_multiprocessing.py @@ -14,6 +14,7 @@ from config import * from TemporalResNetExtractor import TemporalResNetExtractor +from CNN1DResNetExtractor import CNN1DResNetExtractor from onnx_utils import * from WebotsSimulationGymEnvironment import WebotsSimulationGymEnvironment @@ -39,7 +40,7 @@ def make_env(simulation_rank: int, vehicle_rank: int): envs = SubprocVecEnv([lambda simulation_rank=simulation_rank, vehicle_rank=vehicle_rank : make_env(simulation_rank, vehicle_rank) for vehicle_rank in range(n_vehicles) for simulation_rank in range(n_simulations)]) - ExtractorClass = TemporalResNetExtractor + ExtractorClass = CNN1DResNetExtractor policy_kwargs = dict( features_extractor_class=ExtractorClass, @@ -128,6 +129,8 @@ def make_env(simulation_rank: int, vehicle_rank: int): else: model.learn(total_timesteps=500_000) + print("iteration over") + model.save(save_path + str(i)) i += 1 diff --git a/src/Simulateur/WebotsSimulationGymEnvironment.py b/src/Simulateur/WebotsSimulationGymEnvironment.py index 87db88ad..327c46f7 100644 --- a/src/Simulateur/WebotsSimulationGymEnvironment.py +++ b/src/Simulateur/WebotsSimulationGymEnvironment.py @@ -36,17 +36,18 @@ def __init__(self, simulation_rank: int, vehicle_rank: int): os.mkfifo(f"/tmp/autotech/{simulation_rank}_{vehicle_rank}toserver.pipe") os.mkfifo(f"/tmp/autotech/serverto{simulation_rank}_{vehicle_rank}.pipe") + os.mkfifo(f"/tmp/autotech/{simulation_rank}_{vehicle_rank}tosupervisor.pipe") # --mode=fast --minimize --no-rendering --batch --stdout os.system(f""" webots {__file__.rsplit('/', 1)[0]}/worlds/piste{simulation_rank % n_map}.wbt --mode=fast --minimize --no-rendering --batch --stdout & echo $! {simulation_rank}_{vehicle_rank} >>/tmp/autotech/simulationranks """) - log(f"SERVER{simulation_rank}_{vehicle_rank} : {simulation_rank}_{vehicle_rank}toserver.pipe") - self.fifo_r = open(f"/tmp/autotech/{simulation_rank}_{vehicle_rank}toserver.pipe", "rb") log(f"SERVER{simulation_rank}_{vehicle_rank} : serverto{simulation_rank}_{vehicle_rank}.pipe") self.fifo_w = open(f"/tmp/autotech/serverto{simulation_rank}_{vehicle_rank}.pipe", "wb") - log(f"SERVER{simulation_rank}_{vehicle_rank} : fifo opened :D and init done") + log(f"SERVER{simulation_rank}_{vehicle_rank} : {simulation_rank}_{vehicle_rank}toserver.pipe") + self.fifo_r = open(f"/tmp/autotech/{simulation_rank}_{vehicle_rank}toserver.pipe", "rb") + log("-------------------------------------------------------------------") def reset(self, seed=0): @@ -56,6 +57,7 @@ def reset(self, seed=0): # this is true for lidar_horizontal_resolution = camera_horizontal_resolution self.context = obs = np.zeros([2, context_size, lidar_horizontal_resolution], dtype=np.float32) info = {} + log(f"SERVER{self.simulation_rank}_{self.vehicle_rank} : finished reset") return obs, info def step(self, action): @@ -64,6 +66,7 @@ def step(self, action): self.fifo_w.flush() # communication with the supervisor + log(f"SERVER{self.simulation_rank}_{self.vehicle_rank} : trying to read the fifo_r") cur_state = np.frombuffer(self.fifo_r.read(np.dtype(np.float32).itemsize * (n_sensors + lidar_horizontal_resolution + camera_horizontal_resolution)), dtype=np.float32) log(f"SERVER{self.simulation_rank}_{self.vehicle_rank} : received {cur_state=}") reward = np.frombuffer(self.fifo_r.read(np.dtype(np.float32).itemsize), dtype=np.float32)[0] # scalar @@ -79,17 +82,13 @@ def step(self, action): lidar_obs = cur_state[:lidar_horizontal_resolution] camera_obs = cur_state[lidar_horizontal_resolution:] - # apply dropout to the camera - # p = 0.5 - # camera_obs *= np.random.binomial(1, 1-p, camera_obs.shape) # random values in {0, 1} - self.context = obs = np.concatenate([ self.context[:, 1:], [lidar_obs[None], camera_obs[None]] ], axis=1) - # check if the context is correct - # if self.simulation_rank == 0: - # print(f"{(obs[0] == 0).mean():.3f} {(obs[1] == 0).mean():.3f}") + + log(f"SERVER{self.simulation_rank}_{self.vehicle_rank} : step over") + return obs, reward, done, truncated, info diff --git a/src/Simulateur/config.py b/src/Simulateur/config.py index 7970765a..f7c8fcb1 100644 --- a/src/Simulateur/config.py +++ b/src/Simulateur/config.py @@ -11,8 +11,8 @@ lidar_max_range = 12.0 device = "cuda" if is_available() else "cpu" -context_size = 128 -lidar_horizontal_resolution = 128 # DON'T CHANGE THIS VALUE PLS -camera_horizontal_resolution = 128 # DON'T CHANGE THIS VALUE PLS +context_size = 1 +lidar_horizontal_resolution = 1024 # DON'T CHANGE THIS VALUE PLS +camera_horizontal_resolution = 1024 # DON'T CHANGE THIS VALUE PLS -B_DEBUG = False +B_DEBUG = True diff --git a/src/Simulateur/controllers/controllerVehicleDriver/controllerVehicleDriver.py b/src/Simulateur/controllers/controllerVehicleDriver/controllerVehicleDriver.py index 1eb6b398..68ff150f 100644 --- a/src/Simulateur/controllers/controllerVehicleDriver/controllerVehicleDriver.py +++ b/src/Simulateur/controllers/controllerVehicleDriver/controllerVehicleDriver.py @@ -1,9 +1,23 @@ import numpy as np +import psutil import time +import os +import re +import sys +# add src/Simulateur to sys.path +path = __file__.rsplit('/', 3)[0] +sys.path.insert(0, path) + +from config import * from vehicle import Driver + +def log(s: str): + if True: + print(s, file=open("/tmp/autotech/logs", "a")) + class VehicleDriver(Driver): """ This class is a subclass of the Driver class and is used to control the vehicle. @@ -16,8 +30,10 @@ def __init__(self): basicTimeStep = int(self.getBasicTimeStep()) self.sensorTime = basicTimeStep // 4 - self.i = int(self.getName().split("_")[-1]) + self.v_min = 1 + self.v_max = 9 + self.i = int(self.getName().split("_")[-1]) # Lidar self.lidar = self.getDevice("Hokuyo") self.lidar.enable(self.sensorTime) @@ -32,14 +48,28 @@ def __init__(self): self.touch_sensor.enable(self.sensorTime) # Communication - self.receiver = self.getDevice("TT02_receiver") - self.receiver.enable(self.sensorTime) - self.receiver.setChannel(2 * self.i) # corwe ponds the the supervisor's emitter channel - self.emitter = self.getDevice("TT02_emitter") - self.emitter.setChannel(2 * self.i + 1) # corresponds the the supervisor's receiver channel - # Last data received from the supervisor (steering angle) - self.last_data = np.zeros(2, dtype=np.float32) + proc = psutil.Process(os.getpid()) #current + parent = proc.parent() #parent + grandparent = parent.parent() if parent else None #grandparent + pppid = str(grandparent.pid) + + + self.simulation_rank = int( + re.search( + pppid + r" (\d+)", + open("/tmp/autotech/simulationranks", "r").read(), + re.MULTILINE + ).group(1) + ) + + log(f"CLIENT{self.simulation_rank}/{self.i} : serverto{self.simulation_rank}_{self.i}.pipe") + self.fifo_r = open(f"/tmp/autotech/serverto{self.simulation_rank}_{self.i}.pipe", "rb") + log(f"CLIENT{self.simulation_rank}/{self.i} : {self.simulation_rank}_{self.i}tosupervisor.pipe") + self.fifo_w = open(f"/tmp/autotech/{self.simulation_rank}_{self.i}tosupervisor.pipe", "wb") + + + #Vérification de l"état de la voiture def observe(self): @@ -78,14 +108,17 @@ def observe(self): def step(self): # sends observation to the supervisor - self.emitter.send(self.observe().tobytes()) + # First to be executed + self.fifo_w.write(self.observe().tobytes()) + + log(f"CLIENT{self.simulation_rank}/{self.i} : trying to read from fifo") + action = np.frombuffer(self.fifo_r.read(np.dtype(np.int64).itemsize * 2), dtype=np.int64) + log(f"CLIENT{self.simulation_rank}/{self.i} : received {action=}") - if self.receiver.getQueueLength() > 0: - while self.receiver.getQueueLength() > 1: - self.receiver.nextPacket() - self.last_data = np.frombuffer(self.receiver.getBytes(), dtype=np.float32) + # Simulation step - action_steering, action_speed = self.last_data + action_steering = np.linspace(-.4, .4, n_actions_steering, dtype=np.float32)[action[0], None] + action_speed = np.linspace(self.v_min, self.v_max, n_actions_speed, dtype=np.float32)[action[1], None] cur_angle = self.getSteeringAngle() dt = self.getBasicTimeStep() diff --git a/src/Simulateur/controllers/controllerWorldSupervisor/controllerWorldSupervisor.py b/src/Simulateur/controllers/controllerWorldSupervisor/controllerWorldSupervisor.py index 4b3fdadf..0731d6bf 100644 --- a/src/Simulateur/controllers/controllerWorldSupervisor/controllerWorldSupervisor.py +++ b/src/Simulateur/controllers/controllerWorldSupervisor/controllerWorldSupervisor.py @@ -7,7 +7,6 @@ from controller import Supervisor supervisor = Supervisor() - import torch.nn as nn import psutil @@ -82,7 +81,7 @@ def log(s: str): -class WebotsVehicleGymEnvironment(gym.Env): +class WebotsVehicleManager: """ One environment for each vehicle @@ -102,37 +101,36 @@ def __init__(self, vehicle_rank: int): # negative value so that the first reset is not skipped self.last_reset = -1e6 - # Emitter - self.emitter = supervisor.getDevice(f"supervisor_emitter_{vehicle_rank}") - self.emitter.setChannel(2 * self.vehicle_rank) + proc = psutil.Process(os.getpid()) #current + parent = proc.parent() #parent + grandparent = parent.parent() if parent else None #grandparent + pppid = str(grandparent.pid) - # Receiver - self.receiver = supervisor.getDevice(f"supervisor_receiver_{vehicle_rank}") - self.receiver.enable(self.sensorTime) - self.receiver.setChannel(2 * self.vehicle_rank + 1) + self.simulation_rank = int( + re.search( + pppid + r" (\d+)", + open("/tmp/autotech/simulationranks", "r").read(), + re.MULTILINE + ).group(1) + ) - log(f"CLIENT{simulation_rank}/{vehicle_rank}_{vehicle_rank} : begins init") - log(f"CLIENT{simulation_rank}/{vehicle_rank}_{vehicle_rank} : {simulation_rank}_{vehicle_rank}toserver.pipe") - self.fifo_w = open(f"/tmp/autotech/{simulation_rank}_{vehicle_rank}toserver.pipe", "wb") - log(f"CLIENT{simulation_rank}/{vehicle_rank} : serverto{simulation_rank}_{vehicle_rank}.pipe") - self.fifo_r = open(f"/tmp/autotech/serverto{simulation_rank}_{vehicle_rank}.pipe", "rb") - # Last data received from the car - self.last_data = np.zeros(n_sensors + lidar_horizontal_resolution + camera_horizontal_resolution, dtype=np.float32) + log(f"SUPERVISOR{simulation_rank}/{vehicle_rank}_{vehicle_rank} : begins init") + log(f"SUPERVISOR{simulation_rank}/{self.vehicle_rank} : {self.simulation_rank}_{self.vehicle_rank}tosupervisor.pipe") + self.fifo_r = open(f"/tmp/autotech/{self.simulation_rank}_{self.vehicle_rank}tosupervisor.pipe", "rb") + log(f"SUPERVISOR{simulation_rank}/{vehicle_rank}_{vehicle_rank} : {simulation_rank}_{vehicle_rank}toserver.pipe") + self.fifo_w = open(f"/tmp/autotech/{simulation_rank}_{vehicle_rank}toserver.pipe", "wb") + self.translation_field = supervisor.getFromDef(f"TT02_{self.vehicle_rank}").getField("translation") # may cause access issues ... self.rotation_field = supervisor.getFromDef(f"TT02_{self.vehicle_rank}").getField("rotation") # may cause access issues ... # returns the lidar data of all vehicles def observe(self): - # gets from Receiver - if self.receiver.getQueueLength() > 0: - while self.receiver.getQueueLength() > 1: - self.receiver.nextPacket() - self.last_data = np.frombuffer(self.receiver.getBytes(), dtype=np.float32) + # gets from Vehicle - return self.last_data + return np.frombuffer(self.fifo_r.read(np.dtype(np.float32).itemsize * (n_sensors + lidar_horizontal_resolution + camera_horizontal_resolution)), dtype=np.float32) # reset the gym environment reset def reset(self, seed=None): @@ -158,10 +156,9 @@ def reset(self, seed=None): return obs, info # step function of the gym environment - def step(self, action): - action_steering = np.linspace(-.4, .4, n_actions_steering, dtype=np.float32)[action[0], None] - action_speed = np.linspace(self.v_min, self.v_max, n_actions_speed, dtype=np.float32)[action[1], None] - self.emitter.send(np.array([action_steering, action_speed], dtype=np.float32).tobytes()) + def step(self): + #action_steering = np.linspace(-.4, .4, n_actions_steering, dtype=np.float32)[action[0], None] + #action_speed = np.linspace(self.v_min, self.v_max, n_actions_speed, dtype=np.float32)[action[1], None] # we should add a beacon sensor pointing upwards to detect the beacon obs = self.observe() @@ -191,7 +188,7 @@ def step(self, action): def main(): - envs = [WebotsVehicleGymEnvironment(i) for i in range(n_vehicles)] + envs = [WebotsVehicleManager(i) for i in range(n_vehicles)] log(f"CLIENT ALL : envs created") # check_env(env) @@ -218,22 +215,17 @@ def main(): log(f"CLIENT ALL : begin step") #Prédiction pour séléctionner une action à partir de l"observation for e in envs: - log(f"CLIENT{simulation_rank}/{e.vehicle_rank} : trying to read from fifo") - action = np.frombuffer(e.fifo_r.read(np.dtype(np.int64).itemsize * 2), dtype=np.int64) - log(f"CLIENT{simulation_rank}/{e.vehicle_rank} : received {action=}") - - obs, reward, done, truncated, info = e.step(action) - + obs, reward, done, truncated, info = e.step() if done: obs, info = e.reset() - log(f"CLIENT{simulation_rank}/{e.vehicle_rank} : sending {obs=}") + log(f"SUPERVISOR{simulation_rank}/{e.vehicle_rank} : sending {obs=}") e.fifo_w.write(obs.tobytes()) - log(f"CLIENT{simulation_rank}/{e.vehicle_rank} : sending {reward=}") + log(f"SUPERVISOR{simulation_rank}/{e.vehicle_rank} : sending {reward=}") e.fifo_w.write(reward.tobytes()) - log(f"CLIENT{simulation_rank}/{e.vehicle_rank} : sending {done=}") + log(f"SUPERVISOR{simulation_rank}/{e.vehicle_rank} : sending {done=}") e.fifo_w.write(done.tobytes()) - log(f"CLIENT{simulation_rank}/{e.vehicle_rank} : sending {truncated=}") + log(f"SUPERVISOR{simulation_rank}/{e.vehicle_rank} : sending {truncated=}") e.fifo_w.write(truncated.tobytes()) e.fifo_w.flush() From e5e6fdb6f7694fa566be93e70b663b63c9ec3a0b Mon Sep 17 00:00:00 2001 From: ExoRoy Date: Sun, 30 Nov 2025 21:16:31 +0100 Subject: [PATCH 4/6] Mathias's issues give to Nachid test 1 --- scripts/lanch_one_simu.py | 86 ++++++++++++++++++++++++++++++++++++ src/Simulateur/onnx_utils.py | 16 +++---- 2 files changed, 94 insertions(+), 8 deletions(-) create mode 100644 scripts/lanch_one_simu.py diff --git a/scripts/lanch_one_simu.py b/scripts/lanch_one_simu.py new file mode 100644 index 00000000..78319e84 --- /dev/null +++ b/scripts/lanch_one_simu.py @@ -0,0 +1,86 @@ +import os +import sys + +from typing import * +import numpy as np +import onnxruntime as ort + +simu_path = __file__.rsplit('/', 2)[0] + '/src/Simulateur' +if simu_path not in sys.path: + sys.path.insert(0, simu_path) + +from onnx_utils import run_onnx_model +from config import * +from WebotsSimulationGymEnvironment import WebotsSimulationGymEnvironment +from TemporalResNetExtractor import TemporalResNetExtractor +from CNN1DResNetExtractor import CNN1DResNetExtractor +# ------------------------------------------------------------------------- + +def log(s: str): + # Conservez votre fonction de log + if B_DEBUG: + print(s, file=open("/tmp/autotech/logs", "a")) + +# --- Chemin vers le fichier ONNX --- + +ONNX_MODEL_PATH = "model.onnx" + +# --- Initialisation du moteur d'inférence ONNX Runtime (ORT) --- +def init_onnx_runtime_session(onnx_path: str) -> ort.InferenceSession: + if not os.path.exists(onnx_path): + raise FileNotFoundError(f"Le fichier ONNX est introuvable à : {onnx_path}. Veuillez l'exporter d'abord.") + + # Crée la session d'inférence + return ort.InferenceSession(onnx_path) #On peut modifier le providers afin de mettre une CUDA + + +if __name__ == "__main__": + if not os.path.exists("/tmp/autotech/"): + os.mkdir("/tmp/autotech/") + + os.system('if [ -n "$(ls /tmp/autotech)" ]; then rm /tmp/autotech/*; fi') + if B_DEBUG: + print("Webots started", file=open("/tmp/autotech/logs", "w")) + + + # 2. Initialisation de la session ONNX Runtime + try: + ort_session = init_onnx_runtime_session(ONNX_MODEL_PATH) + input_name = ort_session.get_inputs()[0].name + output_name = ort_session.get_outputs()[0].name + print(f"Modèle ONNX chargé depuis {ONNX_MODEL_PATH}") + print(f"Input Name: {input_name}, Output Name: {output_name}") + except FileNotFoundError as e: + print(f"ERREUR : {e}") + print( + "Veuillez vous assurer que vous avez exécuté une fois le script d'entraînement pour exporter 'model.onnx'.") + sys.exit(1) + + # 3. Boucle d'inférence (Test) + env = WebotsSimulationGymEnvironment(0,0) + obs = env.reset() + print("Début de la simulation en mode inférence...") + + max_steps = 5000 + step_count = 0 + + while True: + + action = run_onnx_model(ort_session,obs) + + # 4. Exécuter l'action dans l'environnement + obs, reward, done, info = env.step(action) + + # Note: L'environnement Webots gère généralement son propre affichage + # env.render() # Décommenter si votre env supporte le rendu externe + + # Gestion des fins d'épisodes + if done: + print(f"Épisode(s) terminé(s) après {step_count} étapes.") + obs = env.reset() + + + + # Fermeture propre (très important pour les processus parallèles SubprocVecEnv) + envs.close() + print("Simulation terminée. Environnements fermés.") \ No newline at end of file diff --git a/src/Simulateur/onnx_utils.py b/src/Simulateur/onnx_utils.py index b2ebf87c..7dd7dd5b 100644 --- a/src/Simulateur/onnx_utils.py +++ b/src/Simulateur/onnx_utils.py @@ -3,7 +3,7 @@ import torch.nn as nn import torch from config import * - +import numpy as np from CNN1DExtractor import CNN1DExtractor from TemporalResNetExtractor import TemporalResNetExtractor @@ -38,24 +38,24 @@ def export_onnx(model): model.policy.to(device) model.policy.train() +def run_onnx_model(session : ort.InferenceSession,x : np.ndarray): + + return session.run(None, {"input": x})[0] def test_onnx(model): device = model.policy.device model.policy.eval() true_model = get_true_model(model) + loss_fn = nn.MSELoss() + x = torch.randn(1000, 2, context_size, lidar_horizontal_resolution) + try: session = ort.InferenceSession("model.onnx") except Exception as e: print(f"Error loading ONNX model: {e}") return - def model_onnx(x): - return session.run(None, {"input": x.cpu().numpy()})[0] - - loss_fn = nn.MSELoss() - x = torch.randn(1000, 2, context_size, lidar_horizontal_resolution) - with torch.no_grad(): y_true_test = true_model(x) @@ -63,7 +63,7 @@ def model_onnx(x): y_true_train = true_model(x) true_model.eval() - y_onnx = model_onnx(x) + y_onnx = run_onnx_model(session,x.cpu().numpy()) loss_test = loss_fn(y_true_test, torch.tensor(y_onnx)) loss_train = loss_fn(y_true_train, torch.tensor(y_onnx)) From e02d21907b43bd29edceb869e0a8b29e2533c6de Mon Sep 17 00:00:00 2001 From: Cbampeta Date: Fri, 9 Jan 2026 23:39:43 +0100 Subject: [PATCH 5/6] fix: this simulation and fifo work perfectly --- .../WebotsSimulationGymEnvironment.py | 16 +++++++++----- src/Simulateur/config.py | 2 +- .../controllerVehicleDriver.py | 6 ++++- .../controllerWorldSupervisor.py | 22 ++++++++++++++----- 4 files changed, 33 insertions(+), 13 deletions(-) diff --git a/src/Simulateur/WebotsSimulationGymEnvironment.py b/src/Simulateur/WebotsSimulationGymEnvironment.py index 327c46f7..18d89bbd 100644 --- a/src/Simulateur/WebotsSimulationGymEnvironment.py +++ b/src/Simulateur/WebotsSimulationGymEnvironment.py @@ -18,6 +18,8 @@ class WebotsSimulationGymEnvironment(gym.Env): """ def __init__(self, simulation_rank: int, vehicle_rank: int): + + log("vvvvvvvvvvvvvvvvvvvvvvvvvv Initialisation vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv") super().__init__() self.simulation_rank = simulation_rank self.vehicle_rank = vehicle_rank @@ -39,16 +41,17 @@ def __init__(self, simulation_rank: int, vehicle_rank: int): os.mkfifo(f"/tmp/autotech/{simulation_rank}_{vehicle_rank}tosupervisor.pipe") # --mode=fast --minimize --no-rendering --batch --stdout - os.system(f""" - webots {__file__.rsplit('/', 1)[0]}/worlds/piste{simulation_rank % n_map}.wbt --mode=fast --minimize --no-rendering --batch --stdout & - echo $! {simulation_rank}_{vehicle_rank} >>/tmp/autotech/simulationranks - """) + if vehicle_rank == 0 : + os.system(f""" + webots {__file__.rsplit('/', 1)[0]}/worlds/piste{simulation_rank % n_map}.wbt --mode=fast --minimize --batch --stdout & + echo $! {simulation_rank}_{vehicle_rank} >>/tmp/autotech/simulationranks + """) log(f"SERVER{simulation_rank}_{vehicle_rank} : serverto{simulation_rank}_{vehicle_rank}.pipe") self.fifo_w = open(f"/tmp/autotech/serverto{simulation_rank}_{vehicle_rank}.pipe", "wb") log(f"SERVER{simulation_rank}_{vehicle_rank} : {simulation_rank}_{vehicle_rank}toserver.pipe") self.fifo_r = open(f"/tmp/autotech/{simulation_rank}_{vehicle_rank}toserver.pipe", "rb") - log("-------------------------------------------------------------------") + log("----------------------------- Inititalisation ---------------------------------") def reset(self, seed=0): # basically useless function @@ -61,6 +64,8 @@ def reset(self, seed=0): return obs, info def step(self, action): + + log("vvvvvvvvvvvvvvvvvvvvvvvvvv STEP vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv") log(f"SERVER{self.simulation_rank}_{self.vehicle_rank} : sending {action=}") self.fifo_w.write(action.tobytes()) self.fifo_w.flush() @@ -88,6 +93,7 @@ def step(self, action): ], axis=1) log(f"SERVER{self.simulation_rank}_{self.vehicle_rank} : step over") + log("----------------------------- STEP ---------------------------------") return obs, reward, done, truncated, info diff --git a/src/Simulateur/config.py b/src/Simulateur/config.py index f7c8fcb1..94d2e095 100644 --- a/src/Simulateur/config.py +++ b/src/Simulateur/config.py @@ -3,7 +3,7 @@ n_map = 2 n_simulations = 2 -n_vehicles = 1 +n_vehicles = 2 n_stupid_vehicles = 0 n_actions_steering = 16 n_actions_speed = 16 diff --git a/src/Simulateur/controllers/controllerVehicleDriver/controllerVehicleDriver.py b/src/Simulateur/controllers/controllerVehicleDriver/controllerVehicleDriver.py index 68ff150f..1b883f2d 100644 --- a/src/Simulateur/controllers/controllerVehicleDriver/controllerVehicleDriver.py +++ b/src/Simulateur/controllers/controllerVehicleDriver/controllerVehicleDriver.py @@ -109,7 +109,11 @@ def step(self): # sends observation to the supervisor # First to be executed - self.fifo_w.write(self.observe().tobytes()) + log(f"CLIENT{self.simulation_rank}/{self.i} : trying to write obs") + obs = self.observe() + log(f"CLIENT{self.simulation_rank}/{self.i} : driver sending {obs=}") + self.fifo_w.write(obs.tobytes()) + self.fifo_w.flush() log(f"CLIENT{self.simulation_rank}/{self.i} : trying to read from fifo") action = np.frombuffer(self.fifo_r.read(np.dtype(np.int64).itemsize * 2), dtype=np.int64) diff --git a/src/Simulateur/controllers/controllerWorldSupervisor/controllerWorldSupervisor.py b/src/Simulateur/controllers/controllerWorldSupervisor/controllerWorldSupervisor.py index 0731d6bf..908a4bb1 100644 --- a/src/Simulateur/controllers/controllerWorldSupervisor/controllerWorldSupervisor.py +++ b/src/Simulateur/controllers/controllerWorldSupervisor/controllerWorldSupervisor.py @@ -129,17 +129,22 @@ def __init__(self, vehicle_rank: int): # returns the lidar data of all vehicles def observe(self): # gets from Vehicle - - return np.frombuffer(self.fifo_r.read(np.dtype(np.float32).itemsize * (n_sensors + lidar_horizontal_resolution + camera_horizontal_resolution)), dtype=np.float32) + log(f"SUPERVISOR{simulation_rank}/{self.vehicle_rank} : we observe") + obs = np.frombuffer(self.fifo_r.read(np.dtype(np.float32).itemsize * (n_sensors + lidar_horizontal_resolution + camera_horizontal_resolution)), dtype=np.float32) + log(f"SUPERVISOR{simulation_rank}/{self.vehicle_rank} : observing {obs=}") + return obs + # reset the gym environment reset def reset(self, seed=None): + log(f"SUPERVISOR{simulation_rank}/{self.vehicle_rank} : trying to reset vehicle") # this has to be done otherwise thec cars will shiver for a while sometimes when respawning and idk why if supervisor.getTime() - self.last_reset >= 1e-1: + log(f"SUPERVISOR{simulation_rank}/{self.vehicle_rank} : getting info") self.last_reset = supervisor.getTime() vehicle = supervisor.getFromDef(f"TT02_{self.vehicle_rank}") - + log(f"SUPERVISOR{simulation_rank}/{self.vehicle_rank} : resetting all") self.checkpoint_manager.reset(seed) trans = self.checkpoint_manager.getTranslation() rot = self.checkpoint_manager.getRotation() @@ -149,8 +154,9 @@ def reset(self, seed=None): self.checkpoint_manager.update() vehicle.resetPhysics() + log(f"SUPERVISOR{simulation_rank}/{self.vehicle_rank} : ok it's cooooooooool") - obs = self.observe() + obs = np.zeros(n_sensors + lidar_horizontal_resolution + camera_horizontal_resolution, dtype=np.float32) info = {} log(f"CLIENT{simulation_rank}/{self.vehicle_rank} : reset over") return obs, info @@ -162,8 +168,9 @@ def step(self): # we should add a beacon sensor pointing upwards to detect the beacon obs = self.observe() + log(f"SUPERVISOR{simulation_rank}/{self.vehicle_rank} : observed {obs=}") sensor_data = obs[:n_sensors] - + log(f"SUPERVISOR{simulation_rank}/{self.vehicle_rank} : sensor data {sensor_data=}") reward = 0 done = np.False_ truncated = np.False_ @@ -171,6 +178,7 @@ def step(self): x, y, z = self.translation_field.getSFVec3f() b_past_checkpoint = self.checkpoint_manager.update(x, y) b_collided, = sensor_data # unpack sensor data + log(f"SUPERVISOR{simulation_rank}/{self.vehicle_rank} : be_collided {b_collided=}") if b_collided or (z < -10): #print(f"CLIENT{simulation_rank}/{self.vehicle_rank} : {b_collided=}, {z=}") @@ -216,7 +224,7 @@ def main(): #Prédiction pour séléctionner une action à partir de l"observation for e in envs: obs, reward, done, truncated, info = e.step() - if done: + if done: obs, info = e.reset() log(f"SUPERVISOR{simulation_rank}/{e.vehicle_rank} : sending {obs=}") @@ -229,6 +237,8 @@ def main(): e.fifo_w.write(truncated.tobytes()) e.fifo_w.flush() + + for i in range(n_stupid_vehicles): tr_field = supervisor.getFromDef(f"TT02_{n_vehicles + i}").getField("translation") From 40423fddd6b04f27dcf3d107538cf3f9ceadd1b8 Mon Sep 17 00:00:00 2001 From: Cbampeta Date: Sat, 10 Jan 2026 04:19:37 +0100 Subject: [PATCH 6/6] =?UTF-8?q?feat:=20Impl=C3=A9mentation=20du=20module?= =?UTF-8?q?=20logging=20:=20un=20fichier=20log=20par=20voiture=20par=20sim?= =?UTF-8?q?ulation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/lanch_one_simu.py | 7 +-- scripts/launch_train_multiprocessing.py | 20 +++---- .../WebotsSimulationGymEnvironment.py | 42 ++++++++------ src/Simulateur/config.py | 5 +- .../controllerVehicleDriver.py | 28 +++++---- .../controllerWorldSupervisor.py | 57 +++++++++---------- 6 files changed, 81 insertions(+), 78 deletions(-) diff --git a/scripts/lanch_one_simu.py b/scripts/lanch_one_simu.py index 78319e84..dd39739a 100644 --- a/scripts/lanch_one_simu.py +++ b/scripts/lanch_one_simu.py @@ -16,10 +16,7 @@ from CNN1DResNetExtractor import CNN1DResNetExtractor # ------------------------------------------------------------------------- -def log(s: str): - # Conservez votre fonction de log - if B_DEBUG: - print(s, file=open("/tmp/autotech/logs", "a")) + # --- Chemin vers le fichier ONNX --- @@ -39,8 +36,6 @@ def init_onnx_runtime_session(onnx_path: str) -> ort.InferenceSession: os.mkdir("/tmp/autotech/") os.system('if [ -n "$(ls /tmp/autotech)" ]; then rm /tmp/autotech/*; fi') - if B_DEBUG: - print("Webots started", file=open("/tmp/autotech/logs", "w")) # 2. Initialisation de la session ONNX Runtime diff --git a/scripts/launch_train_multiprocessing.py b/scripts/launch_train_multiprocessing.py index ae5c20e4..edc13a85 100644 --- a/scripts/launch_train_multiprocessing.py +++ b/scripts/launch_train_multiprocessing.py @@ -1,3 +1,4 @@ +import logging import os import sys @@ -12,30 +13,27 @@ if simu_path not in sys.path: sys.path.insert(0, simu_path) +from Simulateur.config import LOG_LEVEL from config import * from TemporalResNetExtractor import TemporalResNetExtractor from CNN1DResNetExtractor import CNN1DResNetExtractor from onnx_utils import * from WebotsSimulationGymEnvironment import WebotsSimulationGymEnvironment -if B_DEBUG: from DynamicActionPlotCallback import DynamicActionPlotDistributionCallback - -def log(s: str): - if B_DEBUG: - print(s, file=open("/tmp/autotech/logs", "a")) - +if LOG_LEVEL == logging.DEBUG: from DynamicActionPlotCallback import DynamicActionPlotDistributionCallback if __name__ == "__main__": + if not os.path.exists("/tmp/autotech/"): os.mkdir("/tmp/autotech/") os.system('if [ -n "$(ls /tmp/autotech)" ]; then rm /tmp/autotech/*; fi') - if B_DEBUG: - print("Webots started", file=open("/tmp/autotech/logs", "w")) + def make_env(simulation_rank: int, vehicle_rank: int): - log(f"CAREFUL !!! created an SERVER env with {simulation_rank}_{vehicle_rank}") + if LOG_LEVEL == logging.DEBUG: + print("CAREFUL !!! created an SERVER env with {simulation_rank}_{vehicle_rank}") return WebotsSimulationGymEnvironment(simulation_rank, vehicle_rank) envs = SubprocVecEnv([lambda simulation_rank=simulation_rank, vehicle_rank=vehicle_rank : make_env(simulation_rank, vehicle_rank) for vehicle_rank in range(n_vehicles) for simulation_rank in range(n_simulations)]) @@ -111,7 +109,7 @@ def make_env(simulation_rank: int, vehicle_rank: int): print(f"{model.batch_size=}") print(f"{model.device=}") - log(f"SERVER : finished executing") + print("SERVER : finished executing") # obs = envs.reset() # while True: @@ -124,7 +122,7 @@ def make_env(simulation_rank: int, vehicle_rank: int): export_onnx(model) test_onnx(model) - if B_DEBUG: + if LOG_LEVEL <= logging.DEBUG: model.learn(total_timesteps=500_000, callback=DynamicActionPlotDistributionCallback()) else: model.learn(total_timesteps=500_000) diff --git a/src/Simulateur/WebotsSimulationGymEnvironment.py b/src/Simulateur/WebotsSimulationGymEnvironment.py index 18d89bbd..213b0688 100644 --- a/src/Simulateur/WebotsSimulationGymEnvironment.py +++ b/src/Simulateur/WebotsSimulationGymEnvironment.py @@ -6,9 +6,6 @@ from config import * -def log(s: str): - if B_DEBUG: - print(s, file=open("/tmp/autotech/logs", "a")) class WebotsSimulationGymEnvironment(gym.Env): """ One environment for each vehicle @@ -19,11 +16,20 @@ class WebotsSimulationGymEnvironment(gym.Env): def __init__(self, simulation_rank: int, vehicle_rank: int): - log("vvvvvvvvvvvvvvvvvvvvvvvvvv Initialisation vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv") + super().__init__() self.simulation_rank = simulation_rank self.vehicle_rank = vehicle_rank + self.handler = logging.FileHandler(f"/tmp/autotech/Voiture_{self.simulation_rank}_{self.vehicle_rank}.log") + self.handler.setFormatter(FORMATTER) + self.log = logging.getLogger("SERVER") + self.log.setLevel(level=LOG_LEVEL) + self.log.addHandler(self.handler) + + + self.log.info("Initialisation started") + # this is only true if lidar_horizontal_resolution = camera_horizontal_resolution box_min = np.zeros([2, context_size, lidar_horizontal_resolution], dtype=np.float32) box_max = np.ones([2, context_size, lidar_horizontal_resolution], dtype=np.float32) * 30 @@ -34,7 +40,7 @@ def __init__(self, simulation_rank: int, vehicle_rank: int): if not os.path.exists("/tmp/autotech"): os.mkdir("/tmp/autotech") - log(f"SERVER{simulation_rank}_{vehicle_rank} : {simulation_rank}_{vehicle_rank}") + self.log.debug(f"Creation of the pipes") os.mkfifo(f"/tmp/autotech/{simulation_rank}_{vehicle_rank}toserver.pipe") os.mkfifo(f"/tmp/autotech/serverto{simulation_rank}_{vehicle_rank}.pipe") @@ -46,12 +52,13 @@ def __init__(self, simulation_rank: int, vehicle_rank: int): webots {__file__.rsplit('/', 1)[0]}/worlds/piste{simulation_rank % n_map}.wbt --mode=fast --minimize --batch --stdout & echo $! {simulation_rank}_{vehicle_rank} >>/tmp/autotech/simulationranks """) - log(f"SERVER{simulation_rank}_{vehicle_rank} : serverto{simulation_rank}_{vehicle_rank}.pipe") + + self.log.debug("Connection to the vehicle") self.fifo_w = open(f"/tmp/autotech/serverto{simulation_rank}_{vehicle_rank}.pipe", "wb") - log(f"SERVER{simulation_rank}_{vehicle_rank} : {simulation_rank}_{vehicle_rank}toserver.pipe") + self.log.debug("Connection to the supervisor") self.fifo_r = open(f"/tmp/autotech/{simulation_rank}_{vehicle_rank}toserver.pipe", "rb") - log("----------------------------- Inititalisation ---------------------------------") + self.log.info("Initialisation finished\n") def reset(self, seed=0): # basically useless function @@ -60,26 +67,26 @@ def reset(self, seed=0): # this is true for lidar_horizontal_resolution = camera_horizontal_resolution self.context = obs = np.zeros([2, context_size, lidar_horizontal_resolution], dtype=np.float32) info = {} - log(f"SERVER{self.simulation_rank}_{self.vehicle_rank} : finished reset") + self.log.info(f"reset finished\n") return obs, info def step(self, action): - log("vvvvvvvvvvvvvvvvvvvvvvvvvv STEP vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv") - log(f"SERVER{self.simulation_rank}_{self.vehicle_rank} : sending {action=}") + self.log.info("Starting step") + self.log.info(f"sending {action=}") self.fifo_w.write(action.tobytes()) self.fifo_w.flush() # communication with the supervisor - log(f"SERVER{self.simulation_rank}_{self.vehicle_rank} : trying to read the fifo_r") + self.log.debug("trying to get info from supervisor") cur_state = np.frombuffer(self.fifo_r.read(np.dtype(np.float32).itemsize * (n_sensors + lidar_horizontal_resolution + camera_horizontal_resolution)), dtype=np.float32) - log(f"SERVER{self.simulation_rank}_{self.vehicle_rank} : received {cur_state=}") + self.log.info(f"received {cur_state=}") reward = np.frombuffer(self.fifo_r.read(np.dtype(np.float32).itemsize), dtype=np.float32)[0] # scalar - log(f"SERVER{self.simulation_rank}_{self.vehicle_rank} : received {reward=}") + self.log.info(f"received {reward=}") done = np.frombuffer(self.fifo_r.read(np.dtype(np.bool).itemsize), dtype=np.bool)[0] # scalar - log(f"SERVER{self.simulation_rank}_{self.vehicle_rank} : received {done=}") + self.log.info(f"received {done=}") truncated = np.frombuffer(self.fifo_r.read(np.dtype(np.bool).itemsize), dtype=np.bool)[0] # scalar - log(f"SERVER{self.simulation_rank}_{self.vehicle_rank} : received {truncated=}") + self.log.info(f"received {truncated=}") info = {} cur_state = np.nan_to_num(cur_state[n_sensors:], nan=0., posinf=30.) @@ -92,8 +99,7 @@ def step(self, action): [lidar_obs[None], camera_obs[None]] ], axis=1) - log(f"SERVER{self.simulation_rank}_{self.vehicle_rank} : step over") - log("----------------------------- STEP ---------------------------------") + self.log.info("step over") return obs, reward, done, truncated, info diff --git a/src/Simulateur/config.py b/src/Simulateur/config.py index 94d2e095..6db6d990 100644 --- a/src/Simulateur/config.py +++ b/src/Simulateur/config.py @@ -1,5 +1,7 @@ # just a file that lets us define some constants that are used in multiple files the simulation from torch.cuda import is_available +import logging + n_map = 2 n_simulations = 2 @@ -15,4 +17,5 @@ lidar_horizontal_resolution = 1024 # DON'T CHANGE THIS VALUE PLS camera_horizontal_resolution = 1024 # DON'T CHANGE THIS VALUE PLS -B_DEBUG = True +LOG_LEVEL = logging.DEBUG +FORMATTER = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') diff --git a/src/Simulateur/controllers/controllerVehicleDriver/controllerVehicleDriver.py b/src/Simulateur/controllers/controllerVehicleDriver/controllerVehicleDriver.py index 1b883f2d..d65b69d7 100644 --- a/src/Simulateur/controllers/controllerVehicleDriver/controllerVehicleDriver.py +++ b/src/Simulateur/controllers/controllerVehicleDriver/controllerVehicleDriver.py @@ -4,6 +4,7 @@ import os import re import sys +import logging # add src/Simulateur to sys.path path = __file__.rsplit('/', 3)[0] @@ -12,12 +13,6 @@ from config import * from vehicle import Driver - - -def log(s: str): - if True: - print(s, file=open("/tmp/autotech/logs", "a")) - class VehicleDriver(Driver): """ This class is a subclass of the Driver class and is used to control the vehicle. @@ -27,6 +22,7 @@ class VehicleDriver(Driver): def __init__(self): super().__init__() + basicTimeStep = int(self.getBasicTimeStep()) self.sensorTime = basicTimeStep // 4 @@ -63,9 +59,15 @@ def __init__(self): ).group(1) ) - log(f"CLIENT{self.simulation_rank}/{self.i} : serverto{self.simulation_rank}_{self.i}.pipe") + self.handler = logging.FileHandler(f"/tmp/autotech/Voiture_{self.simulation_rank}_{self.i}.log") + self.handler.setFormatter(FORMATTER) + self.log = logging.getLogger("CLIENT") + self.log.setLevel(level=LOG_LEVEL) + self.log.addHandler(self.handler) + + self.log.debug("Connection to the server") self.fifo_r = open(f"/tmp/autotech/serverto{self.simulation_rank}_{self.i}.pipe", "rb") - log(f"CLIENT{self.simulation_rank}/{self.i} : {self.simulation_rank}_{self.i}tosupervisor.pipe") + self.log.debug("Connection with the supervisor") self.fifo_w = open(f"/tmp/autotech/{self.simulation_rank}_{self.i}tosupervisor.pipe", "wb") @@ -109,15 +111,16 @@ def step(self): # sends observation to the supervisor # First to be executed - log(f"CLIENT{self.simulation_rank}/{self.i} : trying to write obs") + + self.log.info("Starting step") obs = self.observe() - log(f"CLIENT{self.simulation_rank}/{self.i} : driver sending {obs=}") + self.log.info(f"Observe {obs=}") self.fifo_w.write(obs.tobytes()) self.fifo_w.flush() - log(f"CLIENT{self.simulation_rank}/{self.i} : trying to read from fifo") + self.log.debug("Trying to read action from the server") action = np.frombuffer(self.fifo_r.read(np.dtype(np.int64).itemsize * 2), dtype=np.int64) - log(f"CLIENT{self.simulation_rank}/{self.i} : received {action=}") + self.log.info(f"received {action=}") # Simulation step @@ -149,6 +152,7 @@ def run(self): #----------------Programme principal-------------------- def main(): driver = VehicleDriver() + driver.log.info("Starting the vehicle driver\n") driver.run() diff --git a/src/Simulateur/controllers/controllerWorldSupervisor/controllerWorldSupervisor.py b/src/Simulateur/controllers/controllerWorldSupervisor/controllerWorldSupervisor.py index d6ba6049..b0639ce3 100644 --- a/src/Simulateur/controllers/controllerWorldSupervisor/controllerWorldSupervisor.py +++ b/src/Simulateur/controllers/controllerWorldSupervisor/controllerWorldSupervisor.py @@ -56,10 +56,6 @@ [-2.01029, -2.51669, 0.0391], ] -def log(s: str): - if B_DEBUG: - print(s, file=open("/tmp/autotech/logs", "a")) - # webots precess launches webots-bin internally who is the process that launches the controllers # that's why we need to get the pppid proc = psutil.Process(os.getpid()) #current @@ -77,7 +73,7 @@ def log(s: str): ) -log(f"CLIENT ?{pppid}? {simulation_rank=}") +print(f"CLIENT ?{pppid}? {simulation_rank=}") @@ -115,11 +111,16 @@ def __init__(self, vehicle_rank: int): ).group(1) ) + self.handler = logging.FileHandler(f"/tmp/autotech/Voiture_{self.simulation_rank}_{self.vehicle_rank}.log") + self.handler.setFormatter(FORMATTER) + self.log = logging.getLogger("SUPERVISOR") + self.log.setLevel(level=LOG_LEVEL) + self.log.addHandler(self.handler) + - log(f"SUPERVISOR{simulation_rank}/{vehicle_rank}_{vehicle_rank} : begins init") - log(f"SUPERVISOR{simulation_rank}/{self.vehicle_rank} : {self.simulation_rank}_{self.vehicle_rank}tosupervisor.pipe") + self.log.debug("Connection to the vehicle") self.fifo_r = open(f"/tmp/autotech/{self.simulation_rank}_{self.vehicle_rank}tosupervisor.pipe", "rb") - log(f"SUPERVISOR{simulation_rank}/{vehicle_rank}_{vehicle_rank} : {simulation_rank}_{vehicle_rank}toserver.pipe") + self.log.debug("Connection to the server") self.fifo_w = open(f"/tmp/autotech/{simulation_rank}_{vehicle_rank}toserver.pipe", "wb") @@ -129,22 +130,22 @@ def __init__(self, vehicle_rank: int): # returns the lidar data of all vehicles def observe(self): # gets from Vehicle - log(f"SUPERVISOR{simulation_rank}/{self.vehicle_rank} : we observe") + self.log.debug("trying to observe") obs = np.frombuffer(self.fifo_r.read(np.dtype(np.float32).itemsize * (n_sensors + lidar_horizontal_resolution + camera_horizontal_resolution)), dtype=np.float32) - log(f"SUPERVISOR{simulation_rank}/{self.vehicle_rank} : observing {obs=}") + self.log.info(f"observing {obs=}") return obs # reset the gym environment reset def reset(self, seed=None): - log(f"SUPERVISOR{simulation_rank}/{self.vehicle_rank} : trying to reset vehicle") + self.log.debug("trying to reset vehicle") # this has to be done otherwise thec cars will shiver for a while sometimes when respawning and idk why if supervisor.getTime() - self.last_reset >= 1e-1: - log(f"SUPERVISOR{simulation_rank}/{self.vehicle_rank} : getting info") + self.log.debug("getting info from vehicle") self.last_reset = supervisor.getTime() vehicle = supervisor.getFromDef(f"TT02_{self.vehicle_rank}") - log(f"SUPERVISOR{simulation_rank}/{self.vehicle_rank} : resetting all") + self.log.debug("resetting vehicle physics") self.checkpoint_manager.reset(seed) trans = self.checkpoint_manager.getTranslation() rot = self.checkpoint_manager.getRotation() @@ -154,11 +155,11 @@ def reset(self, seed=None): self.checkpoint_manager.update() vehicle.resetPhysics() - log(f"SUPERVISOR{simulation_rank}/{self.vehicle_rank} : ok it's cooooooooool") + self.log.info("vehicle reset done") + obs = np.zeros(n_sensors + lidar_horizontal_resolution + camera_horizontal_resolution, dtype=np.float32) info = {} - log(f"CLIENT{simulation_rank}/{self.vehicle_rank} : reset over") return obs, info # step function of the gym environment @@ -167,10 +168,10 @@ def step(self): #action_speed = np.linspace(self.v_min, self.v_max, n_actions_speed, dtype=np.float32)[action[1], None] # we should add a beacon sensor pointing upwards to detect the beacon + self.log.debug("getting observation") obs = self.observe() - log(f"SUPERVISOR{simulation_rank}/{self.vehicle_rank} : observed {obs=}") + self.log.info(f"observed {obs=}") sensor_data = obs[:n_sensors] - log(f"SUPERVISOR{simulation_rank}/{self.vehicle_rank} : sensor data {sensor_data=}") reward = 0 done = np.False_ truncated = np.False_ @@ -178,7 +179,6 @@ def step(self): x, y, z = self.translation_field.getSFVec3f() b_past_checkpoint = self.checkpoint_manager.update(x, y) b_collided, = sensor_data # unpack sensor data - log(f"SUPERVISOR{simulation_rank}/{self.vehicle_rank} : be_collided {b_collided=}") if b_collided or (z < -10): #print(f"CLIENT{simulation_rank}/{self.vehicle_rank} : {b_collided=}, {z=}") @@ -197,16 +197,15 @@ def step(self): def main(): envs = [WebotsVehicleManager(i) for i in range(n_vehicles)] - log(f"CLIENT ALL : envs created") + print("CLIENT ALL : envs created") # check_env(env) logdir = "./Webots_tb/" - log(f"CLIENT ALL : {envs=}") supervisor.step() - log("-------------------------------------------------------------------") + for i, e in enumerate(envs): - log(f"CLIENT{simulation_rank}/{e.vehicle_rank} : reset") + e.log.debug("CLIENT : reset") e.reset(i) for i in range(n_vehicles, n_vehicles + n_stupid_vehicles): @@ -220,24 +219,22 @@ def main(): last_moved = [0 for _ in range(n_stupid_vehicles)] while supervisor.step() != -1: - log(f"CLIENT ALL : begin step") + #print("CLIENT ALL : begin step") #Prédiction pour séléctionner une action à partir de l"observation for e in envs: obs, reward, done, truncated, info = e.step() if done: obs, info = e.reset() - log(f"SUPERVISOR{simulation_rank}/{e.vehicle_rank} : sending {obs=}") + e.log.info(f"sending {obs=}") e.fifo_w.write(obs.tobytes()) - log(f"SUPERVISOR{simulation_rank}/{e.vehicle_rank} : sending {reward=}") + e.log.info(f"sending {reward=}") e.fifo_w.write(reward.tobytes()) - log(f"SUPERVISOR{simulation_rank}/{e.vehicle_rank} : sending {done=}") + e.log.info(f"sending {done=}") e.fifo_w.write(done.tobytes()) - log(f"SUPERVISOR{simulation_rank}/{e.vehicle_rank} : sending {truncated=}") + e.log.info(f"sending {truncated=}") e.fifo_w.write(truncated.tobytes()) - e.fifo_w.flush() - - + e.fifo_w.flush() for i in range(n_stupid_vehicles):