Fast map matching  0.1.0
transition_graph.cpp
1 
10 #include "mm/transition_graph.hpp"
11 #include "network/type.hpp"
12 #include "util/debug.hpp"
13 
14 using namespace FMM;
15 using namespace FMM::CORE;
16 using namespace FMM::NETWORK;
17 using namespace FMM::MM;
18 
19 TransitionGraph::TransitionGraph(const Traj_Candidates &tc, double gps_error){
20  for (auto cs = tc.begin(); cs!=tc.end(); ++cs) {
21  TGLayer layer;
22  for (auto iter = cs->begin(); iter!=cs->end(); ++iter) {
23  double ep = calc_ep(iter->dist,gps_error);
24  layer.push_back(TGNode{&(*iter),nullptr,ep,0});
25  }
26  layers.push_back(layer);
27  }
28  if (!tc.empty()) {
29  reset_layer(&(layers[0]));
30  }
31 }
32 
33 double TransitionGraph::calc_tp(double sp_dist,double eu_dist){
34  return eu_dist>=sp_dist ? (sp_dist+1e-6)/(eu_dist+1e-6) : eu_dist/sp_dist;
35 }
36 
37 double TransitionGraph::calc_ep(double dist,double error){
38  double a = dist / error;
39  return exp(-0.5 * a * a);
40 }
41 
42 // Reset the properties of a candidate set
43 void TransitionGraph::reset_layer(TGLayer *layer){
44  for (auto iter=layer->begin(); iter!=layer->end(); ++iter) {
45  iter->cumu_prob = iter->ep;
46  iter->prev = nullptr;
47  }
48 }
49 
50 const TGNode *TransitionGraph::find_optimal_candidate(const TGLayer &layer){
51  const TGNode *opt_c=nullptr;
52  double final_prob = -0.001;
53  for (auto c = layer.begin(); c!=layer.end(); ++c) {
54  if(final_prob < c->cumu_prob) {
55  final_prob = c->cumu_prob;
56  opt_c = &(*c);
57  }
58  }
59  return opt_c;
60 }
61 
62 TGOpath TransitionGraph::backtrack(){
63  SPDLOG_TRACE("Backtrack on transition graph");
64  TGNode* track_cand=nullptr;
65  double final_prob = -0.001;
66  std::vector<TGNode>& last_layer = layers.back();
67  for (auto c = last_layer.begin(); c!=last_layer.end(); ++c) {
68  if(final_prob < c->cumu_prob) {
69  final_prob = c->cumu_prob;
70  track_cand = &(*c);
71  }
72  }
73  TGOpath opath;
74  if (final_prob>0) {
75  opath.push_back(track_cand);
76  // Iterate from tail to head to assign path
77  while ((track_cand=track_cand->prev)!=nullptr) {
78  opath.push_back(track_cand);
79  }
80  std::reverse(opath.begin(), opath.end());
81  }
82  SPDLOG_TRACE("Backtrack on transition graph done");
83  return opath;
84 }
85 
86 std::vector<TGLayer> &TransitionGraph::get_layers(){
87  return layers;
88 }
FMM::MM::Traj_Candidates
std::vector< Point_Candidates > Traj_Candidates
trajectory candidates
Definition: mm_type.hpp:38
FMM::MM::TGOpath
std::vector< const TGNode * > TGOpath
The optimal path of nodes in the transition graph.
Definition: transition_graph.hpp:46
FMM
Fast map matching.
Definition: geom_algorithm.hpp:17
FMM::MM::TGNode::cumu_prob
double cumu_prob
current node's accumulative probability
Definition: transition_graph.hpp:34
FMM::CORE
Core data types.
Definition: geometry.hpp:22
FMM::MM
Class related with map matching.
Definition: composite_graph.hpp:18
FMM::NETWORK
Classes related with network and graph.
Definition: graph.hpp:21
FMM::MM::TGNode
A node in the transition graph.
Definition: transition_graph.hpp:29
FMM::MM::TGLayer
std::vector< TGNode > TGLayer
A layer of nodes in the transition graph.
Definition: transition_graph.hpp:42
FMM::MM::TGNode::prev
TGNode * prev
previous optimal candidate
Definition: transition_graph.hpp:31