-
Notifications
You must be signed in to change notification settings - Fork 64
/
jps.m
221 lines (182 loc) · 4.23 KB
/
jps.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
function [path, goal_reached, cost, EXPAND] = jps(map, start, goal)
% @file: jps.m
% @breif: Jump Point Search motion planning
% @author: Winter
% @update: 2023.7.13
%
% == OPEN and CLOSED ==
% [x, y, g, h, px, py]
% =====================
%
% initialize
OPEN = [];
CLOSED = [];
EXPAND = [];
cost = 0;
goal_reached = false;
motion = [-1, -1, 1.414; ...
0, -1, 1; ...
1, -1, 1.414; ...
-1, 0, 1; ...
1, 0, 1; ...
-1, 1, 1.414; ...
0, 1, 1; ...
1, 1, 1.414];
motion_num = size(motion, 1);
node_s = [start, 0, h(start, goal), start];
OPEN = [OPEN; node_s];
while ~isempty(OPEN(:, 1))
% pop
f = OPEN(:, 3) + OPEN(:, 4);
[~, index] = min(f);
cur_node = OPEN(index, :);
OPEN(index, :) = [];
% exists in CLOSED set
if loc_list(cur_node, CLOSED, [1, 2])
continue
end
% goal found
if cur_node(1) == goal(1) && cur_node(2) == goal(2)
CLOSED = [cur_node; CLOSED];
goal_reached = true;
cost = cur_node(3);
break
end
jp_list = [];
for i = 1:motion_num
[jp, goal_reached] = jump(cur_node, motion(i, :), goal, map);
% exists and not in CLOSED set
if goal_reached && ~loc_list(jp, CLOSED, [1, 2])
jp(5:6) = cur_node(1:2);
jp(4) = h(jp(1:2), goal);
jp_list = [jp; jp_list];
end
end
for j = 1:size(jp_list, 1)
jp = jp_list(j, :);
% update OPEN set
OPEN = [OPEN; jp];
EXPAND = [EXPAND; jp];
% goal found
if jp(1) == goal(1) && jp(2) == goal(2)
break
end
end
CLOSED = [cur_node; CLOSED];
end
% extract path
path = extract_path(CLOSED, start);
end
%%
function h_val = h(cur_node, goal)
% @breif: heuristic function(Manhattan distance)
h_val = abs(cur_node(1) - goal(1)) + abs(cur_node(2) - goal(2));
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 path = extract_path(close, start)
% @breif: Extract the path based on the CLOSED set.
path = [];
closeNum = size(close, 1);
index = 1;
while 1
path = [path; close(index, 1:2)];
if isequal(close(index, 1:2), start)
break;
end
for i = 1:closeNum
if isequal(close(i, 1:2), close(index, 5:6))
index = i;
break
end
end
end
end
function [new_node, flag] = jump(cur_node, motion, goal, map)
flag = false;
% explore a new node
new_node = [cur_node(1) + motion(1), ...
cur_node(2) + motion(2), ...
cur_node(3) + motion(3), ...
0, cur_node(1), cur_node(2)
];
new_node(4) = h(new_node(1:2), goal);
% obstacle
if new_node(1) <= 0 || new_node(2) <= 0 || map(new_node(1), new_node(2)) == 2
return
end
% goal found
if new_node(1) == goal(1) && new_node(2) == goal(2)
flag = true;
return
end
% diagonal
if motion(1) && motion(2)
% if exists jump point at horizontal or vertical
x_dir = [motion(1), 0, 1];
y_dir = [0, motion(2), 1];
[~, flag_x] = jump(new_node, x_dir, goal, map);
[~, flag_y] = jump(new_node, y_dir, goal, map);
if flag_x || flag_y
flag = true;
return
end
end
% if exists forced neighbor
if detect_force(new_node, motion, map)
flag = true;
return
else
[new_node, flag] = jump(new_node, motion, goal, map);
return
end
end
function flag = detect_force(cur_node, motion, map)
flag = true;
x = cur_node(1);
y = cur_node(2);
x_dir = motion(1);
y_dir = motion(2);
% horizontal
if x_dir && ~y_dir
if map(x, y + 1) == 2 && map(x + x_dir, y + 1) ~= 2
return
end
if map(x, y - 1) == 2 && map(x + x_dir, y - 1) ~= 2
return
end
end
% vertical
if ~x_dir && y_dir
if map(x + 1, y) == 2 && map(x + 1, y + y_dir) ~= 2
return
end
if map(x - 1, y) == 2 && map(x - 1, y + y_dir) ~= 2
return
end
end
% diagonal
if x_dir && y_dir
if map(x - x_dir, y) == 2 && map(x - x_dir, y + y_dir) ~= 2
return
end
if map(x, y - y_dir) == 2 && map(x + x_dir, y - y_dir) ~= 2
return
end
end
flag = false;
return
end