简体   繁体   English

是时候使用A *算法解决8格滑动难题了

[英]Time for A* algorithm to solve a 8 tile sliding puzzle

Just wondering if anyone could help me out with some code that I'm currently working on for uni. 我只是想知道是否有人可以为我目前正在为uni处理的代码提供帮助。 It's a sliding tile puzzle that I'm coding and I've implemented an A* algorithm with a Manhattan distance heuristic. 我正在编码这是一个滑动拼图游戏,并且已经实现了具有曼哈顿距离启发式算法的A *算法。 At the moment the time for it to solve the puzzle can range from a few hundered milliseconds to up to about 12 seconds for some configurations. 目前,它解决难题的时间范围从几秒钟到几秒钟,对于某些配置,则可能长达12秒左右。 What I was wanting to know is if this range in time is what I should be expecting? 我想知道的是,这个时间范围是否符合我的预期?

I've never really done any AI before and I'm having to learn this on the fly, so any help would be appreciated. 之前我从未真正做过任何AI,而且我必须即时学习这些知识,因此我们将不胜感激。

What i was wanting to know is if this range in time is what i should be expecting? 我想知道的是,这个时间范围是否应该达到我的预期?

That's a little hard to figure out just from the information you've provided. 仅从您提供的信息中很难找出答案。 It would help if you could describe how you implemented A*, or if you profiled your application and needed help with specific areas that were slow. 如果您可以描述如何实现A *,或者您对应用程序进行了概要分析并需要在速度较慢的特定区域获得帮助,这将有所帮助。

One thing to note that'd probably speed up your average solution time: Half of the starting positions of any n-tile puzzle can never lead to a solution, so you can immediately exclude certain configurations very quickly. 需要注意的一件事可能会加快平均解决时间: 任何n-tile拼图的起始位置的一半永远不会导致解决方案,因此您可以立即快速排除某些配置。 For example, you cannot solve an 8-tile puzzle that looks like this: 例如,您不能解决如下所示的8块拼图:

1 2 3 
4 5 6
8 7 .

To see why, note that because the blank space has to wind up back where it started, the overall number of "up"/"down" moves must be equal, as does the overall number of "left"/"right" moves. 要知道为什么,请注意,因为空白必须在其开始处回退,所以“上” /“下”动作的总数必须相等,“左” /“右”动作的总数也必须相等。 That means that the overall number of moves must be even. 这意味着移动的总数必须是偶数。

But the 7/8 transposition here is one move off from the starting puzzle, without changing the blank position! 但是这里的7/8换位是从起始拼图开始的一个步骤,而不会更改空白位置! So this puzzle can't be solved. 因此,这个难题无法解决。 (However, if we made two transpositions, then it'd be solvable again.) (但是,如果我们进行了两次换位,那么它将再次可以解决。)

Like you should know you cannot expect any general time. 就像您应该知道,您无法期望任何一般时间。 It depends everytime on the code itself especially in which deap your implementation walkes down the tree and also if your code can use the advantages for processor features. 它每次都取决于代码本身,尤其取决于实现的难忘程度以及代码是否可以利用处理器功能的优势。

For debugging I would save or print out (but this takes time!) in which level of your tree you are. 为了进行调试,我将保存或打印出您的树的级别(但这需要时间!)。

Also remember that the weights are very important. 还请记住,权重非常重要。 Eg: 例如:

123
4 6 <- your final state
789
           213                               1 3
To change  4 6  is much more expensive than  426
           789                               789

I hope that helps. 希望对您有所帮助。

Obviously, this depends not only on your hardware, but on your implementation. 显然,这不仅取决于您的硬件,还取决于您的实现。 It's not a good measure of performance, though: What you want to do is determine the effective branching factor of your heuristic, vs the actual branching factor of some other non-heuristic approach. 但是,这并不是衡量性能的好方法:您要做的是确定启发式方法的有效分支因子,而不是其他一些非启发式方法的实际分支因子。

I don't want to say too much more, since this is a homework problem, but if memory serves, Russel and Norvig conver this in the context of the sliding puzzle itself... chapter three, perhaps? 我不想说太多,因为这是一个家庭作业问题,但是如果有记性的话,Russel和Norvig会在滑动拼图本身的背景下实现这一目标……也许是第三章? (My R+N is not at hand.) (我的R + N不在手边。)

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

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