50bool IsTrainable(
const Feature& feature) {
51 if (feature.
id() == FLAGS_ego_vehicle_id) {
63 int caution_thread_idx = 0;
64 for (
int obstacle_id :
67 if (obstacle_ptr ==
nullptr) {
68 AERROR <<
"Null obstacle [" << obstacle_id <<
"] found";
72 ADEBUG <<
"Ignore still obstacle [" << obstacle_id <<
"]";
77 ADEBUG <<
"Skip ignored obstacle [" << obstacle_id <<
"]";
80 caution_thread_idx = caution_thread_idx % FLAGS_max_caution_thread_num;
81 (*id_obstacle_map)[caution_thread_idx].push_back(obstacle_ptr);
82 ADEBUG <<
"Cautioned obstacle [" << obstacle_id <<
"] for thread"
83 << caution_thread_idx;
86 int normal_thread_num =
87 FLAGS_max_thread_num - FLAGS_max_caution_thread_num;
89 obstacle_id % normal_thread_num + FLAGS_max_caution_thread_num;
90 (*id_obstacle_map)[id_mod].push_back(obstacle_ptr);
91 ADEBUG <<
"Normal obstacle [" << obstacle_id <<
"] for thread" << id_mod;
100void EvaluatorManager::RegisterEvaluators() {
109 if (!FLAGS_enable_multi_agent_pedestrian_evaluator) {
112 if (!FLAGS_enable_multi_agent_vehicle_evaluator) {
116 if (FLAGS_enable_multi_agent_pedestrian_evaluator ||
117 FLAGS_enable_multi_agent_vehicle_evaluator) {
123 if (FLAGS_enable_semantic_map) {
125 semantic_map_->Init();
126 AINFO <<
"Init SemanticMap instance.";
128 AINFO <<
"SemanticMap is not enabled.";
131 RegisterEvaluators();
134 if (!obstacle_conf.has_obstacle_type()) {
135 AERROR <<
"Obstacle config [" << obstacle_conf.ShortDebugString()
136 <<
"] has not defined obstacle type.";
140 if (!obstacle_conf.has_evaluator_type()) {
141 ADEBUG <<
"Obstacle config [" << obstacle_conf.ShortDebugString()
142 <<
"] has not defined evaluator type.";
146 if (obstacle_conf.has_obstacle_status()) {
147 switch (obstacle_conf.obstacle_type()) {
151 vehicle_on_lane_caution_evaluator_ =
152 obstacle_conf.evaluator_type();
154 vehicle_on_lane_evaluator_ = obstacle_conf.evaluator_type();
159 vehicle_in_junction_caution_evaluator_ =
160 obstacle_conf.evaluator_type();
162 vehicle_in_junction_evaluator_ = obstacle_conf.evaluator_type();
169 cyclist_on_lane_evaluator_ = obstacle_conf.evaluator_type();
174 if (FLAGS_prediction_offline_mode ==
177 pedestrian_evaluator_ = obstacle_conf.evaluator_type();
183 default_on_lane_evaluator_ = obstacle_conf.evaluator_type();
191 }
else if (obstacle_conf.has_interactive_tag()) {
192 interaction_evaluator_ = obstacle_conf.evaluator_type();
196 AINFO <<
"Defined vehicle on lane obstacle evaluator ["
197 << vehicle_on_lane_evaluator_ <<
"]";
198 AINFO <<
"Defined cyclist on lane obstacle evaluator ["
199 << cyclist_on_lane_evaluator_ <<
"]";
200 AINFO <<
"Defined default on lane obstacle evaluator ["
201 << default_on_lane_evaluator_ <<
"]";
206 auto it = evaluators_.find(type);
207 return it != evaluators_.end() ? it->second.get() :
nullptr;
213 if (FLAGS_enable_semantic_map ||
215 size_t max_num_frame = 10;
219 BuildObstacleIdHistoryMap(obstacles_container, max_num_frame);
220 DumpCurrentFrameEnv(obstacles_container);
224 semantic_map_->RunCurrFrame(obstacle_id_history_map_);
227 std::vector<Obstacle*> dynamic_env;
229 if (FLAGS_enable_multi_agent_pedestrian_evaluator ||
230 FLAGS_enable_multi_agent_vehicle_evaluator) {
231 auto start_time_multi = std::chrono::system_clock::now();
233 auto end_time_multi = std::chrono::system_clock::now();
234 std::chrono::duration<double> time_cost_multi =
235 end_time_multi - start_time_multi;
236 AINFO <<
"multi agents evaluator used time: "
237 << time_cost_multi.count() * 1000 <<
" ms.";
240 if (FLAGS_enable_multi_thread) {
242 GroupObstaclesByObstacleIds(obstacles_container, &id_obstacle_map);
244 id_obstacle_map.begin(), id_obstacle_map.end(),
245 [&](IdObstacleListMap::iterator::value_type& obstacles_iter) {
246 for (auto obstacle_ptr : obstacles_iter.second) {
247 EvaluateObstacle(adc_trajectory_container, obstacle_ptr,
248 obstacles_container, dynamic_env);
252 for (
int id : obstacles_container->curr_frame_considered_obstacle_ids()) {
253 Obstacle* obstacle = obstacles_container->GetObstacle(
id);
255 if (obstacle ==
nullptr) {
259 ADEBUG <<
"Ignore still obstacle [" <<
id <<
"] in evaluator_manager";
263 EvaluateObstacle(adc_trajectory_container, obstacle,
264 obstacles_container, dynamic_env);
269void EvaluatorManager::EvaluateObstacle(
273 std::vector<Obstacle*> dynamic_env) {
276 switch (obstacle->
type()) {
277 case PerceptionObstacle::VEHICLE: {
278 if (FLAGS_enable_multi_agent_vehicle_evaluator) {
279 AINFO <<
"The vehicles are evaluated by multi agent evaluator!";
284 evaluator = GetEvaluator(interaction_evaluator_);
286 evaluator = GetEvaluator(vehicle_in_junction_caution_evaluator_);
288 evaluator = GetEvaluator(vehicle_on_lane_caution_evaluator_);
290 evaluator = GetEvaluator(vehicle_default_caution_evaluator_);
292 CHECK_NOTNULL(evaluator);
293 AINFO <<
"Caution Obstacle: " << obstacle->
id() <<
" used " << evaluator->
GetName();
295 if (evaluator->
GetName() ==
"JOINTLY_PREDICTION_PLANNING_EVALUATOR") {
296 if (evaluator->
Evaluate(adc_trajectory_container,
297 obstacle, obstacles_container)) {
300 AERROR <<
"Obstacle: " << obstacle->
id()
301 <<
" interaction evaluator failed,"
302 <<
" downgrade to normal level!";
305 if (evaluator->
Evaluate(obstacle, obstacles_container)) {
308 AERROR <<
"Obstacle: " << obstacle->
id()
309 <<
" caution evaluator failed, downgrade to normal level!";
317 evaluator = GetEvaluator(vehicle_in_junction_evaluator_);
319 evaluator = GetEvaluator(vehicle_on_lane_evaluator_);
321 AINFO <<
"Obstacle: " << obstacle->
id()
322 <<
" is neither on lane, nor in junction. Skip evaluating.";
325 AINFO <<
"Normal Obstacle: " << obstacle->
id() <<
" used " << evaluator->
GetName();
326 CHECK_NOTNULL(evaluator);
327 if (evaluator->
GetName() ==
"LANE_SCANNING_EVALUATOR") {
328 evaluator->
Evaluate(obstacle, obstacles_container, dynamic_env);
330 evaluator->
Evaluate(obstacle, obstacles_container);
334 case PerceptionObstacle::BICYCLE: {
336 evaluator = GetEvaluator(cyclist_on_lane_evaluator_);
337 CHECK_NOTNULL(evaluator);
338 evaluator->
Evaluate(obstacle, obstacles_container);
342 case PerceptionObstacle::PEDESTRIAN: {
343 if (FLAGS_prediction_offline_mode ==
344 PredictionConstants::kDumpDataForLearning ||
345 (!FLAGS_enable_multi_agent_pedestrian_evaluator &&
347 ObstaclePriority::CAUTION)) {
348 evaluator = GetEvaluator(pedestrian_evaluator_);
349 CHECK_NOTNULL(evaluator);
350 auto start_time_inference = std::chrono::system_clock::now();
351 evaluator->
Evaluate(obstacle, obstacles_container);
352 auto end_time_inference = std::chrono::system_clock::now();
353 std::chrono::duration<double> time_cost_lstm =
354 end_time_inference - start_time_inference;
355 AINFO <<
"semantic lstm evaluator used time: "
356 << time_cost_lstm.count() * 1000 <<
" ms.";
362 evaluator = GetEvaluator(default_on_lane_evaluator_);
363 CHECK_NOTNULL(evaluator);
364 evaluator->
Evaluate(obstacle, obstacles_container);
371void EvaluatorManager::EvaluateMultiObstacle(
375 evaluator = GetEvaluator(multi_agent_evaluator_);
376 CHECK_NOTNULL(evaluator);
380 if (FLAGS_enable_multi_agent_pedestrian_evaluator) {
381 for (
int id : obs_ids) {
386 evaluator->
Evaluate(adc_trajectory_container,
387 obstacle, obstacles_container);
388 AINFO <<
"Succeed to run multi agent pedestrian evaluator!";
395 if (FLAGS_enable_multi_agent_vehicle_evaluator) {
396 for (
int id : obs_ids) {
401 evaluator->
Evaluate(adc_trajectory_container,
402 obstacle, obstacles_container);
403 AINFO <<
"Succeed to run multi agent vehicle evaluator!";
410void EvaluatorManager::EvaluateObstacle(
412 std::vector<Obstacle*> dummy_dynamic_env;
414 EvaluateObstacle(adc_trajectory_container, obstacle,
415 obstacles_container, dummy_dynamic_env);
418void EvaluatorManager::BuildObstacleIdHistoryMap(
420 obstacle_id_history_map_.clear();
421 std::vector<int> obstacle_ids =
423 obstacle_ids.push_back(FLAGS_ego_vehicle_id);
424 for (
int id : obstacle_ids) {
426 if (obstacle ==
nullptr || obstacle->
history_size() == 0) {
429 size_t num_frames = std::min(max_num_frame, obstacle->
history_size());
430 for (
size_t i = 0; i < num_frames; ++i) {
431 const Feature& obstacle_feature = obstacle->
feature(i);
433 feature.set_id(obstacle_feature.id());
434 feature.set_timestamp(obstacle_feature.timestamp());
435 feature.set_type(obstacle_feature.type());
436 feature.mutable_position()->CopyFrom(obstacle_feature.position());
437 feature.set_theta(obstacle_feature.velocity_heading());
438 feature.mutable_interactive_tag()->CopyFrom(
439 obstacle_feature.interactive_tag());
440 feature.mutable_priority()->CopyFrom(obstacle_feature.priority());
442 if (obstacle_feature.id() != FLAGS_ego_vehicle_id) {
443 feature.mutable_polygon_point()->CopyFrom(
444 obstacle_feature.polygon_point());
445 feature.set_length(obstacle_feature.length());
446 feature.set_width(obstacle_feature.width());
448 const auto& vehicle_config =
449 common::VehicleConfigHelper::Instance()->GetConfig();
450 feature.set_length(vehicle_config.vehicle_param().length());
451 feature.set_width(vehicle_config.vehicle_param().width());
453 obstacle_id_history_map_[id].add_feature()->CopyFrom(feature);
455 obstacle_id_history_map_[id].set_is_trainable(
460void EvaluatorManager::DumpCurrentFrameEnv(
461 ObstaclesContainer* obstacles_container) {
462 FrameEnv curr_frame_env;
463 curr_frame_env.set_timestamp(obstacles_container->timestamp());
464 for (
const auto obstacle_id_history_pair : obstacle_id_history_map_) {
465 int id = obstacle_id_history_pair.first;
466 if (
id != FLAGS_ego_vehicle_id) {
467 curr_frame_env.add_obstacles_history()->CopyFrom(
468 obstacle_id_history_pair.second);
470 curr_frame_env.mutable_ego_history()->CopyFrom(
471 obstacle_id_history_pair.second);
474 FeatureOutput::InsertFrameEnv(curr_frame_env);
477std::unique_ptr<Evaluator> EvaluatorManager::CreateEvaluator(
478 const ObstacleConf::EvaluatorType& type) {
479 std::unique_ptr<Evaluator> evaluator_ptr(
nullptr);
481 case ObstacleConf::MLP_EVALUATOR: {
482 evaluator_ptr.reset(
new MLPEvaluator());
485 case ObstacleConf::CRUISE_MLP_EVALUATOR: {
486 evaluator_ptr.reset(
new CruiseMLPEvaluator());
489 case ObstacleConf::JUNCTION_MLP_EVALUATOR: {
490 evaluator_ptr.reset(
new JunctionMLPEvaluator());
493 case ObstacleConf::COST_EVALUATOR: {
494 evaluator_ptr.reset(
new CostEvaluator());
497 case ObstacleConf::CYCLIST_KEEP_LANE_EVALUATOR: {
498 evaluator_ptr.reset(
new CyclistKeepLaneEvaluator());
501 case ObstacleConf::LANE_SCANNING_EVALUATOR: {
502 evaluator_ptr.reset(
new LaneScanningEvaluator());
505 case ObstacleConf::LANE_AGGREGATING_EVALUATOR: {
506 evaluator_ptr.reset(
new LaneAggregatingEvaluator());
509 case ObstacleConf::JUNCTION_MAP_EVALUATOR: {
510 evaluator_ptr.reset(
new JunctionMapEvaluator(semantic_map_.get()));
513 case ObstacleConf::SEMANTIC_LSTM_EVALUATOR: {
514 evaluator_ptr.reset(
new SemanticLSTMEvaluator(semantic_map_.get()));
517 case ObstacleConf::JOINTLY_PREDICTION_PLANNING_EVALUATOR: {
518 evaluator_ptr.reset(
new JointlyPredictionPlanningEvaluator());
521 case ObstacleConf::VECTORNET_EVALUATOR: {
522 evaluator_ptr.reset(
new VectornetEvaluator());
525 case ObstacleConf::MULTI_AGENT_EVALUATOR: {
526 evaluator_ptr.reset(
new MultiAgentEvaluator());
533 return evaluator_ptr;
536void EvaluatorManager::RegisterEvaluator(
537 const ObstacleConf::EvaluatorType& type) {
538 evaluators_[type] = CreateEvaluator(type);
539 AINFO <<
"Evaluator [" << type <<
"] is registered.";
Evaluator * GetEvaluator(const ObstacleConf::EvaluatorType &type)
Get evaluator
void EvaluateMultiObstacle(const ADCTrajectoryContainer *adc_trajectory_container, ObstaclesContainer *obstacles_container)
void Run(const ADCTrajectoryContainer *adc_trajectory_container, ObstaclesContainer *obstacles_container)
Run evaluators
void Init(const PredictionConf &config)
Initializer
EvaluatorManager()
Constructor
virtual bool Evaluate(Obstacle *obstacle, ObstaclesContainer *obstacles_container)=0
Evaluate an obstacle
virtual std::string GetName()=0
Get the name of evaluator
bool HasJunctionFeatureWithExits() const
Check if the obstacle has junction feature.
bool IsNearJunction()
Check if the obstacle is near a junction.
perception::PerceptionObstacle::Type type() const
Get the type of perception obstacle's type.
bool IsCloseToJunctionExit() const
Check if the obstacle is close to a junction exit.
bool IsStill()
Check if the obstacle is still.
int id() const
Get the obstacle's ID.
size_t history_size() const
Get the number of historical features.
const Feature & feature(const size_t i) const
Get the ith feature from latest to earliest.
bool IsInteractiveObstacle() const
bool IsOnLane() const
Check if the obstacle is on any lane.
bool IsSlow()
Check if the obstacle is slow.
const Feature & latest_feature() const
Get the latest feature.
const std::vector< int > & curr_frame_movable_obstacle_ids()
Get movable obstacle IDs in the current frame
const std::vector< int > & curr_frame_considered_obstacle_ids()
Get non-ignore obstacle IDs in the current frame
Obstacle * GetObstacle(const int id)
Get obstacle pointer
static const int kDumpFrameEnv
static const int kDumpDataForLearning
static void ForEach(InputIter begin, InputIter end, F f)
Use container manager to manage all containers
Define the cyclist keep lane predictor class
Define the lane aggregating evaluator class
bool IsTrainable(const Feature &feature)
std::unordered_map< int, std::list< Obstacle * > > IdObstacleListMap
Use evaluator manager to manage all evaluators
Holds all global constants for the prediction module.
optional apollo::perception::PerceptionObstacle::Type type
optional ObstaclePriority priority
@ SEMANTIC_LSTM_EVALUATOR
@ CYCLIST_KEEP_LANE_EVALUATOR
@ LANE_AGGREGATING_EVALUATOR
@ LANE_SCANNING_EVALUATOR
@ JOINTLY_PREDICTION_PLANNING_EVALUATOR
optional Priority priority
repeated ObstacleConf obstacle_conf