Introduction to A* algorithm

Tip: After the article is written, the table of contents can be automatically generated from the ## title. How to generate it can refer to the help document on the right.


1. The origin and application background of the A* algorithm

Insert image description here

2. Basic principles of A* algorithm

1. Basic mathematical principles

A* algorithm is a heuristic search algorithm that updates the value F to search
F = G + H F=G+H F=G+H
F F F: Total cost of the current node
G G G: Movement distance from the starting node to the current node (actual cost)
H H H: Estimated moving distance (estimated cost) from the current node to the end point

calculateG cost is the sum of the distances of all edges passing from the starting point to the current node.
CalculationH costThe heuristic function h includes Manhattan distance, diagonal distance (Chebyshev distance), and Euclidean distance.

Notes:
1. When calculating H, ignore obstacles in the path. This is an estimate of the remaining distance, not the actual value, so the H cost is the estimated cost.

2. When performing global map path planning, the environmental information is converted into a grid map, where each grid represents an area of ​​the environment. The area of ​​obstacles can be marked as an obstacle grid, so the obstacle nodes are known. of.

3. It is known that the condition for the A* algorithm to ensure that it can find the shortest path is to meet the following two conditions:
(1) The heuristic function h(n) must satisfy monotonicity (i.e. The estimated cost will not exceed the actual cost from node n to the target node),
(2) The map must be feasible, that is, there are no insurmountable obstacles or inaccessible areas.

2. Selection of heuristic function

2.1Manhattan distance

Manharan distance: D = ∣ x 2 − x 1 ∣ + ∣ y 2 − y 1 ∣ D=|x_{2}-x_{1}|+|y_ {2}-y_{1}| D=x2x1+y2and1, as shown below:
Coarse=Manhattan distance
If the graph only allows movement in four directions, up, down, left, and right, you can use Manhattan distance. Calculate the number of squares traveled from the current grid horizontally or vertically to reach the target.
Insert image description here
For example, in the above figure, the central node can expand up, down, left, and right. In this case, it is more effective to use Manhattan distance as the heuristic function. When there are obstacles in the expansion node:
Insert image description here
The A* algorithm will determine the status of each node based on the grid in the map, that is, whether it is an obstacle node. When the algorithm expands a node, if the node is an obstacle node, it will not be added to the set of nodes to be expanded, thereby avoiding continuing to search along the node, thus effectively avoiding obstacles.

2.2 Diagonal distance (Chebyshev distance)

Diagonal distance (Chebyshev distance): D = m a x ( ∣ x 2 − x 1 ∣ + ∣ y 2 − y 1 ∣ ) D=max(|x_ {2}-x_{1}|+|y_{2}-y_{1}|) D=atx(x2x1+y2and1), as shown below:
diagonal distance
If the graph allows movement in eight directions, you can Use diagonal distance. Diagonal distance is better suited to handle situations where diagonal movement is allowed because it takes into account the extra distance when moving diagonally, making the estimate more accurate.
Insert image description here
When there are obstacles in the expansion node:
Insert image description here

2.3 Euclidean distance

Euclidean distance: D = ( x 2 − x 1 ) 2 + ( y 2 − y 1 ) 2 D=\sqrt{(x_{2}- x_{1})^2+(y_{2}-y_{1})^2} D=(x2x1)2+(y2and1)2 , as shown below:
Euclidean distance
If movement is allowed in any direction, Euclidean distance can be used. Euclidean distance directly calculates the distance between two points, which is more consistent with the actual situation.

Notice:
There is no certain binding relationship between the heuristic function and the expansion method. The above is just a suggestion to use which heuristic function in which way to expand the node. Which heuristic function to choose depends on the specific circumstances of the application scenario. You can choose based on the actual situation or combine multiple heuristic functions for comprehensive consideration. Part of the above content is provided by ChatGPT-plus4.0. If there are any errors, please make your own judgment.

3. Program implementation of A* algorithm

1.Algorithmic logic

1. Initialize the starting node S, the final node G, the initialization list open, closed, and initialize the global map (grid map)
2. Starting from the starting node S, use S as A square to be checked, put into the open list
3. Find the reachable squares (up to eight) around the starting node S, put them into the open list, and set their parents The node is S
4. Delete the starting node S from the "open list" and put S into the closed list
5. Calculate each surrounding square The F value of the grid F=G+H
6. Select the square a with the smallest F value from the open list and put it from the open list into the closed list
7. Check all adjacent squares of a
1) Obstacles and squares in the close list are not considered
2) If these squares are not In the open list, add them to the open list, calculate the F value of these squares, and set the parent node to a
3) If an adjacent square c is already in the open list, calculate The new path reaches square c from S (that is, the path passing through a). Determine whether the parent node and F value need to be updated: if the G value of the new node c is lower, modify the parent square to square a and recalculate F. value, the H value does not need to change (because the estimated cost of reaching the target point from the grid is fixed). If the G value of the new node c is relatively high, it means that the new path cost is higher, and the value does not change (the G value unchanged and not updated)
8. Continue to find the smallest F value from the open list, delete it from the open list, add it to the close list, and then continue to find the surrounding blocks that can be reached. This cycle
9. End judgment: When the target block G appears in the open list, it means that the path has been found; when there is no data in the open list, it means that there is no suitable path.

2.Experimental results

existing 20 ∗ 20 20*20 20In the raster map of 20, obstacles are generated automatically. The heuristic function h adopts Manhattan distance and Euclidean distance respectively. Each node can expand in four directions. . The resulting path plan is shown on the left. The shaded areas are squares that have been added to the list (or searched).

Insert image description hereWhen we increase the grid map and use A for path planning, what will be the result
Insert image description here in 200 ∗ 200 200*200 200In the raster map of 200, obstacles are randomly generated, and the obstacle coverage accounts for 20% of the map. The heuristic function h uses Manhattan distance and Euclidean distance respectively. The resulting path plan is shown on the left.
A
In the heuristic search process of the algorithm, the closer the estimated value of the heuristic function is to the true value, the higher the planning efficiency.
In a grid map, each node can expand in eight directions, and the Manhattan distance can more accurately reflect the distance between two nodes. In comparison, the H value calculated by Euclidean distance is relatively small, and it requires a square root operation, which takes a long time.

Insert image description hereIf anyone needs the program, please PM me.


4. Summary of A* algorithm

Advantages and disadvantages of A* algorithm

advantage:
1. Optimality: Under certain conditions, the A algorithm can guarantee to find the shortest path.
2. Speed: Compared with other search algorithms, the A
algorithm has a faster search speed because it can reduce the number of search paths through heuristic functions. . This makes the A algorithm very efficient when dealing with large-scale search problems.
3. Wide applicability: A
algorithm can be used to solve various path search problems, such as finding the best path in computer games and robot navigation. Find the shortest path and so on.

shortcoming:
1. The heuristic function is not easy to design: A algorithm needs to use a heuristic function to estimate the distance from each node to the target node, but the design of the heuristic function 2. An easy thing. If the heuristic function is poorly designed, the search efficiency will be greatly affected.
Occupies a large amount of storage space: A
algorithm needs to store node information during the search process, so when the state space of the search is large, it needs to occupy a large amount of space. of storage space.
3. May fall into a local optimal solution: Although the A algorithm can guarantee to find the shortest path, when the heuristic function is not good enough, A The algorithm may fall into the local optimal solution and fail to find the global optimal solution.

Guess you like

Origin blog.csdn.net/m0_51951660/article/details/129794499
Recommended