A*算法

A*算法是把启发式方法(heuristic approaches)如BFS(完全使用贪心策略),和常规方法如Dijsktra算法结合在一起的算法。有点不同的是,类似BFS的启发式方法经常给出一个近似解而不是保证最佳解。然而,尽管A star基于无法保证最佳解的启发式方法,A star却能保证找到一条最短路径。

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
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
// 定义地图上的点
struct Point
{
int x, y; // 栅格行列
Point(int x, int y) :x(x), y(y) {}; // 参数列表初始化
double distance(const Point& p) const // 求距离
{
return sqrt((x - p.x) * (x - p.x) + (y - p.y) * (y - p.y)); // 欧几里得距离
}
};


// 定义节点
struct Node
{
Point point; // 栅格点
double g, h, f;// 代价值,f总价值,g到起点的代价值,h到终点的估计代价(启发式函数)
Node* parent;// 父节点指针
Node(Point point, double g, double h, Node* parent = nullptr) :point(point), g(g), h(h), f(g + h), parent(parent)
{}
};

// 自定义Node*排序规则
struct NodeCompare {
bool operator()(Node* n1, Node* n2) {
return (n1->f) < (n2->f); // 表示升序排列
}
};
// 基于栅格地图的路径规划A*算法,返回由Point组成的路径path,输入为地图,起点和终点
vector<Point> AstarPathPlanning(vector<vector<int>>& gridmap, Point& start, Point& goal)
{
// 获取地图参数
int row = gridmap.size(); // 行,表示地图的宽度
int col = gridmap[0].size(); // 列,表示地图的长
// 定义openlist, closelist
vector<Node*> openlist; // openlist 表示待搜索的节点
vector<Node*> closelist;// closelist表示已搜索的节点

auto cleanup = [&]() {
for (auto o : openlist) delete o;
for (auto c : closelist) delete c;
openlist.clear();
closelist.clear();
};

openlist.push_back(new Node(start, 0.0, start.distance(goal))); // g=0
int count1 = 1;
// 进入循环,开始搜索,搜索到终点则返回路径
vector<Point> path;
while (!openlist.empty()) // 当openlist为空,表示所有可搜索节点已经被搜索,此时循环结束
{
// 获取当前搜索节点current,即openlist中f最小节点
sort(openlist.begin(), openlist.end(), NodeCompare{}); // 先对openlist排序,这里自定义排序规则(从小到大)
Node* current = *openlist.begin(); // *openlist.begin()排序后即为f最小的迭代器位置
// 将current对应的元素从openlist中删除
openlist.erase(openlist.begin());
// 将current加入到closelist中
closelist.push_back(current);
// 对当前搜索节点current进行分类讨论
// 1-current是终点,则返回路径,表示找到路径
if (current->point.x == goal.x && current->point.y == goal.y)
{
while (current != nullptr) // 利用父节点,从终点向起点回溯最短路径,因为起点没有父节点,所以起点current父节点为nullptr
{
path.push_back(current->point);
current = current->parent;
}
reverse(path.begin(), path.end()); // 路径是反的,翻转路径
cleanup();
return path;
}
// 2-current 不是终点,需要讨论其邻近的节点neighbors
int x = current->point.x;
int y = current->point.y;
vector<Point> neighbors = { // 8个邻近节点的坐标
{x - 1,y - 1}, {x - 1,y}, {x - 1,y + 1},
{x,y - 1}, {x,y + 1},
{x + 1,y - 1}, {x + 1,y}, {x + 1,y + 1}
};
// 遍历所有的临近节点,每一个邻近节点n必须满足在地图范围内同时不是障碍物
for (auto n : neighbors)
{
if ((n.x >= 0 && n.x < row) && (n.y >= 0 && n.y < col) && gridmap[n.x][n.y] == 0)
{
bool incloselist = false;
for (auto c : closelist)
{
if (c->point.x == n.x && c->point.y == n.y) // FIX: n.y(原来误写成 y)
{
incloselist = true;
break;
}
}
if (incloselist) continue;

bool inopenlist = false;
for (auto o : openlist)
{
if (o->point.x == n.x && o->point.y == n.y)
{
inopenlist = true;

// 用更小的 g 做松弛,并同步更新 g/h/f 与 parent
double cand_g = current->g + n.distance(current->point);
if (cand_g < o->g)
{
o->g = cand_g;
o->h = n.distance(goal);
o->f = o->g + o->h;
o->parent = current;
}
break;
}
}

if (!inopenlist)
{
double g = current->g + n.distance(current->point);
double h = n.distance(goal);
openlist.push_back(new Node(n, g, h, current));
}
}
}
}

// 搜索完成没有路径:释放内存,返回空路径
cleanup();
return path;
}
int main()
{
// 定义地图
vector<vector<int>> gridmap = {
{0, 1, 0, 0, 0,0,0,0,0,0},
{0, 1, 0, 0, 0,0,0,0,0,0},
{0, 1, 0, 1, 1,1,1,1,1,0},
{0, 1, 0, 0, 0,0,0,0,1,0},
{0, 1, 0, 1, 1,1,1,1,1,0},
{0, 1, 0, 0, 0,1,0,0,0,0},
{0, 0, 0, 0, 0,1,0,0,0,0},
{0, 1, 0, 1, 1,1,0,0,0,0},
{0, 1, 0, 1, 0,0,0,0,0,0},

};
// 定义起点和终点
Point start{ 0, 0 };
Point goal{ 8, 9 };
vector<Point> path = AstarPathPlanning(gridmap, start, goal);
cout << path.size() << endl;
for (auto p : path)
{
if (p.x == goal.x && p.y == goal.y)
{
cout << "(" << p.x << ',' << p.y << ")" << endl;
}
else
{
cout << "(" << p.x << ',' << p.y << ")" << "->";
}
}
return 0;
}