A*算法

http://www.redblobgames.com/pathfinding/a-star/introduction.html

 

Breadth First search

frontier

1. pick and reomove a location from the frontier

2. mark the location as visited so that we know not to process it again

3. Expand it by looking at its neighbors,any neighbors we haven’t seen yet we add to the frontier

 

 
frontier = queue()
frontier.put(start)
visited = {}
visited[start] = true
while nor frontier.empty():
    current = frontier.get()
       for next in graph.neighbors(current):
             if next not in visited:
                   frontier.put(next)
                       visited[next] = true

this article, show how it’s used for tower defence,

 

用來尋路,在循環中,記錄足跡, 把 visited 改爲 came_from

frontier = queue()
frontier.put(start)
came_from= {}
came_from[start] = None
while nor frontier.empty():
    current = frontier.get()
       for next in graph.neighbors(current):
             if next not in came_from:
                   frontier.put(next)
                       came_from[next] = current

這樣 came_from 指向前面一步的足跡,可以根據這點重建路勁

current = goal
path = [current]
while current != start
    current = came_from[current]
   path.append(current)
path.reverse()

現實中,我們並不需要所有的方向路徑,只要記住某個方向的路勁即可。

只要找到了goal,即可停止

frontier = Queue()
frontier.put(start)
came_from = {}
came_form[start] = None
while not frontier.empty():
   current = frontier.get()
       if current == goal
             break
       for next in graph.neghbors(current):
                  if next not in came_from
                         frontier.put(next)
                           came_from[next] = current

Movements costs

Dijkstra 算法, cost’_so_far,  記錄開始以來的cost, 放進frontier,消耗較少的路勁

frontier = priorityQueue()
frontier.put(start, 0)
came_from = {}
cost_so_far = {}
came_from[start] = None
cost_so_far[start] = 0
 
while not frontier.empty():
       current = frontier.get()
 
       if current == goal
           break;
      for next in graph.neighbors(current)
        new_cost  = cost_so_far[current] + graph.cost(current, next)
         if next not in cost_so_far or new_cost<cost_so_far[next]>
                 cost_so_far[next] =  new_cost
                   prioity  = new_cost
                   frontier.put(next, priority)
                    came_from[next] = current


啓發式搜索  Heuristic search

frontier 向目標地方擴展,首先需要定義個啓發函數告訴我們離目標有多近

def heuristic(a,b)

     # Manhattan distance on a square grid

      return abs(a.x – b.x) + abs(a.y – b.y)

在Dijkstra中,我們使用實際的距離來作爲priority隊列的順序。此時,在Greedy best first search

我們使用估算的離目標距離作爲順序。離目標最近的地方會首先被訪問。

frontier = priorityQueue()
frontier.put(start, 0)
came_from = {}
came_from[start] = None
 
while not frontier.empty()
   current = frontier.get()
    if current == goal;
        break;
    for next in graph.neighbors(current)
       if next  not in came_from
               priority = heuristic(goal, next)
               frontier.put(next, priority)
               came_from[next] = current


A* 算法

Dijkstra 算法在計算最短路徑是個好算法,可是花費太多時間在不需要的方向搜索上面。

Greedy First Search 搜索了希望的方向,可是可能並不是找到的是最短的路徑。A*算法即是使用

兩種距離,一個是當前距離出發點的距離,一個是當前距離目標的距離。

Dijsktra算法從中心向目標climb,需要周圍360 角度搜索 ; 而 Greedy Best First Search 算法,我們

從目標出發。

image

A*算法,綜合了兩種方法,如下圖中所示,內部的部分擁有最短的路徑。A*從內部區域開始搜索,

frontier = PriorityQueue()
frontier.put(start, 0 )
came_from = {}
cost_so_far = {}
came_from[start] = None
cost_so_far[start] = 0
 
while nor frontier.empty()
  current = frontier.get()
 
   if current == goal
      break
    for next in graph. neighbors(current)
     new_cost = cost_so_far[current] + graph.cost(current, next)
     if next not in cost_so_far  or new_cost <cost_s0_far[next];
                cost_so_far[next] = new_cost
               priority = new_cost + heuristic(goal, next)
               frontier.put(next, priority)
               came_from[next] = current

You can see that A* tries to stay within the same estimated path length (which we use as the priority for the queue) as much as possible. Why does it run faster than Dijkstra’s Algorithm? Both algorithms explore the same set of locations. However, the heuristic function lets us visit the locations in a different order that makes it more likely that the goal node will be encountered sooner.

發佈了95 篇原創文章 · 獲贊 14 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章