Prolog 爱因斯坦之谜序言
我需要一些帮助,为我的人工智能课做序言作业。问题是为爱因斯坦的谜题编写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
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上讨论过很多次,请参见例如:
3?-时间((斑马),地图列表(国家,[Z,W],R),书写(