国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python 實現A*算法的示例代碼

2020-02-15 22:43:24
字體:
來源:轉載
供稿:網友

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 failurefunction reconstruct_path(cameFrom, current)  total_path := {current}  while current in cameFrom.Keys:    current := cameFrom[current]    total_path.append(current)  return total_path            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 枝江市| 乌鲁木齐市| 鹤峰县| 大庆市| 区。| 大宁县| 中山市| 乌苏市| 乐至县| 洛南县| 余庆县| 全椒县| 方城县| 齐齐哈尔市| 绵阳市| 晋江市| 新余市| 深州市| 宣武区| 云浮市| 平罗县| 时尚| 温州市| 昂仁县| 涟水县| 双流县| 望都县| 沙洋县| 武宣县| 咸阳市| 柯坪县| 花莲市| 正定县| 宜章县| 大埔县| 黎川县| 奎屯市| 沂水县| 敦煌市| 河南省| 惠来县|