Apollo 11.0
自动驾驶开放平台
predictor_manager.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2017 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16
18
19#include <list>
20#include <unordered_map>
21
37
38namespace apollo {
39namespace prediction {
40namespace {
41
44using IdObstacleListMap = std::unordered_map<int, std::list<Obstacle*>>;
45
46void GroupObstaclesByObstacleId(const int obstacle_id,
47 ObstaclesContainer* const obstacles_container,
48 IdObstacleListMap* const id_obstacle_map) {
49 Obstacle* obstacle_ptr = obstacles_container->GetObstacle(obstacle_id);
50 if (obstacle_ptr == nullptr) {
51 AERROR << "Null obstacle [" << obstacle_id << "] found";
52 return;
53 }
54 int id_mod = obstacle_id % FLAGS_max_thread_num;
55 (*id_obstacle_map)[id_mod].push_back(obstacle_ptr);
56}
57
58} // namespace
59
60PredictorManager::PredictorManager() { RegisterPredictors(); }
61
62void PredictorManager::RegisterPredictors() {
65 RegisterPredictor(ObstacleConf::SINGLE_LANE_PREDICTOR);
66 RegisterPredictor(ObstacleConf::FREE_MOVE_PREDICTOR);
67 RegisterPredictor(ObstacleConf::EMPTY_PREDICTOR);
68 RegisterPredictor(ObstacleConf::JUNCTION_PREDICTOR);
70 RegisterPredictor(ObstacleConf::INTERACTION_PREDICTOR);
71}
72
74 for (const auto& conf : config.obstacle_conf()) {
75 if (!conf.has_obstacle_type()) {
76 AERROR << "Obstacle config [" << conf.ShortDebugString()
77 << "] has not defined obstacle type.";
78 continue;
79 }
80
81 if (!conf.has_predictor_type()) {
82 AERROR << "Obstacle config [" << conf.ShortDebugString()
83 << "] has not defined predictor type.";
84 continue;
85 }
86
87 switch (conf.obstacle_type()) {
89 InitVehiclePredictors(conf);
90 break;
91 }
93 InitCyclistPredictors(conf);
94 break;
95 }
97 pedestrian_predictor_ = conf.predictor_type();
98 break;
99 }
101 InitDefaultPredictors(conf);
102 break;
103 }
104 default: { break; }
105 }
106 }
107
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_
117 << "].";
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_ << "].";
122}
123
125 const ObstacleConf::PredictorType& type) {
126 auto it = predictors_.find(type);
127 return it != predictors_.end() ? it->second.get() : nullptr;
128}
129
131 const PerceptionObstacles& perception_obstacles,
132 const ADCTrajectoryContainer* adc_trajectory_container,
133 ObstaclesContainer* obstacles_container) {
134 prediction_obstacles_.Clear();
135
136 if (FLAGS_enable_multi_thread) {
137 PredictObstaclesInParallel(perception_obstacles, adc_trajectory_container,
138 obstacles_container);
139 } else {
140 PredictObstacles(perception_obstacles, adc_trajectory_container,
141 obstacles_container);
142 }
143}
144
145void PredictorManager::PredictObstacles(
146 const PerceptionObstacles& perception_obstacles,
147 const ADCTrajectoryContainer* adc_trajectory_container,
148 ObstaclesContainer* obstacles_container) {
149 for (const PerceptionObstacle& perception_obstacle :
150 perception_obstacles.perception_obstacle()) {
151 int id = perception_obstacle.id();
152 if (id < 0) {
153 ADEBUG << "The obstacle has invalid id [" << id << "].";
154 continue;
155 }
156
157 PredictionObstacle prediction_obstacle;
158 Obstacle* obstacle = obstacles_container->GetObstacle(id);
159
160 // if obstacle == nullptr, that means obstacle is unmovable
161 // Checkout the logic of unmovable in obstacle.cc
162 if (obstacle != nullptr) {
163 PredictObstacle(adc_trajectory_container, obstacle, obstacles_container,
164 &prediction_obstacle);
165 } else { // obstacle == nullptr
166 prediction_obstacle.set_timestamp(perception_obstacle.timestamp());
167 prediction_obstacle.set_is_static(true);
168 }
169
170 prediction_obstacle.set_predicted_period(
171 FLAGS_prediction_trajectory_time_length);
172 prediction_obstacle.mutable_perception_obstacle()->CopyFrom(
173 perception_obstacle);
174
175 prediction_obstacles_.add_prediction_obstacle()->CopyFrom(
176 prediction_obstacle);
177 }
178}
179
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>();
190 }
191 IdObstacleListMap id_obstacle_map;
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());
201 } else {
202 GroupObstaclesByObstacleId(id, obstacles_container, &id_obstacle_map);
203 }
204 }
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,
211 obstacles_container,
212 id_prediction_obstacle_map[id].get());
213 }
214 });
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.";
221 continue;
222 }
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);
229 }
230}
231
232void PredictorManager::PredictObstacle(
233 const ADCTrajectoryContainer* adc_trajectory_container, Obstacle* obstacle,
234 ObstaclesContainer* obstacles_container,
235 PredictionObstacle* const prediction_obstacle) {
236 CHECK_NOTNULL(obstacle);
237 prediction_obstacle->set_timestamp(obstacle->timestamp());
238
239 if (obstacle->ToIgnore()) {
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);
247 } else {
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);
253 } else {
254 RunVehiclePredictor(adc_trajectory_container, obstacle,
255 obstacles_container);
256 }
257 break;
258 }
259 case PerceptionObstacle::PEDESTRIAN: {
260 if (FLAGS_enable_multi_agent_pedestrian_evaluator) {
261 RunEmptyPredictor(adc_trajectory_container, obstacle,
262 obstacles_container);
263 } else {
264 RunPedestrianPredictor(adc_trajectory_container, obstacle,
265 obstacles_container);
266 }
267 break;
268 }
269 case PerceptionObstacle::BICYCLE: {
270 RunCyclistPredictor(adc_trajectory_container, obstacle,
271 obstacles_container);
272 break;
273 }
274 default: {
275 RunDefaultPredictor(adc_trajectory_container, obstacle,
276 obstacles_container);
277 break;
278 }
279 }
280 }
281
282 for (const auto& trajectory :
283 obstacle->latest_feature().predicted_trajectory()) {
284 prediction_obstacle->add_trajectory()->CopyFrom(trajectory);
285 }
286 prediction_obstacle->set_timestamp(obstacle->timestamp());
287 prediction_obstacle->mutable_priority()->CopyFrom(
288 obstacle->latest_feature().priority());
289 prediction_obstacle->mutable_interactive_tag()->CopyFrom(
290 obstacle->latest_feature().interactive_tag());
291 prediction_obstacle->set_is_static(obstacle->IsStill());
292 if (FLAGS_prediction_offline_mode ==
293 PredictionConstants::kDumpPredictionResult) {
294 const Scenario& scenario = obstacles_container->curr_scenario();
295 FeatureOutput::InsertPredictionResult(obstacle, *prediction_obstacle,
296 obstacle->obstacle_conf(), scenario);
297 }
298}
299
300std::unique_ptr<Predictor> PredictorManager::CreatePredictor(
301 const ObstacleConf::PredictorType& type) {
302 std::unique_ptr<Predictor> predictor_ptr(nullptr);
303 switch (type) {
304 case ObstacleConf::LANE_SEQUENCE_PREDICTOR: {
305 predictor_ptr.reset(new LaneSequencePredictor());
306 break;
307 }
308 case ObstacleConf::MOVE_SEQUENCE_PREDICTOR: {
309 predictor_ptr.reset(new MoveSequencePredictor());
310 break;
311 }
312 case ObstacleConf::SINGLE_LANE_PREDICTOR: {
313 predictor_ptr.reset(new SingleLanePredictor());
314 break;
315 }
316 case ObstacleConf::FREE_MOVE_PREDICTOR: {
317 predictor_ptr.reset(new FreeMovePredictor());
318 break;
319 }
320 case ObstacleConf::JUNCTION_PREDICTOR: {
321 predictor_ptr.reset(new JunctionPredictor());
322 break;
323 }
324 case ObstacleConf::EXTRAPOLATION_PREDICTOR: {
325 predictor_ptr.reset(new ExtrapolationPredictor());
326 break;
327 }
328 case ObstacleConf::INTERACTION_PREDICTOR: {
329 predictor_ptr.reset(new InteractionPredictor());
330 break;
331 }
332 case ObstacleConf::EMPTY_PREDICTOR: {
333 predictor_ptr.reset(new EmptyPredictor());
334 break;
335 }
336 default: { break; }
337 }
338 return predictor_ptr;
339}
340
341void PredictorManager::RegisterPredictor(
342 const ObstacleConf::PredictorType& type) {
343 predictors_[type] = CreatePredictor(type);
344 AINFO << "Predictor [" << type << "] is registered.";
345}
346
347const PredictionObstacles& PredictorManager::prediction_obstacles() {
348 return prediction_obstacles_;
349}
350
351void PredictorManager::InitVehiclePredictors(const ObstacleConf& conf) {
352 if (!conf.has_obstacle_status() && conf.has_interactive_tag()) {
353 vehicle_interactive_predictor_ = conf.predictor_type();
354 }
355 switch (conf.obstacle_status()) {
356 case ObstacleConf::ON_LANE: {
357 if (conf.priority_type() == ObstaclePriority::CAUTION) {
358 vehicle_on_lane_caution_predictor_ = conf.predictor_type();
359 } else {
360 vehicle_on_lane_predictor_ = conf.predictor_type();
361 }
362 break;
363 }
364 case ObstacleConf::OFF_LANE: {
365 vehicle_off_lane_predictor_ = conf.predictor_type();
366 break;
367 }
368 case ObstacleConf::IN_JUNCTION: {
369 if (conf.priority_type() == ObstaclePriority::CAUTION) {
370 vehicle_in_junction_caution_predictor_ = conf.predictor_type();
371 } else {
372 vehicle_in_junction_predictor_ = conf.predictor_type();
373 }
374 break;
375 }
376 default: { break; }
377 }
378}
379
380void PredictorManager::InitCyclistPredictors(const ObstacleConf& conf) {
381 switch (conf.obstacle_status()) {
382 case ObstacleConf::ON_LANE: {
383 cyclist_on_lane_predictor_ = conf.predictor_type();
384 break;
385 }
386 case ObstacleConf::OFF_LANE: {
387 cyclist_off_lane_predictor_ = conf.predictor_type();
388 break;
389 }
390 default: { break; }
391 }
392}
393
394void PredictorManager::InitDefaultPredictors(const ObstacleConf& conf) {
395 switch (conf.obstacle_status()) {
396 case ObstacleConf::ON_LANE: {
397 default_on_lane_predictor_ = conf.predictor_type();
398 break;
399 }
400 case ObstacleConf::OFF_LANE: {
401 default_off_lane_predictor_ = conf.predictor_type();
402 break;
403 }
404 default: { break; }
405 }
406}
407
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)) {
416 return;
417 } else {
418 AERROR << "Obstacle: " << obstacle->id()
419 << " interactive predictor failed!";
420 }
421 }
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_);
427 } else {
428 predictor = GetPredictor(vehicle_default_caution_predictor_);
429 }
430 CHECK_NOTNULL(predictor);
431 if (predictor->Predict(adc_trajectory_container, obstacle,
432 obstacles_container)) {
433 return;
434 } else {
435 AERROR << "Obstacle: " << obstacle->id()
436 << " caution predictor failed, downgrade to normal level!";
437 }
438 }
439
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_);
445 } else {
446 predictor = GetPredictor(vehicle_on_lane_predictor_);
447 }
448
449 if (predictor == nullptr) {
450 AERROR << "Nullptr found for obstacle [" << obstacle->id() << "]";
451 return;
452 }
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);
457 }
458}
459
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() << "]";
467 return;
468 }
469 predictor->Predict(adc_trajectory_container, obstacle, obstacles_container);
470}
471
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_);
478 } else {
479 predictor = GetPredictor(cyclist_off_lane_predictor_);
480 }
481 if (predictor == nullptr) {
482 AERROR << "Nullptr found for obstacle [" << obstacle->id() << "]";
483 return;
484 }
485 predictor->Predict(adc_trajectory_container, obstacle, obstacles_container);
486}
487
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_);
494 } else {
495 predictor = GetPredictor(default_off_lane_predictor_);
496 }
497 if (predictor == nullptr) {
498 AERROR << "Nullptr found for obstacle [" << obstacle->id() << "]";
499 return;
500 }
501 predictor->Predict(adc_trajectory_container, obstacle, obstacles_container);
502}
503
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() << "]";
510 return;
511 }
512 predictor->Predict(adc_trajectory_container, obstacle, obstacles_container);
513}
514
515} // namespace prediction
516} // namespace apollo
Prediction obstacle.
Definition obstacle.h:52
double timestamp() const
Get the obstacle's timestamp.
Definition obstacle.cc:62
perception::PerceptionObstacle::Type type() const
Get the type of perception obstacle's type.
Definition obstacle.cc:54
bool IsStill()
Check if the obstacle is still.
Definition obstacle.cc:95
int id() const
Get the obstacle's ID.
Definition obstacle.cc:60
bool ToIgnore()
Check if the obstacle can be ignored.
Definition obstacle.cc:123
const Feature & latest_feature() const
Get the latest feature.
Definition obstacle.cc:78
const ObstacleConf & obstacle_conf()
Definition obstacle.h:246
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
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 extrapolation predictor
Define lane sequence predictor
Define interaction predictor
Define junction predictor
Define lane sequence predictor
#define ADEBUG
Definition log.h:41
#define AERROR
Definition log.h:44
#define AINFO
Definition log.h:42
Define move sequence predictor
std::unordered_map< int, std::list< Obstacle * > > IdObstacleListMap
class register implement
Definition arena_queue.h:37
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
optional ObstacleStatus obstacle_status