A * path finding algorithm

Source: Internet
Author: User

Before reading this article, we will introduce a few theoretical knowledge to help you understand a * algorithm.

heuristic Search: Heuristic search is the search in the state space to evaluate the location of each search, get the best position, and then search from this location until the target. This can omit a large number of unnecessary search paths, referring to efficiency. In heuristic search, it is very important to estimate the location. Different valuations can be used to have different effects.

Valuation Function : The estimated cost of moving from the current node to the target node; this estimate is heuristic. In the problem of pathfinding and maze, we usually use the Manhattan (Manhattan) Valuation function (described below) to estimate the cost.

A * Algorithms and BFS: it can be said that BFS is a special case of a * algorithm. For a BFS algorithm, each node that is extended from the current node (if it has not been accessed) is put into the queue for further expansion. That is, the BFS estimate function h is always equal to 0, there is no heuristic information, you can think of BFS is the "worst" a * algorithm .

Choose the minimum estimate: If you learn the data structure, you should be able to know, for each time to select the minimum value of the node, you should use the minimum priority queue (also known as the minimum binary heap). In C + + STL, there is a ready-made data structure priority_queue, which can be used directly. Of course, don't forget to reload the comparison operator for the custom node.

A * features of the algorithm: A * algorithm is theoretically time-optimal, but there are drawbacks: its spatial growth is exponential .

ida* algorithm: This algorithm is called iterative deepening A * algorithm, it can effectively solve the problem of a * space growth, even without the priority queue.

(1) Search area:

Suppose someone wants to move from point A to point B of separated, such as Green is the beginning a, the red is the end B, the Blue Square is the middle wall:

A simple map, where the green block is the starting point (denoted by A), the middle blue is an obstacle, the red block (denoted by B) is the destination. In order to use a two-dimensional array to represent the map, we divide the map into small squares.

Two-dimensional arrays are used in games, for example, the basic principle of snake and Tetris is to move a block. The map of large-scale games is to spread the various "landforms" on such a small square.

(2) Start search

1, start with point A and put it as a pending point into an "open list". Opening a list is like a shopping list. Although there is only one element in the list, there will be more in the future. Your path may pass through the squares it contains, or it may not. Basically, this is a list of squares to check.

2, look for all reachable or accessible squares around the starting point, skipping walls, water, or other squares that cannot pass through the terrain. Also add them to the open list. Save point A as a "parent square" for all of these squares. When we want to describe the path, the data of the parent square is very important. The specific purpose of this will be explained later.

3, remove point A from the open list, add it to a "close list", and save all squares in the list that do not need to be checked again. At this point, you should form the structure. In the picture, the dark green squares are the center of your starting square. It is stroked with a light blue color to indicate that it is added to the closed list. All adjacent cells are now in the open list, and they are stroked with a light green color. Each square has a gray pointer that refers to their parent square, which is the starting square.

(3) Path scoring

From the "open list" to find the relatively most reliable box, what is the most reliable? They are calculated by formula F=g+h.

F = g + H    # G indicates the movement cost (which can be moved diagonally) from start A to the specified squares on the grid.    # h represents the estimated cost of moving from the specified square to the end B (H has many calculations, here we can only move up and down, notice that the upper and lower left and right movement is only used when calculating H, when the path is moved, it can have 8 directions ).

We assume that the cost of moving a lattice sideways is 10, and for ease of calculation, moving a lattice in a diagonal direction is 14. In order to show more intuitively how to operate the FGH, the upper-left corner of the block in the figure represents F, the lower-left corner represents G, and the lower-right corner represents H. For example:

(4) Continue search

From the "Open list", select the lowest F-value square C (the square to the right of the Green start square), and then treat it as follows (reference):

4. Remove it from the "open list" and place it in the "Close list".

5. Check that it is all adjacent and reachable (obstructions and "closed list" squares are not considered, in this case, also do not consider crossing corners of the box). If these squares are not in the "open list", add them to the "open list", calculate the G, H, and F values of the squares, and set their "parent square" to "C".

6. If an adjacent square D is already in the "Open list", check if the new path (that is, through the path of C) to reach it, the G value will be lower, if the new G is low, then the "Parent square" to the currently selected grid C, and then recalculate its F value and The G value (h value does not need to be recalculated, because the H value is constant for each block). If the new G-value is higher, then it is not a wise choice to go through C and then D, because it takes a long way, and we do nothing.

For example, if we select C because it has the smallest F value, we remove it from the "open list" and add it to the "close list". It's right up and down three walls, so don't think about them. It is the starting block on the left and has been added to the "close list" and is not considered. So there are only 4 candidate blocks around it. Let's take a look at the grid below C, where the current G is 14, and if you reach it by C, G will be 10 + 10, which is bigger than 14, so we do nothing.

Then we continue to find the lowest F value from the "open list", but we find that the top and bottom of C are 54 at the same time. At this point you can take whichever, for example, we chose the Block D under C.

D right side of the upper right is the wall, so do not consider, but why the lower right corner is not added to the "open list" it? Because you can't go straight to that grid without crossing the corner. You really need to go down and get to that one and step through that corner. (Note: Rules that traverse corners are optional and depend on how your nodes are placed.) The example in our diagram does not allow this to go.

In this way, we find the lowest F value from the open list, remove it from the open list and add it to the close list. Then continue to find the blocks around it that can be reached, so loop down ...

So when does it stop? -- When we find the destination end block in the "Start list", the path has been found . such as:

(5) Return path

The search is complete. Now, how do we determine this path? Very simple, start with the red target, and move in the direction of the arrow toward the parent node. This will eventually lead you back to the start grid, which is your path! It should look like that in the picture. Moving from start cell A to target B is simply a simple move from the midpoint of each lattice (node) to the next, until you reach the target point. It's so simple. Such as:

Note: The parent node of the 2nd lattice (the lower left corner of the D lattice) below the starting grid is already different from the previous one. Its G -value was 88 and pointed to the upper-right lattice (D-lattice). Now its G value is 80, pointing to the lattice above it (the lattice to the left of the D lattice). This occurs somewhere in the pathfinding process, when the new path is applied, theg value is checked for low, and the parent node is re-specified, and theG and F values are recalculated.

(6) The entire search process

1 , add the start grid to the open list.

2 , repeat the following work:

a) look for the grid with the lowest F value in the Open list. We call it the current lattice.

b) switch it to the off list.

c) to each of the adjacent lattices?

* if it does not pass or is already in the close list, skip it. The reverse is as follows.

* if it is not in the open list, add it. Use the current grid as the parent node for this grid. Record the f,g, and H values for this grid.

* if it is already in the open list, it is better to check the new path with the G value as a reference. A lower G -value means a better path. If so, change the parent node of the grid to the current grid and recalculate the G and F values of the grid. If you keep your open list sorted by F -value, you may need to re-order the open list after the change.

d) stop, when you

* The target is added to the close list ( note ), when the path is found, or

* no target is found, the open list is empty. At this time, the path does not exist.

3. Save the path. Starting from the target, move along the parent node of each cell until you return to the start grid. This is your path.

(7) turn off list adjustment .

See

Http://www.360doc.com/content/14/0126/16/10724725_348131477.shtml

Http://www.cppblog.com/mythit/archive/2009/04/19/80492.aspx

Http://www.cnblogs.com/technology/archive/2011/05/26/2058842.html

A * path finding algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.