A * path finding algorithm (2011-02-15 10:53:11) reproduced
Tags: games |
Classification: Algorithms |
Overview
Although the person who mastered the A * algorithm thinks it is easy, for beginners, A * algorithm is still very complex.
Search area
Let's say someone moves from point A to Point B, but the two points are separated by a wall. 1, Green is a, red is B, Middle blue is wall.
Figure 1
You should have noticed that we have divided the area to be searched into square squares. This is the first step in finding a way to simplify the search area, just as we do here. This particular method simplifies our search area to 2-dimensional arrays. Each item of an array represents a lattice, and its state is walkalbe and cannot be walked (unwalkable). The path is found by calculating which squares need to be traversed from A to B. Once the path is found, the character moves from the center of the Square to the center of the other square until it reaches the destination.
The center point of the grid we become "node (nodes)". If you've read other articles about A * pathfinding algorithm, you'll find that people are often talking about nodes. Why not just describe it as a square? Because it is possible for us to delimit the search area for other variants rather than squares, such as hexagonal, rectangular, or even arbitrary multiple deformations. The nodes can be placed in any polygon, can be placed in the center of the multi-deformation, can also be placed on the edge of the polygon. We use this system because it is the simplest.
Start searching (starting the search)
Once we have simplified the search area to a set of quantifiable nodes, as we did above, the next step is to find the shortest path. In a *, we start with the starting point, check its adjacent squares, and then expand around until we find the target.
So let's start our path-finding journey:
1. Start from start a, and add it to an open list of squares. This open list is a bit like a shopping list. Of course, there is only one item in the open list, which is the starting point a, and it will slowly add more items later. The grid in the Open list is the path that might pass along the way, or it might not. Basically open list is a list of squares to check.
2. View the squares adjacent to start a (ignoring the squares occupied by the walls, the squares occupied by the rivers, and other squares occupied by the illegal terrain), and add the walkable or reachable (reachable) squares to the open list. Set start A to the father of these squares (parent node or parent square). The content of these parent nodes is important when we are tracking paths. Explained later.
3. Remove A from the open list and add it to the close list, where each square in the list is now no longer in need of attention.
As shown, the dark green Square is the starting point and its outer frame is bright blue, indicating that the square is added to the close list. The black squares adjacent to it need to be checked, and their outer frames are bright green. Each black square has a gray pointer pointing to their parent node, and here is the starting point A.
Figure 2.
Next, we need to select a square from the open list that is adjacent to the start a, repeating the previous steps in more or less the same way as described below. But which squares do you choose? The one with the minimum F value.
Path ordering (path sorting)
The key to calculating the squares that make up the path is the following equation:
F = G + H
Over here
G = The moving cost of moving from start A to the specified square, along the path that is generated when the grid is reached.
H = Estimated cost to move from the specified square to the end point B. This is often referred to as heuristics and is somewhat confusing. Why do you call it that, because it's a guess. Until we find the path we will know the true distance, because there are all kinds of things (such as walls, water, etc.) on the way. This tutorial will teach you a way to calculate H, and you can find other ways to do it online.
Our path is this: Iterate through the Open list and select the square with the lowest F value. This process is described in detail later. Let's start by looking at how to calculate the equation above.
As mentioned above, G is the moving cost of moving from start a to the specified square. In this case, the horizontal and vertical movement cost is 10, and the diagonal movement cost is 14. This data is used because the actual diagonal moving distance is the square root of 2, or is approximately 1.414 times times the cost of horizontal or vertical movement. Using 10 and 14 is just for simplicity. The proportions are right, and we avoid the calculation of open and fractional numbers. It's not that we don't have the ability or we don't like maths. Using these numbers can also make your computer faster. Later, you will find that if you do not use these techniques, the pathfinding algorithm will be slow.
Since we are calculating the G value along the path to the specified square, the method of calculating the G value of the square is to find out the G value of its father and then add 10 or 14 according to whether the father is in a straight or diagonal direction. As we leave the starting point and get more squares, this approach becomes clearer.
There are many ways to estimate H values. Here we use the Manhattan method to calculate the number of squares through which the current grid moves horizontally or vertically to reach the target, ignoring the diagonal movement and multiplying the total by 10. It's called the Manhattan method because it's like counting the number of blocks that have been passed from one location to another, and you can't cross the block diagonally. It is important to calculate that H is to ignore obstructions in the path. This is an estimate of the remaining distance, not the actual value, and is therefore called heuristics.
Add G and H to get F. The result of our first step is as shown. Each square is marked with a value of F, G, H, as in the right square of the starting point, the upper-left corner is F, the lower-left corner is G, and the lower-right corner is H.
Figure 3
OK, now let's look at some of those squares. In a square labeled with letters, G = 10. This is because the horizontal direction from the beginning to the distance there is only one square. Directly above the starting point, below, the left square of the G value is 10, the diagonal square g value is 14.
The H value is obtained by estimating the Manhattan distance from the end point (Red Square), moving only horizontally and vertically, and ignoring the walls along the way. In this way, the square to the right of the starting point has a distance of 3 squares, so H = 30. The squares above this square have a distance of 4 squares to the end point (note that only horizontal and vertical distances are calculated), so H = 40. For other squares, you can use the same method to know how the H value is derived.
The F-value of each square, and again, the G-value and H-value can be added directly.
Continue searching (Continuing the search)
To continue the search, we select the lowest F-value (square) node from the open list, and then do the following for the selected squares:
4. Take it out of the open list and put it in the close list.
5. Check all squares adjacent to it, ignoring the squares in the close list or unwalkable (such as walls, water, or other illegal terrain), and add them to the open list if the squares are not in open lsit.
Set our selected squares to the father of these newly added squares.
6. If an adjacent square is already in the open list, check to see if the path is better, that is, to reach the square with a smaller G value through the current square (our checked squares). If not, do nothing.
Conversely, if the G value is small, then the father of that Square is set to the current square (our checked squares) and then the F and G values of that square are recalculated. If you are still confused, please refer to.
Figure 4
Ok, let's see how it works. of our first 9 squares, there are 8 in the open list, where the starting point is placed in the close list. In these squares, the square to the right of the beginning of the grid has the lowest F value of 40, so we select this grid as the next square to be processed. Its outer frame is lit with a blue line.
First, we move it from the open list to the close list (that's why the Blue Line is lit). Then we examine the squares adjacent to it. The square on the right is the wall, we ignore it. The square on the left is the starting point, and in the close list, we also ignore it. The other 4 adjacent squares are in the open list, and we need to check to see if the path through the square is better and use the G value to determine it. Let's look at the squares above. It now has a G value of 14. If we get there through the current square, the G value will be 20 (where 10 is the G value to reach the current square, plus the G-value 10 that moves from the current square to the upper square). Obviously 20:14 large, so this is not the optimal path. If you look at the picture, you will understand. It is better to move diagonally from the starting point to the square than to move it vertically.
After checking the 4 adjacent squares already in the open list, we did not find a better path through the current grid, so we did not make any changes. Now that we have checked all the adjacent squares of the current square and handled them, it is time to select the next box to be processed.
So traversing our open list again, now it's only 7 squares, and we need to choose the one with the lowest F value. Interestingly, this time there are two squares of the F value of 54, which to choose? It doesn't matter. In terms of speed, it is faster to choose the last to add the open list to the grid. This leads to preference for the newly found squares when approaching the target in the pathfinding process. But it doesn't matter. (Different treatment of the same data, resulting in the two version of A * found a different path of equal length).
We select the square at the bottom right of the starting point as shown in.
Figure 5
This time, when we check the adjacent squares, we find that the square on the right is the wall, ignoring it. The same is true of the above.
We ignored a lattice below the wall. Why? Because if you don't cross the corner, you can't move directly from the current square to that square. You need to go down first and then move to that square to get around the corner. (Note: The rules that pass through a corner are optional and depend on how your nodes are placed)
So there are 5 adjacent squares left. The 2 squares below the current grid have not been added to the open list, so add them and set the current grid as their father. of the remaining 3 squares, 2 are already in the close list (one is the starting point, one is the square above the current square, the outer frame is highlighted), and we ignore them. The last square, the square to the left of the current square, checks to see if it has a smaller G value through the current square. No. So we're going to select the next pending box from the open list.
Repeat this process until you add the end point to the open list, as shown in.
Figure 6
Note that the father of the 2 squares below the starting point is already different from the front. Its G value is 28 and points to the upper right square. It now has a G value of 20 and points to the square directly above it. This occurs somewhere in the pathfinding process, where the G value is checked and becomes lower when the new path is used, so the parent node is reset and the G and F values are recalculated. Although this change is not important in this case, in many cases, this change can lead to great changes in the pathfinding results.
So how do we determine the actual path? It's simple, starting at the end, moving the arrow toward the parent node, so you're taken back to the beginning, and that's your path. As shown in. Moving from start A to end B is simply moving from the center of one square on the path to the center of another square until the target. It's so simple!
Figure 7
A * algorithm summary (Summary of the * method)
Ok, now that you have read the whole introduction, now we put all the steps together:
1. Add the starting point to the open list.
2. Repeat the following procedure:
A. Traverse the open list to find the node with the lowest F value, and use it as the node that is currently being processed.
B. Move the node to the close list.
C. For each square of the 8 adjacent squares of the current square?
If it is not reachable or it is in the close list, ignore it. Otherwise, do the following.
If it is not in the open list, add it to the open list and set the current square as its father, recording the square's F, G, and H values.
If it is already in the open list, it is better to check the path (that is, to reach it through the current square) and use the G value for reference. A smaller G-value indicates that this is a better path. If so, set its father to the current square and recalculate its G and F values. If your open list is sorted by the F value, you may need to reorder it after the change.
D. Stop, when you
The end is added to the open list, where the path has been found, or
The find endpoint failed, and the open list is empty and there is no path at this time.
3. Save the path. From the end point, each square moves along the parent node until it starts, and that's your path.
Heuristic search for A * algorithm (2011-03-07 11:14:32) reproduced
Tags: games |
Classification: Algorithms |
According to Drew, the shortest path algorithm is now an important application of computer network routing algorithms, Robot Pathfinder, traffic route navigation, artificial intelligence, game design and so on. The d* (D Star) algorithm is the core pathfinding algorithm of the American Mars probe.
The shortest path is calculated by calculating the minimum short-circuit calculation and dynamic short-circuit calculation.
The static path shortest path algorithm is the same as the external environment, and calculates the shortest path. There are mainly Dijkstra algorithms, A * (A Star) algorithm.
The shortest path of dynamic paths is the continuous change of the external environment, that is, the shortest path can not be calculated with the prediction. In the event of a game in which an enemy or an obstacle is constantly moving. There is a typical d* algorithm.
This is the Drew program implementation of the 10,000 nodes of the random road network three non-intersecting shortest path
Real road network calculation K-path Example: node 5696 to node 3006, three fastest way, you can see the path is basically a loop or trunk road. The black line is the first, the blue one is the second, the Red line is the third. The constraint factor is 1.2. Share part of the road. The Display calculation section is completely completed by the drew self-developed program.
See K-Route algorithm test procedure
Dijkstra algorithm to find the shortest path:
The Dijkstra algorithm is a typical shortest path algorithm used to calculate a node to all other nodes. The main feature is to extend from the center of the starting point to the outer layer until it expands to the end point. The Dijkstra algorithm can get the optimal solution of the shortest path, but it is inefficient because it traverses many nodes.
Dijkstra algorithm is a very representative of the shortest path algorithm, in many professional courses as the basic content of the detailed introduction, such as data structure, graph theory, operations research and so on.
Dijkstra general expressions usually have two ways, one with permanent and temporary marking, one is open, close table, drew in order to and the following to introduce a * algorithm and d* algorithm expression consistent, here are used Open,close table way.
Approximate process:
Create two tables, OPEN, CLOSE.
The Open table holds all the nodes that have been generated without being inspected, and the nodes that have been visited are recorded in the closed table.
1. Visit the point in the network where the starting point is most recent and have not been checked, put this point in the open Group and wait for the check.
2. Find the point closest to the starting point from the Open table, find all the sub-nodes of the point, and place the point in the close table.
3. Traverse the sub-nodes that examine the point. The distance value of these child nodes from the starting point is calculated, and the child nodes are placed in the open table.
4. Repeat 2, 3, step. Until the open table is empty, or the target point is found.
This is the drew program in the 4,000-node random road network on the Dijkstra algorithm to search for the shortest-circuiting demonstration, the black circle indicates that the calculated point is traversed by the graph can see the Dijkstra algorithm from the starting point to the surrounding layer to calculate the expansion, after the calculation of a large number of nodes to reach the target point. So the speed is slow and inefficient.
There are many methods to improve the speed of Dijkstra search, according to Drew, there are data structures used in binary heap method, and the method of searching from the starting point and the endpoint by Dijkstra.
Recommended Pages: http://www.cs.ecnu.edu.cn/assist/js04/ZJS045/ZJS04505/zjs045050a.htm
Concise introduction of Dijkstra algorithm, graphical display and source code download.
A * (A Star) algorithm: heuristic (heuristic) algorithm
A * (A-star) algorithm is the most effective method for solving the shortest circuit in a static road network.
The formula is expressed as: F (n) =g (n) +h (n),
where f (n) is the valued function of node n from the initial point to the target point,
g (n) is the actual cost in the state space from the initial node to the N node,
H (N) is the estimated cost of the best path from N to the target node.
To ensure the shortest path (optimal solution) condition is found, the key is to select the value function h (n):
Estimated value h (n) <= N to the target node's distance actual value, in this case, the search for more points, large search range, low efficiency. But the optimal solution can be obtained.
If the value > actual value of the search, the number of points, search scope is small, high efficiency, but can not guarantee the optimal solution.
The closer the estimate is to the actual value, the better the valuation function gets.
For example, for a geometric road network, the Euclidean distance (straight distance) between two nodes can be evaluated, i.e. F=G (n) +sqrt ((DX-NX) * (DX-NX) + (dy-ny) * (Dy-ny)), so that the value function f is in the case of G, will be more or less under the constraints of the value of H, the node is close to the target point, the H value is small, the F value is relatively small, to ensure that the shortest path to the end of the search direction. It is obviously better than the Dijstra algorithm to search around without direction.
Conditions of heuristic
Optimistic (must is less than or equal to the real cost)
As close to the real cost as possible
Main search Process:
Create two tables, the Open table holds all the nodes that have been generated without being inspected, and the nodes that have been visited are recorded in the closed table.
Traverse the nodes of the current node, place the N node in close, take the N node's child node x,-> calculate the value of X
while (Open!=null)
{
From the Open table, the minimum node n of the value F is evaluated.
if (n node = = target node) break;
Else
{
if (X in OPEN) compare two X value f//Note is the value of two different paths of the same node
if (the value of x is less than the valuation value of the Open table)
Update the valuation values in the open table; Take the value of the least-path estimate
if (x in CLOSE) compares the value of two X//note is the valuation value of two different paths of the same node
if (the value of x is less than the valuation value of the close table)
Update the valuation values in the close table; Put the X-node into the open//take the minimum path of the valuation value
if (X not in both)
Evaluate the value of x;
and insert X into the Open table; Not yet sorted
}
Insert the N node into the close table;
Sort the nodes in the open table according to the estimate value; is actually comparing the size of the node F in the Open table, from the node with the smallest path down.
}
is with the above Dijkstra algorithm using the same road network, the same starting point, with a * algorithm, the number of points calculated from the starting point to the target point of the direction of expansion, calculated nodes significantly less than Dijkstra, high efficiency, and can get the best solution.
The difference between a * algorithm and Dijistra algorithm is that there is no estimate value, and the Dijistra algorithm is equivalent to the value of 0 in a * algorithm.
Recommended article Links:
Amit Stanford University a PhD's game website, which has an introduction to a * algorithm and a number of valuable links http://theory.stanford.edu/~amitp/GameProgramming/
wrote two very good introduction to the heuristic and a * algorithm of Chinese articles and a * source download:
Initial knowledge of A * algorithm http://creativesoft.home.shangdu.net/AStart1.htm
In-depth * algorithm http://creativesoft.home.shangdu.net/AStart2.htm
Note that the above article "in-depth A * algorithm" refers to A * game program to explain, and has the source of the download, but it has a bug, is that the new child nodes into the open table is sorted, When the child nodes in the open table and the closed table, after recalculating the valuation value, no re-ordering of the nodes in the Open table, this problem will lead to the calculation sometimes not the optimal solution, in addition, in the network weight disparity is large, the search scope not only more than Dijkstra, even search all the road network, greatly reduce the efficiency.
Drew to this problem as follows, when the child nodes in the open table and the closed table, after recalculating the valuation value, delete the old node in the Open table, the new value of the node inserted into the Open table, re-order, the test effect is good, the modified code is as follows, The red section is the code added for Drew. Add the appropriate part of the program.
In the function generatesucc ()
...................................
g=bestnode->g+1;
Tilenums=tilenum ((int) x, (int) y);
if ((Old=checkopen (tilenums)) = NULL)
{
for (c=0;c<8;c++)
if (bestnode->child[c] = = NULL)
Break
bestnode->child[c]=old;
if (g < old->g)
{
old->parent=bestnode;
old->g=g;
old->f=g+old->h;
Drew Add the following red code to this place
Implement by Drew
NODE *q,*p=open->nextnode, *temp=open->nextnode;
while (p!=null && p->nodenum! = old->nodenum)
{
Q=p;
p=p->nextnode;
}
if (P->nodenum = = Old->nodenum)
{
if (P==open->nextnode)
{
temp = temp->nextnode;
OPEN->nextnode = temp;
}
Else
Q->nextnode = p->nextnode;
}
Insert (old); Insert successor on OPEN list WRT f
}
......................................................
Another * (a Star) algorithm:
This algorithm can not directly use the evaluation value, directly using the Dijkstra algorithm program to implement a * algorithm, drew it tested to achieve the same as a * *, and very simple calculation.
Taking adjacency Matrix as an example, change the original adjacency matrix I row J column element dij to Dij+djq-diq; Start point to target point direction i->j, end Q. Dij (weight or distance of section I to J)
Where: The function of Djq,diq is equivalent to the value djq= (linear distance from J to Q); diq= (straight distance from I to Q)
Principle: I to Q direction conforms to DIJ+DJQ > Diq, take Dij+djq-diq small, if is opposite direction Dij+djq-diq will be very big. Therefore, the function of seeking the road towards the target direction is achieved.
Dynamic network, Shortest path algorithm d*
A * is very effective in a static road network (very efficient for static worlds), but is not suitable for dynamic networks, such as weights and other dynamic environments that are constantly changing.
D* is a dynamic A * (d-star,dynamic A Star) card and Mellon Robotics Center Stentz in two articles in 1994 and 1995, mainly for robotic pathfinder. is the pathfinding algorithm used by the Mars rover.
Optimal and efficient Path planning for partially-known environments
The focussed d* algorithm for real-time replanning
The main methods (these are completely drew in reading the above information and coding procedures of personal understanding, not guaranteed to be completely correct, for reference only):
1. First use the Dijstra algorithm to search from the target node G to the starting node. Stores the shortest path from the target point in the network to each node and the actual value of that position to the target point H,k (k is the smallest value of all changes H, currently k=h. Each node contains the shortest path information from the previous node to the target point 1 (2), 2 (5), 5 (4), 4 (7). The shortest path from 1 to 4 is 1-2-5-4.
The node information in the original open and close is saved.
2. The robot begins to move along the shortest path, no calculation is required when the next node of the move is not changed, and the least-shorted information computed from the previous step is traced back from the starting point, and when the next node X state is detected at the Y-point, the Dijstra changes, such as blockage. The robot first adjusts itself to the actual value H (Y) of the current position Y to the target point G, and H (Y) =x to the original actual value of the new weight c (x, y) of the Y +x of H (x.). X is the next node (to the target point direction y->x->g) and Y is the current point. The k value takes the minimum before and after the H value changes.
3. Calculate with a * or other algorithm, here is assumed to use a A * algorithm, traversing the child nodes of Y, point into close, adjust Y of child Node A's H value, H (a) =h (Y) +y to child node A of the weight C (y,a), compare whether a point exists in open and close, the method is as follows:
while ()
{
Take the node y with the lowest k value from the Open table;
Traverse Y Sub-node A to calculate the H value of a (a) =h (Y) +y to the weight of child node A C (y,a)
{
If (A in OPEN) compares the H values of two A
If (A's H value is less than the H value of open Table a)
{
Update the H value of a in the open table, and the minimum h value for the K value
There is no affected shortest path existence
Break
}
If (A in CLOSE) compares the H value of two A//note is the value of two different paths of the same node
if (the H value of a is less than the H value of the close table)
{
Update the H value of a in the close table; The k value takes the minimum h value, and the A node is placed in the open table
There is no affected shortest path existence
Break
}
If (a not in both)
Insert a into the open table; Not yet sorted
}
Put Y to the close table;
The Open table compares the size of the K value to sort;
}
The shortest path from point A to the target point is carried out by the robot using the first step Dijstra.
The d* algorithm is very effective in the dynamic environment, and in moving to the target point, only check the changes of the upper and lower nodes of the shortest path or adjacent nodes, such as the robot pathfinding. For changes that occur on the shortest path far away, the feeling is not applicable.
Is the drew on the 4,000-node random road network to do the analysis, the thin black line for the first calculation of the shortest, red dot part of the path changes in the blockage point, when the robot is located at 982 points, detects the previous road block, at which the new information to calculate the path, You can see that the circle point is a recalculation of the traversed points, and only a few points are calculated to find the shortest path, indicating that the calculation is very effective and fast. The Green Line is the new shortest path to the calculated bypass blockage section.
Heuristic search for A * algorithm