A * pathfinding algorithm (Manhattan distance)

Source: Internet
Author: User

Some days ago, people in the group were asked a * algorithm for the problem. I've done it before and put it on GitHub (Https://github.com/XJM2013/A_Star); interested can download it and see it.

Here upload a fairly good * algorithm demo program, you can download down to see the effect: http://download.csdn.net/detail/a374826954/8781185.

The following description is excerpted from the Tsinghua University Press "AI" book:

The evaluation function is in the following form:

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

where n is the node to be evaluated.

g* (N): Represents the shortest dissipation value from the initial node s to the node n;

h* (N): Represents the shortest dissipation value from node n to the target node G;

f* (n) =g* (n) +h* (n): Represents the dissipation value of the shortest path from node s through node n to the target node G.

The f (n), g (n), and H (N) represent estimates of the 3 function values of f* (n), g* (n), and h* (n), respectively. A algorithm is to use this prediction to achieve the purpose of the search.

When in algorithm A's evaluation function, the heuristic function h (n) is in the lower bound of h* (n), that is, when the H (n) ≤h* (n) is satisfied, the algorithm is called the algorithm A *.

It is necessary to note that the program is slightly slower than it actually is due to the addition of some debugging information.

Here's a look at the program demo interface:


Bai Gezi represents a pathway, Hegez means blocking.

The black lattice here is randomly generated and generated in the receiving code:

(function () {map.init (0.3); Map.createui ();}) ();

The random number is 0.3, and when set to 0 o'clock, it is all a pathway.

Talk about 3 buttons:

"Set block": After pressing this button, you can customize the blocking area by the left click of the mouse to facilitate testing.

"Closed": Displays the node information that has been extended and placed into the closed table. Display information starts with C: 4 data is f,g,h and extended index respectively; It is important to note that the extended index is not necessarily contiguous because the same node may be extended multiple times.

"Open": Displays the remaining open table node information. The display information starts with O: 3 data are f,g and H respectively.


In fact, some people do not understand a * algorithm, mainly do not understand f,g,h is how to take value.

Here's an example:

From Guangzhou to Beijing (it is best to open the Baidu map to see, the following distance is not equal to the distance between the actual city)

In other words, Guangzhou is the starting point, Beijing is the target point.

Guangzhou to Guangzhou distance is 0, that is g=0, Guangzhou to Beijing's straight line distance is 5000, namely h=5000. f=g+h,f=5000.

Put Guangzhou into open table, expand Guangzhou This node, and will Guangzhou from open table, put into closed table; get Chongqing and Shanghai.


The distance between Guangzhou and Chongqing is 2500, which is the g=2500 of Chongqing, and the straight distance from Chongqing to Beijing is 4500, that is h=4500;f=7000.

Put Chongqing in the open table.

The distance between Guangzhou and Shanghai is 3000, which is the g=3000 of Shanghai, and the straight distance from Shanghai to Beijing is 3000, that is h=3000;f=6000.

Put Shanghai in the Open table, as Shanghai's F is less than Chongqing's F, the Shanghai node in the Open table in front of the Chongqing node.


Expand this node in Shanghai and find Beijing, so the search is over. The final route is Guangzhou-Shanghai-Beijing.

The core code of this program is:

Search:function () {while (1) {if (map.openTable.length = = 0) {break;            } var _fminnode = Map.openTable.shift ();            if (_fminnode.place = = 2) {continue;            } map.searcharound (_fminnode);            _fminnode.index = Globalindex;            globalindex++;            _fminnode.place = 2;closedtable.push (_fminnode);            if (Map.endnode = = _fminnode) {break;        }}}, Searcharound:function (node) {var nodex;        var Nodey;            for (var x =-1; x <= 1; × x + +) {Nodex = node.x + x;                for (var y =-1, Mapnode, _obj, Tmpnode; y <= 1; y++) {Nodey = Node.y + y;                Remove itself if (x = = 0 && y = = = 0) continue;                 if (nodex >= 0 && nodey >= 0 && nodex < map.gridwidth && Nodey < Map.gridheight) {Mapnode = Map.data[nodex][nodey];                    if (Mapnode.isroadblock) {continue; }_obj = MAP.GETFGH (node, mapnode);d o{if (mapnode.place = 2) {if (_obj. G >= mapnode.obj.g) {break;}} else if (mapnode.place = = 1) {//Monotonic limit if (mapnode.obj.g <= _obj.                                                G) {continue;}}                        Mapnode.obj = _obj;                        Mapnode.front = node;                        Mapnode.place = 1; Map.insertopentable (Mapnode);}            while (0)};        };    }; },

The workflow is to iterate through the open table (sorted according to the F value), extending the read-out node until the target point is found.

Where GETFGH is the f,g,h information that computes the node.

You can see the comments in the code that write//monotone restrictions, and give the same example.

Guangzhou also expanded a node: Wuhan.

Wuhan's f=5900,g=2400,h=3500. This is the first expansion of Wuhan, and then found Shanghai.

The distance between Wuhan and Shanghai is 1900. At this time through the Wuhan to Shanghai node information is g=2400+1900=4300,h is unchanged is also 3000.

Shanghai node Mapnode saved before the g=3000, now calculate the distance through Wuhan to Shanghai is g=4300. Therefore, the distance from Wuhan to Shanghai is far from direct to Shanghai, so the f,g,h is not replaced by the open table.

This is the monotony limit.

The following is the code for this article and the demo program:



The deficiency of A * algorithm:

When the target point is in a dead-end area, a * algorithm traverses the entire map. This is obviously inefficient.


A * pathfinding algorithm (Manhattan distance)

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.