Einsteins Riddle Prolog

Gas*_*sim 14 logic artificial-intelligence prolog zebra-puzzle

我的AI课程需要一些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
Run Code Online (Sandbox Code Playgroud)

我知道我需要使用列表作为房屋,因为它们是订购的.我也想用列表作为房子特征,但我在这里遇到了问题.

我打算使用匿名变量house(englishman,red,_,_,_).但我不知道如何解释这个作业.

以下是约束:您应该使用以下二元谓词符号:

owns(N,Pet)
smokes(N, Cigarette).
drinks(N, Drink).
Run Code Online (Sandbox Code Playgroud)

除此之外,您可以自由使用任意数量的谓词.

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

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).
Run Code Online (Sandbox Code Playgroud)

它有点意义,但同时看起来完全错误.我不认为我可以去任何地方.:/

Cap*_*liC 11

该网站致力于用CLP(FD)解决这些难题.但是CLP(FD)的全部功能在这里是过度的:当您充分描述约束时,可以有效地搜索整个解决方案空间.

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

请注意为每个属性使用相同的符号(即greengreen_house是错误的,请选择其中一个).

next_to似乎也是错误的:如果您从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),
...
Run Code Online (Sandbox Code Playgroud)

member/2它是更简单的Prolog内置,但在这种情况下足以解决问题:当所有约束都已发布时,变量将绑定到适当的值.关键是成员非确定性地选择解决方案的成员(duh)的能力.

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

吸食切斯特雷德的男人和狐狸一起住在旁边的房子里

将被翻译成

....,
member(house(N, _, _, _, _, chesterelds), Problem),
member(house(M, _, _, fox, _, _), Problem),
next_to(N, M),
...
Run Code Online (Sandbox Code Playgroud)

当以这种方式表达许多约束时,要注意符号标识:在单独的过程中对每个谓词进行编码可能很有用,以避免不适当的别名.但是couterpart也是如此:如果超过约束涉及相同的符号,则需要传递符号,以缩小搜索范围.

我将让你考虑'几何'谓词的正确表示:next_to和right_of可以枚举,或通过算术表示.