20#include <unordered_map>
46void GroupObstaclesByObstacleId(
const int obstacle_id,
47 ObstaclesContainer*
const obstacles_container,
49 Obstacle* obstacle_ptr = obstacles_container->GetObstacle(obstacle_id);
50 if (obstacle_ptr ==
nullptr) {
51 AERROR <<
"Null obstacle [" << obstacle_id <<
"] found";
54 int id_mod = obstacle_id % FLAGS_max_thread_num;
55 (*id_obstacle_map)[id_mod].push_back(obstacle_ptr);
62void PredictorManager::RegisterPredictors() {
75 if (!conf.has_obstacle_type()) {
76 AERROR <<
"Obstacle config [" << conf.ShortDebugString()
77 <<
"] has not defined obstacle type.";
81 if (!conf.has_predictor_type()) {
82 AERROR <<
"Obstacle config [" << conf.ShortDebugString()
83 <<
"] has not defined predictor type.";
87 switch (conf.obstacle_type()) {
89 InitVehiclePredictors(conf);
93 InitCyclistPredictors(conf);
97 pedestrian_predictor_ = conf.predictor_type();
101 InitDefaultPredictors(conf);
108 AINFO <<
"Defined vehicle on lane obstacle predictor ["
109 << vehicle_on_lane_predictor_ <<
"].";
110 AINFO <<
"Defined vehicle off lane obstacle predictor ["
111 << vehicle_off_lane_predictor_ <<
"].";
112 AINFO <<
"Defined bicycle on lane obstacle predictor ["
113 << cyclist_on_lane_predictor_ <<
"].";
114 AINFO <<
"Defined bicycle off lane obstacle predictor ["
115 << cyclist_off_lane_predictor_ <<
"].";
116 AINFO <<
"Defined pedestrian obstacle predictor [" << pedestrian_predictor_
118 AINFO <<
"Defined default on lane obstacle predictor ["
119 << default_on_lane_predictor_ <<
"].";
120 AINFO <<
"Defined default off lane obstacle predictor ["
121 << default_off_lane_predictor_ <<
"].";
126 auto it = predictors_.find(type);
127 return it != predictors_.end() ? it->second.get() :
nullptr;
134 prediction_obstacles_.Clear();
136 if (FLAGS_enable_multi_thread) {
137 PredictObstaclesInParallel(perception_obstacles, adc_trajectory_container,
138 obstacles_container);
140 PredictObstacles(perception_obstacles, adc_trajectory_container,
141 obstacles_container);
145void PredictorManager::PredictObstacles(
150 perception_obstacles.perception_obstacle()) {
151 int id = perception_obstacle.id();
153 ADEBUG <<
"The obstacle has invalid id [" <<
id <<
"].";
157 PredictionObstacle prediction_obstacle;
158 Obstacle* obstacle = obstacles_container->
GetObstacle(
id);
162 if (obstacle !=
nullptr) {
163 PredictObstacle(adc_trajectory_container, obstacle, obstacles_container,
164 &prediction_obstacle);
166 prediction_obstacle.set_timestamp(perception_obstacle.timestamp());
167 prediction_obstacle.set_is_static(
true);
170 prediction_obstacle.set_predicted_period(
171 FLAGS_prediction_trajectory_time_length);
172 prediction_obstacle.mutable_perception_obstacle()->CopyFrom(
173 perception_obstacle);
175 prediction_obstacles_.add_prediction_obstacle()->CopyFrom(
176 prediction_obstacle);
180void PredictorManager::PredictObstaclesInParallel(
181 const PerceptionObstacles& perception_obstacles,
182 const ADCTrajectoryContainer* adc_trajectory_container,
183 ObstaclesContainer* obstacles_container) {
184 std::unordered_map<int, std::shared_ptr<PredictionObstacle>>
185 id_prediction_obstacle_map;
186 for (
const PerceptionObstacle& perception_obstacle :
187 perception_obstacles.perception_obstacle()) {
188 int id = perception_obstacle.id();
189 id_prediction_obstacle_map[id] = std::make_shared<PredictionObstacle>();
192 for (
const auto& perception_obstacle :
193 perception_obstacles.perception_obstacle()) {
194 int id = perception_obstacle.id();
195 Obstacle* obstacle = obstacles_container->GetObstacle(
id);
196 if (obstacle ==
nullptr) {
197 std::shared_ptr<PredictionObstacle> prediction_obstacle_ptr =
198 id_prediction_obstacle_map[id];
199 prediction_obstacle_ptr->set_is_static(
true);
200 prediction_obstacle_ptr->set_timestamp(perception_obstacle.timestamp());
202 GroupObstaclesByObstacleId(
id, obstacles_container, &id_obstacle_map);
206 id_obstacle_map.begin(), id_obstacle_map.end(),
207 [&](IdObstacleListMap::iterator::value_type& obstacles_iter) {
208 for (auto obstacle_ptr : obstacles_iter.second) {
209 int id = obstacle_ptr->id();
210 PredictObstacle(adc_trajectory_container, obstacle_ptr,
212 id_prediction_obstacle_map[id].get());
215 for (
const PerceptionObstacle& perception_obstacle :
216 perception_obstacles.perception_obstacle()) {
217 int id = perception_obstacle.id();
218 auto prediction_obstacle_ptr = id_prediction_obstacle_map[id];
219 if (prediction_obstacle_ptr ==
nullptr) {
220 AERROR <<
"Prediction obstacle [" <<
id <<
"] not found.";
223 prediction_obstacle_ptr->set_predicted_period(
224 FLAGS_prediction_trajectory_time_length);
225 prediction_obstacle_ptr->mutable_perception_obstacle()->CopyFrom(
226 perception_obstacle);
227 prediction_obstacles_.add_prediction_obstacle()->CopyFrom(
228 *prediction_obstacle_ptr);
232void PredictorManager::PredictObstacle(
236 CHECK_NOTNULL(obstacle);
237 prediction_obstacle->set_timestamp(obstacle->
timestamp());
240 ADEBUG <<
"Ignore obstacle [" << obstacle->
id() <<
"]";
241 RunEmptyPredictor(adc_trajectory_container, obstacle, obstacles_container);
242 prediction_obstacle->mutable_priority()->set_priority(
243 ObstaclePriority::IGNORE);
244 }
else if (obstacle->
IsStill()) {
245 ADEBUG <<
"Still obstacle [" << obstacle->
id() <<
"]";
246 RunEmptyPredictor(adc_trajectory_container, obstacle, obstacles_container);
248 switch (obstacle->
type()) {
249 case PerceptionObstacle::VEHICLE: {
250 if (FLAGS_enable_multi_agent_vehicle_evaluator) {
251 RunEmptyPredictor(adc_trajectory_container, obstacle,
252 obstacles_container);
254 RunVehiclePredictor(adc_trajectory_container, obstacle,
255 obstacles_container);
259 case PerceptionObstacle::PEDESTRIAN: {
260 if (FLAGS_enable_multi_agent_pedestrian_evaluator) {
261 RunEmptyPredictor(adc_trajectory_container, obstacle,
262 obstacles_container);
264 RunPedestrianPredictor(adc_trajectory_container, obstacle,
265 obstacles_container);
269 case PerceptionObstacle::BICYCLE: {
270 RunCyclistPredictor(adc_trajectory_container, obstacle,
271 obstacles_container);
275 RunDefaultPredictor(adc_trajectory_container, obstacle,
276 obstacles_container);
282 for (
const auto& trajectory :
284 prediction_obstacle->add_trajectory()->CopyFrom(trajectory);
286 prediction_obstacle->set_timestamp(obstacle->
timestamp());
287 prediction_obstacle->mutable_priority()->CopyFrom(
289 prediction_obstacle->mutable_interactive_tag()->CopyFrom(
291 prediction_obstacle->set_is_static(obstacle->
IsStill());
292 if (FLAGS_prediction_offline_mode ==
293 PredictionConstants::kDumpPredictionResult) {
295 FeatureOutput::InsertPredictionResult(obstacle, *prediction_obstacle,
300std::unique_ptr<Predictor> PredictorManager::CreatePredictor(
302 std::unique_ptr<Predictor> predictor_ptr(
nullptr);
304 case ObstacleConf::LANE_SEQUENCE_PREDICTOR: {
308 case ObstacleConf::MOVE_SEQUENCE_PREDICTOR: {
309 predictor_ptr.reset(
new MoveSequencePredictor());
312 case ObstacleConf::SINGLE_LANE_PREDICTOR: {
313 predictor_ptr.reset(
new SingleLanePredictor());
316 case ObstacleConf::FREE_MOVE_PREDICTOR: {
317 predictor_ptr.reset(
new FreeMovePredictor());
320 case ObstacleConf::JUNCTION_PREDICTOR: {
321 predictor_ptr.reset(
new JunctionPredictor());
324 case ObstacleConf::EXTRAPOLATION_PREDICTOR: {
325 predictor_ptr.reset(
new ExtrapolationPredictor());
328 case ObstacleConf::INTERACTION_PREDICTOR: {
329 predictor_ptr.reset(
new InteractionPredictor());
332 case ObstacleConf::EMPTY_PREDICTOR: {
333 predictor_ptr.reset(
new EmptyPredictor());
338 return predictor_ptr;
341void PredictorManager::RegisterPredictor(
342 const ObstacleConf::PredictorType& type) {
343 predictors_[type] = CreatePredictor(type);
344 AINFO <<
"Predictor [" << type <<
"] is registered.";
348 return prediction_obstacles_;
351void PredictorManager::InitVehiclePredictors(
const ObstacleConf& conf) {
352 if (!conf.has_obstacle_status() && conf.has_interactive_tag()) {
356 case ObstacleConf::ON_LANE: {
364 case ObstacleConf::OFF_LANE: {
368 case ObstacleConf::IN_JUNCTION: {
380void PredictorManager::InitCyclistPredictors(
const ObstacleConf& conf) {
381 switch (conf.obstacle_status()) {
382 case ObstacleConf::ON_LANE: {
383 cyclist_on_lane_predictor_ = conf.predictor_type();
386 case ObstacleConf::OFF_LANE: {
387 cyclist_off_lane_predictor_ = conf.predictor_type();
394void PredictorManager::InitDefaultPredictors(
const ObstacleConf& conf) {
395 switch (conf.obstacle_status()) {
396 case ObstacleConf::ON_LANE: {
397 default_on_lane_predictor_ = conf.predictor_type();
400 case ObstacleConf::OFF_LANE: {
401 default_off_lane_predictor_ = conf.predictor_type();
408void PredictorManager::RunVehiclePredictor(
409 const ADCTrajectoryContainer* adc_trajectory_container, Obstacle* obstacle,
410 ObstaclesContainer* obstacles_container) {
411 Predictor* predictor =
nullptr;
412 if (obstacle->IsInteractiveObstacle()) {
413 predictor = GetPredictor(vehicle_interactive_predictor_);
414 if (predictor->Predict(adc_trajectory_container, obstacle,
415 obstacles_container)) {
418 AERROR <<
"Obstacle: " << obstacle->id()
419 <<
" interactive predictor failed!";
422 if (obstacle->IsCaution()) {
423 if (obstacle->IsNearJunction()) {
424 predictor = GetPredictor(vehicle_in_junction_caution_predictor_);
425 }
else if (obstacle->IsOnLane()) {
426 predictor = GetPredictor(vehicle_on_lane_caution_predictor_);
428 predictor = GetPredictor(vehicle_default_caution_predictor_);
430 CHECK_NOTNULL(predictor);
431 if (predictor->Predict(adc_trajectory_container, obstacle,
432 obstacles_container)) {
435 AERROR <<
"Obstacle: " << obstacle->id()
436 <<
" caution predictor failed, downgrade to normal level!";
440 if (!obstacle->IsOnLane()) {
441 predictor = GetPredictor(vehicle_off_lane_predictor_);
442 }
else if (obstacle->HasJunctionFeatureWithExits() &&
443 !obstacle->IsCloseToJunctionExit()) {
444 predictor = GetPredictor(vehicle_in_junction_predictor_);
446 predictor = GetPredictor(vehicle_on_lane_predictor_);
449 if (predictor ==
nullptr) {
450 AERROR <<
"Nullptr found for obstacle [" << obstacle->id() <<
"]";
453 predictor->Predict(adc_trajectory_container, obstacle, obstacles_container);
454 if (FLAGS_enable_trim_prediction_trajectory) {
455 CHECK_NOTNULL(adc_trajectory_container);
456 predictor->TrimTrajectories(*adc_trajectory_container, obstacle);
460void PredictorManager::RunPedestrianPredictor(
461 const ADCTrajectoryContainer* adc_trajectory_container, Obstacle* obstacle,
462 ObstaclesContainer* obstacles_container) {
463 Predictor* predictor =
nullptr;
464 predictor = GetPredictor(pedestrian_predictor_);
465 if (predictor ==
nullptr) {
466 AERROR <<
"Nullptr found for obstacle [" << obstacle->id() <<
"]";
469 predictor->Predict(adc_trajectory_container, obstacle, obstacles_container);
472void PredictorManager::RunCyclistPredictor(
473 const ADCTrajectoryContainer* adc_trajectory_container, Obstacle* obstacle,
474 ObstaclesContainer* obstacles_container) {
475 Predictor* predictor =
nullptr;
476 if (obstacle->IsOnLane()) {
477 predictor = GetPredictor(cyclist_on_lane_predictor_);
479 predictor = GetPredictor(cyclist_off_lane_predictor_);
481 if (predictor ==
nullptr) {
482 AERROR <<
"Nullptr found for obstacle [" << obstacle->id() <<
"]";
485 predictor->Predict(adc_trajectory_container, obstacle, obstacles_container);
488void PredictorManager::RunDefaultPredictor(
489 const ADCTrajectoryContainer* adc_trajectory_container, Obstacle* obstacle,
490 ObstaclesContainer* obstacles_container) {
491 Predictor* predictor =
nullptr;
492 if (obstacle->IsOnLane()) {
493 predictor = GetPredictor(default_on_lane_predictor_);
495 predictor = GetPredictor(default_off_lane_predictor_);
497 if (predictor ==
nullptr) {
498 AERROR <<
"Nullptr found for obstacle [" << obstacle->id() <<
"]";
501 predictor->Predict(adc_trajectory_container, obstacle, obstacles_container);
504void PredictorManager::RunEmptyPredictor(
505 const ADCTrajectoryContainer* adc_trajectory_container, Obstacle* obstacle,
506 ObstaclesContainer* obstacles_container) {
507 Predictor* predictor = GetPredictor(ObstacleConf::EMPTY_PREDICTOR);
508 if (predictor ==
nullptr) {
509 AERROR <<
"Nullptr found for obstacle [" << obstacle->id() <<
"]";
512 predictor->Predict(adc_trajectory_container, obstacle, obstacles_container);
double timestamp() const
Get the obstacle's timestamp.
perception::PerceptionObstacle::Type type() const
Get the type of perception obstacle's type.
bool IsStill()
Check if the obstacle is still.
int id() const
Get the obstacle's ID.
bool ToIgnore()
Check if the obstacle can be ignored.
const Feature & latest_feature() const
Get the latest feature.
const ObstacleConf & obstacle_conf()
const Scenario & curr_scenario() const
Get current scenario
Obstacle * GetObstacle(const int id)
Get obstacle pointer
static void ForEach(InputIter begin, InputIter end, F f)
Predictor * GetPredictor(const ObstacleConf::PredictorType &type)
Get predictor
PredictorManager()
Constructor
void Run(const apollo::perception::PerceptionObstacles &perception_obstacles, const ADCTrajectoryContainer *adc_trajectory_container, ObstaclesContainer *obstacles_container)
Execute the predictor generation
void Init(const PredictionConf &config)
Initializer
void PredictObstacle(const ADCTrajectoryContainer *adc_trajectory_container, Obstacle *obstacle, ObstaclesContainer *obstacles_container, PredictionObstacle *const prediction_obstacle)
Predict a single obstacle
Use container manager to manage all containers
Define lane sequence predictor
Define interaction predictor
Define junction predictor
Define lane sequence predictor
Define move sequence predictor
std::unordered_map< int, std::list< Obstacle * > > IdObstacleListMap
Holds all global constants for the prediction module.
Use predictor manager to manage all predictors
optional ObstacleInteractiveTag interactive_tag
optional ObstaclePriority priority
repeated Trajectory predicted_trajectory
optional ObstaclePriority::Priority priority_type
optional PredictorType predictor_type
@ EXTRAPOLATION_PREDICTOR
@ MOVE_SEQUENCE_PREDICTOR
@ LANE_SEQUENCE_PREDICTOR
optional ObstacleStatus obstacle_status
repeated ObstacleConf obstacle_conf