Map Matching
solver.cpp
Go to the documentation of this file.
1 #include "solver.h"
2 #define DEBUG_SOLVER true
3 
4 Solver::Solver(QObject* parent)
5  : QObject(parent)
6 {
7 }
8 
10 {
13  track.spaceFilter(15);
15  emit signalMessage(QString::fromStdString(track.infos()));
18 
19  emit signalMessage(QString::fromStdString(grid.infos()));
20 
24 
26  emit signalMessage("Markov Matrix built");
27 
28  // temp
29  viterbiSetup();
30 }
31 
33 {
34  int noOfStates = grid.getNoOfRoads();
35  int noOfObs = track.getNoOfPoints();
36  // initialize T1, T2 with 0
37  for (int i = 0; i < noOfStates; i++) {
38  T1.push_back(std::vector<float>(noOfObs));
39  T2.push_back(std::vector<float>(noOfObs));
40  }
41  // build bidirectionnal mapping between roadId and index
42  int index = 0;
43  for (auto x : grid.m_mapOfAllRoads) {
44  m_fromIndexToRoadId.push_back(x.first);
45  m_fromRoadIdToIndex[x.first] = index;
46  ++index;
47  }
48 
49  m_currentStep = -1;
50 }
51 
53 {
54  const std::vector<int>& listOfPointId = r->vectorOfPointsId();
55  double d, bestDistance = std::numeric_limits<double>::max();
56 
57  for (uint i = 1; i < listOfPointId.size(); i++) {
58  d = p->distanceToSegment(m_roadPoints->at(listOfPointId[i - 1]), m_roadPoints->at(listOfPointId[i]));
59  if (d < bestDistance) {
60  bestDistance = d;
61  }
62  }
63 
64  if (bestDistance < DISTANCE_THRESHOLD)
65  p->addEmissionProbability(r->edgeId(), bestDistance);
66 
67  if (DEBUG_SOLVER) {
68  std::cout << p->infos() << std::endl;
69  }
70 }
71 
73 {
74  unsigned int i;
75  std::vector<long>* roadPath = new std::vector<long>(0);
76  int roadIndex, prevrI = -1;
77  float bestProbaSoFar = 0.0;
78  for (i = 0; i < m_fromIndexToRoadId.size(); i++) {
79  if (T1[i][m_currentStep] > bestProbaSoFar) {
80  bestProbaSoFar = T1[i][m_currentStep];
81  roadIndex = i;
82  }
83  }
84  roadPath->push_back(m_fromIndexToRoadId[roadIndex]);
85  prevrI = roadIndex;
86  for (int i = m_currentStep - 1; i > 0; i--) {
87  int roadIndex = T2[prevrI][i];
88  if (roadIndex != prevrI)
89  roadPath->push_back(m_fromIndexToRoadId[roadIndex]);
90  prevrI = roadIndex;
91  }
92  emit signalRoadPath(roadPath);
93 }
94 
95 void Solver::onSignalSetGrid(QString s)
96 {
97  m_gridFilename = s;
98 }
99 
101 {
102  m_trackFilename = s;
103 }
104 
106 {
107  emit signalMessage("Solver started");
108  start();
109 }
110 
112 {
113  m_currentStep++;
114  if (m_currentStep < m_trackPoints->size())
116 }
117 
118 void Solver::onSignalNeighbours(std::vector<long>* roadsId)
119 {
120  std::unordered_map<long, Road>::iterator idToRoad; // find road from its id
121  Road* r;
122  std::cout << "Neighbour roads " << roadsId->size() << std::endl;
123  // compute distance current point -> roads then probabilities (emission matrix)
125  if (p->m_emissionProbability.size() > 0)
126  return;
127  for (const auto id : *roadsId) {
128  idToRoad = grid.m_mapOfAllRoads.find(id);
129  // Road *r= ...
130  r = &idToRoad->second;
131  std::cout << p->x() << " distance " << r->edgeId() << std::endl;
132  setDistance(p, r);
133  }
134  p->updateProbability();
135  /*
136  // compute straight distance over road distance then probabilities (transition matrix)
137  double v;
138  if (m_currentStep>0) {
139  for (const auto prevId: *prevRoadIds) {
140  for (const auto id: *roadsId) {
141  v= grid.computeDistanceFraction(prevPoint, p, prevId, id);
142  }
143  }
144  }
145 */
146 
147  // update T1, T2
148  int roadIndex;
149  int neighIndex;
150  std::unordered_map<long, int>::iterator idToIdx; // find index of road from its id
151  if (m_currentStep == 0) {
152  // for all neighbouring roads of the first point, apply the emission probability (here PI=1 for any couple (state,obs) (ie (road, point))
153  for (const auto ep : p->m_emissionProbability) {
154  idToIdx = m_fromRoadIdToIndex.find(ep.roadId());
155  roadIndex = idToIdx->second;
156  T1[roadIndex][m_currentStep] = ep.probability();
157  }
158  } else {
159  for (const auto ep : p->m_emissionProbability) {
160  idToIdx = m_fromRoadIdToIndex.find(ep.roadId());
161  roadIndex = idToIdx->second;
162  // find all connexions to this road
163  idToRoad = grid.m_mapOfAllRoads.find(ep.roadId());
164  r = &idToRoad->second;
165  float maxT1 = 0.0; // used to build T2
166  int maxT1roadId = 0;
167  for (auto neigh : r->m_setOfNeighbors) {
168  neighIndex = getIndexFromRoadId(neigh);
169  T1[roadIndex][m_currentStep] = std::max(T1[roadIndex][m_currentStep],
170  T1[neighIndex][m_currentStep - 1] * 1 * ep.probability());
171  if (maxT1 < T1[neighIndex][m_currentStep - 1] * 1) {
172  maxT1 = T1[neighIndex][m_currentStep - 1] * 1;
173  maxT1roadId = neighIndex;
174  }
175  }
176  T2[roadIndex][m_currentStep] = maxT1roadId;
177  }
178  }
179  buildRoadPath();
180 }
181 
182 void Solver::readFiles(File file1, File file2)
183 {
184  File fileTrack = file1;
185  File fileGrid = file2;
186  QString fileT = fileTrack.filePath.at(0) + fileTrack.fileName.at(0) + "." + fileTrack.fileExtension.at(0);
187  QString fileG = fileGrid.filePath.at(0) + fileGrid.fileName.at(0) + "." + fileGrid.fileExtension.at(0);
188 
189  track.readFromCSV(fileT);
190  emit signalMessage(QString::fromStdString(track.infos()));
191 
193  grid.readFromCSV(fileG);
194  emit signalMessage(QString::fromStdString(grid.infos()));
196 }
197 
198 void Solver::filterSpace(double val)
199 {
200  track.spaceFilter(val);
204 }
205 
206 void Solver::filterTemp(int val)
207 {
208  track.temporalFilter(val);
212 }
213 
215 {
216  std::unordered_map<long, int>::iterator idToIdx = m_fromRoadIdToIndex.find(id);
217  return idToIdx->second;
218 }
void buildMarkovMatrix()
buildMarkovMatrix
Definition: grid.cpp:264
void onSignalStart()
Definition: solver.cpp:105
void viterbiSetup()
Definition: solver.cpp:32
double distanceToSegment(const Point &p1, const Point &p2) const
Compute the distance between a point and a segment.
Definition: point.cpp:32
double m_xMin
Definition: track.h:107
void temporalFilter(uint interval)
This is a temporal filter, which deletes points depending on a time value.
Definition: track.cpp:231
std::vector< EmissionProbability > m_emissionProbability
Definition: pointGPS.h:42
QStringList fileName
Definition: file.h:54
void signalCurrentPoint(int id)
std::vector< std::vector< float > > T2
Definition: solver.h:79
The File class.
Definition: file.h:13
std::set< long > m_setOfNeighbors
Set of all roadId connected to this one (including this one)
Definition: road.h:79
unsigned int m_currentStep
Definition: solver.h:78
const std::vector< int > & vectorOfPointsId() const
Get the vector of points composing the road.
Definition: road.h:71
double yMaxGrid() const
Definition: grid.h:127
Track track
Definition: solver.h:57
Solver(QObject *parent=0)
Definition: solver.cpp:4
void onSignalNeighbours(std::vector< long > *roadsId)
Definition: solver.cpp:118
QString m_gridFilename
Definition: solver.h:54
void readFromCSV(QString filename)
Reads a csv file and inserts each point in m_points vector.
Definition: track.cpp:36
std::vector< PointGPS * > * getPointsAsPointer()
Definition: track.h:99
int getIndexFromRoadId(long id)
Definition: solver.cpp:214
AllRoadMap m_mapOfAllRoads
Definition: grid.h:129
QStringList filePath
Definition: file.h:53
Road is an element of a network. Road are strongly linked with Points.
Definition: road.h:14
int getNoOfRoads() const
Getters.
Definition: grid.h:118
void setDistance(PointGPS *p, Road *r)
setDistance Calculate distance between GPS point et Road
Definition: solver.cpp:52
double xMaxGrid() const
Definition: grid.h:125
QStringList fileExtension
Definition: file.h:55
#define DEBUG_SOLVER
Definition: solver.cpp:2
void addEmissionProbability(long roadId, double distance)
Definition: pointGPS.cpp:5
double x() const
Definition: point.cpp:75
Grid grid
Definition: solver.h:58
void setTrackBoundingBox(double xMin, double xMax, double yMin, double yMax)
Save data about our area of roads.
Definition: grid.cpp:34
std::vector< PointGPS * > * m_trackPoints
Definition: solver.h:61
std::vector< PointRoad > m_vectorOfPoints
Definition: grid.h:130
#define DISTANCE_THRESHOLD
Definition: track.h:21
void start()
Definition: solver.cpp:9
double m_yMin
Definition: track.h:109
std::vector< std::vector< float > > T1
Definition: solver.h:79
std::string infos()
Definition: grid.cpp:244
void spaceFilter(double interval)
This is a space filter, which deletes points depending on a distance interval.
Definition: track.cpp:258
int getNoOfPoints()
Definition: track.h:100
double yMinGrid() const
Definition: grid.h:126
void signalMessage(QString)
void filterSpace(double val)
filterSpace Use Spatial filter with a value
Definition: solver.cpp:198
std::unordered_map< long, int > m_fromRoadIdToIndex
Definition: solver.h:81
void filterTemp(int val)
filterTemp Use Temporal filter with a value
Definition: solver.cpp:206
void onSignalNextStep()
Definition: solver.cpp:111
long edgeId() const
Get the id of the road.
Definition: road.h:65
void signalDimension(double xMin, double xMax, double yMin, double yMax)
std::string infos() const
Definition: pointGPS.cpp:20
void signalRoadPath(std::vector< long > *rp)
void onSignalSetTrack(QString s)
Definition: solver.cpp:100
double m_yMax
Definition: track.h:110
std::vector< long > m_fromIndexToRoadId
Definition: solver.h:80
void readFiles(File file1, File file2)
readFiles Read files : track and grid
Definition: solver.cpp:182
std::vector< PointRoad > * m_roadPoints
Definition: solver.h:60
std::string infos()
Return a string containing information about the content of this object.
Definition: track.cpp:311
QString m_trackFilename
Definition: solver.h:55
double m_xMax
Definition: track.h:108
void signalAllPoints(std::vector< PointGPS * > *)
void signalAllRoads(std::unordered_map< long, Road > *, std::vector< PointRoad > *)
void readFromCSV(QString filename)
Reads a csv file and inserts info into the corresponding attributs.
Definition: grid.cpp:42
void onSignalSetGrid(QString s)
Definition: solver.cpp:95
double xMinGrid() const
Definition: grid.h:124
void updateProbability()
Definition: pointGPS.cpp:11
void buildRoadPath()
Definition: solver.cpp:72