1

我是 Prolog 的新手,我想编写poppler(Nums, Plate, Tastiness)一个包含 9 个数字的列表作为输入,如果可能,返回这些数字的排列,当Plate以行主要格式读取时,这些数字形成一个美味的波普勒板。

如果三行、三列和两条主对角线中每一行的 Poppler 之和相同,则称一个 Poppler 盘子好吃。这个共同的金额被称为它的美味。

例如,这是一个美味的波普勒盘子15

2 7 6

9 5 1

4 3 8

这是我的尝试:

size([], 0).
size([Head|T], N) :-
   size(T, N1),
   N is N1+1.

is_equal([U, V, W], [X, Y, Z], Sum) :-
    Sum is U + V + W,
    Sum is X + Y + Z.

poppler(Nums, Plate, Tastiness):- 
    size(Nums, 9),
    poppler(Nums, [A, B, C, D, E, F, G, H, I], Tastiness),
    member(A, Nums),
    member(B, Nums),
    member(C, Nums),
    member(D, Nums),
    member(E, Nums),
    member(F, Nums),
    member(G, Nums),
    member(H, Nums),
    member(I, Nums),
    is_equal([A, B, C], [D, E, F], Tastiness),
    is_equal([A, B, C], [G, H, I], Tastiness),
    is_equal([G, H, I], [D, E, F], Tastiness),
    is_equal([A, D, G], [B, E, H], Tastiness),
    is_equal([A, D, G], [C, F, I], Tastiness),
    is_equal([B, E, H], [C, F, I], Tastiness),
    is_equal([A, E, I], [C, E, G], Tastiness).

但这不起作用。我该如何解决?

4

3 回答 3

2

这是带有一些注释的代码的固定版本。在 SWI-Prolog 中测试。

它有效,但它真的很慢(对于您的示例将工作几分钟)。这是因为搜索空间很大,没有搜索空间剪枝。

您应该真正使用约束编程方法来解决这个问题 - 它以一种巧妙的方式修剪搜索空间,并且该程序可以立即运行。

% should really just use length/2
size([], 0).
size([Head|T],N) :- size(T,N1), N is N1+1.

% could use simpler version of this like "is_equal([X, Y, Z], Sum)"
is_equal([U, V, W], [X, Y, Z], Sum) :- Sum is U + V + W, Sum is X + Y + Z.

poppler(Nums, Plate, Tastiness) :- 
    size(Nums, 9),
    [A, B, C, D, E, F, G, H, I] = Plate,

    msort(Nums, Sorted),

    member(A, Nums),
    member(B, Nums),
    member(C, Nums),
    member(D, Nums),
    member(E, Nums),
    member(F, Nums),
    member(G, Nums),
    member(H, Nums),
    member(I, Nums),

    % Check if Plate is a permutation of Nums
    msort(Plate, Sorted),

    is_equal([A, B, C], [D, E, F], Tastiness),
    is_equal([A, B, C], [G, H, I], Tastiness),
    is_equal([G, H, I], [D, E, F], Tastiness),
    is_equal([A, D, G], [B, E, H], Tastiness),
    is_equal([A, D, G], [C, F, I], Tastiness),
    is_equal([B, E, H], [C, F, I], Tastiness),
    is_equal([A, E, I], [C, E, G], Tastiness).
于 2014-03-19T03:00:44.753 回答
1

看起来是一个用约束逻辑编程来解决的完美问题。

这是我在ECLiPSe CLP Prolog 中的解决方案(可以翻译到其他 Prolog 系统):

:- lib(gfd).

poppler(Nums, Plate, S) :-
   [A, B, C, D, E, F, G, H, I] = Plate,
   sorted(Nums, Sorted), sorted(Plate, Sorted),
   % rows
   A + B + C #= S,
   D + E + F #= S,
   G + H + I #= S,
   % colums
   A + D + G #= S,
   B + E + H #= S,
   C + F + I #= S,
   % diagonals
   A + E + I #= S,
   C + E + G #= S,
   labeling(Plate).

测试运行:

[eclipse]: poppler([1, 2, 3, 4, 5, 6, 7, 8, 9], Plate, 15).
Plate = [2, 7, 6, 9, 5, 1, 4, 3, 8]
于 2014-03-19T02:23:17.067 回答
0

我认为你的主要问题是使用 member/2 你产生的尝试比以后丢弃的要多得多您可以改为使用 permutation/2:

poppler0(Nums, Plate, Tastiness):-
    Plate = [A, B, C, D, E, F, G, H, I],
    permutation(Nums, Plate),
    is_equal([A, B, C], [D, E, F], Tastiness),
    is_equal([A, B, C], [G, H, I], Tastiness),
    is_equal([G, H, I], [D, E, F], Tastiness),
    is_equal([A, D, G], [B, E, H], Tastiness),
    is_equal([A, D, G], [C, F, I], Tastiness),
    is_equal([B, E, H], [C, F, I], Tastiness),
    is_equal([A, E, I], [C, E, G], Tastiness).

产量

?- numlist(1,9,L),poppler0(L,X,15).
L = [1, 2, 3, 4, 5, 6, 7, 8, 9],
X = [2, 7, 6, 9, 5, 1, 4, 3, 8] ;
L = [1, 2, 3, 4, 5, 6, 7, 8, 9],
X = [2, 9, 4, 7, 5, 3, 6, 1, 8] ;
...

而不是 member/3,select/3 将不重复:

poppler1(Nums, Plate, Tastiness):-
    Plate = [A, B, C, D, E, F, G, H, I],
    %permutation(Nums, Plate),
    select(A, Nums, Num1),
    select(B, Num1, Num2),
    select(C, Num2, Num3),
    select(D, Num3, Num4),
    select(E, Num4, Num5),
    select(F, Num5, Num6),
    select(G, Num6, Num7),
    select(H, Num7, Num8),
    select(I, Num8, []),
    is_equal([A, B, C], [D, E, F], Tastiness),
    is_equal([A, B, C], [G, H, I], Tastiness),
    is_equal([G, H, I], [D, E, F], Tastiness),
    is_equal([A, D, G], [B, E, H], Tastiness),
    is_equal([A, D, G], [C, F, I], Tastiness),
    is_equal([B, E, H], [C, F, I], Tastiness),
    is_equal([A, E, I], [C, E, G], Tastiness).

此外,由于现在的排列是“切片”的,我们可以提前“推送”一些测试,以使整体更快:

poppler2(Nums, Plate, Tastiness):-
    Plate = [A, B, C, D, E, F, G, H, I],
    select(A, Nums, Num1),
    select(B, Num1, Num2),
    select(C, Num2, Num3),
    select(D, Num3, Num4),
    select(E, Num4, Num5),
    select(F, Num5, Num6),
    is_equal([A, B, C], [D, E, F], Tastiness),
    select(G, Num6, Num7),
    select(H, Num7, Num8),
    select(I, Num8, []),
    is_equal([A, B, C], [G, H, I], Tastiness),
    is_equal([G, H, I], [D, E, F], Tastiness),
    is_equal([A, D, G], [B, E, H], Tastiness),
    is_equal([A, D, G], [C, F, I], Tastiness),
    is_equal([B, E, H], [C, F, I], Tastiness),
    is_equal([A, E, I], [C, E, G], Tastiness).

?- numlist(1,9,L),time(poppler0(L,X,15)).
% 642,293 inferences, 0.253 CPU in 0.256 seconds (99% CPU, 2540589 Lips)
L = [1, 2, 3, 4, 5, 6, 7, 8, 9],
X = [2, 7, 6, 9, 5, 1, 4, 3, 8] 
.

8 ?- numlist(1,9,L),time(poppler1(L,X,15)).
% 385,446 inferences, 0.217 CPU in 0.217 seconds (100% CPU, 1777885 Lips)
L = [1, 2, 3, 4, 5, 6, 7, 8, 9],
X = [2, 7, 6, 9, 5, 1, 4, 3, 8] 
.

9 ?- numlist(1,9,L),time(poppler2(L,X,15)).
% 48,409 inferences, 0.029 CPU in 0.029 seconds (100% CPU, 1643812 Lips)
L = [1, 2, 3, 4, 5, 6, 7, 8, 9],
X = [2, 7, 6, 9, 5, 1, 4, 3, 8] 

另一个小问题是某些总和被多次评估,这可能是由于您选择使用 is_equal/3 对测试进行编码。我会写

poppler3(Nums, Plate, Tastiness):-
    Plate = [A, B, C, D, E, F, G, H, I],
    select(A, Nums, Num1),
    select(B, Num1, Num2),
    select(C, Num2, Num3),
    sumlist([A, B, C], Tastiness),
    select(D, Num3, Num4),
    select(E, Num4, Num5),
    select(F, Num5, Num6),
    sumlist([D, E, F], Tastiness),
    select(G, Num6, Num7),
    sumlist([A, D, G], Tastiness),
    sumlist([C, E, G], Tastiness),
    select(H, Num7, Num8),
    sumlist([B, E, H], Tastiness),
    select(I, Num8, []),
    sumlist([G, H, I], Tastiness),
    sumlist([C, F, I], Tastiness),
    sumlist([A, E, I], Tastiness).

产生

?- numlist(1,9,L),time(poppler3(L,X,15)).
% 14,371 inferences, 0.004 CPU in 0.004 seconds (99% CPU, 3359784 Lips)
L = [1, 2, 3, 4, 5, 6, 7, 8, 9],
X = [2, 7, 6, 9, 5, 1, 4, 3, 8] 
.

但同样, sumlist/2 比要求的更通用,并且内联总和还有进一步的好处:

poppler4(Nums, Plate, Tastiness):-
    Plate = [A, B, C, D, E, F, G, H, I],
    select(A, Nums, Num1),
    select(B, Num1, Num2),
    select(C, Num2, Num3),
    A+B+C =:= Tastiness,
    select(D, Num3, Num4),
    select(E, Num4, Num5),
    select(F, Num5, Num6),
    D+E+F =:= Tastiness,
    select(G, Num6, Num7),
    A+D+G =:= Tastiness,
    C+E+G =:= Tastiness,
    select(H, Num7, Num8),
    B+E+H =:= Tastiness,
    select(I, Num8, []),
    G+H+I =:= Tastiness,
    C+F+I =:= Tastiness,
    A+E+I =:= Tastiness.

产量

?- numlist(1,9,L),time(poppler4(L,X,15)).
% 3,394 inferences, 0.002 CPU in 0.002 seconds (100% CPU, 1827856 Lips)
L = [1, 2, 3, 4, 5, 6, 7, 8, 9],
X = [2, 7, 6, 9, 5, 1, 4, 3, 8] 
.
于 2014-03-19T06:45:01.600 回答