A*作为最常用的路径搜索算法,值得我们去深刻的研究。路径规划项目。先看一下维基百科给的算法解释:https://en.wikipedia.org/wiki/A*_search_algorithm

A *是最佳优先搜索它通过在解决方案的所有可能路径(目标)中搜索导致成本最小(行进距离最短,时间最短等)的问题来解决问题。 ),并且在这些路径中,它首先考虑那些似乎最快速地引导到解决方案的路径。它是根据加权图制定的:从图的特定节点开始,它构造从该节点开始的路径,一次一步地扩展路径,直到其一个路径在预定目标节点处结束。

在其主循环的每次迭代中,A *需要确定将其部分路径中的哪些扩展为一个或多个更长的路径。它是基于成本(总重量)的估计仍然到达目标节点。具体而言,A *选择最小化的路径

其中n是路径上的最后一个节点,gn)是从起始节点到n的路径的开销,hn)是一个启发式,用于估计从n到目标的最便宜路径的开销。启发式是特定于问题的。为了找到实际最短路径的算法,启发函数必须是可接受的,这意味着它永远不会高估实际成本到达最近的目标节点。

维基百科给出的伪代码:

function A*(start, goal)
// The set of nodes already evaluated
closedSet := {} // The set of currently discovered nodes that are not evaluated yet.
// Initially, only the start node is known.
openSet := {start} // For each node, which node it can most efficiently be reached from.
// If a node can be reached from many nodes, cameFrom will eventually contain the
// most efficient previous step.
cameFrom := an empty map // For each node, the cost of getting from the start node to that node.
gScore := map with default value of Infinity // The cost of going from start to start is zero.
gScore[start] := 0 // For each node, the total cost of getting from the start node to the goal
// by passing by that node. That value is partly known, partly heuristic.
fScore := map with default value of Infinity // For the first node, that value is completely heuristic.
fScore[start] := heuristic_cost_estimate(start, goal) while openSet is not empty
current := the node in openSet having the lowest fScore[] value
if current = goal
return reconstruct_path(cameFrom, current) openSet.Remove(current)
closedSet.Add(current) for each neighbor of current
if neighbor in closedSet
continue // Ignore the neighbor which is already evaluated. if neighbor not in openSet // Discover a new node
openSet.Add(neighbor) // The distance from start to a neighbor
//the "dist_between" function may vary as per the solution requirements.
tentative_gScore := gScore[current] + dist_between(current, neighbor)
if tentative_gScore >= gScore[neighbor]
continue // This is not a better path. // This path is the best until now. Record it!
cameFrom[neighbor] := current
gScore[neighbor] := tentative_gScore
fScore[neighbor] := gScore[neighbor] + heuristic_cost_estimate(neighbor, goal) return failure function reconstruct_path(cameFrom, current)
total_path := {current}
while current in cameFrom.Keys:
current := cameFrom[current]
total_path.append(current)
return total_path

下面是UDACITY课程中路径规划项目,结合上面的伪代码,用python 实现A*

import math
def shortest_path(M,start,goal):
sx=M.intersections[start][]
sy=M.intersections[start][]
gx=M.intersections[goal][]
gy=M.intersections[goal][]
h=math.sqrt((sx-gx)*(sx-gx)+(sy-gy)*(sy-gy))
closedSet=set()
openSet=set()
openSet.add(start)
gScore={}
gScore[start]=
fScore={}
fScore[start]=h
cameFrom={}
sumg=
NEW=
BOOL=False
while len(openSet)!=:
MAX=
for new in openSet:
print("new",new)
if fScore[new]<MAX:
MAX=fScore[new]
#print("MAX=",MAX)
NEW=new
current=NEW
print("current=",current)
if current==goal:
return reconstruct_path(cameFrom,current)
openSet.remove(current)
closedSet.add(current)
#dafult=M.roads(current)
for neighbor in M.roads[current]:
BOOL=False
print("key=",neighbor)
a={neighbor}
if len(a&closedSet)>:
continue
print("key is not in closeSet")
if len(a&openSet)==:
openSet.add(neighbor)
else:
BOOL=True
x= M.intersections[current][]
y= M.intersections[current][]
x1=M.intersections[neighbor][]
y1=M.intersections[neighbor][]
g=math.sqrt((x-x1)*(x-x1)+(y-y1)*(y-y1))
h=math.sqrt((x1-gx)*(x1-gx)+(y1-gy)*(y1-gy)) new_gScore=gScore[current]+g
if BOOL==True:
if new_gScore>=gScore[neighbor]:
continue
print("new_gScore",new_gScore)
cameFrom[neighbor]=current
gScore[neighbor]=new_gScore
fScore[neighbor] = new_gScore+h
print("fScore",neighbor,"is",new_gScore+h)
print("fScore=",new_gScore+h) print("__________++--------------++_________") def reconstruct_path(cameFrom,current):
print("已到达lllll")
total_path=[]
total_path.append(current)
for key,value in cameFrom.items():
print("key",key,":","value",value) while current in cameFrom.keys(): current=cameFrom[current]
total_path.append(current)
total_path=list(reversed(total_path))
return total_path

python 实现A*算法的更多相关文章

  1. python数据结构与算法

    最近忙着准备各种笔试的东西,主要看什么数据结构啊,算法啦,balahbalah啊,以前一直就没看过这些,就挑了本简单的<啊哈算法>入门,不过里面的数据结构和算法都是用C语言写的,而自己对p ...

  2. 【转】你真的理解Python中MRO算法吗?

    你真的理解Python中MRO算法吗? MRO(Method Resolution Order):方法解析顺序. Python语言包含了很多优秀的特性,其中多重继承就是其中之一,但是多重继承会引发很多 ...

  3. Python数据结构与算法--List和Dictionaries

    Lists 当实现 list 的数据结构的时候Python 的设计者有很多的选择. 每一个选择都有可能影响着 list 操作执行的快慢. 当然他们也试图优化一些不常见的操作. 但是当权衡的时候,它们还 ...

  4. Python数据结构与算法--算法分析

    在计算机科学中,算法分析(Analysis of algorithm)是分析执行一个给定算法需要消耗的计算资源数量(例如计算时间,存储器使用等)的过程.算法的效率或复杂度在理论上表示为一个函数.其定义 ...

  5. Python实现ID3算法

    自己用Python写的数据挖掘中的ID3算法,现在觉得Python是实现算法的最好工具: 先贴出ID3算法的介绍地址http://wenku.baidu.com/view/cddddaed0975f4 ...

  6. 以图搜图(一):Python实现dHash算法(转)

    近期研究了一下以图搜图这个炫酷的东西.百度和谷歌都有提供以图搜图的功能,有兴趣可以找一下.当然,不是很深入.深入的话,得运用到深度学习这货.Python深度学习当然不在话下. 这个功能最核心的东西就是 ...

  7. Python之排序算法:快速排序与冒泡排序

    Python之排序算法:快速排序与冒泡排序 转载请注明源地址:http://www.cnblogs.com/funnyzpc/p/7828610.html 入坑(简称IT)这一行也有些年头了,但自老师 ...

  8. python实现排序算法 时间复杂度、稳定性分析 冒泡排序、选择排序、插入排序、希尔排序

    说到排序算法,就不得不提时间复杂度和稳定性! 其实一直对稳定性不是很理解,今天研究python实现排序算法的时候突然有了新的体会,一定要记录下来 稳定性: 稳定性指的是 当排序碰到两个相等数的时候,他 ...

  9. python常见排序算法解析

    python——常见排序算法解析   算法是程序员的灵魂. 下面的博文是我整理的感觉还不错的算法实现 原理的理解是最重要的,我会常回来看看,并坚持每天刷leetcode 本篇主要实现九(八)大排序算法 ...

  10. Python使用DDA算法和中点Bresenham算法画直线

    title: "Python使用DDA算法和中点Bresenham算法画直线" date: 2018-06-11T19:28:02+08:00 tags: ["图形学&q ...

随机推荐

  1. JPA实体

    Java类可以很容易地转换成实体. 对于实体转换,基本要求是 - 无参数构造函数 注解 @Entity和@Id注解. @Entity - 这是一个标记注释,表明这个类是一个实体.这个注释必须放在类名称 ...

  2. Python当前进程信息 (os包)

    Python当前进程信息 (os包) 我们在Linux的概念与体系,多次提及进程的重要性.Python的os包中有查询和修改进程信息的函数.学习Python的这些工具也有助于理解Linux体系. (o ...

  3. 多机MySQL一主双从详细安装主从复制

    多机MySQL一主双从详细安装 一.复制的工作原理 要想实现AB复制,那么前提是master上必须要开启二进制日志 1.首先master将数据更新记录到二进制日志文件 2.从slave start开始 ...

  4. JavaBean与Map的相互转换

    package com.bizvane.utils.tools; import java.lang.reflect.Field; import java.util.Arrays; import jav ...

  5. C#基础之Async和Await 的异步编程

    官方文档地址:https://docs.microsoft.com/zh-cn/dotnet/csharp/programming-guide/concepts/async/ Coffee cup = ...

  6. 深入理解PHP类的自动载入机制方法

    第一种情况:文件A.php中内容如下 <?phpclass A{ public function __construct(){ echo 'fff'; }}?> 文件C.php 中内容如下 ...

  7. Ionic.Zip

    1.Ionic.zIP 实现文件压缩和解压 2.压缩: /// <summary>        /// 压缩文件        /// </summary>        / ...

  8. hdu 1671&& poj 3630 (trie 树应用)

    Phone List Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 25280   Accepted: 7678 Descr ...

  9. Ajax.dll使用方法和步骤(比较方便的实现ajax)

    转载自   博客频道 - CSDN.NET http://blog.csdn.net/houhanxin1/article/details/6671470 1. 有直接用框架的:Ajaxpro和Aja ...

  10. Redis 扫盲

    Redis扫盲 非关系型数据库分类: 键值存储数据库:主要会使用到一个 Hash 表,这个表有一个特定的键和一个指针指向特定的数据,Redis 列存储数据库:应对分布式存储的海量数据,键仍然存在,但是 ...