用 prolog 解决 Caliban 问题 [英] Solving Caliban problems with prolog

查看:16
本文介绍了用 prolog 解决 Caliban 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  1. Brown、Clark、Jones 和 Smith 是 4 位为他们服务的重要公民作为建筑师、银行家、医生和律师的社区,尽管不一定分别.

  1. Brown, Clark, Jones and Smith are 4 substantial citizens who serve their community as achitect, banker, doctor and lawyer, though not necessarily respectively.

布朗比琼斯更保守,但比史密斯更自由,比比他年轻并且拥有比克拉克年长的男性收入更高.

Brown, who is more conservative than Jones but more liberal than Smith, is a better golfer than the men who are younger than he is and has a larger income than the men who are older than Clark.

比建筑师挣得多的银行家也不是最年轻的也不是最老的.

The banker, who earns more than the architect, is neither the youngest nor the oldest.

打高尔夫球比律师差的医生也没有那么保守比建筑师.

The doctor, who is a poorer golfer than the lawyer, is less conservative than the architect.

正如所料,最年长的男人是最保守的,并且拥有收入最高,最年轻的人是最好的高尔夫球手.

As might be expected, the oldest man is the most conservative and has the largest income, and the youngest man is the best golfer.

每个人的职业是什么?

提示:根据财富、能力、相对年龄等对人进行排名使用数字 1,2,3,4 注意说明 1 是否代表,例如,最小的或最年长的.这样做可以使比较易于编码.

hint: to rank people for weath, ability, relative age, etc use the numbers 1,2,3,4 Be careful to state whether 1 represents, e.g., youngest or oldest. Doing this makes comparisons easy to code.

To code(如下)将线索给出的所有关系解释为列表的列表,其中每个列表定义了

To code (as follows) interprets all the relationships, given by the clues, as a list of lists, wherein each list defines the

 %[profession,surname,politics,relative_age, relative_salary, golf_ability]:    

profession(L) :- L = [[_,'Brown',_,_,_,_],[_,'Jones',_,_,_,_],[_,'Clark',_,_,_,_],
    [_,'Smith',_,_,_,_]],
member([_,'Brown',P1,A6,M3,G3],L),
member([_,'Jones',P2,_,_,_],L),
member([_,'Clark',_,A3,_,_],L),
member([_,'Smith',P3,_,_,_],L),
    moreconservative(P1,P2),
    moreliberal(P1,P3),
    bettergolfer(G3,younger(_,A6)),
    richer(M3,older(_,A3)),
member(['banker',_,_,A1,M1,_],L),
member(['architect',_,P5,_,M2,_],L),
    richer(M1,M2),
    (A1 = 2;A1 = 3),
member(['doctor',_,P4,_,_,G1],L),
member(['lawyer',_,_,_,_,G2],L),
    worsegolfer(G1,G2),
    moreliberal(P4,P5),
member([_,_,4,4,4,_],L),
member([_,_,_,1,_,4],L).

我这样定义 relative_politics、relative_salary、relative_age 和 golf_ability 关系

I define the relative_politics,relative_salary,relative_age, and golf_ability relationships like so

EG:

    richer(4,1).
    moreconservative(4,1).
    poorer(1,4).
    poorer(1,3).

它适用于所有关系.

我想我已经忠实地将所有线索翻译成 prolog,但是当我查询数据库时它只是说失败.例如:

I think I have faithfully translated all of the clues to prolog but it just says fail when I query the database. EG:

   ?- profession(L).
    fail.

我正在使用 NU Prolog.我想知道我在翻译线索时是否出错,或者我遗漏了数据库满足列表 L 的所有条件所需的事实.

I am using NU Prolog. I'm wondering if I made an error in my translation of the clues or I omitted a fact that is needed for the database to satisfy all the conditions of the list L.

推荐答案

bettergolfer(G3,younger(_,A6)) ...它不起作用这样,在 Prolog 中.相反,有这个

bettergolfer(G3,younger(_,A6)) ... it doesn't work this way, in Prolog. Instead, have this

   (  member( X,L), age(X,AX), golf(X,GX),
      (  younger(AX,A6) -> better_golfer(G3,GX) ; true )),
   .....

age( [_,_,_,A,_,_],A).
golf([_,_,_,_,_,G],G).
.....

这意味着,所有比布朗年轻的人(包括)必须是比他更穷的高尔夫球手.

this means, all the persons (including none) that are younger than Brown, must be poorer golfers than he is.

这里也有一个问题.由于我们被告知比布朗年轻的人,这意味着必须存在至少一个这样的人(与数学中的 蕴涵定义).我们也必须对此进行编码.例如,

There is a catch here, too. Since we're told about the men younger than Brown, it means there must exist at least one such man (unlike in the mathematical definition of implication). We have to code this too. For example,

    ( member(X,L), age(X,AX), younger(AX,A6) -> true ),
    .....

(当然为新的日志变量使用唯一的名称).您必须对 richer(M3,older(_,A3)) 进行相同的转换.

(using unique names for the new logvars of course). You'll have to make the same transformation for your richer(M3,older(_,A3)).

BTW 好主意,以生成方式定义比较谓词:

Great idea BTW, to have the comparison predicates defined in a generative fashion:

poorer(1,2). 
poorer(1,3). 
poorer(1,4). 
poorer(2,3). 
poorer(2,4). 
poorer(3,4).
richer(A,B):- poorer(B,A)

如果您将它们定义为算术比较,poorer(A,B):- A<B.,您可能会遇到未实例化变量的问题(最近 在这里讨论过).

If you were to define them as arithmetic comparisons, poorer(A,B):- A<B., you could potentially run into problems with uninstantiated variables (as recently discussed here).

这篇关于用 prolog 解决 Caliban 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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