-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathd_star.m
294 lines (251 loc) · 7.32 KB
/
d_star.m
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
function [path, flag, cost, EXPAND] = d_star(map, start, goal)
% @file: d_star.m
% @breif: D* motion planning
% @paper: Optimal and Efficient Path Planning for Partially-Known Environments.
% @author: Zhanyu Guo
% @update: 2023.7.15
%
% ========= MAP =========
% [x, y, t, h, k, px, py]
% =======================
% NEW = 0, OPEN = 1, CLOSED = 2
%
% initialize
path = [];
flag = false;
cost = 0;
EXPAND = [];
siz = size(map);
persistent MAP; % static variable
if isempty(MAP) % first plan
MAP = zeros(siz(1) * siz(2), 7);
for y = 1:siz(2)
for x = 1:siz(1)
cur_ind = sub2ind(siz, x, y);
MAP(cur_ind, 1) = x; % x
MAP(cur_ind, 2) = y; % y
MAP(cur_ind, 3) = 0; % tag = NEW
MAP(cur_ind, 4) = Inf; % h
MAP(cur_ind, 5) = Inf; % key
end
end
start_ind = sub2ind(siz, start(1), start(2));
goal_ind = sub2ind(siz, goal(1), goal(2));
MAP = insert(MAP, goal_ind, 0);
while 1
[MAP, EXPAND, k_min] = processState(MAP, EXPAND, siz, map);
if k_min == -1
return
end
if MAP(start_ind, 3) == 2
flag = true;
break
end
end
% extract path
[path, cost] = extract_path(MAP, siz, start_ind, map);
else
start_ind = sub2ind(siz, start(1), start(2));
cur_ind = start_ind;
while 1
if isequal(MAP(cur_ind, 6:7), [0, 0])
break
end
par_ind = sub2ind(siz, MAP(cur_ind, 6), MAP(cur_ind, 7));
if isCollision(MAP(cur_ind, :), MAP(par_ind, :), map)
[MAP, EXPAND] = modify(cur_ind, par_ind, MAP, siz, map, EXPAND);
continue
end
cur_ind = par_ind;
end
[path, cost] = extract_path(MAP, siz, start_ind, map);
end
i = 1;
expand_num = size(EXPAND, 1);
while i <= expand_num
if map(EXPAND(i, 1), EXPAND(i, 2)) == 2
EXPAND(i, :) = [];
expand_num = expand_num - 1;
continue
end
i = i + 1;
end
end
%%
function MAP = insert(MAP, ind, h_new)
%
% ========= MAP =========
% [x, y, t, h, k, px, py]
% =======================
% NEW = 0, OPEN = 1, CLOSED = 2
%
if MAP(ind, 3) == 0
MAP(ind, 5) = h_new;
elseif MAP(ind, 3) == 1
MAP(ind, 5) = min(MAP(ind, 5), h_new);
elseif MAP(ind, 3) == 2
MAP(ind, 5) = min(MAP(ind, 4), h_new);
end
MAP(ind, 4) = h_new;
MAP(ind, 3) = 1;
end
function [MAP, EXPAND, k_min] = processState(MAP, EXPAND, siz, map)
% get open list
OPEN = MAP(MAP(:, 3) == 1, :);
if isempty(OPEN)
k_min = -1;
return
end
% get node with min k in open
[k_old, open_ind] = min(OPEN(:, 5));
cur_ind = sub2ind(siz, OPEN(open_ind, 1), OPEN(open_ind, 2));
% set to closed
MAP(cur_ind, 3) = 2;
% add to expand
if ~loc_list(MAP(cur_ind, :), EXPAND, [1, 2])
EXPAND = [EXPAND; MAP(cur_ind, 1:2)];
end
% get neighbors
motion = [-1, -1; ...
0, -1; ...
1, -1; ...
-1, 0; ...
1, 0; ...
-1, 1; ...
0, 1; ...
1, 1];
motion_num = size(motion, 1);
neighbors = zeros(motion_num, 4);
for i = 1:motion_num
neighbors(i, 1) = MAP(cur_ind, 1) + motion(i, 1);
neighbors(i, 2) = MAP(cur_ind, 2) + motion(i, 2);
neb_ind = sub2ind(siz, neighbors(i, 1), neighbors(i, 2));
neighbors(i, 3) = neb_ind;
neighbors(i, 4) = getCost(MAP(cur_ind, :), MAP(neb_ind, :), map);
end
if k_old < MAP(cur_ind, 4)
for i = 1:motion_num
neb_ind = neighbors(i, 3);
if MAP(neb_ind, 3) ~= 0 ...
&& MAP(neb_ind, 4) <= k_old ...
&& MAP(cur_ind, 4) > MAP(neb_ind, 4) + neighbors(i, 4)
MAP(cur_ind, 6) = MAP(neb_ind, 1);
MAP(cur_ind, 7) = MAP(neb_ind, 2);
MAP(cur_ind, 4) = MAP(neb_ind, 4) + neighbors(i, 4);
end
end
end
if k_old == MAP(cur_ind, 4)
for i = 1:motion_num
neb_ind = neighbors(i, 3);
if MAP(neb_ind, 3) == 0 ...
|| ((MAP(neb_ind, 6) == MAP(cur_ind, 1) && MAP(neb_ind, 7) == MAP(cur_ind, 2)) && MAP(neb_ind, 4) ~= MAP(cur_ind, 4) + neighbors(i, 4)) ...
|| ((MAP(neb_ind, 6) ~= MAP(cur_ind, 1) || MAP(neb_ind, 7) ~= MAP(cur_ind, 2)) && MAP(neb_ind, 4) > MAP(cur_ind, 4) + neighbors(i, 4))
MAP(neb_ind, 6) = MAP(cur_ind, 1);
MAP(neb_ind, 7) = MAP(cur_ind, 2);
MAP = insert(MAP, neb_ind, MAP(cur_ind, 4) + neighbors(i, 4));
end
end
else
for i = 1:motion_num
neb_ind = neighbors(i, 3);
if MAP(neb_ind, 3) == 0 ...
|| ((MAP(neb_ind, 6) == MAP(cur_ind, 1) && MAP(neb_ind, 7) == MAP(cur_ind, 2)) && MAP(neb_ind, 4) ~= MAP(cur_ind, 4) + neighbors(i, 4))
MAP(neb_ind, 6) = MAP(cur_ind, 1);
MAP(neb_ind, 7) = MAP(cur_ind, 2);
MAP = insert(MAP, neb_ind, MAP(cur_ind, 4) + neighbors(i, 4));
elseif (MAP(neb_ind, 6) ~= MAP(cur_ind, 1) || MAP(neb_ind, 7) ~= MAP(cur_ind, 2)) ...
&& MAP(neb_ind, 4) > MAP(cur_ind, 4) + neighbors(i, 4)
MAP = insert(MAP, cur_ind, MAP(cur_ind, 4));
elseif (MAP(neb_ind, 6) ~= MAP(cur_ind, 1) || MAP(neb_ind, 7) ~= MAP(cur_ind, 2)) ...
&& MAP(cur_ind, 4) > MAP(neb_ind, 4) + neighbors(i, 4) ...
&& MAP(neb_ind, 3) == 2 ...
&& MAP(neb_ind, 4) > k_old
MAP = insert(MAP, neb_ind, MAP(neb_ind, 4));
end
end
end
% get open
OPEN = MAP(MAP(:, 3) == 1, :);
if isempty(OPEN)
k_min = -1;
return
end
% get node with min k in open
[k_min, ~] = min(OPEN(:, 5));
end
function index = loc_list(node, list, range)
% @breif: locate the node in given list
num = size(list);
index = 0;
if ~num(1)
return
else
for i = 1:num(1)
if isequal(node(range), list(i, range))
index = i;
return
end
end
end
end
function flag = isCollision(node1, node2, map)
flag = false;
if map(node1(1), node1(2)) == 2 || map(node2(1), node2(2)) == 2
flag = true;
return
end
if node1(1) ~= node2(1) && node1(2) ~= node2(2)
if node2(1) - node1(1) == node1(2) - node2(2)
s1 = [min(node1(1), node2(1)), min(node1(2), node2(2))];
s2 = [max(node1(1), node2(1)), max(node1(2), node2(2))];
else
s1 = [min(node1(1), node2(1)), max(node1(2), node2(2))];
s2 = [max(node1(1), node2(1)), min(node1(2), node2(2))];
end
if map(s1(1), s1(2)) == 2 || map(s2(1), s2(2)) == 2
flag = true;
end
end
end
function cost = getCost(node1, node2, map)
if isCollision(node1, node2, map)
cost = Inf;
else
if abs(node1(1) - node2(1)) + abs(node1(2) - node2(2)) > 1
cost = 1.414;
else
cost = 1;
end
end
end
function [path, cost] = extract_path(MAP, siz, start_ind, map)
% @breif: Extract the path based on the CLOSED set.
path = [];
cost = 0;
cur_ind = start_ind;
path = [path; MAP(cur_ind, 1:2)];
while 1
if isequal(MAP(cur_ind, 6:7), [0, 0])
break
end
par_ind = sub2ind(siz, MAP(cur_ind, 6), MAP(cur_ind, 7));
cost = cost + getCost(MAP(cur_ind, :), MAP(par_ind, :), map);
cur_ind = par_ind;
path = [path; MAP(cur_ind, 1:2)];
end
end
function [MAP, EXPAND] = modify(cur_ind, par_ind, MAP, siz, map, EXPAND)
if MAP(cur_ind, 3) == 2
MAP = insert(MAP, cur_ind, MAP(cur_ind, 4));
end
if MAP(par_ind, 3) == 2
MAP = insert(MAP, par_ind, MAP(par_ind, 4));
end
while 1
[MAP, EXPAND, k_min] = processState(MAP, EXPAND, siz, map);
if k_min >= MAP(cur_ind, 4)
break
end
end
end