Artificial Intelligence: Automatic pathfinding Algorithm implementation (four, D, d* algorithm)

Source: Internet
Author: User

Blog reprinted from: 1871520

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 all use OPEN, The method of the close table.

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 Dijkstra algorithm, there are graphical display and source code download.

A * 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 from the initial node to the N node in the state space, and H (N) is the estimated cost of the best path from N to the target node. Guaranteed to find the shortest path (optimal solution) condition, the key is the evaluation function h (n) Selection: Estimate the value of h (n) <= n to the target node distance actual value, in this case, search more points, search scope, 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.

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 and calculate the value of X.


Break else {if (x in OPEN) compares the valuation value of two X f//Note is the valuation value of the two different paths of the same node if (the value of X is less than the value of the Open table) updates the valuation value in the Open table;//Take the value of the minimum path if (x In CLOSE) Compare the value of two X//note that the valuation value of the two different paths of the same node (the value of x is less than the value of the close table) updates the valuation value in the close table;       Put the X node in the Open//take the minimum path of the value if (x not in both) to find the value of x; and insert X into the Open table; No sort} inserts n nodes into the close table, and sorts 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/

Sunway wrote two good introduction to the heuristic and a * algorithm of Chinese articles and a * source download: The first knowledge of a * algorithm http://creativesoft.home.shangdu.net/AStart1.htm and 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 there is the source of the download, but it has a small bug, is that the new child nodes placed in the open table is Sunway, and when the child nodes in the open table and the closed table, After recalculating the valuation value, there is 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 very large, the search scope not only more than Dijkstra, or even search all the road network, so that the efficiency greatly reduced. 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; /* g (successor) =g (Bestnode) +cost of getting from Bestnode to successor */tilenums=tilenum ((int) x, (int) y); /* Identification purposes */if ((Old=checkopen (tilenums))! = NULL) {for (c=0;c<8;c++) if (bestnode->child[c] = = NULL)/* Add old to the list of Bestnode ' s children (or successors). */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 here//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  

......................................................

A * (A Star) algorithm and the conversion of D 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 is the weight or distance from the (I to J section) Where: The Djq,diq function is equivalent to the estimated value djq= (linear distance from J to Q), diq= (straight distance from I to Q) principle: I to Q direction according to DIJ+DJQ > Diq, take Dij+djq-diq small, If it is in the opposite direction Dij+djq-diq will be very large. 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, see appendix.

The main methods (these are completely drew in reading the above information and the compilation process of personal understanding, cannot guarantee completely correct, for reference only)

1. First, the Dijstra algorithm is used 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 a point exists in open and close, the method is as follows: while (      {The node y with the lowest k value from the OPEN table, and the child node A of Y, and the H value of a, H (a) =h (Y) +y to child Node A (y,a) {if (A in open) compare the H value of two A (the H value of a 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 of the K value has an unaffected shortest path through the existence of a break;          If (A in CLOSE) compares the H value of two A//note is the value of the 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; Put the a node in the Open table    There is no affected shortest path through the existence of a break; If (a not-in both) inserts a into the open table; There is no sort} put Y to close table; The Open table compares the K value size to sort; the robot takes the shortest path from point A to target by 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.

Appendix

1. Optimal and efficient Path planning for partially-known environments

2. The focussed d* algorithm for real-time replanning

Artificial Intelligence: Automatic pathfinding Algorithm implementation (four, D, d* 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.