A*作为最常用的路径搜索算法,值得我们去深刻的研究。路径规划项目。先看一下维基百科给的算法解释:https://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 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][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("已到达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
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。
更新日志
- 【雨果唱片】中国管弦乐《鹿回头》WAV
- APM亚流新世代《一起冒险》[FLAC/分轨][106.77MB]
- 崔健《飞狗》律冻文化[WAV+CUE][1.1G]
- 罗志祥《舞状元 (Explicit)》[320K/MP3][66.77MB]
- 尤雅.1997-幽雅精粹2CD【南方】【WAV+CUE】
- 张惠妹.2007-STAR(引进版)【EMI百代】【WAV+CUE】
- 群星.2008-LOVE情歌集VOL.8【正东】【WAV+CUE】
- 罗志祥《舞状元 (Explicit)》[FLAC/分轨][360.76MB]
- Tank《我不伟大,至少我能改变我。》[320K/MP3][160.41MB]
- Tank《我不伟大,至少我能改变我。》[FLAC/分轨][236.89MB]
- CD圣经推荐-夏韶声《谙2》SACD-ISO
- 钟镇涛-《百分百钟镇涛》首批限量版SACD-ISO
- 群星《继续微笑致敬许冠杰》[低速原抓WAV+CUE]
- 潘秀琼.2003-国语难忘金曲珍藏集【皇星全音】【WAV+CUE】
- 林东松.1997-2039玫瑰事件【宝丽金】【WAV+CUE】