-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdstar.cpp
158 lines (144 loc) · 3.44 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
#include "dstar.h"
DStar::DStar(Node* start_, Node* goal_, Graph* graph_) : start(start_), goal(goal_),graph(graph_) {
rhs[goal] = 0.0;
U[goal] = calculateKey(goal);
for (auto node : graph->nodes) {
rhs[node] = INT_MAX;
g[node] = INT_MAX;
}
};
vector<Node*> DStar::searching() {
while (true) {
auto pair = topKey();
auto s = pair.first;
auto v = pair.second;
if (v >= calculateKey(start) && rhs[start] == g[start]) {
return extractPath();
//break;
}
auto k_old = v;
U.erase(start);
if (k_old < calculateKey(s)) {
U[s] = calculateKey(s);
}
else if (g[s] > rhs[s]) {
g[s] = rhs[s];
for (auto neigh : s->neighbors) {
updateVertex(neigh);
}
}
else {
g[s] = INT_MAX;
updateVertex(s);
for (auto neigh : s->neighbors) {
updateVertex(neigh);
}
}
}
return {};
}
void DStar::updateVertex(Node* s) {
if (s != goal) {
rhs[s] = INT_MAX;
for (auto nei : s->neighbors) {
rhs[s] = min(rhs[s], g[nei] + cost(s, nei));
}
}
if (U.find(s) != U.end()) {
U.erase(s);
}
if (g[s] != rhs[s]) {
U[s] = calculateKey(s);
}
}
vector<double> DStar::calculateKey(Node* s) {
return { min(g[s], rhs[s]) + h(start, s), min(g[s], rhs[s]) };
}
pair<Node*, vector<double>> DStar::topKey() {
auto cmp = [](auto const& lhs, auto const& rhs) {
return lhs.second < rhs.second;
};
auto s = min_element(U.begin(), U.end(), cmp)->first;
return make_pair(s, U[s]);
}
vector<Node*> DStar::extractPath() {
vector<Node*> path;
path.emplace_back(start);
auto s = start;
for (int i = 0; i < 1000; i++) {
unordered_map<Node*, double> g_list;
for (auto neigh : s->neighbors) {
if (!neigh->is_obstacle) {
g_list[neigh] = g[neigh];
}
}
auto cmp = [](auto const& lhs, auto const& rhs) {
return lhs.second < rhs.second;
};
auto s = min_element(g_list.begin(), g_list.end(), cmp)->first;
path.emplace_back(s);
if (s == goal) {
break;
}
}
return path;
}
double DStar::cost(Node* cur_node, Node* neigh_node) {
if (isBesideObstacle(neigh_node) == 2) {
return INT_MAX;
}
if (isBesideObstacle(neigh_node) == 1) {
return 1000;
}
return 1.0;
}
double DStar::h(Node* start, Node* goal) {
// 欧几里得距离 任意方向移动
return distance(start->coordinate, goal->coordinate);
}
int DStar::isBesideObstacle(Node* node) {
auto neighbors = node->neighbors;
// 节点在角落里
if (neighbors.size() == 3) {
return 2;
}
if (neighbors.size() == 8) {
// 节点上下左右有障碍
if (neighbors[0]->is_obstacle || neighbors[1]->is_obstacle || neighbors[2]->is_obstacle || neighbors[3]->is_obstacle) {
return 2;
}
// 节点斜向有障碍
else if (neighbors[4]->is_obstacle || neighbors[5]->is_obstacle || neighbors[6]->is_obstacle || neighbors[7]->is_obstacle) {
return 1;
}
}
return 0;
}
vector<Vec2> DStar::getCoorPath(const vector<Node*>& path) {
vector<Vec2> coor_path;
for (auto node : path) {
coor_path.emplace_back(node->coordinate);
}
return coor_path;
}
void DStar::smoothPath(vector<Node*>& path) {
if (path.size() < 50)
return;
int maxIter = 100;
int iter = 0;
bool changed = true;
while (changed && iter < maxIter) {
changed = false;
for (int i = 1; i < path.size() - 1; i++) {
Node* prev = path[i - 1];
Node* cur = path[i];
Node* next = path[i + 1];
if (!prev->is_obstacle && !next->is_obstacle && cost(prev, next) <= cost(prev, cur) + cost(cur, next)) {
path.erase(path.begin() + i);
path.insert(path.begin() + i, parent[next]);
changed = true;
}
}
iter++;
}
}