-
Notifications
You must be signed in to change notification settings - Fork 1
/
search.cpp
371 lines (334 loc) · 11.2 KB
/
search.cpp
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#include <iostream>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <stdio.h>
#include <queue>
#include <unordered_set>
#include <set>
#include "xtensor/xio.hpp"
#include "xtensor/xview.hpp"
#define FORCE_IMPORT_ARRAY
#include "xtensor-python/pyarray.hpp"
#include "xtensor-python/pyvectorize.hpp"
using namespace std;
#define numNeighbors 8
int neighborX[numNeighbors] = {-1,-1,-1,0,1,1,1,0};
int neighborY[numNeighbors] = {-1,0,1,1,1,0,-1,-1};
int sizeX;
int sizeY;
float epsilon = 1;
int goalX;
int goalY;
struct node{
int x;
int y;
double t;
int parentx;
int parenty;
double parentT;
double g;
double f;
bool open;
bool closed;
bool inconsistent;
};
struct nodeComparator
{
bool operator()(const node& lhs, const node& rhs) const
{
return (lhs.x == rhs.x) && (lhs.y == rhs.y) && (lhs.t == rhs.t);
}
};
struct nodeHasher
{
size_t operator()(const node& state) const
{
return state.y + sizeY*state.x + sizeX*sizeY*round(state.t*10000);
}
};
double heuristic(int x, int y);
void indexToXY(int index, int* x, int* y);
double distance(int x1, int y1, int x2, int y2);
int XYtoIndex(int x, int y);
node ComputePathWithReuse(double speed, unordered_set<node,nodeHasher,nodeComparator> *states,
int startX, int startY, xt::pyarray<double> &predictions, xt::pyarray<double> &predictionTimes);
xt::pyarray<double> ARAstar(double speed, int startX, int startY, int _goalX, int _goalY, xt::pyarray<double> &predictions, xt::pyarray<double> &predictionTimes);
bool reachedGoal(node nodeToCheck);
xt::pyarray<double> backTrace(unordered_set<node,nodeHasher,nodeComparator> *states, node lastNode,int startX, int startY, double lastPredictTime);
double fVal(double g, int x, int y);
xt::pyarray<double> testFunc(xt::pyarray<double> input);
class fCompare
{
public:
//comp();
bool operator() (const node& lhs, const node& rhs) const
{
return (lhs.f > rhs.f);//(lhsF > rhsF);
}
};
// PYBIND11_MODULE(searcher, m)
// {
// xt::import_numpy();
// m.doc() = "Searches graph for path to goal";
// m.def("graphSearch", ARAstar, "");
// }
// PYBIND11_MODULE(mainMod, m)
// {
// xt::import_numpy();
// m.doc() = "Searches graph for path to goal";
// m.def("main", main, "");
// }
void test_preds()
{
// set size of map and goal position
int _sizeX = 200;
int _sizeY = 200;
int _goalX = 199;
int _goalY = 199;
xt::pyarray<double> predictionTimes;
xt::pyarray<double> predictions;
predictionTimes = {0,100,200};
predictions = xt::zeros<double>({_sizeX,_sizeY,3});
cout << "starting search\n";
int startX = 0; int startY = 0; double speed = 10;
vector<int> PathX; vector<int> PathY; vector<double> PathT;
xt::pyarray<double> solution = ARAstar(speed, startX, startY,_goalX,_goalY,predictions,predictionTimes);
cout << solution << endl;
}
void test_main(xt::pyarray<double> & predictionTimes,
xt::pyarray<double> & predictions)
{
// set size of map and goal position
//int _sizeX = 200;
//int _sizeY = 200;
int _goalX = 199;
int _goalY = 199;
cout << "starting search\n";
int startX = 0; int startY = 0; double speed = 10;
vector<int> PathX; vector<int> PathY; vector<double> PathT;
xt::pyarray<double> solution = ARAstar(speed, startX, startY,_goalX,_goalY,predictions,predictionTimes);
cout << solution << endl;
}
xt::pyarray<double> testFunc(xt::pyarray<double> input)
{
return input+2;
}
xt::pyarray<double> ARAstar(double speed, int startX, int startY, int _goalX, int _goalY, xt::pyarray<double> &predictions, xt::pyarray<double> &predictionTimes)
{
sizeX = predictions.shape()[0];//_sizeX;
sizeY = predictions.shape()[1];//_sizeY;
goalX = _goalX;
goalY = _goalY;
// initialize g values and open list for the first weighted Astar
unordered_set<node, nodeHasher, nodeComparator> states;
node newState;
newState.x = startX;
newState.y = startY;
newState.t = 0;
newState.g = 0;
newState.open = true;
states.insert(newState);
int numOfEpsilons = 1;
float epsilonList[numOfEpsilons] = {1};
xt::pyarray<double> solution;
for (int i = 0; i < numOfEpsilons; i++)
{
epsilon = epsilonList[i];
// init values for search
unordered_set<node, nodeHasher, nodeComparator> tempStates;
for (node thisNode : states)
{
thisNode.f = fVal(thisNode.g, thisNode.x,thisNode.y);//thisNode.g + epsilon*heuristic(thisNode.x,thisNode.y);
thisNode.open = (thisNode.open || thisNode.inconsistent);
thisNode.inconsistent = false;
thisNode.closed = false;
tempStates.insert(thisNode);
}
states = tempStates;
node lastNode = ComputePathWithReuse(speed, &states, startX, startY,predictions,predictionTimes);
//publish solution
double lastPredictTime = predictionTimes(predictionTimes.size()-1);
solution = backTrace(&states, lastNode, startX, startY,lastPredictTime);
}
return solution;
}
node ComputePathWithReuse(double speed, unordered_set<node,nodeHasher,nodeComparator> *states,
int startX, int startY, xt::pyarray<double> &predictions, xt::pyarray<double> &predictionTimes)
{
// initialize priority queue used to choose states to expand
priority_queue<node,vector<node>,fCompare> OPEN;
// add nodes that should be in OPEN to the priority queue
for (node thisNode : *states)
{
if (thisNode.open)
{
OPEN.push(thisNode);
}
}
node lastExpand;
// Loop until either goal is next to expand (f goal is the smallest in open list) or no more nodes in open list
while((OPEN.top().t < predictionTimes(predictionTimes.size()-1)) && !reachedGoal(OPEN.top()) && !OPEN.empty())
{
auto expand = states->find(OPEN.top());
lastExpand = *expand;
OPEN.pop();
//cout << "state to expand1 x = " << OPEN.top().x << ", y = " << OPEN.top().y << ", t = " << OPEN.top().t << endl; // Don't expand nodes that were already expanded and put into the CLOSED list
//cout << "f = " << OPEN.top().f << endl;
if (!(expand->closed))
{
//cout << "state to expand x = " << expand->x << ", y = " << expand->y << ", t = " << expand->t << endl;
// Get X and Y position of the node to be expanded
int thisX = expand->x; int thisY = expand->y;
// Loop through node's neghbor
for (int i =0; i < numNeighbors; i++)
{
// Get X and Y position as well as index for this neighbor
int tempX = thisX + neighborX[i];
int tempY = thisY + neighborY[i];
double tempT = expand->t + distance(tempX,tempY,thisX,thisY)/speed;
// make sure it is actually a valid location
if ((tempX >= 0) && (tempX < sizeX) && (tempY >= 0) && (tempY < sizeY))
{
// update g value of this neighbor (g value of expanded node + distance times linearly interpolated prediction)
int upper = 0;
int lower = 0;
while ((predictionTimes(upper) < tempT))
{
lower = upper;
if (upper < (int)predictionTimes.size()-1)
{
upper++;
}
else
break;
}
double lastPredict = predictions(tempX,tempY,lower);
double nextPredict = predictions(tempX,tempY,upper);
double tempP;
if (predictionTimes(upper) == predictionTimes(lower))
{
tempP = lastPredict;
}
else
{
tempP = lastPredict + (nextPredict-lastPredict)*(tempT-predictionTimes(lower))/(predictionTimes(upper)-predictionTimes(lower));
}
double tempG = (expand->g) + distance(thisX, thisY, tempX,tempY) + tempP;
node tempState;
tempState.x = tempX;
tempState.y = tempY;
tempState.t = tempT;
auto thisNeighbor = states->find(tempState);
if (thisNeighbor == states->end()) // new state. Add to states list
{
tempState.parentx = thisX;
tempState.parenty = thisY;
tempState.parentT = expand->t;
tempState.g = tempG;
tempState.f = fVal(tempG, tempX, tempY);//tempG + epsilon * heuristic(tempX,tempY);
tempState.open = true;
tempState.closed = false;
tempState.inconsistent = false; // inconsistency recorded in the fact that it is in open
states->insert(tempState);
OPEN.push(tempState);
//cout << "new state x = " << tempState.x << ", y = " << tempState.y << ", t = " << tempT << endl;
}
else
{
node thisNeighborModified = *(thisNeighbor);
if (tempG < thisNeighbor->g)
{
thisNeighborModified.g = tempG;
thisNeighborModified.f = fVal(tempG, tempX, tempY); //tempG + epsilon * heuristic(tempX,tempY);
thisNeighborModified.parentx = thisX;
thisNeighborModified.parenty = thisY;
thisNeighborModified.parentT = expand->t;
if(!(thisNeighbor->closed)) // insert this neighbor into OPEN list only if it isn't in closed list
{
thisNeighborModified.open = true;
OPEN.push(thisNeighborModified);
}
else // otherwise, the neighbor becomes inconsistent
{
thisNeighborModified.inconsistent = true;
}
}
states->erase(thisNeighbor);
states->insert(thisNeighborModified);
}
}
}
// remove the expanded node from the OPEN list and insert into the CLOSED list
node expandModified = *(expand);
expandModified.open = false;
expandModified.closed = true;
states->erase(expand);
states->insert(expandModified);
}
}
if (OPEN.empty())
{
cout << "\n\n empty \n\n";
return lastExpand;
}
return OPEN.top();
}
xt::pyarray<double> backTrace(unordered_set<node,nodeHasher,nodeComparator> *states, node lastNode,
int startX, int startY, double lastPredictTime)
{
vector<int> PathX; vector<int> PathY; vector<double> PathT;
node tempState = lastNode;
auto it = states->find(tempState);
int timeStep = ceil(it->t);
while ((tempState.x != startX) || (tempState.y != startY))
{
if (it->t <= timeStep)
{
timeStep=timeStep-1;
PathX.insert(PathX.begin(),it->x); PathY.insert(PathY.begin(),it->y); PathT.insert(PathT.begin(),it->t);
}
tempState.x = it->parentx; tempState.y = it->parenty; tempState.t = it->parentT;
it = states->find(tempState);
}
PathX.insert(PathX.begin(),it->x); PathY.insert(PathY.begin(),it->y); PathT.insert(PathT.begin(),it->t);
xt::pyarray<double> solution = xt::zeros<double>({3,(int)PathX.size()});
for (int i = 0; i < (int)PathX.size();i++)
{
solution(0,i) = PathX[i];
solution(1,i) = PathY[i];
solution(2,i) = PathT[i];
}
return solution;
}
double fVal(double g, int x, int y)
{
//return heuristic(x,y) + 1;
return (g + epsilon*heuristic(x,y));
}
double heuristic(int x, int y)
{
double diffX = (goalX-x);
double diffY = (goalY-y);
double cost = sqrt(diffX*diffX + diffY*diffY);
return cost;
}
double distance(int x1, int y1, int x2, int y2)
{
double diffX = (x2 - x1);
double diffY = (y2 - y1);
double cost = sqrt(diffX*diffX + diffY*diffY);
return cost;
}
bool reachedGoal(node nodeToCheck)
{
return (nodeToCheck.x == goalX) && (nodeToCheck.y == goalY);
}
PYBIND11_MODULE(search, m)
{
xt::import_numpy();
m.doc() = "Searches graph for path to goal";
m.def("testFunc", testFunc, "");
m.def("test_main", test_main, "");
m.def("graphSearch", ARAstar, "");
}