python 實(shí)現(xiàn)A*算法的示例代碼
A*作為最常用的路徑搜索算法,值得我們?nèi)ド羁痰难芯?。路徑?guī)劃項(xiàng)目。先看一下維基百科給的算法解釋:https://en.wikipedia.org/wiki/A*_search_algorithm
A *是最佳優(yōu)先搜索它通過在解決方案的所有可能路徑(目標(biāo))中搜索導(dǎo)致成本最?。ㄐ羞M(jìn)距離最短,時(shí)間最短等)的問題來解決問題。 ),并且在這些路徑中,它首先考慮那些似乎最快速地引導(dǎo)到解決方案的路徑。它是根據(jù)加權(quán)圖制定的:從圖的特定節(jié)點(diǎn)開始,它構(gòu)造從該節(jié)點(diǎn)開始的路徑樹,一次一步地?cái)U(kuò)展路徑,直到其一個(gè)路徑在預(yù)定目標(biāo)節(jié)點(diǎn)處結(jié)束。
在其主循環(huán)的每次迭代中,A *需要確定將其部分路徑中的哪些擴(kuò)展為一個(gè)或多個(gè)更長(zhǎng)的路徑。它是基于成本(總重量)的估計(jì)仍然到達(dá)目標(biāo)節(jié)點(diǎn)。具體而言,A *選擇最小化的路徑
F(N)= G(N)+ H(n)
其中n是路徑上的最后一個(gè)節(jié)點(diǎn),g(n)是從起始節(jié)點(diǎn)到n的路徑的開銷,h(n)是一個(gè)啟發(fā)式,用于估計(jì)從n到目標(biāo)的最便宜路徑的開銷。啟發(fā)式是特定于問題的。為了找到實(shí)際最短路徑的算法,啟發(fā)函數(shù)必須是可接受的,這意味著它永遠(yuǎn)不會(huì)高估實(shí)際成本到達(dá)最近的目標(biāo)節(jié)點(diǎ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課程中路徑規(guī)劃項(xiàng)目,結(jié)合上面的偽代碼,用python 實(shí)現(xiàn)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:
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("已到達(dá)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
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python中基礎(chǔ)數(shù)據(jù)類型 set集合知識(shí)點(diǎn)總結(jié)
在本篇文章里小編給大家總結(jié)了一篇關(guān)于Python中基礎(chǔ)數(shù)據(jù)類型 set集合知識(shí)點(diǎn)總結(jié)內(nèi)容,有需要的朋友們可以學(xué)習(xí)下。2021-08-08
python簡(jiǎn)單線程和協(xié)程學(xué)習(xí)心得(分享)
下面小編就為大家?guī)硪黄猵ython簡(jiǎn)單線程和協(xié)程學(xué)習(xí)心得(分享)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06
python設(shè)計(jì)微型小說網(wǎng)站(基于Django+Bootstrap框架)
這篇文章主要介紹了python設(shè)計(jì)微型小說網(wǎng)站(基于Django+Bootstrap框架),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07
python+JS?實(shí)現(xiàn)逆向?SMZDM?的登錄加密
這篇文章主要介紹了python+JS?實(shí)現(xiàn)逆向?SMZDM?的登錄加密,文章通過利用SMZDM平臺(tái)展開詳細(xì)的內(nèi)容介紹,需要的小伙伴可以參考一下2022-05-05
python實(shí)現(xiàn)一組典型數(shù)據(jù)格式轉(zhuǎn)換
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)一組典型數(shù)據(jù)格式轉(zhuǎn)換,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12
10行Python代碼就能實(shí)現(xiàn)的八種有趣功能詳解
Python憑借其簡(jiǎn)潔的代碼,贏得了許多開發(fā)者的喜愛,因此也就促使了更多開發(fā)者用Python開發(fā)新的模塊。面我們來看看,我們用不超過10行代碼能實(shí)現(xiàn)些什么有趣的功能吧2022-03-03
python機(jī)器學(xué)習(xí)實(shí)現(xiàn)神經(jīng)網(wǎng)絡(luò)示例解析
這篇文章主要為大家介紹了python機(jī)器學(xué)習(xí)python實(shí)現(xiàn)神經(jīng)網(wǎng)絡(luò)的示例解析,在同樣在進(jìn)行python機(jī)器學(xué)習(xí)的同學(xué)可以借鑒參考下,希望能夠有所幫助2021-10-10
python中CURL 和python requests的相互轉(zhuǎn)換實(shí)現(xiàn)
本文主要介紹了python中CURL 和python requests的相互轉(zhuǎn)換實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03

