This repository was archived by the owner on Aug 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBestFirstSearchSolver.cpp
More file actions
217 lines (148 loc) · 5.74 KB
/
Copy pathBestFirstSearchSolver.cpp
File metadata and controls
217 lines (148 loc) · 5.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include "BestFirstSearchSolver.h"
BestFirstSearchSolver::BestFirstSearchSolver(const GraphPtr &graphArg): m_graph(graphArg),
m_vertexPtrSet(make_shared<VertexPtrSet> (std::bind(&BestFirstSearchSolver::compareFunction,
this,std::placeholders::_1, std::placeholders::_2)))
{
}
bool BestFirstSearchSolver::solveImpl(const GraphPtr &graphPtr, const VertexPtr &src, const VertexPtr& dest)
{
return solveImpl( graphPtr,src,dest, true); //enable by default the early exit
}
bool BestFirstSearchSolver::solveImpl(const GraphPtr &graphPtr, const VertexPtr& src, const VertexPtr& dest, bool earlyExit)
{
currentTarget = dest;
//vertices are ordered in the set based on Manhattan distance to currentTarget
for (const auto &i : graph()->getVertices()){
vertexPtrSet()->insert(i.second);
}
// printManhattanDistances();
VertexPtrMap verticesMap = graphPtr->getVertices();
if ( src==nullptr || dest==nullptr )
return false;
if(verticesMap.size() < 2){
cerr << "No solution for " << graph()->getVertices().size() << " vertices" << endl;
return false;
}
//checking base case - only 2 points, or less
if( updateVertex(src, 0 ) == false){
//break on error
return false;
}
if(graph()->getVertices().size() == 2){
cerr << "\nFinish! " << "Distance from " << src->getName() << " to " \
<< dest->getName() << " is " << src->getEdges()->front()->getWeight() << endl;
return true;
}
VertexPtr v1 = src;
VertexPtr vertMin = nullptr;
u_int32_t counter = 0; //measuring algorithm efficiency
while(true){
float distManhMin = std::numeric_limits<float>::max();
u_int32_t distMin = std::numeric_limits<u_int32_t>::max();
bool foundTarget = false;
auto edgesListPtr = v1->getEdges();
//find the vertex with lowest Manhattan distance
for (const EdgePtr &e : *edgesListPtr ){
counter++;
if(VertexPtr v2 = e->getDestination()){ //weak_ptr check
if( v2->isVisited() == true)
continue;
v2->setVisited(true);
if(earlyExit && (currentTarget.get() == v2.get())){
vertMin = v2;
distMin = e->getWeight();
foundTarget = true;
break;
}
float dist = manhatanDistanceToTarget(v2);
// cerr << "Comparing " << v2->getName() << " ManhDist: "<< dist << endl;
if(dist < distManhMin){
distManhMin = dist;
vertMin = v2;
distMin = e->getWeight();
}
}
}
VertexPtr vcopy = vertMin;
if(vertMin->getDistance() > v1->getDistance() + distMin){
auto newDist = v1->getDistance() + distMin;
// updateVertex(v2,newDist); // FIXIT: doesn't work
VertexPtr x (vertMin); //copy to reinsert updated Vertex
x->setDistance(newDist);
vertexPtrSet()->erase(vertMin); //update element
vertexPtrSet()->insert(x);
}
// cerr << "Visiting " << vcopy->getName() << " with ManhDist "<< manhatanDistanceToTarget(vcopy) << endl;
//earlyExit is checked already here
if(foundTarget == true){
cerr << "\nEarly exit, took " << counter << " steps to finish."<< endl;
return true;
}
v1 = vcopy;
}
cerr << "\nTook " << counter << " steps to finish."<< endl;
return true;
}
bool BestFirstSearchSolver::solveImplOptimized(const GraphPtr& graphPtr, const VertexPtr &src, const VertexPtr& dest)
{
return solveImplOptimized(graphPtr,src,dest,true);
}
bool BestFirstSearchSolver::solveImplOptimized(const GraphPtr &graphPtr, const VertexPtr &src, const VertexPtr &dest, bool earlyExit)
{
//TODO:
return false;
}
void BestFirstSearchSolver::printManhattanDistances()const{
if(graph()->getVertices().empty()){
return;
}
cerr << "\nManhattan distances: \n" << endl;
for (const auto& p : graph()->getVertices()){
float dist = manhatanDistanceToTarget(p.second);
cerr << p.second->getName() << ": " << dist << endl ;
}
}
bool BestFirstSearchSolver::compareFunction(const VertexPtr &lhs, const VertexPtr &rhs)const
{
float dist1 = manhatanDistanceToTarget(lhs);
float dist2 = manhatanDistanceToTarget(rhs);
return dist1 < dist2;
}
float BestFirstSearchSolver::manhatanDistanceToTarget(const VertexPtr &source)const{
auto iter = manhattanDistanceMap.find(source);
if (iter != manhattanDistanceMap.end() )
{
return iter->second;
}
return -1;
}
float BestFirstSearchSolver::calcManhatanDistanceToTarget(const VertexPtr &source)
{
if(!currentTarget)
return 0;
float dist = manhatanDistanceToTarget(source);
if(dist < 0){
dist = graph()->calculateManhattanDistance(currentTarget,source);
manhattanDistanceMap[source] = dist;
}
return dist;
}
bool BestFirstSearchSolver::updateVertex(const VertexPtr &vert, u_int32_t distance){
if(!vert)
return false;
auto vertexIter = vertexPtrSet()->find(vert);
if (vertexIter == vertexPtrSet()->end()){
return false;
}
VertexPtr x (*vertexIter); //copy to reinsert updated Vertex
x->setDistance(distance);
vertexPtrSet()->erase(vertexIter); //update element
vertexPtrSet()->insert(x);
return true;
}
void BestFirstSearchSolver::printDistances() const{
auto vertPtrSet = vertexPtrSet();
for (const VertexPtr & i : *vertPtrSet ){
cout << i->getName() << " : " << i->getDistance() << endl;
}
}