我對A *的嘗試存在問題 - 每次運行時都不會發生,但每隔一段時間就會發生這種情況,因此兩個單元成爲彼此的父母。 當我嘗試重新創建路徑時,這反過來導致無限循環。我一直在試圖找出爲什麼這會發生一段時間,但無法弄清楚,所以任何幫助將不勝感激。A *算法實現節點 - 父母錯誤
def find_path(self, start, end):
tstart = time.time()
count = 0
evaled = set()
notEvaled = set([start])
start.g_score(start)
start.h_score(end)
start.f_score()
while len(notEvaled) != 0:
current = min(notEvaled, key=attrgetter('f'))
notEvaled.remove(current)
for neighbour in current.neighbours:
if neighbour.full or neighbour in evaled: continue
if neighbour in notEvaled:
if neighbour.parent != None and neighbour.parent.g > current.g:
neighbour.parent = current
neighbour.g_score(start)
neighbour.f_score()
else:
neighbour.parent = current
neighbour.g_score(start)
neighbour.h_score(end)
neighbour.f_score()
notEvaled.add(neighbour)
if neighbour == end:
path = []
while end != None:
path.insert(0, end)
end = end.parent
return path
evaled.add(current)
return None
這裏是我的得分功能,但我懷疑他們不管
def g_score(self, start):
if self == start:
self.g = 0
else:
self.g = self.parent.g + 1
def h_score(self, end):
self.h = abs(end.x - self.x) + abs(end.y - self.y)
def f_score(self):
self.f = self.g + self.h
是什麼'neighbor.full'嗎? –