Prolog 爱因斯坦之谜序言

Prolog 爱因斯坦之谜序言,prolog,logic,zebra-puzzle,Prolog,Logic,Zebra Puzzle,我需要一些帮助,为我的人工智能课做序言作业。问题是为爱因斯坦的谜题编写prolog代码。我知道如何写在我自己的作业,但有一些限制在家庭作业 there are 5 houses the Englishman lives in the red house the Spaniard owns the dog coffee is drunk in the green house the Ukrainian drinks tea the green house is immediately

我需要一些帮助,为我的人工智能课做序言作业。问题是为爱因斯坦的谜题编写prolog代码。我知道如何写在我自己的作业,但有一些限制在家庭作业

 there are 5 houses
 the Englishman lives in the red house
 the Spaniard owns the dog
 coffee is drunk in the green house
 the Ukrainian drinks tea
 the green house is immediately to the right of the ivory house
 the Old Gold smoker owns snails
 Kools are smoked in the yellow house
 milk is drunk in the middle house
 the Norwegian lives in the first house
 the man who smokes Chesterelds lives in the house next to the man with the fox
 3 Kools are smoked in the house next to the house where the horse is kept
 the Lucky Strike smoker drinks orange juice
 the Japanese smokes Parliaments
 the Norwegian lives next to the blue house
我知道我需要使用房屋清单,因为它们是订购的。我也想用这个列表来描述房子的特征,但我遇到了一个问题

我打算使用匿名变量house(英国人,瑞德,,,,,)。但是我不知道如何在作业中解释这个

 there are 5 houses
 the Englishman lives in the red house
 the Spaniard owns the dog
 coffee is drunk in the green house
 the Ukrainian drinks tea
 the green house is immediately to the right of the ivory house
 the Old Gold smoker owns snails
 Kools are smoked in the yellow house
 milk is drunk in the middle house
 the Norwegian lives in the first house
 the man who smokes Chesterelds lives in the house next to the man with the fox
 3 Kools are smoked in the house next to the house where the horse is kept
 the Lucky Strike smoker drinks orange juice
 the Japanese smokes Parliaments
 the Norwegian lives next to the blue house
以下是限制条件: 应使用以下二进制谓词符号:

owns(N,Pet)
smokes(N, Cigarette).
drinks(N, Drink).
除此之外,您可以自由使用任意数量的谓词

下面是我如何初始化事实,但我不知道如何在这种情况下制定规则

next_to(X,Y) :- right_of(X,Y); right_of(Y,X).

owns(spaniard, dog).
drinks(ukrainian, tea).
smokes(japanese, parliaments).
right_of(ivory, green).
lives(englishman, red).
owns(X, snail) :- smokes(X, old_gold).
smokes(X, kools) :- owns(X, yellow).
smokes(X, lucky_strike) :- drinks(X, orange_juice).
drinks(X, coffee) :- owns(X, green_house).
这有点道理,但同时看起来完全错了。我想我拿着这个哪儿也去不了/

致力于用CLP(FD)解决此类难题。但是,CLP(FD)的全部功能在这里是多余的:当您充分描述了约束条件时,您可以通过搜索整个解决方案空间来有效地解决分配问题

解决方案将由5个房屋组成,其中每个属性满足描述施加的所有约束

请注意对每个属性使用完全相同的符号(即绿色和绿色房子是错误的,请选择其中一个)

下一个似乎也是错误的:如果你的数字是从1到5,这个数字可以被计算或枚举,但它指的是最近的邻居

完成“解决方案搜索空间”数据表示,如

Problem = [
 house(1, Nationality1, Color1, Pet1, Drinks1, Smokes1),
 house(2, Nationality2, Color2, Pet2, Drinks2, Smokes2),
 ...
],
% place constraints
member(house(_, englishman, red, _, _, _), Problem),
member(house(_, spaniard, _, dog, _, _), Problem),
...
member/2这是一个更简单的Prolog内置程序,但在这种情况下就足以解决这个问题:当所有约束都已发布时,变量将绑定到适当的值。关键是成员能够非确定性地选择解决方案的成员(duh)

因此,当您需要在两个不同的元素之间表示约束时,调用2次成员,并将约束放置在适当的变量之间:即

抽栗子的人住在和狐狸在一起的人旁边的房子里

将被翻译为

....,
member(house(N, _, _, _, _, chesterelds), Problem),
member(house(M, _, _, fox, _, _), Problem),
next_to(N, M),
...
当以这种方式表达许多约束时,注意符号标识:可以在单独的过程中对每个谓词进行编码,以避免不必要的别名。但是couterpart 这也是正确的:如果同一个符号所涉及的约束不止一个,则有必要绕过该符号,以缩小搜索范围

我将让您思考“几何”谓词的正确表示方式:next_to和right_of可以被枚举,或通过算术表示。

这个谜题(也称为斑马谜题)以前在Stackoverflow上讨论过很多次,请参见例如:


序言翻译可以是直截了当的,一条又一条的规则,仍然遵循。这里是房屋属性的领域;室内属性由人类程序员确定,域是实际居住的房屋,这允许非常简洁的编码

换句话说,不同之处在于符号:一个复杂的符号已经把我们带到了一半,但发明它并遵循它的是一个人(就像程序员必须在第一宫的规范中,在适当的参数位置直接写下挪威语),而不是一台计算机

在这里,我们试图按照家庭作业的限制,尽可能少地向代码中注入人类知识。(当然,任何事情都有争议,避免人为干扰的最终方法是一个以英文文本为输入的计算机程序……这将再次受到批评,因为该程序是如何专门针对这一特定谜题或谜题类型等找到解决方案的。)

我们用自上而下的方式编码。显然,这个问题不存在了。应该是“谁喝水?谁拥有斑马?”:

斑马(Z、W、HS):- 长度(HS,5),%国家?颜色那是什么?以后再定义它。。。 成员(H1,HS),国家(H1,英语),颜色(H1,红色), 成员(H2,HS),国家(H2,spa),拥有(H2,狗), 会员(H3,HS)、饮料(H3,咖啡)、颜色(H3,绿色), 成员(H4,HS)、国家(H4,ukr)、饮料(H4,茶), (B,A,HS)的右上角,颜色(A,象牙色),颜色(B,绿色), 成员(H5,HS),烟雾(H5,oldgold),拥有(H5,蜗牛), 成员(H6,HS),烟雾(H6,kools),颜色(H6,黄色), 中等(C,HS),饮料(C,牛奶), 第一(D,HS),国家(D,nor), 仅次于(E,F,HS),smoke(E,chester),拥有(F,fox), 其次是烟(G,H,HS),拥有(H,马), 会员(H7,HS)、吸烟(H7,幸运)、饮料(H7,橙色), 成员(H8,HS),国家(H8,jpn),烟雾(H8,parlamt), 其次是(I,J,HS),国家(I,nor),颜色(J,blue), 会员(W,HS),饮料(W,水), 成员(Z,HS),拥有(Z,斑马)。 (B,A,HS)的右_:-追加(u,[A,B | HS],HS)。 (A,B,HS)的下一位:-在(B,A,HS)的右边;(A,B,HS)中的右。 中间(A,[[uu,[uu,A,[uu,[uu])。 第一(A,[A |)]。 国家(H,V):-attr(H,V)。 拥有(H,V):-attr(H,拥有-V)。%选择一个属性 烟雾(H,V):-attr(H,烟雾-V)。%从可扩展记录H 颜色(H,V):-attr(H,颜色-V)。%房屋属性 饮料(H,V):-attr(H,饮料-V)。%哪个是房子 属性(房屋,属性值):- memberchk(Attr-X,House),%唯一属性名称 X=值。 测试,使用故障驱动回路执行彻底搜索

3?-时间((斑马),地图列表(国家,[Z,W],R),书写(