Prolog Logic 与相邻的房间

Prolog Logic with adjacent rooms

这是我的问题:

Hunter, Laura, Addiley (jack), Ramey(Sally), and Arnie(Jim) all live in the same dorm with five adjacent bedrooms. Hunter doesn’t sleep in the 5th bedroom and Laura doesn’t sleep in the first bedroom. Arnie doesn’t sleep in the first or last bedroom, and he is not in an bedroom adjacent to Addiley or Laura. Ramey sleeps in some bedroom higher than Laura’s. Who sleeps in which bedrooms? Write a Prolog program to solve this problem.

Define what adjacency is, then what the bedrooms are, and then create a layout(X) that allows you to put in all the rules.


这是我目前的代码,我也尝试了一些变体:

adjcnt(X,Y) :- X = (Y+1;Y-1).

rooms([ bedroom(_, 1), bedroom(_, 2), bedroom(_, 3), bedroom(_, 4), bedroom(_, 5) ]).

layout(X) :- rooms(X),
        member( bedroom(hunter, V), X),
        member( bedroom(laura,  W), X),
        member( bedroom(arnie,  X), X),
        member( bedroom(ramey,  Y), X),
        member( bedroom(addiley,Z), X),

        V \= 5,
        W \= 1,
        X \= 1,
        X \= 5,
        X \= adjcnt(X,Z),
        X \= adjcnt(X,W),
        Y @> W.

主要问题是,我对相邻房间的计算是否正确?以及如何正确实施。当我尝试 运行 代码时,我不断收到 "NO"。感谢任何能帮助我的人!!

乍一看,你这里有错字

adjcnt(X,Y) :- X = (Y+1;Y-1).

因为 (=)/2 doesn't assign to X, but attempts to unify 它的两个参数。因此,它显然失败了。您很可能正在寻找

adjcnt(X,Y) :- X is Y+1; X is Y-1.

还有两件事:

  • 你使用 X 来做两件完全不同的事情:一个是房间结构,另一个是 arnies 房间的变量。建议:将room(X)替换为room(Rooms),其他需要的地方也替换为Rooms

  • 用于否定两个房间不相邻的 not 运算符应编码为 \+,例如\+adjcnt(X,Z).