Principle of A * algorithm < turn >

Source: Internet
Author: User

The first part: A * algorithm introduction
The original intention of writing this article is to be a netizen's request, of course, I also found that the Chinese site about artificial intelligence is too little, I am here to stimulate, I hope we all come to enthusiastic participation.
Or the point, I first take a * algorithm operation, is because a * in the game has its very typical usage, is the artificial intelligence in the game representative.
A * algorithm in artificial intelligence is a typical heuristic search algorithm, in order to clarify a * algorithm, I would say first of all, what is the heuristic algorithm.

First, what is the heuristic search algorithm:

Mention the state space search before saying it. State space Search, if by professional point of view is the problem solving process as a result from the initial state to the target State to find the path of the process. Popular point, is to solve a problem, find a solution to the process can be from the beginning to the problem of the result (as if not popular OH). Because there are many branches in the process of solving the problem, it is the uncertainty of the solution condition in the process of solving, the incompleteness, which makes the path of solving a lot of this constitutes a graph, we say this graph is the state space. The solution to the problem is actually finding a path from the beginning to the result in this diagram. The process of searching for this is state space search.

The common State space search has depth first and breadth first. Breadth first is a layer down from the initial state, until a target is found. The depth priority is to find a branch before a certain order, and then find another branch to find the target. Both of these algorithms are described in the data structure book, which can be explained in more detail in these books.

The breadth and depth of the previous search has a big drawback is that they are all in a given state space in the poor lift. This is an appropriate algorithm in the case of small state space, but it is not available when the state space is very large and unpredictable. His efficiency is too low, not even complete. We're going to use heuristic search here.

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 intrepid search paths and mention efficiency. In heuristic search, it is very important to estimate the location. Different valuations can be used to have different effects. Let's start by looking at how the valuation is expressed.

The estimate in the heuristic is expressed in a valuation function, such as:

F (n) = g (n) + H (n)

where f (n) is the value function of node n, and the actual cost of G (n) from the initial node to the N node in the state space, H (n) is the estimated cost of the best path from N to the target node. Here the main is H (N) embodies the heuristic information of the search, because g (n) is known. If detailed, g (n) represents the preferred trend for the breadth of the search. However, when H (n) >> g (n), it is possible to omit G (n) and improve efficiency. These are deep, do not understand also does not affect! Let's continue to look at what a * algorithm is.

Second, the first knowledge of a * algorithm:

Heuristic search actually has a lot of algorithms, such as: Local Search method, the best priority search method and so on. Of course A * is. These algorithms use heuristic functions, but they differ in the specific selection of the best search nodes. Like partial search method, it is in the process of the search for "best node" discard the other sibling nodes, Father node, and has to search down. The result of this search is obvious, because discarding other nodes, may also discard the best nodes, because the best node to solve is only at this stage of the best and not necessarily the best overall. The best priority is much smarter, when he searches, he did not abandon the node (unless the node is a dead node), in each step of the valuation of the current node and the previous node compared to the value of a "best node." This will effectively prevent the loss of the "Best node". What kind of algorithm is a * algorithm? In fact, A * algorithm is also a best priority algorithm. Just add some restraints. Because in solving some problems, we want to be able to solve the shortest path of state space search, that is to solve the problem with the quickest method, a * is to do this kind of thing! We define it first, and if a valuation function can find the shortest path, we call it admissibility. A * algorithm is one of the best preferred algorithms to adopt. The value function of a * algorithm is expressed as:

F ' (n) = G ' (n) + h ' (n)

Here, F ' (n) is the valuation function, and G ' (n) is the shortest path value from the starting point to the end point, and H ' (n) is the heuristic value of N to the target's most open circuit. Since this f ' (n) is actually not known in advance, we use the previous estimate function f (n) to approximate it. g (n) instead of G ' (n), but G (n) >=g ' (n) can (in most cases, be satisfied, without consideration), h (n) instead of H ' (n), but H (n) <=h ' (n) (this is particularly important). It can be proved that the application of such a valuation function can find the shortest path, that is, can be adopted. We say that the best first algorithm to use this valuation function is a * algorithm. Ha! Do you understand me? I'm sure I don't understand! Keep looking!

For example, the breadth-first algorithm is a special case of a * algorithm. where g (n) is the number of layers where the node is located, h (n) = 0, this h (n) is definitely less than H ' (n), so the above-mentioned known breadth-first algorithm is a possible adoption. Actually, too. Of course it is one of the most smelly * algorithms.

Another problem is the informational nature of the H (N) heuristic function. The informational popular point of H (N) is actually the constraint when estimating the value of a node, the more information or the more constraints, the more nodes are excluded, the better the valuation function or the better the algorithm. That's why the breadth-first algorithm is so smelly, and who calls it h (n) = 0, none of the heuristic information. But in game development because of the real-time problem, the more information of H (n), the more it calculates, the more time is spent. It should be appropriate to reduce the information of H (n), that is, to reduce the constraint conditions. But the accuracy of the algorithm is poor, here there is a balance problem. It's hard, it's up to you!

All right, I said the same thing, I think you must be a fog, in fact, this is written to understand A * algorithm of the comrades to see. Ha ha! You should look for an artificial intelligence book carefully! My hundreds of words are not enough to be clear about a * algorithm. Just play a role in the hope that everyone enthusiastic participation!

For an application that predicts a * algorithm, see the in-depth * algorithm.

Part II: In-depth A * algorithm ——— an analysis of the application of a * algorithm in search Shortest path

First, preface

Here I will discuss the actual application of a * algorithm, and give an example of a * algorithm in the shortest path search. It is important to note that the basic concept of a * is not introduced here, if you are not clear about a * algorithm, see the sister article, "The first knowledge of a * algorithm."

The example here is to refer to a source program in the Amit home page, which you can download on the Amit site or on my site. When you use this source program, you should abide by certain conventions.

Two, A * algorithm programming principle

As I said in the first A * algorithm, A * algorithm is one of the best priority algorithms. There are only constraints. Let's start by looking at how the best priority algorithm is written.

Like the state space below: (The starting position is a, the target position is P, the number after the letter indicates the value of the node)

Two tables are set during the search: open and closed. 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. One step in the algorithm is to rearrange the open table based on the valuation function. Each step in this loop considers only the best nodes in the Open table. The specific search process is as follows:

1) Initial state:
Open=[a5];closed=[];
2) estimate the A5, obtain the child nodes, and put into the open table;
OPEN=[B4,C4,D6];CLOSED=[A5]
3) estimate the B4, obtain the child nodes, and put into the open table;
OPEN=[C4,E5,F5,D6];CLOSED=[B4,A5]
4) Estimating C4; Obtaining a child node and putting it into the open table;
OPEN=[H3,G4,E5,F5,D6];CLOSED=[C4,B4,A5]
5) estimate the H3, obtain the child nodes, and put into the open table;
OPEN=[O2,P3,G4,E5,F5,D6];CLOSED=H3C4,B4,A5]
6) estimate the O2, obtain the child nodes, and put into the open table;
OPEN=[P3,G4,E5,F5,D6];CLOSED=[O2,H3,C4,B4,A5]
7) Estimated P3, has been solved;
Look at the specific process, and then look at the pseudo-program it. The pseudo-Program of the algorithm is as follows:

Best_first_search ()
{
Open = [start node]; Closed = [];
while (open table non-empty)
{
Get a node x from open and remove it from the open table.
if (x is the target node)
{
The path is obtained, the path is returned;
}
for (child node y for each X)
{
If (Y is not in the open table and the close table)
{
The value of Y is evaluated and the Y is inserted into the open table;//Not yet sorted
}
Else
if (Y in the Open table)
{
if (the value of Y is less than the valuation value of the Open table)
Update the valuation values in the open table;
}
Else//y in the close table
{
if (the value of Y is less than the valuation value of the close table)
{
Update the valuation values in the close table;
Remove the node from the close table and put it in the open table;
}
}
Insert the X node into the close table;
Sort the nodes in the open table according to the estimate value;
}//end for
}//end while
}//end func

Ah! Pseudo-Program came out, write a source program should not be a problem, according to gourd painting scoop can. The procedure for A * algorithm is the same as this, as long as you pay attention to the H (n) constraint of the G (n) in the valuation function. It is not clear to look at the "first knowledge A * algorithm". Well, we can get into another important topic, using a * algorithm to achieve the shortest path search. Before you do this, you'd better understand the previous algorithm carefully. I'm not sure I can be found. My email is at the end of the article.

Third, A * algorithm to achieve the shortest path search

In the game design, often involves the shortest path search, now a better method is to use a * algorithm design. We don't have to take care of his good, anyway! ^_*


Note that the following is based on the Classastar program as a blueprint, you can download this program here. This program is a complete project. There is an EXE file in it. You can take a look first.

To review, the core of A * algorithm is the value function f (n), which includes the G (n) and H (n) parts. g (n) is the price that has been traversed, and H (N) is the estimated cost of N to the target. In this example, G (n) represents the depth of the state space from the start node to the N node, and h (n) represents the straight distance from the location of the map of the N node to the target location. Ah! One is the state space, one is the actual map, do not be mistaken. In more detail, there is an object A, the coordinates on the map are (Xa,ya), the coordinates of the target B to be reached (XB,YB). When you start the search, set a starting node of 1, generating eight child nodes 2-9 because there are eight directions.

Take a closer look at how the G (N) and H (n) of nodes 1, 9, and 17 are calculated. Now you should know how f (n) in the following program is calculated. began to explain the source program. In fact, this program is a very typical textbook-like program, that is, as long as you understand the above pseudo-program, this program is very easy to understand. But he's a little different from the pseudo-program above, and I'll put it in the back.

First look at the main function of the search:

void Astarpathfinder::findpath (int sx, int sy, int dx, int dy)
{
NODE *node, *bestnode;
int tilenumdest;
Get the target position for judgment
Tilenumdest = Tilenum (SX, SY);
Generate open and Closed tables
Open= (node*) calloc (1,sizeof (NODE));
Closed= (node*) calloc (1,sizeof (NODE));
Generate the start node and put it in the open table
Node= (node*) calloc (1,sizeof (NODE));
node->g = 0;
This is the calculated H value
Node->h = (DX-SX) * (DX-SX) + (dy-sy) * (Dy-sy); Should really use sqrt ().
This is the calculation of the F value, which is the valuation value
Node->f = node->g+node->h;
Node->nodenum = tilenum (dx, dy);
node->x = DX;
Node->y = dy;
open->nextnode=node; Make Open List point to first node
for (;;)
{//Get one of the best-valued nodes from the Open table
Bestnode=returnbestnode ();
If the node is the target node, exit
if (Bestnode->nodenum = = tilenumdest)//If we ' ve found the end, break and finish
Break
Otherwise, the child node is generated
Generatesuccessors (Bestnode,sx,sy);
}
PATH = Bestnode;
}
And look at the Generate child node function generatesuccessors:
void Astarpathfinder::generatesuccessors (NODE *bestnode, int dx, int dy)
{
int x, y;
Oh! Generate eight-direction sub-nodes in turn, simple!
Upper-left
if (Freetile (X=bestnode->x-tilesize, y=bestnode->y-tilesize))
GENERATESUCC (Bestnode,x,y,dx,dy);
Upper
if (Freetile (X=bestnode->x, y=bestnode->y-tilesize))
GENERATESUCC (Bestnode,x,y,dx,dy);
Upper-right
if (Freetile (X=bestnode->x+tilesize, y=bestnode->y-tilesize))
GENERATESUCC (Bestnode,x,y,dx,dy);
Right
if (Freetile (X=bestnode->x+tilesize, Y=bestnode->y))
GENERATESUCC (Bestnode,x,y,dx,dy);
Lower-right
if (Freetile (X=bestnode->x+tilesize, y=bestnode->y+tilesize))
GENERATESUCC (Bestnode,x,y,dx,dy);
Lower
if (Freetile (X=bestnode->x, y=bestnode->y+tilesize))
GENERATESUCC (Bestnode,x,y,dx,dy);
Lower-left
if (Freetile (X=bestnode->x-tilesize, y=bestnode->y+tilesize))
GENERATESUCC (Bestnode,x,y,dx,dy);
Left
if (Freetile (X=bestnode->x-tilesize, Y=bestnode->y))
GENERATESUCC (Bestnode,x,y,dx,dy);
}

Take a look at the most important function GENERATESUCC:
void Astarpathfinder::generatesucc (NODE *bestnode,int x, int y, int dx, int dy)
{
int g, tilenums, c = 0;
NODE *old, *successor;
Calculates the G-value of a child node
g = bestnode->g+1; G (successor) =g (Bestnode) +cost of getting from Bestnode to successor
Tilenums = Tilenum (x, y); Identification purposes
Child node again in the Open table?
if (Old=checkopen (tilenums) = null)//if equal to null and not in OPEN list, else it returns the Node
{
If the
for (c = 0; C <8; C + +) if (bestnode->child[c] = = NULL)//ADD old to the list of Bestnode ' s children (or successors ).
Break
BESTNODE-&GT;CHILD[C] = old;
Compare the estimate value in the open table with the current value (as long as you compare the G value)
if (g g)//If our new g value is Parent = Bestnode;
Old->g = g;
Old->f = g + old->h;
}
}
else//In the closed table?
if (old=checkclosed (tilenums) = null)//if equal to null and not in OPEN list, else it returns the Node
{
If the
for (c = 0; c<8; C + +) if (bestnode->child[c] = = NULL)//ADD old to the list of Bestnode ' s children (or successors ).
Break
BESTNODE-&GT;CHILD[C] = old;
Compare the estimated value and the current valuation value in the Closed table (as long as you compare the G value)
if (g g)//If our new g value is Parent = Bestnode;
Old->g = g;
Old->f = g + old->h;
Update the value of all child nodes of old again in turn
Propagatedown (old); Since we changed the G value of old, we need
To propagate this new value downwards, i.e.
Do a depth-first traversal of the tree!
}
}
else//is not in the open table nor in the close table
{
To generate a new node
successor = (node*) calloc (1,sizeof (NODE));
Successor->parent = Bestnode;
Successor->g = g;
Successor->h = (X-DX) * (X-DX) + (y-dy) * (Y-dy); should do sqrt (), but since we don ' t really
Successor->f = g+successor->h; Care about the distance but just which branch looks
successor->x = x; Better this should suffice. Anyayz it ' s faster.
Successor->y = y;
Successor->nodenum = Tilenums;
And then insert the Open table, sorting at the same time.
Insert (successor); Insert successor on OPEN list WRT f
for (c =0; c <8; C + +) if (bestnode->child[c] = = NULL)//ADD old to the list of Bestnode ' s children (or successors ).
Break
BESTNODE-&GT;CHILD[C] = successor;
}
}


Ha ha! A * algorithm I understand! Of course, I hope you have this feeling! But I have to say a few more words. Take a closer look at this program and you will find that this program is somewhat different from the pseudo-program I said earlier, in the GENERATESUCC function, when the child node is in the closed table, the child node is not removed from the closed table and placed in the Open table. Instead, the evaluation value of all child nodes of the node is computed directly (with the Propagatedown function). This can be faster! When the child node is in the open table and the closed table, after the evaluation value is recalculated, there is no re-ordering of the nodes in the Open table, I have some wonder, why not row it? :-(, will it be a small bug. You know, just tell me, okay?

Principle of A * algorithm < turn >

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.