爱因斯坦谜语序言 [英] Einsteins Riddle Prolog

查看:82
本文介绍了爱因斯坦谜语序言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的AI课的序言作业中,我需要一些帮助.问题是为爱因斯坦的难题编写序言代码.我知道如何自己写下来,但作业中有一些限制.

I need some help with a prolog homework for my AI class. The question is to write prolog code for einstein's puzzle. I know how to write it down in my own but there are some constraints in the homework.

 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

我知道我需要使用房屋清单,因为房屋是有序的.我也想将列表用于房屋特性,但是在这里遇到了问题.

I know that I need to use list for houses because they are ordered. I wanted to use list for the house characteristics too but I got a problem here.

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

I was going to use anonymous variables house(englishman, red, _, _, _). but I dont know how to interpret that for the homework.

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

Here are the constraints: you should use the following binary predicate symbols:

owns(N,Pet)
smokes(N, Cigarette).
drinks(N, Drink).

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

Other than that you are free to use any number of predicates.

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

here is how I initialized the facts but I dont know how to make the rules in this case

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).

有点道理,但同时看起来完全错误.我认为我无法用这个去任何地方. :/

it makes sense a little bit but it looks completely wrong at the same time. I don't think I can go anywhere with this one. :/

推荐答案

本网站致力于解决此类问题CLP(FD)拼图.但是CLP(FD)的全部功能在这里是过大的:在充分描述约束之后,可以有效地搜索整个解决方案空间,从而解决您的任务.

This site is devoted to solve such puzzles with CLP(FD). But the full power of CLP(FD) is overkill here: your assignment can be solved effectively searching the entire solution space when you have adequately described the constraints.

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

The solution will be composed of 5 houses, where each attribute satisfy all constraints imposed by description.

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

Be aware to use the very same symbol for each attribute (i.e. green and green_house is wrong, choose one of them).

next_to似乎也是错误的:如果您的数字是1到5,则可以计算或枚举,但它指的是直接邻居.

Also next_to seems wrong: if you number from 1 to 5, this can be computed or enumerated, but refers to the immediate neighbour.

因此,请完成解决方案搜索空间"的数据表示形式,例如

So complete the 'solution search space' data representation, something like

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).

member/2 it's the simpler Prolog builtin, but in this case suffices to solve the problem: when all constraints have been posted, the variables will bind to appropriate values. The key is the ability of member to non deterministically select a member (duh) of the solution.

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

So when you need to express a constraint between 2 different elements, invoke 2 times member, and place the constraints between appropriate variables: i.e.

抽切斯特尔德斯的男人住在狐狸旁边的房子里

the man who smokes Chesterelds lives in the house next to the man with the fox

将被翻译为

....,
member(house(N, _, _, _, _, chesterelds), Problem),
member(house(M, _, _, fox, _, _), Problem),
next_to(N, M),
...

以这种方式表达许多约束时,请注意符号标识:在单独的过程中对每个谓词进行编码可能会很有用,以避免不必要的混叠.但是库珀 也是这样:如果同一个符号涉及的不仅仅是一个约束,则有必要绕过该符号,以缩小搜索范围.

When expressing many constraints in such way, beware to symbols identity: could be useful to code each predicate in a separate procedure, to avoid undue aliasing. But the couterpart is also true: if the same symbol is involved in more than a constraint, will be necessary to pass around the symbol, to narrow down the search.

我将让您考虑几何"谓词的正确表示形式:next_to和right_of可以枚举,也可以通过算术表示.

I will let you to think about the right representation of 'geometric' predicates: next_to and right_of could be enumerated, or expressed by means of arithmetic.

这篇关于爱因斯坦谜语序言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆