python实现A*搜索算法的学习


维基百科:

A*算法:一种在图形平面上,有多个节点的路径,求出最低通过成本的算法。该算法像Dijkstra算法一样,可以找到一条最短路径;也想BFS一样,进行启发式的搜索。

公式:f(n) = g(n) + h(n),g(n)表示起点到任一点n的实际距离,h(n)表示任一点n到目标顶点的估算距离。

特性:a,若h(n)为0,只需求出g(n),既求出起点到任一点的最短路径,则转化为单元最短路径问题,Dijkstra算法

b,若h(n) <= 'n到目标的实际距离',则一定可以求出最优解。而已h(n)越小,需要计算的节点越多,算法效率越低。

伪代码:

 function A*(start,goal)
     closedset := the empty set                 //已经被估算的节点集合    
     openset := set containing the initial node //将要被估算的节点集合
     came_from := empty map
     g_score[start] := 0                        //g(n)
     h_score[start] := heuristic_estimate_of_distance(start, goal)    //h(n)
     f_score[start] := h_score[start]            //f(n)=h(n)+g(n),由于g(n)=0,所以……
     while openset is not empty
         x := the node in openset having the lowest f_score[] value
         if x = goal
             return reconstruct_path(came_from,goal)
         remove x from openset
         add x to closedset
         foreach y in neighbor_nodes(x)  //foreach=for each
             if y in closedset
                 continue
             tentative_g_score := g_score[x] + dist_between(x,y)
 
             if y not in openset
                 add y to openset
 
                 tentative_is_better := true
             elseif tentative_g_score < g_score[y]
                 tentative_is_better := true
             else
                 tentative_is_better := false
             if tentative_is_better = true
                 came_from[y] := x
                 g_score[y] := tentative_g_score
                 h_score[y] := heuristic_estimate_of_distance(y, goal)
                 f_score[y] := g_score[y] + h_score[y]
     return failure
 
 function reconstruct_path(came_from,current_node)
     if came_from[current_node] is set
         p = reconstruct_path(came_from,came_from[current_node])
         return (p + current_node)
     else
         return current_node

python实现:

def astar(graph, initial_node, goal_node, h):
    closed_set = set() # set of nodes already evaluated
    nodes = set() # set of tentative nodes to be evaluated
    nodes.add(initial_node)
 
    visited = {} # map of navigated nodes
    g_score = {initial_node: 0} # distance from start along optimal path
    h_score = {initial_node: h(initial_node, goal_node)} # heuristic estimate
    f_score = {initial_node: h_score[initial_node]} # estimated dist
 
    while nodes:
        x = None
        for node in nodes:     //pick the shortest distance node
            if x is None:
                x = node
            elif f_score[node] < f_score[x]:
                x = node
 
        nodes.remove(x)
        if x == goal_node:
            return visited
 
        closed_set.add(x)
        for y in graph.edges[x]://calculate the distance
            if y in closed_set:
                continue
            tentative_g_score = g_score[x] + graph.distances[(x, y)]
 
            flag = False
            if y not in nodes or tentative_g_score < g_score[y]: //
                nodes.add(y)
                flag = True
 
            if flag:
                visited[y] = x
 
                g_score[y] = tentative_g_score
                h_score[y] = h(y, goal_node)
                f_score[y] = g_score[y] + h_score[y]
 
    return False

参考网址: http://forrst.com/posts/A_algorithm_in_python-B4c


你可能感兴趣的:(python)