简体   繁体   English

用于 2x2 魔方求解的 A* 算法中的 h(n) 选择

[英]h(n) selection in A* algorithm for 2x2 rubik's cube solving

I am currently working on a 2x2 Rubik's cube solving robot project.我目前正在研究一个 2x2 魔方求解机器人项目。 It takes in the cube data via a 2x2 color sensor array and solves it using some servo motors and arms.它通过一个 2x2 颜色传感器阵列接收立方体数据,并使用一些伺服电机和臂来解决它。 I was looking on wiki and I think A* would be a possible way to write the program to solve it.我在看 wiki,我认为 A* 可能是编写程序来解决它的一种方法。 However, I can't figure out how to define the expected cost function(h) for the cube.但是,我不知道如何定义立方体的预期成本函数(h)。 It isn't finding the shortest path on a 2D plane, where h(n) can simply be some form of actual distance(Euclidean, Manhattan, etc).它不是在 2D 平面上找到最短路径,其中 h(n) 可以只是某种形式的实际距离(欧几里得、曼哈顿等)。 I was originally thinking about counting how many tiles have already grouped up, but it won't really work since I can just do 2~3 moves on the cube and many tiles are disconnected.我本来想计算已经组合了多少块,但它不会真正起作用,因为我只能在立方体上移动 2~3 次,并且很多块都断开了。 How exactly should I write this cost function?我应该如何写这个成本function? Or is there a better alternative to A* in my case?或者在我的情况下是否有更好的 A* 替代方案? (2x2 is too simple to use IDA*, I guess. The max possible move count to solve it is only 11) (我猜 2x2 使用 IDA* 太简单了。解决它的最大可能移动计数只有 11)

A* with iterative-deepening, depth-first search (IDA*) is a good way to go.具有迭代深化、深度优先搜索 (IDA*) 的 A* 是 go 的好方法。 For the heuristic, define one or more pattern databases, where each key is a cube state, and each value is the distance to the solved state from that cube state.对于启发式,定义一个或多个模式数据库,其中每个键是一个立方体 state,每个值是从那个立方体 state 到求解的 state 的距离。 To keep the databases reasonably small, each database can hold a subset of the cube's overall state.为了使数据库保持在合理范围内,每个数据库都可以保存多维数据集整体 state 的一个子集。 Regarding the heuristic, given a cube state, look up the distance in every database.关于启发式,给定一个立方体 state,在每个数据库中查找距离。 The maximum of those distances is the best estimated distance to the solved state (ie it takes at least that many moves to solve the cube).这些距离中的最大值是到已解决的 state 的最佳估计距离(即,解决立方体至少需要很多移动)。

For example, one could make a database that has the orientations of all 8 cubies.例如,可以创建一个包含所有 8 个立方体的方向的数据库。 Just the orientations mind you, not the positions.只是方向在乎你,而不是位置。 Since each piece can be in one of three orientations--on the top layer, any sticker can point up, for example--there are 3^8 possible states.由于每一块都可以处于三个方向之一 - 在顶层,任何贴纸都可以指向,例如 - 有 3^8 种可能的状态。 I'm not an expert in the 2x2, but I'm pretty sure that it's impossible to disorient just one piece.我不是 2x2 的专家,但我很确定不可能只迷惑一件。 That is, it's not possible to have a solved 2x2 and then flip just one cubie.也就是说,不可能有一个已解决的 2x2 然后只翻转一个立方体。 Assuming that's correct, there are actually 3^7=2187 possible orientation states.假设这是正确的,实际上有 3^7=2187 种可能的方向状态。 You would then iterate using BFS from the solved state and find each of these 2187 states, encode each state into an integer from 0-2186, and store the distance from that state to the solved state. You would then iterate using BFS from the solved state and find each of these 2187 states, encode each state into an integer from 0-2186, and store the distance from that state to the solved state. Put another way, a database is just a hash table.换句话说,数据库只是一个 hash 表。

You could make multiple databases in the same manner.您可以以相同的方式创建多个数据库。 For example, the positions of the 8 pieces.例如,8 块的位置。 The positions and orientations of 4 of the 8 pieces. 8 件中的 4 件的位置和方向。 Etc.等等。

When it comes time to solve the cube, use IDA*.当需要解决多维数据集时,请使用 IDA*。 Each time a state is encountered, check the maximum distance provided from all of the pattern databases.每次遇到 state 时,检查所有模式数据库提供的最大距离。 There's your heuristic function.有你的启发式 function。 Prune accordingly.相应地修剪。

I have an article on Medium that goes into a lot more detail, but it's for the 3x3.我有一篇关于 Medium 的文章进行了更详细的介绍,但它是针对 3x3 的。 The same strategy works, though.不过,同样的策略也有效。

Ps The 2x2 is small enough that one can make a perfect pattern database with the exact distance from each state to the solved state and store that database on disk in only 1.8MB. Ps 2x2 足够小,可以创建一个完美的模式数据库,从每个 state 到已解决的 state 的精确距离,并将该数据库存储在磁盘上仅 1.8MB。 (There are 3,674,160 states, with a God's number of 11. 11 can fit in a nibble. 3,674,160/1024^2/2 ~= 1.8MB.) With that database, IDA* would be able to solve the 2x2 effectively instantaneously. (有 3,674,160 个状态,上帝的数字是 11。11 可以放在一个半字节中。3,674,160/1024^2/2 ~= 1.8MB。)使用该数据库,IDA* 将能够立即有效地解决 2x2。

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

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