python 实现A_算法的示例代码

来源:中文源码网    浏览: 次    日期:2018年9月2日

【下载文档:  python 实现A_算法的示例代码.txt 】

(友情提示:右键点上行txt文档名->目标另存为)

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

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

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

F(N)= G(N)+ H(n)

其中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 failurefunction 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][0]

sy=M.intersections[start][1]

gx=M.intersections[goal][0]

gy=M.intersections[goal][1]

h=math.sqrt((sx-gx)*(sx-gx)+(sy-gy)*(sy-gy))

closedSet=set()

openSet=set()

openSet.add(start)

gScore={}

gScore[start]=0

fScore={}

fScore[start]=h

cameFrom={}

sumg=0

NEW=0

BOOL=False

while len(openSet)!=0:

MAX=1000

for new in openSet:

print("new",new)

if fScore[new]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)>0:

continue

print("key is not in closeSet")

if len(a&openSet)==0:

openSet.add(neighbor)

else:

BOOL=True

x= M.intersections[current][0]

y= M.intersections[current][1]

x1=M.intersections[neighbor][0]

y1=M.intersections[neighbor][1]

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算法实现源码_python 实现A_算法的示例代码相关推荐

  1. python关键词提取源码_Python 结巴分词 关键词抽取分析

    关键词抽取就是从文本里面把跟这篇文档意义最相关的一些词抽取出来.这个可以追溯到文献检索初期,当时还不支持全文搜索的时候,关键词就可以作为搜索这篇论文的词语.因此,目前依然可以在论文中看到关键词这一项. ...

  2. python人脸识别源码_Python 抖音机器人,让你找到漂亮小姐姐

    本项目作者沉迷于抖音无法自拔,常常花好几个小时在抖音漂亮小姐姐身上. 本着高效.直接地找到漂亮小姐姐的核心思想,我用 Python + ADB 做了一个 Python 抖音机器人 Douyin-Bot ...

  3. python贪吃蛇源码_Python:游戏:贪吃蛇(附源码)

    Python:游戏:贪吃蛇(附源码) 发布时间:2018-09-05 09:59, 浏览次数:1295 , 标签: Python 贪吃蛇是个非常简单的游戏,适合练手. 首先分析一下这个游戏 1.蛇怎么 ...

  4. python字符串替换源码_Python实现字符串中某个字母的替代功能

    Python实现字符串中某个字母的替代功能 今晚想实现这样一个功能:将输入字符串中的字母 "i" 变成字母 "p".当时想的很简单,直接用for循环遍历,然后替 ...

  5. python关键字提取源码_python实现按关键字筛选日志文件

    最近忙成了狗,五六个项目堆在一起,头疼的是测试还失惊无神的给我丢来一个几十甚至上百M的日志文件,动不动就几十上百万行,就算是搜索也看得头昏眼花的,因此自己花了点时间写了一段小脚本去过滤日志,当然这样的 ...

  6. python量化策略源码_Python量化交易进阶讲堂-创建自定义量化回测框架

    欢迎大家订阅<Python实战-构建基于股票的量化交易系统>小册子,小册子会陆续推出与小册内容相关的专栏文章,对涉及到的知识点进行更全面的扩展介绍,并且会有选择地收录至小册中,更便于广大读 ...

  7. python爬网页源码_python爬虫爬取网页的内容和网页源码不同?

    可以看到这里id为k_total的元素值不同,爬出来是1,网页源码是55. 附还未完成的代码:import requests from bs4 import BeautifulSoup import ...

  8. python爬虫框架源码_python爬虫的基本框架

    1.爬虫的基本流程: 通过requests库的get方法获得网站的url 浏览器打开网页源码分析元素节点 通过BeautifulSoup或者正则表达式提取想要的数据 储存数据到本地磁盘或者数据库 2. ...

  9. python怎么表白源码_Python浪漫表白源码(附带详细教程)-Go语言中文社区

    要知道我们程序猿也是需要浪漫的,小博我之前在网上搜寻了很多代码,确发现好多都不是最新的,所以自己就整理了一下代码,现在与广大博友们分享下 我们需要用到的包 使用pip install +(包名) tu ...

最新文章

  1. matlab canny边缘,matlab – 定向Canny边缘检测
  2. 【转载】关于RabbitMQ的高可用性
  3. 微信公布6月朋友圈十大谣言 包括不打疫苗不让上飞机高铁等
  4. 感觉自己不适合编程 确学了计算机专业,高薪程序员:“我劝他学医,别学计算机,却被冤枉在害人”...
  5. C++面向对象高级编程(上) 第三周笔记 GeekBand
  6. 1分钟学会python,分分钟钟学会Python -基础运算符
  7. stm32的命名及选型介绍
  8. 个人简历网站模板源码
  9. c语言中整形常量表达形式,C语言中整型常量的表示方法.doc
  10. Cipher的初应用
  11. latex 多张子图,横栏/双栏
  12. 2016版连接池和装饰设计模式保存记录
  13. linux使用certbot,如何自动续期~
  14. 五分钟学会前端打包工具webpack
  15. python 线程(1)-- 常用方法与属性,锁,同步
  16. 赵神牛的游戏(过程分析与结果分析)
  17. win10系统桌面计算机图标怎么删除,win10 移除快捷方式的图标怎么操作_win10怎样删除桌面上的快捷方式图标...
  18. SQLServer2016(理论知识点与一些简单的习题汇总)
  19. 机器学习-无监督学习-聚类:聚类方法(二)--- 基于密度的聚类算法【DBSCAN文本聚类算法,密度最大值文本聚类算法】
  20. Oceanus:美团点评HTTP流量定制化路由的实践

热门文章

  1. c libxml2解析html,简单的libxml2 HTML解析示例,使用Objective-c,Xcode和HTMLparser.h
  2. c++冒泡排序代码_数据结构和算法必知必会的50个代码实现
  3. cpanel重启PHP服务_8款基于Web控制面板的服务器管理工具,开源免费,系统管理员利器...
  4. 静态库调用_静态链接和动态链接对比简析
  5. rideo选中 vue_适用于 Vue 的播放器组件Vue-Video-Player操作
  6. android 应用状态,保持应用程序状态在Android上
  7. java 文件读入 数组,将文本文件读入2d数组java
  8. php编写开机启动脚本,设置 msyql php-fpm 开机自动启动脚本
  9. python的shell无法输入_python中shell如何逐行输入?
  10. 博士申请 | 香港浸会大学万人杰教授招收计算机视觉全奖博士生/研究助理