python 实现A*算法
A*作为最常用的路径搜索算法,值得我们去深刻的研究。路径规划项目。先看一下维基百科给的算法解释:https://en.wikipedia.org/wiki/A*_search_algorithm
A *是最佳优先搜索它通过在解决方案的所有可能路径(目标)中搜索导致成本最小(行进距离最短,时间最短等)的问题来解决问题。 ),并且在这些路径中,它首先考虑那些似乎最快速地引导到解决方案的路径。它是根据加权图制定的:从图的特定节点开始,它构造从该节点开始的路径树,一次一步地扩展路径,直到其一个路径在预定目标节点处结束。
在其主循环的每次迭代中,A *需要确定将其部分路径中的哪些扩展为一个或多个更长的路径。它是基于成本(总重量)的估计仍然到达目标节点。具体而言,A *选择最小化的路径
其中n是路径上的最后一个节点,g(n)是从起始节点到n的路径的开销,h(n)是一个启发式,用于估计从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*算法的更多相关文章
- python数据结构与算法
最近忙着准备各种笔试的东西,主要看什么数据结构啊,算法啦,balahbalah啊,以前一直就没看过这些,就挑了本简单的<啊哈算法>入门,不过里面的数据结构和算法都是用C语言写的,而自己对p ...
- 【转】你真的理解Python中MRO算法吗?
你真的理解Python中MRO算法吗? MRO(Method Resolution Order):方法解析顺序. Python语言包含了很多优秀的特性,其中多重继承就是其中之一,但是多重继承会引发很多 ...
- Python数据结构与算法--List和Dictionaries
Lists 当实现 list 的数据结构的时候Python 的设计者有很多的选择. 每一个选择都有可能影响着 list 操作执行的快慢. 当然他们也试图优化一些不常见的操作. 但是当权衡的时候,它们还 ...
- Python数据结构与算法--算法分析
在计算机科学中,算法分析(Analysis of algorithm)是分析执行一个给定算法需要消耗的计算资源数量(例如计算时间,存储器使用等)的过程.算法的效率或复杂度在理论上表示为一个函数.其定义 ...
- Python实现ID3算法
自己用Python写的数据挖掘中的ID3算法,现在觉得Python是实现算法的最好工具: 先贴出ID3算法的介绍地址http://wenku.baidu.com/view/cddddaed0975f4 ...
- 以图搜图(一):Python实现dHash算法(转)
近期研究了一下以图搜图这个炫酷的东西.百度和谷歌都有提供以图搜图的功能,有兴趣可以找一下.当然,不是很深入.深入的话,得运用到深度学习这货.Python深度学习当然不在话下. 这个功能最核心的东西就是 ...
- Python之排序算法:快速排序与冒泡排序
Python之排序算法:快速排序与冒泡排序 转载请注明源地址:http://www.cnblogs.com/funnyzpc/p/7828610.html 入坑(简称IT)这一行也有些年头了,但自老师 ...
- python实现排序算法 时间复杂度、稳定性分析 冒泡排序、选择排序、插入排序、希尔排序
说到排序算法,就不得不提时间复杂度和稳定性! 其实一直对稳定性不是很理解,今天研究python实现排序算法的时候突然有了新的体会,一定要记录下来 稳定性: 稳定性指的是 当排序碰到两个相等数的时候,他 ...
- python常见排序算法解析
python——常见排序算法解析 算法是程序员的灵魂. 下面的博文是我整理的感觉还不错的算法实现 原理的理解是最重要的,我会常回来看看,并坚持每天刷leetcode 本篇主要实现九(八)大排序算法 ...
- Python使用DDA算法和中点Bresenham算法画直线
title: "Python使用DDA算法和中点Bresenham算法画直线" date: 2018-06-11T19:28:02+08:00 tags: ["图形学&q ...
随机推荐
- mac 终端 常用命令,MacOS 常用终端命令大全,mac 在当前目录打开终端
MacOS 常用终端命令大全:目录操作dircmp——比较两个目录的内容——dircmp dir1 dir2文件操作pg分页格式化显示文件内容——pg filenameod——显示非文本文件的内容—— ...
- 使用session实现一次性验证码
在登录页面和各种页面,会看到有验证码输入,这样做的目的是为了防止密码猜测工具破解密码,保护了用户密码安全,验证码只能使用一次,这样就给密码猜测工具带来了很大的困难,基本上阻断了密码猜测工具的使用. 可 ...
- 2019-3-1-WPF-从零开始开发-dotnet-Remoting-程序
title author date CreateTime categories WPF 从零开始开发 dotnet Remoting 程序 lindexi 2019-03-01 09:30:45 +0 ...
- python中bisect模块的使用
一般用于二分查找, 当然列表应该是有序表 参考于: http://blog.csdn.net/xiaocaiju/article/details/6975714
- pug的安装与使用
说明 Pug原名不叫Pug,是大名鼎鼎的jade,后来由于商标的原因,改为Pug,哈巴狗.其实只是换个名字,语法都与jade一样.丑话说在前面,Pug有它本身的缺点--可移植性差,调试困难,性能并不出 ...
- vue项目开发过程常见问题
更新时间:2018-07-29 1.data functions should return an object // 这个问题是 Vue 实例内,单组件的data必须返回一个对象;如下 <sc ...
- iOS 中的 Deferred Deep Linking(延迟深度链接)
http://www.cocoachina.com/ios/20160105/14871.html Deep Linking 其实 deep linking 并不是一个新名词,在 web 开发领域,区 ...
- 详细介绍Java中的堆、栈和常量池
下面主要介绍JAVA中的堆.栈和常量池: 1.寄存器 最快的存储区, 由编译器根据需求进行分配,我们在程序中无法控制. 2. 栈 存放基本类型的变量数据和对象的引用,但对象本身不存放在栈中,而是存放在 ...
- 重磅!阿里云Promtheus 正式免费公测
每日头条 重磅!容器集群监控利器 阿里云Promtheus 正式免费公测 Prometheus 作为容器生态下集群监控的首选方案,是一套开源的系统监控报警框架.2019 年7月3日,阿里云Promth ...
- This Product is covered by one or more of the folloWing patents
借用一下网络图片,作为描述: 原因: 启动方式使用了网络启动, 解决方案: 进入bios,修改启动方式,禁用网卡驱动,使用从硬盘启动或者从U盘启动即可.