evaluator_manager.cc 4.0 KB
Newer Older
C
Calvin Miao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/******************************************************************************
 * Copyright 2017 The Apollo Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *****************************************************************************/

#include "modules/prediction/evaluator/evaluator_manager.h"

19
#include "modules/common/log.h"
20
#include "modules/prediction/evaluator/vehicle/mlp_evaluator.h"
21 22 23
#include "modules/prediction/container/container_manager.h"
#include "modules/prediction/container/obstacles/obstacles_container.h"

C
Calvin Miao 已提交
24 25 26
namespace apollo {
namespace prediction {

27 28
using ::apollo::perception::PerceptionObstacles;
using ::apollo::perception::PerceptionObstacle;
29
using ::apollo::common::adapter::AdapterConfig;
30

31 32 33 34 35 36 37
EvaluatorManager::EvaluatorManager() {
  RegisterEvaluators();
}

void EvaluatorManager::RegisterEvaluators() {
  RegisterEvaluator(ObstacleConf::MLP_EVALUATOR);
}
C
Calvin Miao 已提交
38

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
void EvaluatorManager::Init(const PredictionConf& config) {
  for (const auto& obstacle_conf : config.obstacle_conf()) {
    if (!obstacle_conf.has_obstacle_type()) {
      ADEBUG << "Obstacle config ["
             << obstacle_conf.ShortDebugString()
             << "] has not defined obstacle type, status or evaluator type.";
      continue;
    }

    if (obstacle_conf.obstacle_type() == PerceptionObstacle::VEHICLE) {
      if (!obstacle_conf.has_obstacle_status() ||
          !obstacle_conf.has_evaluator_type()) {
        ADEBUG << "Vehicle obstacle config ["
               << obstacle_conf.ShortDebugString()
               << "] has not defined obstacle status and evaluator type.";
        continue;
      } else if (obstacle_conf.obstacle_status() == ObstacleConf::ON_LANE) {
        vehicle_on_lane_evaluator_ = obstacle_conf.evaluator_type();
      }
    }
  }

  AINFO << "Defined vehicle on lane obstacle evaluator ["
        << vehicle_on_lane_evaluator_ << "]";
}
64

65 66
Evaluator* EvaluatorManager::GetEvaluator(
    const ObstacleConf::EvaluatorType& type) {
67 68
  auto it = evaluators_.find(type);
  return it != evaluators_.end() ? it->second.get() : nullptr;
C
Calvin Miao 已提交
69 70 71
}

void EvaluatorManager::Run(
72 73
    const ::apollo::perception::PerceptionObstacles& perception_obstacles) {
  ObstaclesContainer *container = dynamic_cast<ObstaclesContainer*>(
74 75
      ContainerManager::instance()->GetContainer(
      AdapterConfig::PERCEPTION_OBSTACLES));
76
  CHECK_NOTNULL(container);
77 78

  Evaluator *evaluator = nullptr;
79 80 81
  for (const auto& perception_obstacle :
      perception_obstacles.perception_obstacle()) {
    int id = perception_obstacle.id();
82 83
    Obstacle* obstacle = container->GetObstacle(id);
    CHECK_NOTNULL(obstacle);
A
Aaron Xiao 已提交
84
    switch (perception_obstacle.type()) {
85
      case PerceptionObstacle::VEHICLE: {
86
        if (obstacle->IsOnLane()) {
87
          evaluator = GetEvaluator(vehicle_on_lane_evaluator_);
88 89
          CHECK_NOTNULL(evaluator);
        }
90 91 92 93 94 95
        break;
      }
      default: {
        break;
      }
    }
96 97 98
    if (evaluator != nullptr) {
      evaluator->Evaluate(obstacle);
    }
99 100
  }
}
C
Calvin Miao 已提交
101

102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
std::unique_ptr<Evaluator> EvaluatorManager::CreateEvaluator(
    const ObstacleConf::EvaluatorType& type) {
  std::unique_ptr<Evaluator> evaluator_ptr(nullptr);
  switch (type) {
    case ObstacleConf::MLP_EVALUATOR: {
      evaluator_ptr.reset(new MLPEvaluator());
      break;
    }
    default: {
      break;
    }
  }
  return evaluator_ptr;
}

void EvaluatorManager::RegisterEvaluator(
    const ObstacleConf::EvaluatorType& type) {
  evaluators_[type] = CreateEvaluator(type);
C
Calvin Miao 已提交
120
  AINFO << "Evaluator [" << type << "] is registered.";
121 122
}

C
Calvin Miao 已提交
123 124
}  // namespace prediction
}  // namespace apollo