我试图在6*6互连节点网格上的python中实现a*搜索算法,使用networkx组织节点和matplotlib进行显示.我有它的工作,所以它找到了最短的路径,但没有启发式,它只是强力搜索 - 这太昂贵了.如何在创建节点时为我的节点分配x,y坐标,还是有其他方法可以使启发式工作?(我知道networkx有一个我可以使用的内置*函数,但我想证明我可以实现该算法)
这是代码(StackOverflow格式有点乱):
import networkx as nx
G=nx.Graph()
import matplotlib.pyplot as plt
def add_nodes():
G.add_nodes_from([0, 1, 2, 3, 4, 5, \
6, 7, 8, 9, 10, 11, \
12, 13, 14, 15, 16, 17, \
18, 19, 20, 21, 22, 23, \
24, 25, 26, 27, 28, 29,\
30, 31, 32, 33, 34, 35])
c = 0
for y in range (0,5):
for x in range (0,5):
G.add_node(c, pos=(x/10,y/10))
c=c+1
#http://stackoverflow.com/questions/477486/how-to-use-a-decimal-range-step-value
#prev code for brute force search:
#https://pastebin.com/DT76bvw5
#node …Run Code Online (Sandbox Code Playgroud)