-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathastar.h
More file actions
109 lines (88 loc) · 2.9 KB
/
astar.h
File metadata and controls
109 lines (88 loc) · 2.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
#ifndef astar_HEADER
#define astar_HEADER
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include "point.h"
#include "graph.h"
#include "heuristics.h"
#include <vector>
template <class Graph, class Heuristic>
struct AStar {
typedef typename Graph::vertex vertex;
typedef typename Graph::iterator iterator;
AStar(const Graph *G): G_(G) { }
unsigned path (vertex s, vertex t) {
/* TODO! use a hashtable and fib heap instead of vector! */
std::vector<entry> fringe, visited;
typedef typename std::vector<entry>::iterator fringe_iterator;
fringe.push_back(entry(s, 0));
while (!fringe.empty()) {
/* Get the min-weight element off the fringe. */
unsigned best_i = 0;
for (unsigned i = 1; i < fringe.size(); ++i) {
if (fringe[i].value(t) < fringe[best_i].value(t)) {
best_i = i;
}
}
entry e = fringe[best_i];
fringe[best_i] = fringe.back();
fringe.pop_back();
/* if it's the destination, congratuations, we win a prize! */
if (e.v_ == t) { return e.cost_; }
/* mark it visited if not already visited */
if (std::find(visited.begin(), visited.end(), e) == visited.end()) {
visited.push_back(e);
} else {
continue;
}
// relax neighbours
iterator end = G_->end(e.v_);
for (iterator nit = G_->begin(e.v_); nit != end; ++nit) {
vertex neigh = *nit;
entry eneigh (neigh, e.cost_ + G_->weight(neigh));
fringe_iterator it;
// try to find neigh in the fringe
it = std::find(fringe.begin(), fringe.end(), eneigh);
if (it != fringe.end()) {
/* it's here, we just haven't gotten there yet; decrease_key
if applicable */
if (eneigh.cost_ < (*it).cost_) {
(*it).cost_ = eneigh.cost_;
}
continue;
}
// try to find ey in the visited set
it = std::find(visited.begin(), visited.end(), eneigh);
if (it != visited.end()) {
/* it's here; if we have a new, better path, then
we seach from that path again. Otherwise just toss ey. */
if (eneigh.cost_ < (*it).cost_) {
// try again with the new, shorter path
fringe.push_back(eneigh);
(*it).cost_ = eneigh.cost_;
}
continue;
}
/* ey was found neither in the fringe nor in visited; add it
to the fringe */
fringe.push_back(eneigh);
} // end loop over neighbours
} // end loop over fringe
return std::numeric_limits<int>::max();
} // end function path()
private:
struct entry {
vertex v_;
unsigned cost_;
entry(vertex v, unsigned cost) : v_(v), cost_(cost) { }
unsigned value(vertex t) {
return cost_ + Heuristic() (v_, t);
}
bool operator == (const entry& other) const {
return v_ == other.v_;
}
};
const Graph *G_;
};
#endif