-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAStar.m
111 lines (101 loc) · 4.14 KB
/
AStar.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
% AStar algorithm for path finding
% XiaoCY, 2024-10-26
%%
function astar_path = AStar(map, start, goal, varargin)
p = inputParser;
p.KeepUnmatched = true;
p.addParameter('ShowProcess', false);
p.addParameter('FrameRate', 0.1);
p.addParameter('Filename', 'AStarProcess.gif');
p.addParameter('CurrentColor', [1.0000, 0.7451, 0.4784]);
p.addParameter('OpenColor', [0.3333, 0.6588, 0.4078])
p.addParameter('ClosedColor', [0.2980, 0.4471, 0.6902]);
p.addParameter('LineWidth', 2);
p.addParameter('MarkerSize', 15);
p.parse(varargin{:});
plot_inited = false;
% heuristic function handle
heuristic = @(node) sqrt((goal.row - node.row)^2 + (goal.col - node.col)^2);
% find path
open = PriorityQueue;
open.push(start, 0);
closed = dictionary; % use `containers.Map` for MATLAB version lower than R2020b
while open.size > 0
current = open.pop;
closed(current.key) = current;
if current == goal
goal = current; % refresh cost/parent infos
break
end
neighbors = map.neighbors(current);
for k = 1:length(neighbors)
key = neighbors(k).key;
priority = neighbors(k).cost + heuristic(neighbors(k));
if isKey(closed, key)
if neighbors(k).cost < closed(key).cost
closed(key) = [];
open.push(neighbors(k), priority)
end
else
open.push(neighbors(k), priority)
end
end
if p.Results.ShowProcess
clf
go = map.show(p.Unmatched);
plot(start.col, start.row, 'color', p.Results.CurrentColor, 'Marker', 'o', 'MarkerSize', p.Results.MarkerSize)
plot(goal.col, goal.row, 'color', p.Results.CurrentColor, 'Marker', 'pentagram', 'MarkerSize', p.Results.MarkerSize)
keys = closed.keys;
for k = 1:length(keys)
row = closed(keys(k)).row;
col = closed(keys(k)).col;
go(row, col).FaceColor = p.Results.ClosedColor;
go(row, col).EdgeColor = p.Results.ClosedColor;
go(row, col).EdgeAlpha = 1;
go(row, col).LineWidth = p.Results.LineWidth;
end
for k = 1:open.size
row = open.data{k, 1}.row;
col = open.data{k, 1}.col;
go(row, col).FaceColor = p.Results.OpenColor;
go(row, col).EdgeColor = p.Results.OpenColor;
go(row, col).EdgeAlpha = 1;
go(row, col).LineWidth = p.Results.LineWidth;
end
row = current.row;
col = current.col;
go(row, col).FaceColor = p.Results.CurrentColor;
go(row, col).EdgeColor = p.Results.CurrentColor;
go(row, col).EdgeAlpha = 1;
go(row, col).LineWidth = p.Results.LineWidth;
pause(p.Results.FrameRate)
frame = getframe(gcf);
im = frame2im(frame);
[imind, cm] = rgb2ind(im, 256);
if ~plot_inited
plot_inited = true;
imwrite(imind, cm, p.Results.Filename, 'gif', 'Loopcount', inf);
else
imwrite(imind, cm, p.Results.Filename, 'gif', 'WriteMode', 'append', 'DelayTime', p.Results.FrameRate);
end
end
end
% generate path: [row, col, cost]
node = goal;
astar_path = zeros(goal.depth, 3);
for k = 1:goal.depth
astar_path(k, :) = [node.row, node.col, node.cost];
node = node.parent;
end
astar_path = astar_path(end:-1:1, :);
if p.Results.ShowProcess
clf
map.show('ShowPath', astar_path, p.Unmatched);
plot(start.col, start.row, 'color', p.Results.CurrentColor, 'Marker', 'o', 'MarkerSize', p.Results.MarkerSize)
plot(goal.col, goal.row, 'color', p.Results.CurrentColor, 'Marker', 'pentagram', 'MarkerSize', p.Results.MarkerSize)
frame = getframe(gcf);
im = frame2im(frame);
[imind, cm] = rgb2ind(im, 256);
imwrite(imind, cm, p.Results.Filename, 'gif', 'WriteMode', 'append', 'DelayTime', 1);
end
end