A*算法(A-star Algorithm)是一种启发式搜索算法,广泛用于路径规划和图搜索问题,如地图导航、游戏AI、机器人路径规划等。它结合了广度优先搜索和贪婪最佳优先搜索的优点,能够有效地找到从起点到目标点的最短路径。
NO.1|A*算法详解,群智能算法小狂人
1、A*算法的核心思想
A*算法使用启发式函数(heuristic function)来估算当前节点到目标节点的代价,从而决定下一步搜索哪个节点。具体来说,A*算法在搜索过程中会考虑以下两个代价函数:
(1)g(n): 从起点到节点n的实际代价(路径长度)。
(2)h(n): 从节点n到目标节点的估算代价(启发式函数)。
A*算法通过综合这两个代价函数,使用总的代价函数f(n) = g(n) + h(n)来选择下一步要扩展的节点,其中:
g(n)确保了路径的实际代价被考虑,避免盲目贪心。
h(n)提供了前瞻性,引导搜索方向朝向目标。
2、A*算法的步骤
(1)初始化
创建一个开放列表(open list),初始时只包含起点节点。
创建一个关闭列表(closed list),初始为空。
将起点节点的 g 值设为 0,f 值设为 h(起点)。
(2)主循环
在开放列表中选择 f 值最小的节点(称为当前节点),将其移出开放列表,并加入关闭列表。
检查当前节点是否为目标节点。如果是,搜索结束,已找到最短路径。否则,对当前节点的所有邻居节点进行处理
如果邻居节点在关闭列表中,跳过该节点。
如果邻居节点不在开放列表中,将其添加进去,并计算其 g 值和 f 值。
如果邻居节点已经在开放列表中,并且新的 g 值更小,更新该节点的 g 值和 f 值,并更新其父节点为当前节点。
3. 重复主循环,直到找到目标节点或者开放列表为空(意味着无解)。
4. 回溯路径:从目标节点开始,依次通过各个节点的父节点回溯到起点节点,得到最短路径。
3、例子
假设我们有一个5x5的网格地图,起点在左上角(0,0),目标在右下角(4,4),我们使用曼哈顿距离作为 h(n)。A*算法将会根据 f(n) 值选择路径,并最终找到最短路径。A*算法在图搜索和路径规划中具有广泛应用,其效率和最优性使其成为了许多实际问题的首选算法.
import heapq
import matplotlib.pyplot as plt
# A* Algorithm Implementation
def a_star(grid, start, goal):
# Initialize open list and closed list
open_list = []
heapq.heappush(open_list, (0 + heuristic(start, goal), 0, start, []))
closed_set = set()
while open_list:
# Get the current node with the lowest f(n)
f, g, current, path = heapq.heappop(open_list)
if current in closed_set:
continue
path = path + [current]
closed_set.add(current)
# Check if we have reached the goal
if current == goal:
return path
# Explore neighbors
for neighbor in get_neighbors(grid, current):
if neighbor in closed_set:
continue
tentative_g = g + 1 # Assuming uniform cost for all edges
heapq.heappush(open_list, (tentative_g + heuristic(neighbor, goal), tentative_g, neighbor, path))
return None # No path found
def get_neighbors(grid, node):
neighbors = []
directions = [(-1, 0), (1, 0), (0, -1), (0, 1)] # Up, Down, Left, Right
for direction in directions:
neighbor = (node[0] + direction[0], node[1] + direction[1])
if 0 <= neighbor[0] < len(grid) and 0 <= neighbor[1] < len(grid[0]):
neighbors.append(neighbor)
return neighbors
def heuristic(node, goal):
return abs(node[0] - goal[0]) + abs(node[1] - goal[1]) # Manhattan Distance
# Grid representation (0: free space, 1: obstacle)
grid = [[0, 0, 0, 0, 0],
[0, 1, 1, 1, 0],
[0, 1, 0, 1, 0],
[0, 1, 0, 1, 0],
[0, 0, 0, 0, 0]]
start = (0, 0)
goal = (4, 4)
# Run A* algorithm
path = a_star(grid, start, goal)
# Visualization
def visualize_grid(grid, path):
fig, ax = plt.subplots()
for i in range(len(grid)):
for j in range(len(grid[i])):
if grid[i][j] == 1:
ax.add_patch(plt.Rectangle((j, len(grid) - 1 - i), 1, 1, color='black'))
else:
ax.add_patch(plt.Rectangle((j, len(grid) - 1 - i), 1, 1, color='white', edgecolor='black'))
for (i, j) in path:
ax.add_patch(plt.Rectangle((j, len(grid) - 1 - i), 1, 1, color='blue'))
ax.add_patch(plt.Rectangle((start[1], len(grid) - 1 - start[0]), 1, 1, color='green'))
ax.add_patch(plt.Rectangle((goal[1], len(grid) - 1 - goal[0]), 1, 1, color='red'))
plt.xlim(0, len(grid[0]))
plt.ylim(0, len(grid))
plt.gca().set_aspect('equal', adjustable='box')
plt.show()
# Visualize the path
visualize_grid(grid, path)
代码说明与可视化结果
1. 启发式函数:我们使用曼哈顿距离作为启发式函数,即 ,其中 和 分别是当前节点和目标节点的坐标。
这是A*算法在一个5x5网格上的可视化结果。在图中:绿色方块表示起点位置。红色方块表示目标位置。黑色方块表示障碍物。蓝色方块表示A*算法找到的从起点到目标的最短路径。A*算法成功地绕过障碍物,找到了一条从起点到目标的最短路径。
NO.2|GWO原理,群智能算法小狂人
灰狼优化算法(Grey Wolf Optimizer, GWO)是一种基于灰狼捕猎行为的群体智能优化算法,由Mirjalili等人在2014年提出。GWO算法模仿了灰狼在自然界中的社会等级结构和围猎、追踪猎物的过程,用于解决各种优化问题。灰狼在捕猎过程中通常有一个明确的社会等级结构,其中包含以下几种角色:
1. α狼(Alpha):群体的领导者,负责决策,通常是最优解的代表。
2. β狼(Beta):第二等级的狼,协助α狼决策,代表次优解。
3. δ狼(Delta):第三等级的狼,服从α狼和β狼的指令,代表第三优解。
4. ω狼(Omega):最低等级的狼,服从所有其他狼。
在GWO算法中,搜索过程主要由这些灰狼的角色来实现,模拟了捕猎、包围和攻击猎物的过程,其详细的更新及原理可以看我之前发布的文章:引用过万!灰狼算法(Grey Wolf Optimizer, GWO)原理及实现|含源码
NO.3|应用于路径规划,群智能算法小狂人
相关算法的路径规划原理可以查看我之前发布的文章:
(1)基于粒子群算法(Particle Swarm Optimization, PSO)栅格地图机器人路径规划
(2)COA算法:改进小龙虾优化算法(Crayfish Optimization Algorithm, COA)机器人路径规划
(3)基于牛顿-拉夫逊优化算法(Newton-Raphson-based optimizer, NBRO)的无人机三维路径规划