How to set Traffic Manager while repeating a same traffic situation?

I would like to train my reinforcement model which needs to spawn vehicles in a same situation repeatly and I find Traffic Manager is a helpful tool, but I notice that the documentation mentioned that ’ The TM is not an actor that needs to be destroyed, it will stop when the corresponding client does so. This is automatically managed by the API so the user does not have to take care of it ', so I didn’t add TM to the actor list and destroy it while ending an episode, but I don’t know how TM can figure out the end of the training episode, and I just set TM in a function of the environment class and call this method to reset the environment at the beginning of every training episode:
def reset(self):
self.collision_hist = []
self.actor_list = []
self.auto_list = []

    spawn_point = carla.Transform(carla.Location(x=48, y=2, z=0.3), carla.Rotation(pitch=0, yaw=0, roll=0))
    self.vehicle = self.world.spawn_actor(self.model3, spawn_point)
    self.actor_list.append(self.vehicle)

    spawn_point = carla.Transform(carla.Location(x=60, y=-1.5, z=0.3), carla.Rotation(pitch=0, yaw=0, roll=0))
    self.vehicle1 = self.world.spawn_actor(self.prius, spawn_point)
    self.auto_list.append(self.vehicle1)

    spawn_point = carla.Transform(carla.Location(x=70, y=2, z=0.3), carla.Rotation(pitch=0, yaw=0, roll=0))
    self.vehicle2 = self.world.spawn_actor(self.mustang, spawn_point)
    self.auto_list.append(self.vehicle2)

    spawn_point = carla.Transform(carla.Location(x=100, y=2, z=0.3), carla.Rotation(pitch=0, yaw=0, roll=0))
    self.vehicle3 = self.world.spawn_actor(self.a2, spawn_point)
    self.auto_list.append(self.vehicle3)

    tm = self.client.get_trafficmanager()

    tm_port = tm.get_port()
    for v in self.auto_list:
        v.set_autopilot(True, tm_port)
        tm.ignore_lights_percentage(v, 100)
        tm.distance_to_leading_vehicle(v, 0)
        tm.vehicle_percentage_speed_difference(v, -20)

But my code stopped just after the first episode and received the following warning:
terminate called after throwing an instance of ‘std::out_of_range’
what(): _Map_base::at
Aborted (core dumped)

I want to know how to solve this problem and if there is a need to make an ending sign to let TM stop in every training episode. Thanks a lot for any help!

1 Like

I’m not familiar with the editor so there is something not standard of the format of the former code:pensive: