forked from ArekSredzki/dstar-lite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dstar.cpp
661 lines (513 loc) · 14.3 KB
/
Dstar.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
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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
/* Dstar.cpp
* James Neufeld (neufeld@cs.ualberta.ca)
* Compilation fixed by Arek Sredzki (arek@sredzki.com)
*/
#include "Dstar.h"
#ifdef USE_OPEN_GL
#ifdef MACOS
#include <OpenGL/gl.h>
#else
#include <GL/gl.h>
#endif
#endif
/* void Dstar::Dstar()
* --------------------------
* Constructor sets constants.
*/
Dstar::Dstar() {
maxSteps = 80000; // node expansions before we give up
C1 = 1; // cost of an unseen cell
}
/* float Dstar::keyHashCode(state u)
* --------------------------
* Returns the key hash code for the state u, this is used to compare
* a state that have been updated
*/
float Dstar::keyHashCode(state u) {
return (float)(u.k.first + 1193*u.k.second);
}
/* bool Dstar::isValid(state u)
* --------------------------
* Returns true if state u is on the open list or not by checking if
* it is in the hash table.
*/
bool Dstar::isValid(state u) {
ds_oh::iterator cur = openHash.find(u);
if (cur == openHash.end()) return false;
if (!close(keyHashCode(u), cur->second)) return false;
return true;
}
/* void Dstar::getPath()
* --------------------------
* Returns the path created by replan()
*/
list<state> Dstar::getPath() {
return path;
}
/* bool Dstar::occupied(state u)
* --------------------------
* returns true if the cell is occupied (non-traversable), false
* otherwise. non-traversable are marked with a cost < 0.
*/
bool Dstar::occupied(state u) {
ds_ch::iterator cur = cellHash.find(u);
if (cur == cellHash.end()) return false;
return (cur->second.cost < 0);
}
/* void Dstar::init(int sX, int sY, int gX, int gY)
* --------------------------
* Init dstar with start and goal coordinates, rest is as per
* [S. Koenig, 2002]
*/
void Dstar::init(int sX, int sY, int gX, int gY) {
cellHash.clear();
path.clear();
openHash.clear();
while(!openList.empty()) openList.pop();
k_m = 0;
s_start.x = sX;
s_start.y = sY;
s_goal.x = gX;
s_goal.y = gY;
cellInfo 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;
}
/* void Dstar::makeNewCell(state u)
* --------------------------
* Checks if a cell is in the hash table, if not it adds it in.
*/
void Dstar::makeNewCell(state u) {
if (cellHash.find(u) != cellHash.end()) return;
cellInfo tmp;
tmp.g = tmp.rhs = heuristic(u,s_goal);
tmp.cost = C1;
cellHash[u] = tmp;
}
/* double Dstar::getG(state u)
* --------------------------
* Returns the G value for state u.
*/
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 = keyHashCode(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<ipoint2, double> > toAdd;
pair<ipoint2, double> tp;
ds_ch::iterator i;
list< pair<ipoint2, 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;
cellInfo 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;
}
#ifdef USE_OPEN_GL
void Dstar::draw() {
ds_ch::iterator iter;
ds_oh::iterator iter1;
state t;
list<state>::iterator iter2;
glBegin(GL_QUADS);
for(iter=cellHash.begin(); iter != cellHash.end(); iter++) {
if (iter->second.cost == 1) glColor3f(0,1,0);
else if (iter->second.cost < 0 ) glColor3f(1,0,0);
else glColor3f(0,0,1);
drawCell(iter->first,0.45);
}
glColor3f(1,1,0);
drawCell(s_start,0.45);
glColor3f(1,0,1);
drawCell(s_goal,0.45);
for(iter1=openHash.begin(); iter1 != openHash.end(); iter1++) {
glColor3f(0.4,0,0.8);
drawCell(iter1->first, 0.2);
}
glEnd();
glLineWidth(4);
glBegin(GL_LINE_STRIP);
glColor3f(0.6, 0.1, 0.4);
for(iter2=path.begin(); iter2 != path.end(); iter2++) {
glVertex3f(iter2->x, iter2->y, 0.2);
}
glEnd();
}
void Dstar::drawCell(state s, float size) {
float x = s.x;
float y = s.y;
glVertex2f(x - size, y - size);
glVertex2f(x + size, y - size);
glVertex2f(x + size, y + size);
glVertex2f(x - size, y + size);
}
#else
void Dstar::draw() {}
void Dstar::drawCell(state s, float z) {}
#endif