Unique seed for Traffic Manager

This commit is contained in:
Jacopo Bartiromo 2021-12-13 16:07:09 +01:00 committed by Jacopo Bartiromo
parent 68658f9bd3
commit 9ff76463eb
13 changed files with 39 additions and 54 deletions

View File

@ -27,8 +27,7 @@ ALSM::ALSM(
CollisionStage &collision_stage,
TrafficLightStage &traffic_light_stage,
MotionPlanStage &motion_plan_stage,
VehicleLightStage &vehicle_light_stage,
RandomGeneratorMap &random_devices)
VehicleLightStage &vehicle_light_stage)
: registered_vehicles(registered_vehicles),
buffer_map(buffer_map),
track_traffic(track_traffic),
@ -41,8 +40,7 @@ ALSM::ALSM(
collision_stage(collision_stage),
traffic_light_stage(traffic_light_stage),
motion_plan_stage(motion_plan_stage),
vehicle_light_stage(vehicle_light_stage),
random_devices(random_devices) {}
vehicle_light_stage(vehicle_light_stage) {}
void ALSM::Update() {
@ -375,7 +373,6 @@ void ALSM::RemoveActor(const ActorId actor_id, const bool registered_actor) {
registered_vehicles.Remove({actor_id});
buffer_map.erase(actor_id);
idle_time.erase(actor_id);
random_devices.erase(actor_id);
localization_stage.RemoveActor(actor_id);
collision_stage.RemoveActor(actor_id);
traffic_light_stage.RemoveActor(actor_id);

View File

@ -64,8 +64,6 @@ private:
// Time elapsed since last vehicle destruction due to being idle for too long.
double elapsed_last_actor_destruction {0.0};
cc::Timestamp current_timestamp;
// Random devices.
RandomGeneratorMap &random_devices;
std::unordered_map<ActorId, bool> has_physics_enabled;
// Updates the duration for which a registered vehicle is stuck at a location.
@ -104,8 +102,7 @@ public:
CollisionStage &collision_stage,
TrafficLightStage &traffic_light_stage,
MotionPlanStage &motion_plan_stage,
VehicleLightStage &vehicle_light_stage,
RandomGeneratorMap &random_devices);
VehicleLightStage &vehicle_light_stage);
void Update();

View File

@ -22,14 +22,14 @@ CollisionStage::CollisionStage(
const TrackTraffic &track_traffic,
const Parameters &parameters,
CollisionFrame &output_array,
RandomGeneratorMap &random_devices)
RandomGenerator &random_device)
: vehicle_id_list(vehicle_id_list),
simulation_state(simulation_state),
buffer_map(buffer_map),
track_traffic(track_traffic),
parameters(parameters),
output_array(output_array),
random_devices(random_devices) {}
random_device(random_device) {}
void CollisionStage::Update(const unsigned long index) {
ActorId obstacle_id = 0u;
@ -91,9 +91,9 @@ void CollisionStage::Update(const unsigned long index) {
look_ahead_index);
if (negotiation_result.first) {
if ((other_actor_type == ActorType::Vehicle
&& parameters.GetPercentageIgnoreVehicles(ego_actor_id) <= random_devices.at(ego_actor_id).next())
&& parameters.GetPercentageIgnoreVehicles(ego_actor_id) <= random_device.next())
|| (other_actor_type == ActorType::Pedestrian
&& parameters.GetPercentageIgnoreWalkers(ego_actor_id) <= random_devices.at(ego_actor_id).next())) {
&& parameters.GetPercentageIgnoreWalkers(ego_actor_id) <= random_device.next())) {
collision_hazard = true;
obstacle_id = other_actor_id;
available_distance_margin = negotiation_result.second;

View File

@ -57,7 +57,7 @@ private:
// to avoid repeated computation within a cycle.
GeometryComparisonMap geometry_cache;
GeodesicBoundaryMap geodesic_boundary_map;
RandomGeneratorMap &random_devices;
RandomGenerator &random_device;
// Method to determine if a vehicle is on a collision path to another.
std::pair<bool, float> NegotiateCollision(const ActorId reference_vehicle_id,
@ -90,7 +90,7 @@ public:
const TrackTraffic &track_traffic,
const Parameters &parameters,
CollisionFrame &output_array,
RandomGeneratorMap &random_devices);
RandomGenerator &random_device);
void Update (const unsigned long index) override;

View File

@ -22,7 +22,7 @@ LocalizationStage::LocalizationStage(
Parameters &parameters,
std::vector<ActorId>& marked_for_removal,
LocalizationFrame &output_array,
RandomGeneratorMap &random_devices)
RandomGenerator &random_device)
: vehicle_id_list(vehicle_id_list),
buffer_map(buffer_map),
simulation_state(simulation_state),
@ -31,7 +31,7 @@ LocalizationStage::LocalizationStage(
parameters(parameters),
marked_for_removal(marked_for_removal),
output_array(output_array),
random_devices(random_devices){}
random_device(random_device){}
void LocalizationStage::Update(const unsigned long index) {
@ -120,9 +120,9 @@ void LocalizationStage::Update(const unsigned long index) {
const float perc_keep_right = parameters.GetKeepRightPercentage(actor_id);
const float perc_random_leftlanechange = parameters.GetRandomLeftLaneChangePercentage(actor_id);
const float perc_random_rightlanechange = parameters.GetRandomRightLaneChangePercentage(actor_id);
const bool is_keep_right = perc_keep_right > random_devices.at(actor_id).next();
const bool is_random_left_change = perc_random_leftlanechange >= random_devices.at(actor_id).next();
const bool is_random_right_change = perc_random_rightlanechange >= random_devices.at(actor_id).next();
const bool is_keep_right = perc_keep_right > random_device.next();
const bool is_random_left_change = perc_random_leftlanechange >= random_device.next();
const bool is_random_right_change = perc_random_rightlanechange >= random_device.next();
// Determine which of the parameters we should apply.
if (is_keep_right || is_random_right_change) {
@ -135,7 +135,7 @@ void LocalizationStage::Update(const unsigned long index) {
lane_change_direction = false;
} else {
// Both a left and right lane changes are forced. Choose between one of them.
lane_change_direction = FIFTYPERC > random_devices.at(actor_id).next();
lane_change_direction = FIFTYPERC > random_device.next();
}
}
}
@ -194,7 +194,7 @@ void LocalizationStage::Update(const unsigned long index) {
uint64_t selection_index = 0u;
// Pseudo-randomized path selection if found more than one choice.
if (next_waypoints.size() > 1) {
double r_sample = random_devices.at(actor_id).next();
double r_sample = random_device.next();
selection_index = static_cast<uint64_t>(r_sample*next_waypoints.size()*0.01);
} else if (next_waypoints.size() == 0) {
if (!parameters.GetOSMMode()) {

View File

@ -44,7 +44,7 @@ private:
ActorIdSet vehicles_at_junction;
using SimpleWaypointPair = std::pair<SimpleWaypointPtr, SimpleWaypointPtr>;
std::unordered_map<ActorId, SimpleWaypointPair> vehicles_at_junction_entrance;
RandomGeneratorMap &random_devices;
RandomGenerator &random_device;
SimpleWaypointPtr AssignLaneChange(const ActorId actor_id,
const cg::Location vehicle_location,
@ -74,7 +74,7 @@ public:
Parameters &parameters,
std::vector<ActorId>& marked_for_removal,
LocalizationFrame &output_array,
RandomGeneratorMap &random_devices);
RandomGenerator &random_device);
void Update(const unsigned long index) override;

View File

@ -41,7 +41,7 @@ MotionPlanStage::MotionPlanStage(
const TLFrame &tl_frame,
const cc::World &world,
ControlFrame &output_array,
RandomGeneratorMap &random_devices,
RandomGenerator &random_device,
const LocalMapPtr &local_map)
: vehicle_id_list(vehicle_id_list),
simulation_state(simulation_state),
@ -57,7 +57,7 @@ MotionPlanStage::MotionPlanStage(
tl_frame(tl_frame),
world(world),
output_array(output_array),
random_devices(random_devices),
random_device(random_device),
local_map(local_map) {
// Adding structure to avoid retrieving traffic lights when checking for landmarks.
@ -111,7 +111,7 @@ void MotionPlanStage::Update(const unsigned long index) {
double elapsed_time = current_timestamp.elapsed_seconds - teleportation_instance.at(actor_id).elapsed_seconds;
if (parameters.GetSynchronousMode() || elapsed_time > HYBRID_MODE_DT) {
float random_sample = (static_cast<float>(random_devices.at(actor_id).next())*dilate_factor) + lower_bound;
float random_sample = (static_cast<float>(random_device.next())*dilate_factor) + lower_bound;
NodeList teleport_waypoint_list = local_map->GetWaypointsInDelta(hero_location, ATTEMPTS_TO_TELEPORT, random_sample);
if (!teleport_waypoint_list.empty()) {
for (auto &teleport_waypoint : teleport_waypoint_list) {

View File

@ -42,7 +42,7 @@ private:
std::unordered_map<ActorId, cc::Timestamp> teleportation_instance;
ControlFrame &output_array;
cc::Timestamp current_timestamp;
RandomGeneratorMap &random_devices;
RandomGenerator &random_device;
const LocalMapPtr &local_map;
TLMap tl_map;
@ -83,7 +83,7 @@ public:
const TLFrame &tl_frame,
const cc::World &world,
ControlFrame &output_array,
RandomGeneratorMap &random_devices,
RandomGenerator &random_device,
const LocalMapPtr &local_map);
void Update(const unsigned long index);

View File

@ -18,7 +18,5 @@ private:
std::uniform_real_distribution<double> dist;
};
using RandomGeneratorMap = std::unordered_map<carla::rpc::ActorId, RandomGenerator>;
} // namespace traffic_manager
} // namespace carla

View File

@ -17,14 +17,14 @@ TrafficLightStage::TrafficLightStage(
const Parameters &parameters,
const cc::World &world,
TLFrame &output_array,
RandomGeneratorMap &random_devices)
RandomGenerator &random_device)
: vehicle_id_list(vehicle_id_list),
simulation_state(simulation_state),
buffer_map(buffer_map),
parameters(parameters),
world(world),
output_array(output_array),
random_devices(random_devices) {}
random_device(random_device) {}
void TrafficLightStage::Update(const unsigned long index) {
bool traffic_light_hazard = false;
@ -46,7 +46,7 @@ void TrafficLightStage::Update(const unsigned long index) {
if (is_at_traffic_light &&
traffic_light_state != TLS::Green &&
traffic_light_state != TLS::Off &&
parameters.GetPercentageRunningLight(ego_actor_id) <= random_devices.at(ego_actor_id).next()) {
parameters.GetPercentageRunningLight(ego_actor_id) <= random_device.next()) {
traffic_light_hazard = true;
}
@ -55,7 +55,7 @@ void TrafficLightStage::Update(const unsigned long index) {
!is_at_traffic_light &&
traffic_light_state != TLS::Green &&
traffic_light_state != TLS::Off &&
parameters.GetPercentageRunningSign(ego_actor_id) <= random_devices.at(ego_actor_id).next()) {
parameters.GetPercentageRunningSign(ego_actor_id) <= random_device.next()) {
traffic_light_hazard = HandleNonSignalisedJunction(ego_actor_id, junction_id, current_timestamp);
}

View File

@ -26,7 +26,7 @@ private:
/// Map containing the previous junction visited by a vehicle.
std::unordered_map<ActorId, JunctionID> vehicle_last_junction;
TLFrame &output_array;
RandomGeneratorMap &random_devices;
RandomGenerator &random_device;
cc::Timestamp current_timestamp;
bool HandleNonSignalisedJunction(const ActorId ego_actor_id, const JunctionID junction_id,
@ -39,7 +39,7 @@ public:
const Parameters &parameters,
const cc::World &world,
TLFrame &output_array,
RandomGeneratorMap &random_devices);
RandomGenerator &random_device);
void Update(const unsigned long index) override;

View File

@ -42,7 +42,7 @@ TrafficManagerLocal::TrafficManagerLocal(
parameters,
marked_for_removal,
localization_frame,
random_devices)),
random_device)),
collision_stage(CollisionStage(vehicle_id_list,
simulation_state,
@ -50,7 +50,7 @@ TrafficManagerLocal::TrafficManagerLocal(
track_traffic,
parameters,
collision_frame,
random_devices)),
random_device)),
traffic_light_stage(TrafficLightStage(vehicle_id_list,
simulation_state,
@ -58,7 +58,7 @@ TrafficManagerLocal::TrafficManagerLocal(
parameters,
world,
tl_frame,
random_devices)),
random_device)),
motion_plan_stage(MotionPlanStage(vehicle_id_list,
simulation_state,
@ -74,7 +74,7 @@ TrafficManagerLocal::TrafficManagerLocal(
tl_frame,
world,
control_frame,
random_devices,
random_device,
local_map)),
vehicle_light_stage(VehicleLightStage(vehicle_id_list,
@ -95,8 +95,7 @@ TrafficManagerLocal::TrafficManagerLocal(
collision_stage,
traffic_light_stage,
motion_plan_stage,
vehicle_light_stage,
random_devices)),
vehicle_light_stage)),
server(TrafficManagerServer(RPCportTM, static_cast<carla::traffic_manager::TrafficManagerBase *>(this))) {
@ -190,6 +189,7 @@ void TrafficManagerLocal::Run() {
unsigned long number_of_vehicles = vehicle_id_list.size();
if (registered_vehicles_state != current_registered_vehicles_state || number_of_vehicles != registered_vehicles.Size()) {
vehicle_id_list = registered_vehicles.GetIDList();
std::sort(vehicle_id_list.begin(), vehicle_id_list.end());
number_of_vehicles = vehicle_id_list.size();
// Reserve more space if needed.
@ -282,7 +282,6 @@ void TrafficManagerLocal::Stop() {
track_traffic.Clear();
previous_update_instance = chr::system_clock::now();
current_reserved_capacity = 0u;
random_devices.clear();
simulation_state.Reset();
localization_stage.Reset();
@ -322,14 +321,6 @@ void TrafficManagerLocal::RegisterVehicles(const std::vector<ActorPtr> &vehicle_
std::vector<ActorPtr> sorted_vehicle_list = vehicle_list;
std::sort(sorted_vehicle_list.begin(), sorted_vehicle_list.end(), [](ActorPtr &a, ActorPtr &b) {return a->GetId() > b->GetId(); });
registered_vehicles.Insert(sorted_vehicle_list);
for (const ActorPtr &vehicle: sorted_vehicle_list) {
if (!is_custom_seed) {
seed = vehicle->GetId() + seed;
} else {
seed = 1 + seed;
}
random_devices.insert({vehicle->GetId(), RandomGenerator(seed)});
}
}
void TrafficManagerLocal::UnregisterVehicles(const std::vector<ActorPtr> &actor_list) {
@ -490,6 +481,8 @@ std::vector<ActorId> TrafficManagerLocal::GetRegisteredVehiclesIDs() {
void TrafficManagerLocal::SetRandomDeviceSeed(const uint64_t _seed) {
seed = _seed;
is_custom_seed = true;
random_device = RandomGenerator(seed);
std::cout << random_device.next() << std::endl;
world.ResetAllTrafficLights();
}

View File

@ -109,11 +109,11 @@ private:
std::condition_variable step_end_trigger;
/// Single worker thread for sequential execution of sub-components.
std::unique_ptr<std::thread> worker_thread;
/// Structure holding random devices per vehicle.
RandomGeneratorMap random_devices;
/// Randomization seed.
uint64_t seed {static_cast<uint64_t>(time(NULL))};
bool is_custom_seed {false};
/// Structure holding random devices per vehicle.
RandomGenerator random_device = RandomGenerator(seed);
std::vector<ActorId> marked_for_removal;
/// Mutex to prevent vehicle registration during frame array re-allocation.
std::mutex registration_mutex;