自从我更新它之后,我的其他帖子没有得到回复,我真的需要一些帮助,所以我要重新发布。下面的代码是重要的位,但是完整代码的链接在这里:https://github.com/amstrudy/nao-ncsu/blob/master/oop_a_star.py
我一直在Wikipedia pseudocode跟进Python中的A *实现。我的代码达到了目标,但我似乎无法弄清楚如何重建实际路径。它不起作用,因为更新cameFrom的代码段仅在达到目标时更新一次。我真的不明白,但这就是维基百科文章中的伪代码。
def a_star ():
# create node object for home
home_node = Node(home, goal, home)
# set of nodes already evaluated
closedSet = []
# set of currently discovered nodes that are not evaluated yet
# initially, only the start node is known
openSet = [home_node]
while len(openSet) != 0:
minIndex = 0;
for i in list(range(len(openSet))):
if openSet[i] < openSet[minIndex]:
minIndex = i
cur_node = openSet[minIndex] # expand on node with smallest fScore
cur_node.generateNeighbors() # make new nodes to check
if cur_node.location == home_node.goal:
print("Made it to the goal!")
return reconstruct(cur_node)
openSet.pop(minIndex)
closedSet.append(cur_node)
for neighbor in cur_node.neighbors:
if neighbor in closedSet:
continue
if neighbor not in openSet:
openSet.append(neighbor)
if neighbor.gScore >= cur_node.gScore:
print(neighbor.gScore)
print(cur_node.gScore)
continue # this is not eh better path
# this path is the best for now, so record it
neighbor.cameFrom = cur_node
print(cur_node)
printMap(cur_node.location)
def reconstruct(target):
path = []
while target:
print("here")
path.append(target.location)
target = target.cameFrom
return path
对于评论这是重复的人:是的。我没有收到旧帖子的回复,所以我编辑并重新发布。我将删除旧帖子。