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** 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** 2. **一致性Consistency**
- 一致性的启发式函数也称为单调性启发式函数。如果对于所有节点 \(n\) 和其每个子节点 \(m\),启发式函数 \(h\) 满足 \(h(n) \leq c(n, m) + h(m)\),其中 \(c(n, m)\) 是从节点 \(n\) 到节点 \(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)\) - 数学定义:$h(n) \leq c(n, m) + h(m)$
### 启发式函数的示例 ### 启发式函数的示例
1. **曼哈顿距离Manhattan Distance** 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** 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*搜索? ### 什么是A*搜索?
A*搜索是一种图搜索算法它结合了Dijkstra算法和贪婪最佳优先搜索的优点。A*搜索使用启发式函数来引导搜索方向,从而找到从起始点到目标点的最优路径。 A\*搜索是一种图搜索算法它结合了Dijkstra算法和贪婪最佳优先搜索的优点。A*搜索使用启发式函数来引导搜索方向,从而找到从起始点到目标点的最优路径。
### A*搜索的工作原理 ### A*搜索的工作原理
@ -48,15 +48,14 @@ A*搜索是一种图搜索算法它结合了Dijkstra算法和贪婪最佳优
- 重复上述步骤,直到找到目标节点或优先队列为空。 - 重复上述步骤,直到找到目标节点或优先队列为空。
3. **代价函数** 3. **代价函数**
- A*搜索使用一个代价函数 \(f(n) = g(n) + h(n)\) 来评估每个节点的优先级。 - A*搜索使用一个代价函数 $f(n) = g(n) + h(n)$ 来评估每个节点的优先级。
- 其中,\(g(n)\) 是从起始节点到节点 \(n\) 的实际代价,\(h(n)\) 是从节点 \(n\) 到目标节点的启发式估计代价。 - 其中,$g(n)$ 是从起始节点到节点 $n$ 的实际代价,$h(n)$ 是从节点 $n$ 到目标节点的启发式估计代价。
### A*搜索的伪代码 ### A*搜索的伪代码
```pseudo ```pseudo
function A*(start, goal) function A*(start, goal)
openSet := {start} openSet := {start}
cameFrom := empty map
gScore := map with default value of Infinity gScore := map with default value of Infinity
gScore[start] := 0 gScore[start] := 0
@ -67,26 +66,18 @@ function A*(start, goal)
while openSet is not empty while openSet is not empty
current := node in openSet with lowest fScore[current] current := node in openSet with lowest fScore[current]
if current == goal if current == goal
return reconstruct_path(cameFrom, current) return success
openSet.remove(current) openSet.remove(current)
for each neighbor of current for each neighbor of current
tentative_gScore := gScore[current] + d(current, neighbor) tentative_gScore := gScore[current] + d(current, neighbor)
if tentative_gScore < gScore[neighbor] if tentative_gScore < gScore[neighbor]
cameFrom[neighbor] := current
gScore[neighbor] := tentative_gScore gScore[neighbor] := tentative_gScore
fScore[neighbor] := gScore[neighbor] + heuristic(neighbor, goal) fScore[neighbor] := gScore[neighbor] + heuristic(neighbor, goal)
if neighbor not in openSet if neighbor not in openSet
openSet.add(neighbor) openSet.add(neighbor)
return failure 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*搜索的应用

View File

@ -43,7 +43,7 @@ Minimax 是一种用于两人对弈游戏的决策算法,如国际象棋、井
``` ```
X | O | X X | O | X X | O | X X | O | X X | O | X X | O | X
----------- ----------- ----------- ----------- ----------- -----------
O | X | X O | X | O | X | O | X | X O | X | O | X |
----------- ----------- ----------- ----------- ----------- -----------
| | O X | | O X | | O | | O X | | O X | | O
``` ```
@ -58,7 +58,7 @@ Minimax 是一种用于两人对弈游戏的决策算法,如国际象棋、井
``` ```
X | O | X X | O | X X | O | X X | O | X X | O | X X | O | X
----------- ----------- ----------- ----------- ----------- -----------
O | X | X O | X | O | X | O | X | X O | X | O | X |
----------- ----------- ----------- ----------- ----------- -----------
| | O X | | O X | | O | | O X | | O X | | O
@ -107,6 +107,16 @@ function minimax(node, depth, maximizingPlayer)
Alpha-Beta 剪枝是 Minimax 算法的一种优化。它通过剪枝那些不会影响最终决策的分支,减少需要评估的节点数量。 Alpha-Beta 剪枝是 Minimax 算法的一种优化。它通过剪枝那些不会影响最终决策的分支,减少需要评估的节点数量。
### Alpha-Beta 剪枝的基本思想
Alpha-Beta 剪枝的主要思想是:在某些情况下,可以提前停止对某些节点的评估,因为这些节点不会影响最终的决策。
- **Alpha 值**:当前节点在最大化玩家层面上可以得到的最高分数。
- **Beta 值**:当前节点在最小化玩家层面上可以得到的最低分数。
如果在搜索过程中发现一个节点的评估值无法改进当前的 Alpha 或 Beta 值,就可以停止对该节点的进一步搜索。
### 带 Alpha-Beta 剪枝的伪代码 ### 带 Alpha-Beta 剪枝的伪代码
```pseudo ```pseudo
@ -120,7 +130,7 @@ function alphabeta(node, depth, α, β, maximizingPlayer)
eval = alphabeta(child, depth - 1, α, β, false) eval = alphabeta(child, depth - 1, α, β, false)
maxEval = max(maxEval, eval) maxEval = max(maxEval, eval)
α = max(α, eval) α = max(α, eval)
if β <= α if β < α
break break
return maxEval return maxEval
else else
@ -129,7 +139,7 @@ function alphabeta(node, depth, α, β, maximizingPlayer)
eval = alphabeta(child, depth - 1, α, β, true) eval = alphabeta(child, depth - 1, α, β, true)
minEval = min(minEval, eval) minEval = min(minEval, eval)
β = min(β, eval) β = min(β, eval)
if β <= α if β < α
break break
return minEval return minEval
``` ```

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.