我正在尝试编写一个数独的9 x 9求解器。我使用了以下代码:
:-use_module(library(clpfd)).
solve(X, Grid):-
X = [A1, A2, A3, A4, A5, A6, A7, A8, A9,
B1, B2, B3, B4, B5, B6, B7, B8, B9,
C1, C2, C3, C4, C5, C6, C7, C8, C9,
D1, D2, D3, D4, D5, D6, D7, D8, D9,
E1, E2, E3, E4, E5, E6, E7, E8, E9,
F1, F2, F3, F4, F5, F6, F7, F8, F9,
G1, G2, G3, G4, G5, G6, G7, G8, G9,
H1, H2, H3, H4, H5, H6, H7, H8, H9,
I1, I2, I3, I4, I5, I6, I7, I8, I9],
member(Grid, X),
%rows have to be unique and from 1 to 9
permutation(['1','2', '3', '4', '5', '6', '7', '8', '9'], [A1, A2, A3, A4, A5, A6, A7, A8, A9]),
permutation(['1','2', '3', '4', '5', '6', '7', '8', '9'], [B1, B2, B3, B4, B5, B6, B7, B8, B9]),
permutation(['1','2', '3', '4', '5', '6', '7', '8', '9'], [C1, C2, C3, C4, C5, C6, C7, C8, C9]),
permutation(['1','2', '3', '4', '5', '6', '7', '8', '9'], [D1, D2, D3, D4, D5, D6, D7, D8, D9]),
permutation(['1','2', '3', '4', '5', '6', '7', '8', '9'], [E1, E2, E3, E4, E5, E6, E7, E8, E9]),
permutation(['1','2', '3', '4', '5', '6', '7', '8', '9'], [F1, F2, F3, F4, F5, F6, F7, F8, F9]),
permutation(['1','2', '3', '4', '5', '6', '7', '8', '9'], [G1, G2, G3, G4, G5, G6, G7, G8, G9]),
permutation(['1','2', '3', '4', '5', '6', '7', '8', '9'], [H1, H2, H3, H4, H5, H6, H7, H8, H9]),
permutation(['1','2', '3', '4', '5', '6', '7', '8', '9'], [I1, I2, I3, I4, I5, I6, I7, I8, I9]),
%coloums have to be unique and from 1 to 9
permutation(['1','2', '3', '4', '5', '6', '7', '8', '9'], [A1, B1, C1, D1, E1, F1, G1, H1, I1]),
permutation(['1','2', '3', '4', '5', '6', '7', '8', '9'], [A2, B2, C2, D2, E2, F2, G2, H2, I2]),
permutation(['1','2', '3', '4', '5', '6', '7', '8', '9'], [A3, B3, C3, D3, E3, F3, G3, H3, I3]),
permutation(['1','2', '3', '4', '5', '6', '7', '8', '9'], [A4, B4, C4, D4, E4, F4, G4, H4, I4]),
permutation(['1','2', '3', '4', '5', '6', '7', '8', '9'], [A5, B5, C5, D5, E5, F5, G5, H5, I5]),
permutation(['1','2', '3', '4', '5', '6', '7', '8', '9'], [A6, B6, C6, D6, E6, F6, G6, H6, I6]),
permutation(['1','2', '3', '4', '5', '6', '7', '8', '9'], [A7, B7, C7, D7, E7, F7, G7, H7, I7]),
permutation(['1','2', '3', '4', '5', '6', '7', '8', '9'], [A8, B8, C8, D8, E8, F8, G8, H8, I8]),
permutation(['1','2', '3', '4', '5', '6', '7', '8', '9'], [A9, B9, C9, D9, E9, F9, G9, H9, I9]),
%3X3 boxes have to be unique and from 1 to 9
permutation(['1','2', '3', '4', '5', '6', '7', '8', '9'], [A1, A2, A3, B1, B2, B3, C1, C2, C3]),
permutation(['1','2', '3', '4', '5', '6', '7', '8', '9'], [A4, A5, A6, B4, B5, B6, C4, C5, C6]),
permutation(['1','2', '3', '4', '5', '6', '7', '8', '9'], [A7, A8, A9, B7, B8, B9, C7, C8, C9]),
permutation(['1','2', '3', '4', '5', '6', '7', '8', '9'], [D1, D2, D3, E1, E2, E3, F1, F2, F3]),
permutation(['1','2', '3', '4', '5', '6', '7', '8', '9'], [D4, D5, D6, E4, E5, E6, F4, F5, F6]),
permutation(['1','2', '3', '4', '5', '6', '7', '8', '9'], [D7, D8, D9, E7, E8, E9, F7, F8, F9]),
permutation(['1','2', '3', '4', '5', '6', '7', '8', '9'], [G1, G2, G3, H1, H2, H3, I1, I2, I3]),
permutation(['1','2', '3', '4', '5', '6', '7', '8', '9'], [G4, G5, G6, H4, H5, H6, I4, I5, I6]),
permutation(['1','2', '3', '4', '5', '6', '7', '8', '9'], [G7, G8, G9, H7, H8, H9, I7, I8, I9]).
但是,当我运行查询时:
solve(X,[_,7,2,4,_,_,_,_,1,_,8,_,7,_,_,3,2,_,6,3,1,_,_,_,7,_,_,_,_,_,5,2,_,_,1,4,_,_,5,9,_,4,6,_,_,8,4,_,_,3,7,_,_,_,_,_,9,_,_,_,2,5,3,_,6,8,_,_,5,_,7,_,2,_,_,_,_,9,4,6,_]
)。
程序刚刚挂起。谁能看到我犯了什么错误?感谢。
编辑:注意到查询中的错误,但程序在修复后仍然挂起
答案 0 :(得分:4)
Permute往往是一项昂贵的操作,而CLPFD实际上还有其他一些非常适合数独解决的操作。请注意,CLPFD代表有限域上的约束限制编程。 CLPFD旨在让您记下有关许多变量域的事实,然后尝试一次解决所有变量。
这与你所写的内容相反,每个permutation
实际上是一个独立的,命令性的陈述。这意味着它找到第一行的解决方案,然后是第二行......,直到它最终到达失败的列,并且在尝试不同的组合之前必须进行大量的回溯和重试仅用于第一行 ...然后它将尝试已经失败的剩余行的所有组合。我想你可以很快看到它变得如此昂贵。
label
同时解决所有这些问题。这是懒惰评价的缩影。要编写类似于使用置换的方法,您需要命令in/ins
和all_different
。
in
/ ins
:这些允许您将数字域应用于变量(对于单个变量,列表为ins)。 X ins 1..9
告诉CLPFD X的所有成员必须具有1到9之间的值。all_different
:约束给定列表的每个元素的域,与给定列表的所有其他元素不相等。基本上,列表中的每个元素都必须不同。(我第三次说这个但是它仍然非常重要)这些操作都没有告诉解释器进行任何类型的花式/现场计算。他们只是告诉CLPFD将关于其变量约束的一些事实抛入其约束存储中,并将它们保存在那里直到你最后要求答案。
对于数独,你想要:
答案 1 :(得分:1)
运行独立排列是一项非常繁重的计算。打开系统监视器,查看CPU使用率级别。 100%?
对permutation/2
的每次独立调用均适用于分离。我们应该首先使用共享变量创建结构来保存我们的结果 - 列表。然后排列将对共享结构起作用,并且重复项将更早被拒绝,从而简化了计算。
sudoku( Rows ):-
Rows = [ [A1,B1,C1,D1,E1,F1,G1, ...],
[A2,B2,C2,D2,E2,F2,G2, ...],
[A3,B3,C3,D3,E3,F3,G3, ...] ...],
Cmns = [ [A1,A2,A3 ...],
[B1,B2,B3 ...] ...],
Boxs = [ [A1,B1,C1,A2,B2,C2,A3,B3,C3],
[D1,E1,F1,D2,E2,F2,D3,E3,F3] ...],
findall(X, between(1,9,X), L),
Rows = [R1,R2,R3 ...],
Cmns = [K1,K2,K3 ...],
Boxs = [X1,X2,X3 ...],
maplist( permutation(L), [R1,X1,R2,X2,K1,R3,X3,K2, ...]).
这会更快,因为失败的排列在此过程中会被反驳。
编辑:不,它似乎没什么帮助。独立permutation
s的开销太高。这需要更加细化。使用permute
代替permutation
,
selecting([], S, S).
selecting([A|B], S, R):- select(A,S,S2), selecting(B,S2,R).
permute(L,X):- partition(var, X, Vs, Ns),
selecting(Ns,L,L2), permutation(L2,Vs).
似乎有点帮助,但并不多。我现在可以相对轻松地获得maplist( permute(L), [X1,X2,R1,R2,R3,X3,K1,K2,K3,X4,R4,K4,X5,X6])
,但添加R5
会使运行时间加倍,并且在进一步添加K5
之后,它将在7倍的时间内完成。