while not frontier.empty(): current = frontier.get() if current == goal: break for next in graph.neighbors(current): new_cost = cost_so_far[current] + graph.cost(current,next) if next not in came_from or new_cost < cost_so_far[next]: cost_so_far[next] = new_cost priority = new_cost frontier.put(next,priority) came_from[next] = current # 跟随指向从终点回到起点 current = goal path = [] while current != start: path.append(current) current = came_from[current] path.append(start) path.reverse() # 将顺序正过来,终点到起点,变为起点到终点 return path,cost_so_far
while not frontier.empty(): current = frontier.get() if current == goal: break for next in graph.neighbors(current): if next not in came_from: priority = heuristic(goal, next) frontier.put(next, priority) came_from[next] = current # 跟随指向从终点回到起点 current = goal path = [] while current != start: path.append(current) current = came_from[current] path.append(start) path.reverse() # 将顺序正过来,终点到起点,变为起点到终点 return path
if current == goal: break for next in graph.neighbors(current): new_cost = cost_so_far[current] + graph.cost(current, next) if next not in cost_so_far or new_cost < cost_so_far[next]: cost_so_far[next] = new_cost priority = new_cost + heuristic(goal, next) frontier.put(next, priority) came_from[next] = current # 跟随指向从终点回到起点 current = goal path = [] while current != start: path.append(current) current = came_from[current] path.append(start) path.reverse() # 将顺序正过来,终点到起点,变为起点到终点 return came_from, cost_so_far
JPS(jump point search)算法实际上是对A* 寻路算法的一个改进,A* 算法在扩展节点时会把节点所有邻居都考虑进去,这样openlist中点的数量会很多,搜索效率较慢。JPS算法则是希望直线方向上中途的点不用放入openlist,只放入每段直线子路径的起点和终点,提高搜索效率。根据强迫邻居和跳点的概念,JPS算法可以有效地跳过一些不必要的节点,如果提前存储好跳点数据则为JPS+算法。