Better variable names

This commit is contained in:
Johann Dreo 2014-03-18 11:50:38 +01:00
commit 28e5ce5e8d

25
path.py
View file

@ -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 []