您走在正确的轨道上:尽快实施约束,以修剪搜索空间。
问题是如何“拆分”排列过程,以便能够尽快修剪结果。
一个简单的方法:
diabolic([A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P]) :-
N0=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16],
R1=[A,B,C,D],select_list(N0,R1,N1),sum_list(R1,34),
R2=[E,F,G,H],select_list(N1,R2,N2),sum_list(R2,34),
R3=[I,J,K,L],select_list(N2,R3,N3),sum_list(R3,34),
R4=[M,N,O,P],select_list(N3,R4,[]),sum_list(R4,34),
sum_list([A,E,I,M],34),
sum_list([B,F,J,N],34),
sum_list([C,G,K,O],34),
sum_list([D,H,L,P],34),
sum_list([A,F,K,P],34),
sum_list([M,J,G,D],34).
select_list(X,[],X).
select_list(X,[H|T],Z) :- select(H,X,Y), select_list(Y,T,Z).
这仍然比 CLP(FD) 慢得多,但可能是一个起点......
编辑简单的代码改进。
原作表现:
?- forall(time(diabolic(L)),writeln(L)).
% 74,769,227 inferences, 23.739 CPU in 23.754 seconds (100% CPU, 3149688 Lips)
[1,2,15,16,12,14,3,5,13,7,10,4,8,11,6,9]
% 7,556,909 inferences, 2.396 CPU in 2.448 seconds (98% CPU, 3154252 Lips)
[1,2,15,16,13,14,3,4,12,7,10,5,8,11,6,9]
% 90,103,270 inferences, 28.475 CPU in 28.503 seconds (100% CPU, 3164265 Lips)
[1,2,16,15,13,14,4,3,12,7,9,6,8,11,5,10]
Action (h for help) ? aabort
内联 select_list/3
select_(N0,[A,B,C,D],N1) :-
select(A,N0,T0),
select(B,T0,T1),
select(C,T1,T2),
select(D,T2,N1).
diabol_1([A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P]) :-
N0=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16],
R1=[A,B,C,D],select_(N0,R1,N1),sum_list(R1,34),
R2=[E,F,G,H],select_(N1,R2,N2),sum_list(R2,34),
R3=[I,J,K,L],select_(N2,R3,N3),sum_list(R3,34),
R4=[M,N,O,P],select_(N3,R4,[]),sum_list(R4,34),
sum_list([A,E,I,M],34),
sum_list([B,F,J,N],34),
sum_list([C,G,K,O],34),
sum_list([D,H,L,P],34),
sum_list([A,F,K,P],34),
sum_list([M,J,G,D],34).
我们得到了一个小的改进:
?- forall(time(diabol_1(L)),writeln(L)).
% 65,282,719 inferences, 21.137 CPU in 21.195 seconds (100% CPU, 3088524 Lips)
[1,2,15,16,12,14,3,5,13,7,10,4,8,11,6,9]
% 6,607,508 inferences, 2.074 CPU in 2.075 seconds (100% CPU, 3186362 Lips)
[1,2,15,16,13,14,3,4,12,7,10,5,8,11,6,9]
% 78,691,563 inferences, 24.914 CPU in 24.928 seconds (100% CPU, 3158505 Lips)
[1,2,16,15,13,14,4,3,12,7,9,6,8,11,5,10]
Action (h for help) ? aabort
内联 sum_list/2 我们看到了进一步的小收获:
sum_([A,B,C,D]) :- A+B+C+D =:= 34.
diabol_2([A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P]) :-
N0=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16],
R1=[A,B,C,D],select_(N0,R1,N1),sum_(R1),
R2=[E,F,G,H],select_(N1,R2,N2),sum_(R2),
R3=[I,J,K,L],select_(N2,R3,N3),sum_(R3),
R4=[M,N,O,P],select_(N3,R4,[]),sum_(R4),
sum_([A,E,I,M]),
sum_([B,F,J,N]),
sum_([C,G,K,O]),
sum_([D,H,L,P]),
sum_([A,F,K,P]),
sum_([M,J,G,D]).
?- forall(time(diabol_2(L)),writeln(L)).
% 20,419,167 inferences, 10.425 CPU in 10.431 seconds (100% CPU, 1958699 Lips)
[1,2,15,16,12,14,3,5,13,7,10,4,8,11,6,9]
% 2,058,108 inferences, 1.046 CPU in 1.047 seconds (100% CPU, 1966993 Lips)
[1,2,15,16,13,14,3,4,12,7,10,5,8,11,6,9]
% 24,592,123 inferences, 12.462 CPU in 12.481 seconds (100% CPU, 1973394 Lips)
[1,2,16,15,13,14,4,3,12,7,9,6,8,11,5,10]
Action (h for help) ? aabort