-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtilities.cc
288 lines (236 loc) · 12.7 KB
/
Utilities.cc
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
// Utility functions, in a similar vein as the things in "Misc.cc" and "Misc.h", but in a more
// involved scope. These functions are implementations of handy (hopefully generic) routines
// which might be useful to have lying around sometime.
#include <algorithm>
#include <functional>
#include <map>
#include <vector>
#include "Utilities.h"
#include <cmath> //Used if taxicab distance is used (fabs.)
#include <iostream>
// H_FUNC ---> A* Heuristic function.
// Takes two std::vector<int>'s and returns H_FUNC_TYPE (like, a double, or something.)
// D_FUNC ---> A* Distance function.
// Takes two std::vector<int>'s and returns H_FUNC_TYPE (like, a double, or something.)
// N_FUNC ---> A* Neighbour function.
// Takes one std::vector<int>, a vector of std::vector<int>'s, and returns void.
// Fills the latter with neighbouring nodes.
//
template <typename H_FUNC_TYPE, typename H_FUNC, typename D_FUNC, typename N_FUNC>
std::vector<std::vector<int>> _AStar_FULL(std::vector<int> A, std::vector<int> B, H_FUNC Heuristic_F,
D_FUNC Distance_F, N_FUNC Neighbour_F) {
// A is the starting point and B is the end point.
std::vector<std::vector<int>> result;
// Avoid doing anything if there is no need to.
if((A == B) || (A.size() != B.size())) {
result.push_back(A);
result.push_back(B);
}
std::vector<std::vector<int>> closedset; // A collection of previously-visited nodes.
std::vector<std::vector<int>> openset; // The set of all nodes to be evaluated. Initially holds the starting point.
// openset.push_back(A);
std::map<std::vector<int>, std::vector<int>>
came_from; // A collection of navigated nodes. Keeps track of where each node branched from.
// Score storage.
std::map<std::vector<int>, H_FUNC_TYPE> g_score; // The path *cost* from starting point to current point.
// g_score[A] = static_cast<H_FUNC_TYPE>( 0 );
std::map<std::vector<int>, H_FUNC_TYPE> h_score; // The heuristic cost from a point to the goal point.
// h_score[A] = static_cast<H_FUNC_TYPE>( HeuristicFunc(A,B) );
std::map<std::vector<int>, H_FUNC_TYPE> f_score; // g + h --> This is the metric we want to minimize.
// f_score[A] = h_score[A] + g_score[A];
//------------------------------------------------------------------------------------
//---------------------------- Lambdas for use locally. ------------------------------
//------------------------------------------------------------------------------------
auto _sorter = [&f_score](const std::vector<int> &a, const std::vector<int> &b) {
// Sort the lowest to the top (so we can pop it off cheaply.)
return f_score[a] > f_score[b];
};
std::function<void(const std::vector<int> &)> _reconstruct_path;
_reconstruct_path = [&came_from, &result, &_reconstruct_path](const std::vector<int> &node) -> void {
// Check to see if the previous node exists.
if(came_from.find(node) != came_from.end()) {
_reconstruct_path(came_from[node]);
result.push_back(came_from[node]);
return;
} else {
return;
}
};
//------------------------------------------------------------------------------------
//-------Lambdas which can be overridden by sending in suitable replacements.---------
//------------------------------------------------------------------------------------
std::function<H_FUNC_TYPE(const std::vector<int> &, const std::vector<int> &)> _Distance;
_Distance = [](const std::vector<int> &a, const std::vector<int> &b) -> H_FUNC_TYPE {
H_FUNC_TYPE res = static_cast<H_FUNC_TYPE>(0);
// Polar distance.
for(unsigned int i = 0; i < a.size(); ++i) res += static_cast<H_FUNC_TYPE>((b[i] - a[i]) * (b[i] - a[i]));
res = sqrt(res);
return static_cast<H_FUNC_TYPE>(res);
/*
//Taxicab distance.
for(unsigned int i=0; i<a.size(); ++i) res += static_cast<H_FUNC_TYPE>(fabs(b[i] - a[i]));
return static_cast<H_FUNC_TYPE>(res);
*/
};
if(Distance_F != nullptr) {
_Distance = Distance_F;
}
std::function<void(const std::vector<int> &, std::vector<std::vector<int>> &)> _Neighbour;
_Neighbour = [](const std::vector<int> &p, std::vector<std::vector<int>> &nhbrs) -> void {
// This lambda returns nothing, but sends back a vector of neighbour nodes (nhbrs.)
//
// NOTE: that this lambda assumes that neighbouring points are +- 1 the coordinates
// of the node which was sent in.
//
// NOTE: this function makes no attempt to determine if the neighbours exist or are
// disallowed. Send in your own function to do that!
// if(p.size() != p.size()) return;
std::vector<int> up, dn;
/*
//Non-diagonals only. (Any dimension.)
for(unsigned int i=0; i<p.size(); ++i){
up.clear(); dn.clear();
for(unsigned int j=0; j<p.size(); ++j){
if(i==j){
up.push_back( p[j] + static_cast<int>(1));
dn.push_back( p[j] - static_cast<int>(1));
}else{
up.push_back( p[j] );
dn.push_back( p[j] );
}
}
nhbrs.push_back(up); nhbrs.push_back(dn);
}
*/
// Non-diagonals and diagonals. (Any dimension.)
unsigned int index, max = 1; //(max = 3^dimension. Saves including cmath header..)
for(unsigned int j = 0; j < p.size(); ++j) {
max *= 3;
}
for(unsigned int i = 0; i < max; ++i) {
up.clear();
index = 0;
for(unsigned int j = 1; j < max; j *= 3) {
up.push_back((((i / (j)) % 3) - 1) + p[index]);
++index;
}
if(up != p) nhbrs.push_back(up);
}
return;
};
if(Neighbour_F != nullptr) {
_Neighbour = Neighbour_F;
}
std::function<H_FUNC_TYPE(const std::vector<int> &, const std::vector<int> &)> _Heuristic;
_Heuristic = [&_Distance](const std::vector<int> &somepoint, const std::vector<int> &endpoint) -> H_FUNC_TYPE {
return static_cast<H_FUNC_TYPE>(_Distance(somepoint, endpoint));
};
if(Heuristic_F != nullptr) {
_Heuristic = Heuristic_F;
}
//------------------------------------------------------------------------------------
//------------------------------------ Data prep -------------------------------------
//------------------------------------------------------------------------------------
openset.push_back(A);
g_score[A] = static_cast<H_FUNC_TYPE>(0);
h_score[A] = static_cast<H_FUNC_TYPE>(_Heuristic(A, B));
f_score[A] = h_score[A] + g_score[A];
//------------------------------------------------------------------------------------
//---------------------------------- Temp storage ------------------------------------
//------------------------------------------------------------------------------------
std::vector<std::vector<int>> neighbours;
std::vector<int> up, dn;
std::vector<int> current;
std::vector<std::vector<int>>::iterator vvit;
bool tentatv_is_better;
H_FUNC_TYPE tentatv_g_score;
//------------------------------------------------------------------------------------
//---------------------------- Entry into the Algorithm ------------------------------
//------------------------------------------------------------------------------------
while(openset.size() != 0) {
// Determine which node from openset has the lowest f_score.
// Sorting is NlogN usually, N*N at worst, and hopefully slightly lower in this case
// because the sort happens on each pass. NOTE: It was extremely slow to use a full
// sort, so now we just use nth_element to grab the lowest f_score'd value.
//
// After sort, lowest f_Score'd node is in openset.last().
// std::sort( openset.begin(), openset.end(), _sorter );
std::nth_element(openset.begin(), openset.end() - 1, openset.end(), _sorter);
// If the best node is the target node, we reconstruct the final path and exit.
if(openset[openset.size() - 1] == B) {
_reconstruct_path(came_from[B]);
result.push_back(came_from[B]);
result.push_back(B);
return result;
}
// Remove this node from the openset and put it into the closedset.
closedset.push_back(openset[openset.size() - 1]);
openset.pop_back();
current = closedset[closedset.size() - 1];
// Create the list of neighbours.
neighbours.clear();
// neighbours = <DECIDE IF YOU WANT THIS FUNCTIONALITY OR NOT>
_Neighbour(current, neighbours);
// Cycle through all neighbour nodes to potentially add to the openset.
for(vvit = neighbours.begin(); vvit != neighbours.end(); ++vvit) {
// If this neighbour node is in closedset already, skip this iteration.
if(find(closedset.begin(), closedset.end(), (*vvit)) != closedset.end()) {
continue; // Not 'break'!! FML!
}
// Update the move-by-move distance. Since it is the total distance travelled,
// we can just sum piece-by-piece.
tentatv_g_score = g_score[current] + _Distance(current, (*vvit));
// If this neighbour node is not in openset, add it.
if(find(openset.begin(), openset.end(), (*vvit)) == openset.end()) {
openset.push_back((*vvit));
tentatv_is_better = true;
// If this neighbour node is present, check if the tentative score is better.
} else if(tentatv_g_score <= g_score[*vvit]) {
tentatv_is_better = true;
// Otherwise, this node is not useful to us at the moment.
} else {
tentatv_is_better = false;
}
// If we are going to keep this node, we record the parent node and some
// relevant stats.
if(tentatv_is_better == true) {
came_from[*vvit] = current;
g_score[*vvit] = tentatv_g_score;
h_score[*vvit] = _Heuristic(*vvit, B);
f_score[*vvit] = g_score[*vvit] + h_score[*vvit];
}
}
} // while(openset.size() !+ 0)
// We will only get here if a failure occurs. In this case, we will output a SINGLE
// node. Therefore, if we call this routine and get only a single point, we should
// check what went wrong.
//
// NOTE: that the case where the beginning point = the end point will also return
// a single point: If we get more than one point from this routine, it is
// guaranteed to be a solution.
result.clear();
result.push_back(B);
return result;
}
// AStar template instantiations.
// Type A:
// Data (point) type- --> vector of ints.
// Return type----------> vector of vectors of ints.
// Heuristic function---> double (vector of ints, vector of ints)
// Distance function----> (same as Heuristic function...)
// Neighbour function---> void (vector of ints, vector of vectors of ints).
/*
typedef double(*_Utilities_Globe_etc__Heuristic_Func_Type_A)(std::vector<int>, std::vector<int>);
template std::vector< std::vector<int> >
_AStar_FULL<double,_Utilities_Globe_etc__Heuristic_Func_Type_A>(std::vector<int>, std::vector<int>,
_Utilities_Globe_etc__Heuristic_Func_Type_A);
*/
typedef double (*_Utilities_Globe_etc__Heuristic_Func_Type_A)(const std::vector<int> &, const std::vector<int> &);
typedef double (*_Utilities_Globe_etc__Distance_Func_Type_A)(const std::vector<int> &, const std::vector<int> &);
typedef void (*_Utilities_Globe_etc__Neighbour_Func_Type_A)(const std::vector<int> &, std::vector<std::vector<int>> &);
template std::vector<std::vector<int>>
_AStar_FULL<double, _Utilities_Globe_etc__Heuristic_Func_Type_A, _Utilities_Globe_etc__Distance_Func_Type_A,
_Utilities_Globe_etc__Neighbour_Func_Type_A>(std::vector<int> A, std::vector<int> B,
_Utilities_Globe_etc__Heuristic_Func_Type_A,
_Utilities_Globe_etc__Distance_Func_Type_A,
_Utilities_Globe_etc__Neighbour_Func_Type_A);