lib/astar: handle inconsistent heuristics better

before pruning a node we've already visited, we need to check if it has
a better cost.
main
magical 2022-12-17 19:27:28 -08:00
parent 5a7c8ef732
commit e1fb37d229
1 changed files with 6 additions and 4 deletions

View File

@ -37,11 +37,13 @@ def search(start, is_goal, neighbors, heuristic=None):
return cost_so_far, this, path return cost_so_far, this, path
for c, n in neighbors(this): for c, n in neighbors(this):
if n not in done: c = cost_so_far + c
# calculate the "reduced cost" if n not in done or c < done[n][0]:
c = cost_so_far + c + heuristic(n) h = heuristic(n)
if h is None:
continue
i += 1 i += 1
heappush(q, (c, i, n, this)) heappush(q, (c+h, i, n, this))
return float('inf'), None, [] return float('inf'), None, []