A-star pathfinding optimization plan

1. Store path information between commonly used locations (this optimization solution is generally used when the map is large and the starting point is far away from the end point)

When the map information is known and the positions of each obstacle are fixed, you can save the commonly used locations and the path information between them, and then find the locations closer to the starting point and the end point respectively during pathfinding, and then perform pathfinding respectively. The paths are combined to obtain the final path, but this path may be farther than the path found through normal pathfinding, but it will take less time to find the path.

2. Heuristic function optimization

2.1 Optimization of distance formula from end point

Heuristic function formula in simple pathfinding:

f (pathfinding consumption) = g (actual consumption can also be said to be the distance from the starting point) + h (estimated consumption can also be said to be the distance from the end point)

In general, the h distance uses the Euclidean distance formula, that is, h=\sqrt{(x1-x)^{2}+(y1-y)^{2}}

But for four-direction pathfinding (only forward, backward, left, and right pathfinding, not diagonal pathfinding),

h can use the Manhattan distance formula, that is, h = | x1 - x | + | y1 - y |

When searching for eight directions, h can use the Chebyshev distance formula, that is, h = max (| x1 - x |, | y1 - y |)

2.2 Increase the weight value

In a simple case, the pathfinding consumption is obtained by adding the actual consumption and the estimated consumption at a ratio of 1:1. However, the actual situation is that the estimated consumption is the estimated value, which is not equal to the real value of the distance to the end point. In this case, a coefficient w can be added to The pathfinding consumption formula becomes

f = g + w * h

At this time, you can change the mixing ratio of actual consumption and estimated consumption by modifying the value of w. For example, when w > 1, pathfinding consumption will give priority to the estimated cost of the current path. When w < 1, pathfinding consumption will give priority to Priority will be given to the cost incurred by the current path.

2.3 Optimization of heuristic function when comparing pathfinding consumption

When looking for the point with the minimum pathfinding cost in the open list, it is generally unavoidable to have points with the same pathfinding cost. At this time, it is necessary to make a trade-off between these points, and the way to make the trade-off is to add a small bias to the estimated function. Shift p. The reason why this shift p works is: when the heuristic function values ​​​​of two nodes are the same, we want to prioritize the node that is closer to the end point, and being closer to the end point means that its prediction The estimated function value is smaller, so if we add a small offset p to the estimated function (p can even be about 0.1%), the heuristic function value of the nodes closer to the end point will be smaller, and they will be prioritized. choose. Therefore, we can further optimize the heuristic function into the following form

f = g + w * h + p * h = g + (w + p) * h

2.4 Reduce inflection points

Sometimes there will be a path with frequent turns in pathfinding. Although this path is indeed the path with the least cost, there will also be other paths with the same cost without too many turns. Based on reality, in fact, steering also has consumption, so a value e can be added to represent steering consumption. When there is no steering, e = 0, and when steering, e = 1. At this time, the heuristic function becomes

f = g + (w + p) * h + e

3. Turn on list optimization

The open list is a list that will be frequently inserted and sorted in pathfinding. If data is inserted and sorted every time, it will consume a lot of performance. Therefore, when inserting data, you can compare the data first, and then insert the data, and it will be sure Optimize the open list to a certain extent.

4. Traverse adjacent points to optimize

Pathfinding is a process of repeatedly calculating the distances of all points around a point from the starting point and the end point, but sometimes it is not necessary to include all surrounding points in the calculation. For example, when the end point is in the upper right corner of the starting point, individual points in the lower left corner of the starting point do not need to be calculated. Calculation, because their consumption is generally greater. At this time, it will waste a lot of time to calculate these points in a loop, so some points can be discarded during calculation.

However, sometimes discarding some adjacent points will also cause the path obtained by path finding to be less than the optimal path. For example, when the starting point is surrounded by obstacles and there are only gaps near the discarded surrounding points, the path found will be detoured. At this time, optimization is still needed (but I haven’t found a good optimization solution^_^)

The following is a visual A-star pathfinding optimization project implemented using unity.

Unity Engineering-A Star Pathfinding Optimization

In addition to the above points, there are other optimization solutions for A-Star pathfinding. However, I only implemented the above-mentioned ones in the project and did not realize the optimization of storing path information between commonly used locations. Moreover, A-Star pathfinding sometimes cannot be found. to the optimal path, especially when the obstacle points are complex, such as in my project, so it still needs to be optimized.

Guess you like

Origin blog.csdn.net/qq_42720695/article/details/133812572
Recommended