Better variable names
This commit is contained in:
parent
75e1a855cc
commit
28e5ce5e8d
1 changed files with 13 additions and 12 deletions
25
path.py
25
path.py
|
|
@ -15,7 +15,7 @@ def astar(graph, start, goal, cost = euclidian_distance, heuristic = euclidian_d
|
||||||
closed = set()
|
closed = set()
|
||||||
m_heur = {}
|
m_heur = {}
|
||||||
m_parent = {}
|
m_parent = {}
|
||||||
m_cost = {} # path costs
|
m_cost = {} # absolute path costs
|
||||||
|
|
||||||
def path_from(node):
|
def path_from(node):
|
||||||
def parents(node):
|
def parents(node):
|
||||||
|
|
@ -33,25 +33,26 @@ def astar(graph, start, goal, cost = euclidian_distance, heuristic = euclidian_d
|
||||||
# sort opened nodes based on the heuristic and consider the first one
|
# sort opened nodes based on the heuristic and consider the first one
|
||||||
current = sorted(opened, key=lambda n : m_heur.get( n, heuristic(n,goal) ) )[0]
|
current = sorted(opened, key=lambda n : m_heur.get( n, heuristic(n,goal) ) )[0]
|
||||||
if current == goal:
|
if current == goal:
|
||||||
|
# FIXME add backtrack validation
|
||||||
return path_from(current)
|
return path_from(current)
|
||||||
|
|
||||||
closed.add(current)
|
closed.add(current)
|
||||||
opened.remove(current)
|
opened.remove(current)
|
||||||
|
|
||||||
for node in graph[current]:
|
for neighbor in graph[current]:
|
||||||
if node in closed:
|
if neighbor in closed:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
elif node in opened:
|
elif neighbor in opened:
|
||||||
next_cost = m_cost[current] + cost(current,node)
|
next_cost = m_cost[current] + cost(current,neighbor)
|
||||||
if next_cost < m_cost[node]:
|
if next_cost < m_cost[neighbor]:
|
||||||
m_cost[node] = next_cost
|
m_cost[neighbor] = next_cost
|
||||||
m_parent[node] = current
|
m_parent[neighbor] = current
|
||||||
else:
|
else:
|
||||||
m_cost[node] = m_cost[current] + cost(current,node)
|
m_cost[neighbor] = m_cost[current] + cost(current,neighbor)
|
||||||
m_heur[node] = heuristic( node, goal )
|
m_heur[neighbor] = heuristic( neighbor, goal )
|
||||||
m_parent[node] = current
|
m_parent[neighbor] = current
|
||||||
opened.add(node)
|
opened.add(neighbor)
|
||||||
return []
|
return []
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue