-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsearch2.cpp
292 lines (269 loc) · 9.37 KB
/
search2.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
#include <iostream>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <stdio.h>
#include <queue>
#include <unordered_set>
#include <set>
#include "xtensor/xarray.hpp"
#include "xtensor/xio.hpp"
#include "xtensor/xview.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;
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, vector<xt::xarray<double>> predictions, vector<double> predictionTimes);
void ARAstar(double speed, int startX, int startY,vector<xt::xarray<double>> predictions, vector<double> predictionTimes,
vector<int> *PathX, vector<int> *PathY, vector<double> *PathT);
bool reachedGoal(node nodeToCheck);
void backTrace(unordered_set<node,nodeHasher,nodeComparator> *states, node lastNode,
int startX, int startY, vector<int> *PathX, vector<int> *PathY, vector<double> *PathT);
double fVal(double g, int x, int y);
class fCompare
{
public:
//comp();
bool operator() (const node& lhs, const node& rhs) const
{
return (lhs.f > rhs.f);//(lhsF > rhsF);
}
};
main()
{
// set size of map and goal position
sizeX = 200;
sizeY = 200;
goalX = 199;
goalY = 199;
epsilon = 1;
vector<double> predictionTimes;
vector<xt::xarray<double>> predictions;
predictionTimes.push_back(0);
predictionTimes.push_back(1000);
predictions.push_back(xt::zeros<double>({sizeX,sizeY}));
predictions.push_back(xt::zeros<double>({sizeX,sizeY}));
cout << "starting search\n";
int startX = 0; int startY = 0; double speed = 10;
vector<int> PathX; vector<int> PathY; vector<double> PathT;
ARAstar(speed, startX, startY,predictions,predictionTimes, &PathX, &PathY, &PathT);
for (int i=0;i<PathX.size();i++)
{
cout << "X = " << PathX[i] << ", Y = " << PathY[i] << ", T = " << PathT[i] << endl;
}
}
void ARAstar(double speed, int startX, int startY,vector<xt::xarray<double>> predictions, vector<double> predictionTimes,
vector<int> *PathX, vector<int> *PathY, vector<double> *PathT)
{
// 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};
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;
double tFound;
node lastNode = ComputePathWithReuse(speed, &states, startX, startY,predictions,predictionTimes);
//publish solution
backTrace(&states, lastNode, startX, startY, PathX, PathY, PathT);
}
}
node ComputePathWithReuse(double speed, unordered_set<node,nodeHasher,nodeComparator> *states,
int startX, int startY, vector<xt::xarray<double>> predictions, vector<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);
}
}
// 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.back()) && !reachedGoal(OPEN.top()) && !OPEN.empty())
{
auto expand = states->find(OPEN.top());
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 < predictionTimes.size())
{
upper++;
}
else
break;
}
double lastPredict = (predictions[lower])(tempX,tempY);
double nextPredict = (predictions[upper])(tempX,tempY);
double tempP = lastPredict + (nextPredict-lastPredict)*(tempT-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);
}
}
return OPEN.top();
}
void backTrace(unordered_set<node,nodeHasher,nodeComparator> *states, node lastNode,
int startX, int startY, vector<int> *PathX, vector<int> *PathY, vector<double> *PathT)
{
node tempState = lastNode;
auto it = states->find(tempState);
while ((tempState.x != startX) && (tempState.y != startY))
{
//cout << "X = " << it->x << ", Y = " << it->y << ", t = " << it->t << endl;
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);
}
//cout << "X = " << it->x << ", Y = " << it->y << ", t = " << it->t << endl;
PathX->insert(PathX->begin(),it->x); PathY->insert(PathY->begin(),it->y); PathT->insert(PathT->begin(),it->t);
}
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);
}