Files
PPCA-AIPacMan-2024/docs/A*.md
2024-06-24 15:46:49 +08:00

104 lines
4.3 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 启发式函数与A*搜索算法
## 介绍
在人工智能和路径规划中启发式函数和A*搜索算法是两个重要的概念。启发式函数用于估计从当前状态到目标状态的代价而A\*搜索算法则利用这些估计来找到最优路径。
## 启发式函数
### 什么是启发式函数?
启发式函数Heuristic Function是用于估计当前状态到目标状态之间的最小成本的函数。它的设计是为了加速搜索算法使其更高效地找到解决方案。
### 启发式函数的性质
1. **可接受性Admissibility**
- 一个启发式函数是可接受的,如果它从不高估从节点到目标节点的实际最小成本。
- 数学定义:对于所有节点 \(n\),启发式函数 \(h(n)\) 必须满足 \(h(n) \leq h^*(n)\),其中 \(h^*(n)\) 是从节点 \(n\) 到目标节点的实际成本。
2. **一致性Consistency**
- 一致性的启发式函数也称为单调性启发式函数。如果对于所有节点 \(n\) 和其每个子节点 \(m\),启发式函数 \(h\) 满足 \(h(n) \leq c(n, m) + h(m)\),其中 \(c(n, m)\) 是从节点 \(n\) 到节点 \(m\) 的实际成本。
- 数学定义:\(h(n) \leq c(n, m) + h(m)\)。
### 启发式函数的示例
1. **曼哈顿距离Manhattan Distance**
- 在网格路径规划中,曼哈顿距离是两个点之间沿轴线方向的总距离。
- 公式:\(h(n) = |x_1 - x_2| + |y_1 - y_2|\)。
2. **欧几里得距离Euclidean Distance**
- 欧几里得距离是两点之间的直线距离。
- 公式:\(h(n) = \sqrt{(x_1 - x_2)^2 + (y_1 - y_2)^2}\)。
## A*搜索算法
### 什么是A*搜索?
A*搜索是一种图搜索算法它结合了Dijkstra算法和贪婪最佳优先搜索的优点。A*搜索使用启发式函数来引导搜索方向,从而找到从起始点到目标点的最优路径。
### A*搜索的工作原理
1. **初始化**
- 将起始节点添加到优先队列中初始代价为0。
2. **搜索过程**
- 从优先队列中取出总代价最小的节点作为当前节点。
- 如果当前节点是目标节点,则搜索结束。
- 否则,扩展当前节点的所有邻居节点,并更新它们的代价和优先级。
- 重复上述步骤,直到找到目标节点或优先队列为空。
3. **代价函数**
- A*搜索使用一个代价函数 \(f(n) = g(n) + h(n)\) 来评估每个节点的优先级。
- 其中,\(g(n)\) 是从起始节点到节点 \(n\) 的实际代价,\(h(n)\) 是从节点 \(n\) 到目标节点的启发式估计代价。
### A*搜索的伪代码
```pseudo
function A*(start, goal)
openSet := {start}
cameFrom := empty map
gScore := map with default value of Infinity
gScore[start] := 0
fScore := map with default value of Infinity
fScore[start] := heuristic(start, goal)
while openSet is not empty
current := node in openSet with lowest fScore[current]
if current == goal
return reconstruct_path(cameFrom, current)
openSet.remove(current)
for each neighbor of current
tentative_gScore := gScore[current] + d(current, neighbor)
if tentative_gScore < gScore[neighbor]
cameFrom[neighbor] := current
gScore[neighbor] := tentative_gScore
fScore[neighbor] := gScore[neighbor] + heuristic(neighbor, goal)
if neighbor not in openSet
openSet.add(neighbor)
return failure
function reconstruct_path(cameFrom, current)
total_path := {current}
while current in cameFrom
current := cameFrom[current]
total_path.prepend(current)
return total_path
```
### A*搜索的应用
- **视频游戏**:用于角色路径规划。
- **路径规划问题**:如地图导航、机器人路径规划等。
- **资源规划问题**:如物流和供应链管理。
- **语言分析**:如句法分析。
- **机器翻译和语音识别**:用于寻找最优匹配和路径。
## 结论
启发式函数和A\*搜索算法是解决复杂路径规划和搜索问题的重要工具。通过设计有效的启发式函数可以显著提高搜索算法的效率。A*搜索算法结合了路径代价和启发式估计,是找到最优路径的强大方法。