Apollo  6.0
Open source self driving car software
dst_evidence.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2018 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 #pragma once
17 
18 #include <map>
19 #include <mutex>
20 #include <string>
21 #include <utility>
22 #include <vector>
23 
24 namespace apollo {
25 namespace perception {
26 namespace fusion {
27 
28 struct DstCommonData {
29  // ensure initialize DSTEvidence once
30  bool init_ = false;
31  // fods
32  size_t fod_loc_ = 0;
33  std::vector<uint64_t> fod_subsets_;
34  // for transforming to probability effectively
35  std::vector<size_t> fod_subset_cardinalities_;
36  std::vector<std::string> fod_subset_names_;
37  // for combining two bbas effectively.
38  std::vector<std::vector<std::pair<size_t, size_t>>> combination_relations_;
39  // for computing support vector effectively
40  std::vector<std::vector<size_t>> subset_relations_;
41  // for computing plausibility vector effectively
42  std::vector<std::vector<size_t>> inter_relations_;
43  std::map<uint64_t, size_t> subsets_ind_map_;
44 };
45 
47 
48 // @brief: A singleton class to mange the set of fod subset and the
49 // intersection relationship between them.
50 class DstManager {
51  public:
52  static DstManager* Instance() {
53  static DstManager dst_manager;
54  return &dst_manager;
55  }
56  // brief: app initialization
57  // param [in]: app_name
58  // param [in]: fod_subsets, hypotheses sets
59  // param [in]: fod_subset_names
60  bool AddApp(const std::string& app_name,
61  const std::vector<uint64_t>& fod_subsets,
62  const std::vector<std::string>& fod_subset_names =
63  std::vector<std::string>());
64  bool IsAppAdded(const std::string& app_name);
65 
66  DstCommonDataPtr GetAppDataPtr(const std::string& app_name);
67  size_t FodSubsetToInd(const std::string& app_name,
68  const uint64_t& fod_subset);
69  uint64_t IndToFodSubset(const std::string& app_name, const size_t& ind);
70 
71  private:
72  DstManager() {}
73  void BuildSubsetsIndMap(DstCommonData* dst_data);
74  // fod check, put fod in fod_subsets to ensure BBA's validity after
75  // default construction.
76  void FodCheck(DstCommonData* dst_data);
77  // compute the cardinality of fod_subset which means counting set bits in
78  // an integer
79  void ComputeCardinalities(DstCommonData* st_data);
80  bool ComputeRelations(DstCommonData* dst_data);
81  void BuildNamesMap(const std::vector<std::string>& fod_subset_names,
82  DstCommonData* dst_data);
83 
84  private:
85  // Dst data map
86  std::map<std::string, DstCommonData> dst_common_data_;
87 
88  std::mutex map_mutex_;
89 };
90 
91 class Dst {
92  public:
93  explicit Dst(const std::string& app_name);
94 
95  // setter
96  bool SetBbaVec(const std::vector<double>& bba_vec);
97  // strictly require the fod in bba_map is valid
98  bool SetBba(const std::map<uint64_t, double>& bba_map);
99 
100  void ComputeSptPlsUct() const;
101  void ComputeProbability() const;
102  // getter
103  const std::vector<double>& GetBbaVec() const { return bba_vec_; }
104  const size_t GetBbaSize() const { return bba_vec_.size(); }
105  double GetSubsetBfmass(uint64_t fod_subset) const;
106  double GetIndBfmass(size_t ind) const;
107  const std::vector<double>& GetSupportVec() const { return support_vec_; }
108  const std::vector<double>& GetPlausibilityVec() const {
109  return plausibility_vec_;
110  }
111  const std::vector<double>& GetUncertaintyVec() const {
112  return uncertainty_vec_;
113  }
114  const std::vector<double>& GetProbabilityVec() const {
115  return probability_vec_;
116  }
117  std::string PrintBba() const;
118 
119  friend Dst operator+(const Dst& lhs, const Dst& rhs);
120  friend Dst operator*(const Dst& dst_evidence, double w);
121  std::string Name() const { return app_name_; }
122 
123  private:
124  void Normalize();
125  void SelfCheck() const;
126 
127  private:
128  std::string app_name_;
129  // the construction of following vectors is manual.
130  mutable DstCommonDataPtr dst_data_ptr_ = nullptr;
131  mutable std::vector<double> bba_vec_;
132  mutable std::vector<double> support_vec_;
133  mutable std::vector<double> plausibility_vec_;
134  mutable std::vector<double> uncertainty_vec_;
135  mutable std::vector<double> probability_vec_;
136 };
137 
138 } // namespace fusion
139 } // namespace perception
140 } // namespace apollo
std::vector< std::vector< size_t > > inter_relations_
Definition: dst_evidence.h:42
const std::vector< double > & GetBbaVec() const
Definition: dst_evidence.h:103
std::vector< std::string > fod_subset_names_
Definition: dst_evidence.h:36
Definition: dst_evidence.h:50
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
Angle< T > operator*(Angle< T > lhs, Scalar rhs)
Multiplies an Angle by a scalar.
Definition: angle.h:208
const std::vector< double > & GetSupportVec() const
Definition: dst_evidence.h:107
std::string Name() const
Definition: dst_evidence.h:121
Definition: dst_evidence.h:28
const size_t GetBbaSize() const
Definition: dst_evidence.h:104
std::vector< size_t > fod_subset_cardinalities_
Definition: dst_evidence.h:35
const std::vector< double > & GetProbabilityVec() const
Definition: dst_evidence.h:114
Angle< T > operator+(Angle< T > lhs, Angle< T > rhs)
Sums two angles.
Definition: angle.h:184
std::vector< std::vector< std::pair< size_t, size_t > > > combination_relations_
Definition: dst_evidence.h:38
std::vector< std::vector< size_t > > subset_relations_
Definition: dst_evidence.h:40
Definition: dst_evidence.h:91
std::vector< uint64_t > fod_subsets_
Definition: dst_evidence.h:33
bool init_
Definition: dst_evidence.h:30
const std::vector< double > & GetUncertaintyVec() const
Definition: dst_evidence.h:111
std::map< uint64_t, size_t > subsets_ind_map_
Definition: dst_evidence.h:43
size_t fod_loc_
Definition: dst_evidence.h:32
DstCommonData * DstCommonDataPtr
Definition: dst_evidence.h:46
const std::vector< double > & GetPlausibilityVec() const
Definition: dst_evidence.h:108
void Normalize(Vectord *prob)
static DstManager * Instance()
Definition: dst_evidence.h:52