简体   繁体   English

如何判断A *算法何时是一个好的选择,以及如何选择一个好的启发式算法?

[英]How to tell when the A* algorithm is a good option, and how to choose a good heuristic?

Recently I wrote a solver for the famous "15 puzzle" using the A* algorithm by using a heuristic function based off the sum of the Manhattan distances of the distances for each tile to their destination spots. 最近,我通过使用启发式函数(基于每个图块到目标点的距离的曼哈顿距离之和),使用A *算法编写了著名的“ 15难题”的求解器。

This led me to wonder two things: 这使我想知道两件事:

  1. How do you know when the A* algorithm is even something to use? 您怎么知道什么时候甚至可以使用A *算法? Unless I came across online tutorials, I would have never guessed that the 15 puzzle could be solved this way. 除非我碰到在线教程,否则我永远不会猜到15个难题可以通过这种方式解决。

  2. How do you know which heuristic function to use? 您如何知道要使用哪个启发式函数? At first, for the 15 puzzle, I considered a simple "sum of tiles not in position" heuristic. 首先,对于15个难题,我考虑了一个简单的“未放置的瓷砖总和”启发式方法。 So if all pieces weren't in their right spots, the heuristic for the 15 puzzle might return 15, whereas 0 would indicate a solved board. 因此,如果所有棋子的位置都不正确,则15个难题的试探法可能会返回15,而0则表示棋盘已解决。 But somehow the sum of the distances are better. 但是总之,距离总和更好。 How does one know, going into it? 怎么知道,进入其中?

If you're exploring a graph to find a path that is in some way "shortest" (the cost doesn't have to be a "distance", but it has to be monotone), you can already use Dijkstra's. 如果要浏览图形以某种方式找到“最短”的路径(成本不必是“距离”,但必须是单调的),则可以使用Dijkstra方法。 Your problem will typically look nothing like path-finding at a first glance though, as in, you're not planning to "travel over a route". 乍一看,您的问题通常看起来并不像寻路,例如,您不打算“穿越路线”。 It's more abstract than that. 比这更抽象。

Then if you can use Dijkstra and you have some admissible heuristic (that's the hard part), you can use A*. 然后,如果可以使用Dijkstra, 并且有一些可以接受的启发式方法(这是最困难的部分),则可以使用A *。

An often used technique for finding heuristics is dropping some constraint of your problem. 查找启发式方法的一种常用技术是放弃对问题的某些约束。 For example, if you can teleport each tile to its destination regardless of whether there's already a tile there, it will take #displacements teleports. 例如,如果您可以将每个图块传送到其目的地,而不管那里是否已有图块,则将进行#displacements传送。 So there's the first heuristic. 因此,这是第一个启发式方法。 If you have to slide the tiles but they can slide through each other, the cost for each tile is the Manhattan distance to its destination. 如果您必须滑动磁贴但它们可以彼此滑动,则每个磁贴的成本就是曼哈顿到其目的地的距离。 Then you can look at improving the heuristic, for example the Manhattan distance heuristic obviously ignores that tiles interfere with each other as they move, but there is a simple case where we know where tiles must conflict and use more moves: consider two tiles (pretend there are no other tiles) in the same row and their destinations are also on that row but in order to get there they'd have to pass through each other. 然后,您可以考虑改进启发式方法,例如,曼哈顿距离启发式方法显然忽略了图块在移动时会相互干扰,但是在一个简单的情况下,我们知道图块必须发生冲突并需要更多移动:考虑两个图块(假装在同一行中没有其他图块),它们的目的地也在该行中,但要到达那里,它们必须彼此穿过。 They'd have to go around each other, adding two vertical moves. 他们必须互相走动,增加两个垂直方向的移动。 This gives the Linear Conflicts heuristic. 这给出了线性冲突的启发式方法。 Even more interference can be taken into account, for example with pattern databases. 甚至可以考虑更多的干扰,例如使用模式数据库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM