再譯《A *路徑搜尋入門》之二
■路徑評分
Path Scoring
計算出的路徑時,確定要使用的方格的關鍵是下面的公式:
The key to determining which squares to use when figuring out the path is the following equation:
F = G + H
■這裡
where
G =從起點A沿著生成的路徑移動到一個給定的方形網格上執行成本。
G = the movement cost to move from the starting point A to a given square on the grid, following the path generated to get there.
H =從格子中給定方塊移動到最終目的地,B點的估計的執行成本。這通常稱為啟發式,這可有點混亂。因為是一個猜測所以這樣稱呼。在找到路徑之前,我們真的不知道實際的距離,因為各種各樣的事情都在途中(牆,水等)。在本教程中給出一個計算H的方法,但在網絡上你能找到許多計算H方法的其他文章。
H = the estimated movement cost to move from that given square on the grid to the final destination, point B. This is often referred to as the heuristic, which can be a bit confusing. The reason why it is called that is because it is a guess. We really don't know the actual distance until we find the path, because all sorts of things can be in the way (walls, water, etc.). You are given one way to calculate H in this tutorial, but there are many others that you can find in other articles on the web.
通過反覆遍歷開啟列表,選擇具有最小F值的方塊來生成我們的路徑。在本文中這個過程將有進一步更詳細的描述。首先來仔細看看如何計算公式。
Our path is generated by repeatedly going through our open list and choosing the square with the lowest F score. This process will be described in more detail a bit further in the article. First let's look more closely at how we calculate the equation.
如上所述,G是從起始點移動到給定點所生成路徑的運動成本。在這個例子中,我們將指定每個移動水平或垂直方成本為10,對角線移動成本為14。我們使用這些數字是因為斜角移動是2的平方根(不要害怕),是水平或垂直移動的大約1.414倍。我們使用10和14是為了簡單起見。比例大致是正確的,又能避免計算平方根和小數。這不只是因為我們是愚笨的,不喜歡數學。採用這些數字是讓計算機更快,太快了。你很快就會發現,如果你不使用這些捷徑,路徑搜尋可能會很緩慢的。
As described above, G is the movement cost to move from the starting point to the given square using the path generated to get there. In this example, we will assign a cost of 10 to each horizontal or vertical square moved, and a cost of 14 for a diagonal move. We use these numbers because the actual distance to move diagonally is the square root of 2 (don't be scared), or roughly 1.414 times the cost of moving horizontally or vertically. We use 10 and 14 for simplicity's sake. The ratio is about right, and we avoid having to calculate square roots and we avoid decimals. This isn't just because we are dumb and don't like math. Using whole numbers like these is a lot faster for the computer, too. As you will soon find out, pathfinding can be very slow if you don't use short cuts like these.
由於我們計算G值是沿特定的路徑給定的平方,該辦法找出那個方塊的父親的G值,然後加10或14取決於它從父方格是正交(非對角線)還是對角線。需要這種方法將在本實施例進一步上變得明顯一點,因為我們得到一個以上的方塊。
Since we are calculating the G cost along a specific path to a given square, the way to figure out the G cost of that square is to take the G cost of its parent, and then add 10 or 14 depending on whether it is diagonal or orthogonal (non-diagonal) from that parent square. The need for this method will become apparent a little further on in this example, as we get more than one square away from the starting square.
H可以用各種方式估計。我們在這裡使用的方法被稱為曼哈頓方法,在這裡計算從當前方塊到目標方水平和垂直方向移動方塊的總數,忽略對角運動,忽略可能在過程中的任何障礙。然後,我們將總數乘以10,我們的成本水平或垂直移動一格。這是(可能)被稱為曼哈頓方法,因為它像計算城市街區的數量從一個地方到另一個地方,在那裡你不能穿過塊對角。
H can be estimated in a variety of ways. The method we use here is called the Manhattan method, where you calculate the total number of squares moved horizontally and vertically to reach the target square from the current square, ignoring diagonal movement, and ignoring any obstacles that may be in the way. We then multiply the total by 10, our cost for moving one square horizontally or vertically. This is (probably) called the Manhattan method because it is like calculating the number of city blocks from one place to another, where you can't cut across the block diagonally.
閱讀本說明,您可能已經猜到了啟發式僅僅是當前方格與目標之間的剩餘距離的一個“像烏鴉飛似的”粗略的估計。不是這種情況。我們實際上試圖估計沿路徑的剩餘距離(通常是更遠)。越接近我們的估計是實際剩餘距離,就會越快的演算法。如果我們高估了這個距離,但是,它不能保證給我們的最短路徑。在這樣的情況下,我們有所謂的“不可接受啟發式”。
Reading this description, you might guess that the heuristic is merely a rough estimate of the remaining distance between the current square and the target "as the crow flies." This isn't the case. We are actually trying to estimate the remaining distance along the path (which is usually farther). The closer our estimate is to the actual remaining distance, the faster the algorithm will be. If we overestimate this distance, however, it is not guaranteed to give us the shortest path. In such cases, we have what is called an "inadmissible heuristic."
從技術上講,在這個例子中,曼哈頓方法是不可接受的,因為它稍稍高估了剩下的距離。但是我們會用也無妨,因為它是一個更容易理解我們的目的,因為它只是一個輕微的高估。在極少的情況下,得到的路徑不是最短的,這將是近短。想了解更多?你你可以在這裡(http://www.policyalmanac.org/games/heuristics.htm)找到方程和附加說明啟發式。
Technically, in this example, the Manhattan method is inadmissible because it slightly overestimates the remaining distance. But we will use it anyway because it is a lot easier to understand for our purposes, and because it is only a slight overestimation. On the rare occasion when the resulting path is not the shortest possible, it will be nearly as short. Want to know more? You can find equations and additional notes on heuristics here.
F是G加H中的和。搜尋的第一個步驟的結果可以從下面的說明中看出。在F,G和H的分數被寫入在每個方格。正如在緊挨著開始方塊右側的方塊上,F被列印在左上角,G印在左下方,而H顯示在右下方。
F is calculated by adding G and H. The results of the first step in our search can be seen in the illustration below. The F, G, and H scores are written in each square. As is indicated in the square to the immediate right of the starting square, F is printed in the top left, G is printed in the bottom left, and H is printed in the bottom right.
[圖3]
[Figure 3]
所以,讓我們來看看其中的一些方塊。在有字母的方塊上,G = 10,這是因為它是在一個水平方向的距離起始方塊僅一個方格。正方形緊鄰上方,下方,及在起始方格的左邊有10相同的G值,對角線方塊有14個G值。
So let's look at some of these squares. In the square with the letters in it, G = 10. This is because it is just one square from the starting square in a horizontal direction. The squares immediately above, below, and to the left of the starting square all have the same G score of 10. The diagonal squares have G scores of 14.
H值是通過估計到紅色目標方塊的曼哈頓距離,僅移動水平和垂直方向,而忽略牆上那就是在計算方式。使用這種方法,方塊上開始的直接右邊3格是紅格,為30。那麼高於這個方塊的方塊的H值是4格距離(記住,只能水平移動和垂直)的一個H值40.你也許可以看到了為其他方塊計算H值的方法。
The H scores are calculated by estimating the Manhattan distance to the red target square, moving only horizontally and vertically and ignoring the wall that is in the way. Using this method, the square to the immediate right of the start is 3 squares from the red square, for a H score of 30. The square just above this square is 4 squares away (remember, only move horizontally and vertically) for an H score of 40. You can probably see how the H scores are calculated for the other squares.
每個方格的F值,再次,只是通過加G和H一起計算。
The F score for each square, again, is simply calculated by adding G and H together.
(待續)
轉載於:https://my.oschina.net/dubenju/blog/426134