■继续搜索
Continuing the Search
要继续搜索,我们简单地选择在开启列表中具有最小F值的方块。然后,我们用选择方块作以下事情:
To continue the search, we simply choose the lowest F score square from all those that are on the open list. We then do the following with the selected square:
1.把它从开启列表取出,并加入到关闭列表。
1.Drop it from the open list and add it to the closed list.
2.检查所有的相邻方块。忽略那些在关闭列表里或不可行走的(墙,水或其他非法地形),如果它们还不在开启列表中,加方块到开启列表。将选定方块作为新方块的“父”。
2.Check all of the adjacent squares. Ignoring those that are on the closed list or unwalkable (terrain with walls, water, or other illegal terrain), add squares to the open list if they are not on the open list already. Make the selected square the "parent" of the new squares.
3.如果相邻的方块已经在开启列表,查看这条路径到那个方块是否是一个更好的。换句话说,检查,看看方块的G值是否是较低,如果我们使用当前方块到那里。如果没有,什么也不做。
3.If an adjacent square is already on the open list, check to see if this path to that square is a better one. In other words, check to see if the G score for that square is lower if we use the current square to get there. If not, don't do anything.
另一方面,如果新路径的G值较低,改变相邻方格的父到选定方格(图中上方,改变指针的方向指向在所选择的方格)。最后,重新计算那个方块的两个F和G的值。如果这似乎令人困惑,你会看到它所示。
On the other hand, if the G cost of the new path is lower, change the parent of the adjacent square to the selected square (in the diagram above, change the direction of the pointer to point at the selected square). Finally, recalculate both the F and G scores of that square. If this seems confusing, you will see it illustrated below.
好吧,看看这是如何工作的。我们最初的9个方格,在起始方块被放到关闭列表后还有8个在开启列表。其中,具有最低F值的是一个起点方块紧邻右侧的,其F值为40。因此,我们选择这个方块作为我们的下一个方块。如下图所示高亮的蓝色的部分。
Okay, so let's see how this works. Of our initial 9 squares, we have 8 left on the open list after the starting square was switched to the closed list. Of these, the one with the lowest F cost is the one to the immediate right of the starting square, with an F score of 40. So we select this square as our next square. It is highlight in blue in the following illustration.
[图4]
[Figure 4]
首先,从开启列表中删除它,并把它添加到关闭列表(这就是它以高亮蓝色显示的原因)。然后检查相邻的方块。好了,这个方块的相邻右边的是墙上的方块,忽略它。一个眼前的左边是开始方块。这是关闭的名单上,所以也忽略它。
First, we drop it from our open list and add it to our closed list (that's why it's now highlighted in blue). Then we check the adjacent squares. Well, the ones to the immediate right of this square are wall squares, so we ignore those. The one to the immediate left is the starting square. That's on the closed list, so we ignore that, too.
其他四个方格已经在开启列表中,所以我们需要检查如果使用那些方块作为路径是否比使用这个方块到那里更好,将G值作为我们的参照点。来看看选择的这个方块吧。它的G值是14,如果经由当前方块到达那里,G值将等于20(10,现在方块的G值,再加上10来垂直移到它的上面)。G值20比14高,所以这不是一个更好的路径。如果你看一下图能更好的理解这些。从开始方格方块沿对角线移动一个方格到那里更直接些,而不是水平移动一个方块,再垂直移动一个方块。
The other four squares are already on the open list, so we need to check if the paths to those squares are any better using this square to get there, using G scores as our point of reference. Let's look at the square right above our selected square. Its current G score is 14. If we instead went through the current square to get there, the G score would be equal to 20 (10, which is the G score to get to the current square, plus 10 more to go vertically to the one just above it). A G score of 20 is higher than 14, so this is not a better path. That should make sense if you look at the diagram. It's more direct to get to that square from the starting square by simply moving one square diagonally to get there, rather than moving horizontally one square, and then vertically one square.
当对在开启列表中的4个相邻方格重复作这个过程,我们发现没有路径比当前的方块有提高,因此我们不会改变任何东西。所以,现在,我们看了所有的相邻方块,我们都与这个方块作完毕,并准备移动到下一个方块。
When we repeat this process for all 4 of the adjacent squares already on the open list, we find that none of the paths are improved by going through the current square, so we don't change anything. So now that we looked at all of the adjacent squares, we are done with this square, and ready to move to the next square.
那么,通过开启列表,现在减到7个方格的列表,我们选择了一个具有最小F值。有趣的是,在这种情况下,有两个方块的F值是54,那么我们选择哪个?这其实并不重要。为了快速的目的,它可以更快地选择您添加到开启列表中最后一个。这种偏向得到后来发现在搜索时,当你更接近目标时。但它其实并不重要。 (不同的处理造成了两个版本的A *可能找到不同的等长路径。)
So we go through the list of squares on our open list, which is now down to 7 squares, and we pick the one with the lowest F cost. Interestingly, in this case, there are two squares with a score of 54. So which do we choose? It doesn't really matter. For the purposes of speed, it can be faster to choose the last one you added to the open list. This biases the search in favor of squares that get found later on in the search, when you have gotten closer to the target. But it doesn't really matter. (Differing treatment of ties is why two versions of A* may find different paths of equal length.)
因此,让我们选择一个下方,并在开始方块的右边,如下图所示。
So let's choose the one just below, and to the right of the starting square, as is shown in the following illustration.
[图5]
[Figure 5]
这一次,当我们检查相邻的方块,我们发现,一到右边立即是一堵墙方块,所以我们忽略。这同样适用于一个刚刚上面。我们还忽略略低于墙上的方块。为什么呢?因为你不能得到该方块直接从当前方块没有穿越附近的墙角切割。你真的需要往下走,然后再动过那个方块,围绕在这个过程中角落移动。 (注:上偷工减料这条规则是可选的它的使用依赖于你的节点如何放置。)
This time, when we check the adjacent squares we find that the one to the immediate right is a wall square, so we ignore that. The same goes for the one just above that. We also ignore the square just below the wall. Why? Because you can't get to that square directly from the current square without cutting across the corner of the nearby wall. You really need to go down first and then move over to that square, moving around the corner in the process. (Note: This rule on cutting corners is optional. Its use depends on how your nodes are placed.)
那剩下5个方块。另外两个方格低于当前方块尚未在开启列表中,所以我们将它们添加并把当前方块变成他们的父。其他三个平方,有两个已经在关闭列表(开始方块,和一个略高于目前的方块上,在蓝色突出两个图中),所以我们忽略他们。而最后的方块,眼前的当前方块左侧,进行检查,看是否G值低了,如果你去通过当前方块到那里。没有方块了。所以,我们就大功告成了,并准备检查开启列表中的下一个方块。
That leaves five other squares. The other two squares below the current square aren't already on the open list, so we add them and the current square becomes their parent. Of the other three squares, two are already on the closed list (the starting square, and the one just above the current square, both highlighted in blue in the diagram), so we ignore them. And the last square, to the immediate left of the current square, is checked to see if the G score is any lower if you go through the current square to get there. No dice. So we're done and ready to check the next square on our open list.
我们重复这个过程,直到我们添加目标方块关闭列表,此时它看起来像下面的插图。
We repeat this process until we add the target square to the closed list, at which point it looks something like the illustration below.
[图6]
[Figure 6]
需要注意的是从上图低于初始方格的两格的父方格已经改变。之前,它的G值为28,指向回它右上的方块。现在它有一个值20分,并指向它上面方块。这发生在我们的搜索,对G值被检查那里的方式,它竟然采用了要低一些的作为新的路径- 这样的父被切换,G值和F值也重新计算。在这个例子中虽然这种变化似乎并不太重要,有很多可能的情况下,这种持续的检查将会使所有的差异在确定目标的最佳路径。
Note that the parent square for the square two squares below the starting square has changed from the previous illustration. Before it had a G score of 28 and pointed back to the square above it and to the right. Now it has a score of 20 and points to the square just above it. This happened somewhere along the way on our search, where the G score was checked and it turned out to be lower using a new path – so the parent was switched and the G and F scores were recalculated. While this change doesn't seem too important in this example, there are plenty of possible situations where this constant checking will make all the difference in determining the best path to your target.
那么,我们如何确定路径?简单,刚开始在红色的目标方格,并努力从一个方格向后移到它的父,下面的箭头。这最终将带你回到开始方块,这就是你的路径。它应该看起来像下面的插图。移动从开始方块A到目标方块B就是从路径上每一个方块(节点)的中心移动到下一个方块的中心的问题,直到到达目标。
So how do we determine the path? Simple, just start at the red target square, and work backwards moving from one square to its parent, following the arrows. This will eventually take you back to the starting square, and that's your path. It should look like the following illustration. Moving from the starting square A to the destination square B is simply a matter of moving from the center of each square (the node) to the center of the next square on the path, until you reach the target.
[图7]
[Figure 7]
(待续)
来源:oschina
链接:https://my.oschina.net/u/660460/blog/464312