Prolog A「;建筑";序言中的谜语

Prolog A「;建筑";序言中的谜语,prolog,zebra-puzzle,Prolog,Zebra Puzzle,我想用序言解一个谜。谜语是: 有两栋建筑,每栋都有树型公寓(每层公寓):一栋公寓有3个房间,一栋公寓有4个房间,一栋公寓有5个房间 达娜、乔尼和诺亚住在1号楼。 罗恩、盖尔和阿隆住在2号楼 乔尼公寓比达娜和诺亚高。这意味着他住在1号楼的三楼。 诺亚和盖尔住在同一层楼(不同的建筑)。 罗恩比阿伦多了一个房间。 罗恩住在盖尔楼上一层。 2号楼最高的公寓是5个房间的公寓 我需要找到每个人住在哪一层 我写了这段代码: solve([dana,building1,F1,R1],[noah,building

我想用序言解一个谜。谜语是: 有两栋建筑,每栋都有树型公寓(每层公寓):一栋公寓有3个房间,一栋公寓有4个房间,一栋公寓有5个房间

达娜、乔尼和诺亚住在1号楼。 罗恩、盖尔和阿隆住在2号楼

乔尼公寓比达娜和诺亚高。这意味着他住在1号楼的三楼。 诺亚和盖尔住在同一层楼(不同的建筑)。 罗恩比阿伦多了一个房间。 罗恩住在盖尔楼上一层。 2号楼最高的公寓是5个房间的公寓

我需要找到每个人住在哪一层

我写了这段代码:

solve([dana,building1,F1,R1],[noah,building1,F2,R2],[joni,building1,F3,R3],[gale,building2,F4,R4],[ron,building2,F5,R5],[aron,building2,F6,R6]
      ) :-
   L =[[dana,building1,1,3],[dana,building1,1,4],[dana,building1,1,5],[dana,building1,2,3],[dana,building1,2,4],[dana,building1,2,5],[dana,building1,3,3],[dana,building1,3,4],[dana,building1,3,5]
,[noah,building1,1,3],[noah,building1,1,4],[noah,building1,1,5],[noah,building1,2,3],[noah,building1,2,4],[noah,building1,2,5],[noah,building1,3,3],[noah,building1,3,4],[noah,building1,3,5]
,[joni,building1,1,3],[joni,building1,1,4],[joni,building1,1,5],[joni,building1,2,3],[joni,building1,2,4],[joni,building1,2,5],[joni,building1,3,3],[joni,building1,3,4],[joni,building1,3,5]
,[gale,building1,1,3],[gale,building1,1,4],[gale,building1,1,5],[gale,building1,2,3],[gale,building1,2,4],[gale,building1,2,5],[gale,building1,3,3],[gale,building1,3,4],[gale,building1,3,5]
,[ron,building1,1,3],[ron,building1,1,4],[ron,building1,1,5],[ron,building1,2,3],[ron,building1,2,4],[ron,building1,2,5],[ron,building1,3,3],[ron,building1,3,4],[ron,building1,3,5]
,[aron,building1,1,3],[aron,building1,1,4],[aron,building1,1,5],[aron,building1,2,3],[aron,building1,2,4],[aron,building1,2,5],[aron,building1,3,3],[aron,building1,3,4],[aron,building1,3,5]],
   F3 > F2,
   F3>F1,
   F2 == F4,
   R5 == R6-1,
   F5 == F4+1,
   (F4 == 3, R4 == 5;F5 == 3, R5 == 5; F6 == 3, R6 == 5),
   del([dana,building1,F1,R1],L,List1),
   del([noah,building1,F2,R2],List1,List2),
   del([joni,building1,F3,R3],List2,List3),
   del([gale,building2,F4,R4],List3,List4),
   del([ron,building2,F5,R5],List4,List5),
   del([aron,building2,F6,R6],List5,_).

del(X,L,L1) :-
   remove(X,L,L1).
但当我执行查询时:

solve([dana,building1,F1,R1],[noah,building1,F2,R2],[joni,building1,F3,R3],[gale,building2,F4,R4],[ron,building2,F5,R5],[aron,building2,F6,R6]).
我得到:

"Error 22 : Instantiation Error"
有人吗? 我不明白我做错了什么。

我终于做到了。 答案是:

solve([dana,building1,F1,R1],
      [noah,building1,F2,R2],
      [joni,building1,F3,R3],
      [gale,building2,F4,R4],
      [ron,building2,F5,R5],
      [aron,building2,F6,R6]) :-
   assign([1,2,3],[F1,F2,F3]),
   assign([1,2,3],[F4,F5,F6]),
   assign([3,4,5],[R1,R2,R3]),
   assign([3,4,5],[R4,R5,R6]),
   F3 > F2,
   F3>F1,
   F2 =:= F4,
   R5 =:= R6-1,
   F5 =:= F4+1,
   (  F4 =:= 3, R4 =:= 5
   ;  F5 =:= 3, R5 =:= 5
   ;  F6 =:= 3, R6 =:= 5
   ).

assign(_,[]).
assign(Digs,[D|Vars]):-
   del(D,Digs,Digs1),
   assign(Digs1,Vars).

del(X,L,L1):-
   remove(X,L,L1).

您希望执行的查询是什么?这些(你发布的)是你所有的事实和规则吗?如果你试图从你的目标中画一棵证明树,结果就会出现tono@DebasishJana我在问题的末尾添加了我执行的查询,我发布的是我所有的事实和规则。我将尝试绘制一个证明树…(这与Alt有一些相似之处,尽管它不完全是一个)@false我认为这非常有用:)谢谢。重写您的解决方案以使用
库(clpfd)
?(在SICStus、SWI、YAP和更多版本中提供)@false-Good idea。谢谢。然后您可以在
assign/3
之前声明类似
F3>F2
的内容!这样可以避免很多回溯。然后,您还可以使用CLP(FD)约束来表示赋值:
assignment(Ns,Vs):-pairs\u keys\u value(pairs,Ns,Os)、maplist(=(1),Os)、global\u基数(Vs,pairs)。
这进一步有助于避免回溯。只有代码的答案很少是好的。您可能希望移动注释并将其作为解释代码的段落。
% all zebra-like problems solved in CLP or ASP very easy.
% ECLiPSe CLP
:-lib(ic).
ap(Apart1,Rooms1,Apart2,Rooms2) :-
        Apart1 = [Dana,Joni,Noah], Rooms1 = [DanaRm,JoniRm,NoahRm],
        Apart2 = [Ron,Gale,Aron], Rooms2 = [RonRm,GaleRm,AronRm],
        Apart1 :: [1..3], Rooms1 :: [3..5],
        Apart2 :: [1..3], Rooms2 :: [3..5],
        alldifferent(Apart1), alldifferent(Rooms1),
        alldifferent(Apart2), alldifferent(Rooms2),
        Joni #= 3, % so Dana Noah = 1..2
        Noah #= Gale,
        RonRm #= AronRm + 1, % so RonRm = 4..5 and AronRm=3..4
        Ron #= Gale + 1, % so Ron = 2..3 and Gale=1..2
    % building2 3floor is 5room. Gale cant be 3. so Ron=3 or Aron=3.
    % but AronRm cant be 5 so Aron cant be 3. So Ron is 3 and RonRm is 5.
    % ...but in search of holy declarativity lets Prolog to figure that :-)
        ( Ron #= 3, RonRm #= 5
        ; Gale #= 3, GaleRm #=5
        ; Aron #= 3, AronRm #=5
        ),
        labeling(Apart1),labeling(Rooms1),
        labeling(Apart2),labeling(Rooms2).
solve:-
        ap(A1,R1,A2,R2),
        write(A1),write(R1),write(A2),write(R2),nl.
run :-
        statistics(hr_time, Start),
        (\+ (solve, false)),
        statistics(hr_time, End), Time is End - Start,
        write('All solutions found in '), write(Time), write(' seconds'), nl.
/*  hardware: pentium4-1mb-3.0ggz at ddr1-333mhz
    software: eclipse-6.2.24 on slackware-14.0
    [eclipse 1]: [apart].
    apart.pl   compiled 4160 bytes in 0.18 seconds
    [eclipse 2]: run.
    [1, 3, 2][3, 4, 5][3, 2, 1][5, 3, 4]
    [1, 3, 2][3, 5, 4][3, 2, 1][5, 3, 4]
    [1, 3, 2][4, 3, 5][3, 2, 1][5, 3, 4]
    [1, 3, 2][4, 5, 3][3, 2, 1][5, 3, 4]
    [1, 3, 2][5, 3, 4][3, 2, 1][5, 3, 4]
    [1, 3, 2][5, 4, 3][3, 2, 1][5, 3, 4]
    All solutions found in 0.00655293464660645 seconds
more or less, we have only 1 solution. but we not have any info about
size of rooms in first building. and because of that we just get all 6
possibly combinations for it.
*/