-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDstar.cpp
More file actions
556 lines (424 loc) · 11.9 KB
/
Dstar.cpp
File metadata and controls
556 lines (424 loc) · 11.9 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
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
#include "Dstar.h"
Dstar::Dstar() {
ExpLim = 50000;
C1 = 1;
}
float Dstar::Hash_key(Mesh u) {
return (float)(u.key.first + /*1193*/u.key.second);
}
bool Dstar::isValid(Mesh u) {
ds_oh::iterator current = openHash.find(u);
if (current == openHash.end()) return false;
if (!close(Hash_key(u), current->second)) return false;
return true;
}
list<Mesh> Dstar::getPath() {
return path;
}
bool Dstar::occupied(Mesh u) {
ds_ch::iterator current = cellHash.find(u);
if (current == cellHash.end()) return false;
return (current->second.cost < 0);
}
void Dstar::init(int sX, int sH, int gX, int gH) {
cellHash.clear();
path.clear();
openHash.clear();
while(!openList.empty()) openList.pop();
km = 0;
nodestart.x = sX;
nodestart.h = sH;
nodegoal.x = gX;
nodegoal.h = gH;
nodeData temp;
temp.g = temp.rhs = 0;
temp.cost = C1;
cellHash[nodegoal] = temp;
temp.g = temp.rhs = heuristic(s_start,s_goal);
temp.cost = C1;
cellHash[nodestart] = temp;
nodestart = calculateKey(nodestart);
nodelast = nodestart;
}
void Dstar::makeNewCell(Mesh u) {
if (cellHash.find(u) != cellHash.end()) return;
nodeData tmp;
tmp.g = tmp.rhs = heuristic(u,s_goal);
tmp.cost = C1;
cellHash[u] = tmp;
}
double Dstar::getG(state u) {
if (cellHash.find(u) == cellHash.end())
return heuristic(u,s_goal);
return cellHash[u].g;
}
/* double Dstar::getRHS(state u)
* --------------------------
* Returns the rhs value for state u.
*/
double Dstar::getRHS(state u) {
if (u == s_goal) return 0;
if (cellHash.find(u) == cellHash.end())
return heuristic(u,s_goal);
return cellHash[u].rhs;
}
/* void Dstar::setG(state u, double g)
* --------------------------
* Sets the G value for state u
*/
void Dstar::setG(state u, double g) {
makeNewCell(u);
cellHash[u].g = g;
}
/* void Dstar::setRHS(state u, double rhs)
* --------------------------
* Sets the rhs value for state u
*/
double Dstar::setRHS(state u, double rhs) {
makeNewCell(u);
cellHash[u].rhs = rhs;
}
/* double Dstar::eightCondist(state a, state b)
* --------------------------
* Returns the 8-way distance between state a and state b.
*/
double Dstar::eightCondist(state a, state b) {
double temp;
double min = fabs(a.x - b.x);
double max = fabs(a.y - b.y);
if (min > max) {
double temp = min;
min = max;
max = temp;
}
return ((M_SQRT2-1.0)*min + max);
}
/* int Dstar::computeShortestPath()
* --------------------------
* As per [S. Koenig, 2002] except for 2 main modifications:
* 1. We stop planning after a number of steps, 'maxsteps' we do this
* because this algorithm can plan forever if the start is
* surrounded by obstacles.
* 2. We lazily remove states from the open list so we never have to
* iterate through it.
*/
int Dstar::computeShortestPath() {
list<state> s;
list<state>::iterator i;
if (openList.empty()) return 1;
int k=0;
while ((!openList.empty()) &&
(openList.top() < (s_start = calculateKey(s_start))) ||
(getRHS(s_start) != getG(s_start))) {
if (k++ > maxSteps) {
fprintf(stderr, "At maxsteps\n");
return -1;
}
state u;
bool test = (getRHS(s_start) != getG(s_start));
// lazy remove
while(1) {
if (openList.empty()) return 1;
u = openList.top();
openList.pop();
if (!isValid(u)) continue;
if (!(u < s_start) && (!test)) return 2;
break;
}
ds_oh::iterator cur = openHash.find(u);
openHash.erase(cur);
state k_old = u;
if (k_old < calculateKey(u)) { // u is out of date
insert(u);
} else if (getG(u) > getRHS(u)) { // needs update (got better)
setG(u,getRHS(u));
getPred(u,s);
for (i=s.begin();i != s.end(); i++) {
updateVertex(*i);
}
} else { // g <= rhs, state has got worse
setG(u,INFINITY);
getPred(u,s);
for (i=s.begin();i != s.end(); i++) {
updateVertex(*i);
}
updateVertex(u);
}
}
return 0;
}
/* bool Dstar::close(double x, double y)
* --------------------------
* Returns true if x and y are within 10E-5, false otherwise
*/
bool Dstar::close(double x, double y) {
if (isinf(x) && isinf(y)) return true;
return (fabs(x-y) < 0.00001);
}
/* void Dstar::updateVertex(state u)
* --------------------------
* As per [S. Koenig, 2002]
*/
void Dstar::updateVertex(state u) {
list<state> s;
list<state>::iterator i;
if (u != s_goal) {
getSucc(u,s);
double tmp = INFINITY;
double tmp2;
for (i=s.begin();i != s.end(); i++) {
tmp2 = getG(*i) + cost(u,*i);
if (tmp2 < tmp) tmp = tmp2;
}
if (!close(getRHS(u),tmp)) setRHS(u,tmp);
}
if (!close(getG(u),getRHS(u))) insert(u);
}
/* void Dstar::insert(state u)
* --------------------------
* Inserts state u into openList and openHash.
*/
void Dstar::insert(state u) {
ds_oh::iterator cur;
float csum;
u = calculateKey(u);
cur = openHash.find(u);
csum = Hash_key(u);
// return if cell is already in list. TODO: this should be
// uncommented except it introduces a bug, I suspect that there is a
// bug somewhere else and having duplicates in the openList queue
// hides the problem...
//if ((cur != openHash.end()) && (close(csum,cur->second))) return;
openHash[u] = csum;
openList.push(u);
}
/* void Dstar::remove(state u)
* --------------------------
* Removes state u from openHash. The state is removed from the
* openList lazilily (in replan) to save computation.
*/
void Dstar::remove(state u) {
ds_oh::iterator cur = openHash.find(u);
if (cur == openHash.end()) return;
openHash.erase(cur);
}
/* double Dstar::trueDist(state a, state b)
* --------------------------
* Euclidean cost between state a and state b.
*/
double Dstar::trueDist(state a, state b) {
float x = a.x-b.x;
float y = a.y-b.y;
return sqrt(x*x + y*y);
}
/* double Dstar::heuristic(state a, state b)
* --------------------------
* Pretty self explanitory, the heristic we use is the 8-way distance
* scaled by a constant C1 (should be set to <= min cost).
*/
double Dstar::heuristic(state a, state b) {
return eightCondist(a,b)*C1;
}
/* state Dstar::calculateKey(state u)
* --------------------------
* As per [S. Koenig, 2002]
*/
state Dstar::calculateKey(state u) {
double val = fmin(getRHS(u),getG(u));
u.k.first = val + heuristic(u,s_start) + k_m;
u.k.second = val;
return u;
}
/* double Dstar::cost(state a, state b)
* --------------------------
* Returns the cost of moving from state a to state b. This could be
* either the cost of moving off state a or onto state b, we went with
* the former. This is also the 8-way cost.
*/
double Dstar::cost(state a, state b) {
int xd = fabs(a.x-b.x);
int yd = fabs(a.y-b.y);
double scale = 1;
if (xd+yd>1) scale = M_SQRT2;
if (cellHash.count(a) == 0) return scale*C1;
return scale*cellHash[a].cost;
}
/* void Dstar::updateCell(int x, int y, double val)
* --------------------------
* As per [S. Koenig, 2002]
*/
void Dstar::updateCell(int x, int y, double val) {
state u;
u.x = x;
u.y = y;
if ((u == s_start) || (u == s_goal)) return;
makeNewCell(u);
cellHash[u].cost = val;
updateVertex(u);
}
/* void Dstar::getSucc(state u,list<state> &s)
* --------------------------
* Returns a list of successor states for state u, since this is an
* 8-way graph this list contains all of a cells neighbours. Unless
* the cell is occupied in which case it has no successors.
*/
void Dstar::getSucc(state u,list<state> &s) {
s.clear();
u.k.first = -1;
u.k.second = -1;
if (occupied(u)) return;
u.x += 1;
s.push_front(u);
u.y += 1;
s.push_front(u);
u.x -= 1;
s.push_front(u);
u.x -= 1;
s.push_front(u);
u.y -= 1;
s.push_front(u);
u.y -= 1;
s.push_front(u);
u.x += 1;
s.push_front(u);
u.x += 1;
s.push_front(u);
}
/* void Dstar::getPred(state u,list<state> &s)
* --------------------------
* Returns a list of all the predecessor states for state u. Since
* this is for an 8-way connected graph the list contails all the
* neighbours for state u. Occupied neighbours are not added to the
* list.
*/
void Dstar::getPred(state u,list<state> &s) {
s.clear();
u.k.first = -1;
u.k.second = -1;
u.x += 1;
if (!occupied(u)) s.push_front(u);
u.y += 1;
if (!occupied(u)) s.push_front(u);
u.x -= 1;
if (!occupied(u)) s.push_front(u);
u.x -= 1;
if (!occupied(u)) s.push_front(u);
u.y -= 1;
if (!occupied(u)) s.push_front(u);
u.y -= 1;
if (!occupied(u)) s.push_front(u);
u.x += 1;
if (!occupied(u)) s.push_front(u);
u.x += 1;
if (!occupied(u)) s.push_front(u);
}
/* void Dstar::updateStart(int x, int y)
* --------------------------
* Update the position of the robot, this does not force a replan.
*/
void Dstar::updateStart(int x, int y) {
s_start.x = x;
s_start.y = y;
k_m += heuristic(s_last,s_start);
s_start = calculateKey(s_start);
s_last = s_start;
}
/* void Dstar::updateGoal(int x, int y)
* --------------------------
* This is somewhat of a hack, to change the position of the goal we
* first save all of the non-empty on the map, clear the map, move the
* goal, and re-add all of non-empty cells. Since most of these cells
* are not between the start and goal this does not seem to hurt
* performance too much. Also it free's up a good deal of memory we
* likely no longer use.
*/
void Dstar::updateGoal(int x, int y) {
list< pair<Meshpoint, double> > toAdd;
pair<Meshpoint, double> tp;
ds_ch::iterator i;
list< pair<Meshpoint, double> >::iterator kk;
for(i=cellHash.begin(); i!=cellHash.end(); i++) {
if (!close(i->second.cost, C1)) {
tp.first.x = i->first.x;
tp.first.y = i->first.y;
tp.second = i->second.cost;
toAdd.push_back(tp);
}
}
cellHash.clear();
openHash.clear();
while(!openList.empty())
openList.pop();
k_m = 0;
s_goal.x = x;
s_goal.y = y;
nodeData tmp;
tmp.g = tmp.rhs = 0;
tmp.cost = C1;
cellHash[s_goal] = tmp;
tmp.g = tmp.rhs = heuristic(s_start,s_goal);
tmp.cost = C1;
cellHash[s_start] = tmp;
s_start = calculateKey(s_start);
s_last = s_start;
for (kk=toAdd.begin(); kk != toAdd.end(); kk++) {
updateCell(kk->first.x, kk->first.y, kk->second);
}
}
/* bool Dstar::replan()
* --------------------------
* Updates the costs for all cells and computes the shortest path to
* goal. Returns true if a path is found, false otherwise. The path is
* computed by doing a greedy search over the cost+g values in each
* cells. In order to get around the problem of the robot taking a
* path that is near a 45 degree angle to goal we break ties based on
* the metric euclidean(state, goal) + euclidean(state,start).
*/
bool Dstar::replan() {
path.clear();
int res = computeShortestPath();
//printf("res: %d ols: %d ohs: %d tk: [%f %f] sk: [%f %f] sgr: (%f,%f)\n",res,openList.size(),openHash.size(),openList.top().k.first,openList.top().k.second, s_start.k.first, s_start.k.second,getRHS(s_start),getG(s_start));
if (res < 0) {
fprintf(stderr, "NO PATH TO GOAL\n");
return false;
}
list<state> n;
list<state>::iterator i;
state cur = s_start;
if (isinf(getG(s_start))) {
fprintf(stderr, "NO PATH TO GOAL\n");
return false;
}
while(cur != s_goal) {
path.push_back(cur);
getSucc(cur, n);
if (n.empty()) {
fprintf(stderr, "NO PATH TO GOAL\n");
return false;
}
double cmin = INFINITY;
double tmin;
state smin;
for (i=n.begin(); i!=n.end(); i++) {
//if (occupied(*i)) continue;
double val = cost(cur,*i);
double val2 = trueDist(*i,s_goal) + trueDist(s_start,*i); // (Euclidean) cost to goal + cost to pred
val += getG(*i);
if (close(val,cmin)) {
if (tmin > val2) {
tmin = val2;
cmin = val;
smin = *i;
}
} else if (val < cmin) {
tmin = val2;
cmin = val;
smin = *i;
}
}
n.clear();
cur = smin;
}
path.push_back(s_goal);
return true;
}