python - A*算法在Python中找不到目标
问题描述
我对 Python 还是很陌生,这是我在 stackoverflow 上的第一个问题,我在实现 A* 算法方面遇到了一周的麻烦。
我得到的代码找到了一个带有直墙的目标,但是正如您将看到的,只要我将墙延伸到起点以下并且它必须向后或绕过它,它就会开始永远循环。
我一直在试图修复它以及如何实现引发代码以使其停止循环。任何帮助将不胜感激。
我的代码:
class Node:
"""A node class for A* Pathfinding"""
def __init__(self, parent=None, position=None):
self.parent = parent
self.position = position
self.g = 0
self.h = 0
self.f = 0
def __eq__(self, other):
return self.position == other.position
def astar(maze, start, end):
"""Returns a list of tuples as a path from the given start to the given end in the given maze"""
# Create start and end node
start_node = Node(None, start)
start_node.g = start_node.h = start_node.f = 0
end_node = Node(None, end)
end_node.g = end_node.h = end_node.f = 0
# Initialize both open and closed list
open_list = []
closed_list = []
# Add the start node
open_list.append(start_node)
# Loop until you find the end
while len(open_list) > 0:
# Get the current node
current_node = open_list[0]
current_index = 0
for index, item in enumerate(open_list):
if item.f < current_node.f:
current_node = item
current_index = index
# Pop current off open list, add to closed list
open_list.pop(current_index)
closed_list.append(current_node)
# Found the goal
if current_node == end_node:
path = []
current = current_node
while current is not None:
path.append(current.position)
current = current.parent
return path[::-1] # Return reversed path
# Generate children
children = []
for new_position in [(0, -1), (0, 1), (-1, 0), (1, 0), (-1, -1), (-1, 1), (1, -1), (1, 1)]: # Adjacent squares
# Get node position
node_position = (current_node.position[0] + new_position[0], current_node.position[1] + new_position[1])
# Make sure within range
if node_position[0] > (len(maze) - 1) or node_position[0] < 0 or node_position[1] > (
len(maze[len(maze) - 1]) - 1) or node_position[1] < 0:
continue
# Make sure walkable terrain
if maze[node_position[0]][node_position[1]] != 0:
print("node -", node_position[0],node_position[1], "is blocked")
continue
# Create new node
new_node = Node(current_node, node_position)
# Append
children.append(new_node)
# Loop through children
for child in children:
# Child is on the closed list
for closed_child in closed_list:
if child == closed_child:
continue
# Create the f, g, and h values
child.g = current_node.g + 1
child.h = ((child.position[0] - end_node.position[0]) ** 2) + (
(child.position[1] - end_node.position[1]) ** 2)
child.f = child.g + child.h
# Child is already in the open list
for open_node in open_list:
if child == open_node and child.g > open_node.g:
continue
# Add the child to the open list
open_list.append(child)
def main():
maze = [[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 1, 1, 1, 1, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
start = (0, 0)
end = (1, 8)
path = astar(maze, start, end)
print(path)
main()
解决方案
你有这两个部分:
# Child is on the closed list
for closed_child in closed_list:
if child == closed_child:
continue
# Child is already in the open list
for open_node in open_list:
if child == open_node and child.g > open_node.g:
continue
将continue
继续内部for
循环。因此,它没有任何效果。你宁愿寻找这样的东西:
# Child is on the closed list
is_in_closed = False
for closed_child in closed_list:
if child == closed_child:
is_in_closed = True
break
if is_in_closed:
continue
# Child is already in the open list
is_in_open = False
for open_node in open_list:
if child == open_node and child.g > open_node.g:
is_in_open = True
break
if is_in_open:
continue
还有两条评论:
您的距离测量仅计算步数。因此,对角线与水平/垂直台阶一样昂贵。您可能想要更改它并获取实际长度或它的近似值以找到真正最短的路径。
您的启发式方法是到目标的平方距离。这不是一个可接受的启发式方法,因为它高估了目标的实际成本。因此,您可能找不到正确的最短路径。对于您的成本函数(步数),更好且可接受的启发式方法将是max(child.position[0] - end_node.position[0], child.position[1] - end_node.position[1])
(您至少需要此步数才能达到目标)。
推荐阅读
- python - 谁能帮我理解这个 Python 数组访问语法?
- python - 使用 lxml 构建多阶段 Docker
- javascript - 如何从网络本地构建前端示例?
- schema.org - 模式标记:在同一页面上使用服务和网页类型
- java - 为什么字节码不是人类可读的?
- android - 重置我在 sqlite 中特定列值的运行总计
- wordpress - 图像构建后安装 wp 插件
- python - 如何在 Dash-Plotly 中折叠侧边栏(Dash-Bootstrap-Components)
- opentbs - 如何解决 TinyButStrong 中的“未初始化的字符串偏移量:”错误
- javascript - CkEditor 图像上传在 laravel 中不起作用