fix: a* in docs

This commit is contained in:
zsq259
2024-06-25 09:59:02 +08:00
parent 6327553edf
commit bd221efd21
13 changed files with 24 additions and 23 deletions

View File

@ -14,27 +14,27 @@
1. **可接受性Admissibility**
- 一个启发式函数是可接受的,如果它从不高估从节点到目标节点的实际最小成本。
- 数学定义:对于所有节点 \(n\),启发式函数 \(h(n)\) 必须满足 \(h(n) \leq h^*(n)\),其中 \(h^*(n)\) 是从节点 \(n\) 到目标节点的实际成本。
- 数学定义:对于所有节点 $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)\)
- 一致性的启发式函数也称为单调性启发式函数。如果对于所有节点 $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|\)
- 公式:$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}\)
- 公式:$h(n) = \sqrt{(x_1 - x_2)^2 + (y_1 - y_2)^2}$
## A*搜索算法
### 什么是A*搜索?
A*搜索是一种图搜索算法它结合了Dijkstra算法和贪婪最佳优先搜索的优点。A*搜索使用启发式函数来引导搜索方向,从而找到从起始点到目标点的最优路径。
A\*搜索是一种图搜索算法它结合了Dijkstra算法和贪婪最佳优先搜索的优点。A*搜索使用启发式函数来引导搜索方向,从而找到从起始点到目标点的最优路径。
### A*搜索的工作原理
@ -48,15 +48,14 @@ A*搜索是一种图搜索算法它结合了Dijkstra算法和贪婪最佳优
- 重复上述步骤,直到找到目标节点或优先队列为空。
3. **代价函数**
- A*搜索使用一个代价函数 \(f(n) = g(n) + h(n)\) 来评估每个节点的优先级。
- 其中,\(g(n)\) 是从起始节点到节点 \(n\) 的实际代价,\(h(n)\) 是从节点 \(n\) 到目标节点的启发式估计代价。
- 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
@ -67,26 +66,18 @@ function A*(start, goal)
while openSet is not empty
current := node in openSet with lowest fScore[current]
if current == goal
return reconstruct_path(cameFrom, current)
return success
openSet.remove(current)
for each neighbor of current
tentative_gScore := gScore[current] + d(current, neighbor)
if tentative_gScore < gScore[neighbor]
cameFrom[neighbor] := current
if tentative_gScore < gScore[neighbor]
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*搜索的应用