Implementing A-Star algorithm

Source: Internet
Author: User

Tag: Represents none the title GES algorithm for finding the path efficiency

Update

Modify the * algorithm a little bit, use BFS (sort the open table by F-value), and add a new evaluation function to measure whether a random point on a segment of the current-to-end line is a wall or an Access node , or return 1, otherwise 0.

functionPath_add_barrial_tracing (state, PT1, PT2)LocalXabs, Yabs = Math.Abs (pt1.x-pt2.x), Math.Abs (PT1.Y-PT2.Y)ifXabs = = 0orYabs = = 0 Then return0EndLocalXR, yr = Math.ceil (Math.random (1,xabs)), Math.ceil (Math.random (1,yabs))Localv = path_get_point (state, {x= xr, y= yr})ifV ~= 1 Then return1Else return0EndEnd

-----------------------------------------------------

Write in front

You have seen similar articles that visualize the pathfinding algorithm.

    • A * algorithm visualization and implementation-code voluptuous-Blog channel-csdn.net
    • Exploration and improvement of A * pathfinding algorithm (i.)-the column of knowledge

Thinking about integrating them into the game framework, overall, integration is less difficult.

Implementation ideas

Previously realized Tablelayout, set the length of the width, you can evenly arrange the elements within the container. The grid in the graph is also the realization of this idea.

Only the breadth of the BFS is traversed and the deep traversal of Dfs, both of which are classic traversal algorithms.

The book generally uses open and closed two tables, here for simple diagram, it is implemented with a table. the structure of the graph is represented by a two-dimensional matrix, 1 means no access, 3, 4 for the start and end, 2 for the wall, 6 and above for the access. Then each access is marked by the values in the matrix.

    • BFS is the adjacent block of the current access block is added to the end of the table, each time from the header to remove the block that will be accessed. Since the added blocks will be accessed over a period of time, the size of the table may increase rapidly. As a result, the BFS is less efficient, and it traverses all the squares, and it can find the global optimal solution. The "Day After Tomorrow" in the water flooded the city is also the embodiment of BFS.
    • Each time Dfs adds an adjacent block of the current block to the head of the table, each time it takes a block from the table header. So unlike BFS, the newly added blocks are immediately accessed, so the size of the table does not increase rapidly. It can find the solution quickly, but not necessarily the global optimal solution. The way the lightning grows in life is DFS.

The two algorithms just add a different way to the table, DFS is added to the head, and BFS is added to the tail.

The main difficulty here is that the color of the block is gradual, from the beginning (red) near the black, far blue. The calculation is the distance from any point to the starting point, and then the hue that is converted to RGB,HSL according to HSL is fixed, and the brightness can be adjusted to achieve the gradient effect.

-------------------------------------------------

The following implementation (pseudo) A * algorithm.

Both BFS and Dfs methods have drawbacks: BFS can find the global optimal, but it needs to access all the locations once, time consuming, DFS fast, but only the local optimal, and how DFS picks the nodes that need to be expanded is not clearly defined.

A * has improved the approach described above. A * gives an evaluation function f. F=g+h. G is the current amount of movement, H is the estimated distance to move, the sum of the two is the current node evaluation value, of course, the smaller the value, the more likely to go this route.

The solution is simple: the moving distance that g= accumulates, and the Hamiltonian distance from the end point of the current position of the h= . When the node is expanded, the selection of nodes is sorted by the F value, so that the minimum F value is expanded first, thus saving time.

A * is typically implemented with open and closed tables, which sort the nodes in the open table by F and choose the least expensive expansion. that is, a * algorithm sorts the nodes in the open table every time, and a * is expanded in a manner similar to BFS. Why is it based on BFS? Because the Open Table node produced by BFS is the contour of the currently traversed node, it is somewhat like the minimal spanning tree algorithm, which is selected from the current contour to select the least expensive node to expand. The only effect of a * efficiency is the calculation method of F.

PS: Because of laziness, realize a * with the above difference! The above is to sort the open, and I will only order the adjacent nodes of the current node, so the algorithm effect is certainly not good. Moreover, the goal of this series is to do the GUI, the algorithm visualization is just a demo.

Periodic summary

The key to A * algorithm is to set up an evaluation function, just as the Afado estimates the situation. It uses a method similar to the minimum spanning tree (the lattice is thought to be connected to the top and bottom), but the selection rule of the minimum spanning tree is determined (path length determination), and a * selection rule is indeterminate (the evaluation function is not accurate). This results in the optimal solution for the minimum spanning tree, while a * does not necessarily get the optimal solution.

Backed up by http://zhuanlan.zhihu.com/p/25593280.

Implementing A-Star 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.