在Prolog中填一个table,给定具体线索(爱因斯坦谜语)

Filling a table in Prolog, given specific clues (Einstein riddle)

假设 table 有五个办公室。每个办公室有一个颜色,一个部门,一台电脑,一杯饮料,一部手机和一个从1到5的具体职位:

我正在尝试为 Prolog 提供以下线索,以便它可以正确填写 table:

  1. the man drinking milk is in office #3.
  2. the man working in public relations ('pubrel') department is in office #1.
  3. the man from public relations department is next to the blue office.
  4. the green office is on the right of the pink office.
  5. the man in green office is drinking coffee.
  6. the man in the yellow office has a blackberry cellphone, the yellow office is next to the office of the man who has a windows7 pc.
  7. the man working in the red office is from 'cs' department.
  8. the man from 'ode' department has mac_pro pc.
  9. the man from finance ('fin') department is drinking tea.
  10. the man who has an iphone also has a mac_air pc.
  11. the man with a nokia cellphone is next to the man who has a netbook pc.
  12. the man who has an android phone is drinking orange juice.
  13. the man from supplies department has an ericsson phone.

我正在使用

of(X,Y) 

其中 X 是办公室的属性,Y 是所述办公室的位置。 例如:

of(color(yellow),1) -->office with yellow color is office #1
of(dept(cs),3) -->employee from cs department is in office #3

我将这些线索定义为 'rules',它们应该同时起作用。

rule1:-of(drink(milk),3),perm(drink(milk),3).
rule2:-of(dept(relations),1),perm(dept(relations),1).
rule3:-of(dept(relations),1),of(color(blue),2),perm(dept(relations),1),perm(color(blue),2).
rule3:-of(dept(relations),2),of(color(blue),3),perm(dept(relations),2),perm(color(blue),3).
rule3:-of(dept(relations),3),of(color(blue),4),perm(dept(relations),3),perm(color(blue),4).
rule3:-of(dept(relations),4),of(color(blue),5),perm(dept(relations),4),perm(color(blue),5).
rule4:-of(color(pink),1),of(color(green),2),perm(color(pink),1),perm(color(green),2).
rule4:-of(color(pink),2),of(color(green),3),perm(color(pink),2),perm(color(green),3).
rule4:-of(color(pink),3),of(color(green),4),perm(color(pink),3),perm(color(green),4).
rule4:-of(color(pink),4),of(color(green),5),perm(color(pink),4),perm(color(green),5).
rule5:-of(color(green),1),of(drink(coffee),1),perm(color(green),1),perm(drink(coffee),1).
rule5:-of(color(green),2),of(drink(coffee),2),perm(color(green),2),perm(drink(coffee),2).
rule5:-of(color(green),3),of(drink(coffee),3),perm(color(green),3),perm(drink(coffee),3).
rule5:-of(color(green),4),of(drink(coffee),4),perm(color(green),4),perm(drink(coffee),4).
rule5:-of(color(green),5),of(drink(coffee),5),perm(color(green),5),perm(drink(coffee),5).
rule6:-of(cell(blackberry),1),of(color(yellow),1),of(pc(win7),2),perm(cell(blackberry),1),perm(color(yellow),1),perm(pc(win7),2).
rule6:-of(cell(blackberry),2),of(color(yellow),2),of(pc(win7),3),perm(cell(blackberry),2),perm(color(yellow),2),perm(pc(win7),3).
rule6:-of(cell(blackberry),3),of(color(yellow),3),of(pc(win7),4),perm(cell(blackberry),3),perm(color(yellow),3),perm(pc(win7),4).
rule6:-of(cell(blackberry),4),of(color(yellow),4),of(pc(win7),5),perm(cell(blackberry),4),perm(color(yellow),4),perm(pc(win7),5).
rule6:-of(cell(blackberry),2),of(color(yellow),2),of(pc(win7),1),perm(cell(blackberry),2),perm(color(yellow),2),perm(pc(win7),1).
rule6:-of(cell(blackberry),3),of(color(yellow),3),of(pc(win7),2),perm(cell(blackberry),3),perm(color(yellow),3),perm(pc(win7),2).
rule6:-of(cell(blackberry),4),of(color(yellow),4),of(pc(win7),3),perm(cell(blackberry),4),perm(color(yellow),4),perm(pc(win7),3).
rule6:-of(cell(blackberry),5),of(color(yellow),5),of(pc(win7),4),perm(cell(blackberry),5),perm(color(yellow),5),perm(pc(win7),4).
rule7:-of(dept(cs),1),of(color(red),1),perm(dept(cs),1),perm(color(red),1).
rule7:-of(dept(cs),2),of(color(red),2),perm(dept(cs),2),perm(color(red),2).
rule7:-of(dept(cs),3),of(color(red),3),perm(dept(cs),3),perm(color(red),3).
rule7:-of(dept(cs),4),of(color(red),4),perm(dept(cs),4),perm(color(red),4).
rule7:-of(dept(cs),5),of(color(red),5),perm(dept(cs),5),perm(color(red),5).
rule8:-of(dept(ode),1),of(pc(mac_pro),1),perm(dept(ode),1),perm(pc(mac_pro),1).
rule8:-of(dept(ode),2),of(pc(mac_pro),2),perm(dept(ode),2),perm(pc(mac_pro),2).
rule8:-of(dept(ode),3),of(pc(mac_pro),3),perm(dept(ode),3),perm(pc(mac_pro),3).
rule8:-of(dept(ode),4),of(pc(mac_pro),4),perm(dept(ode),4),perm(pc(mac_pro),4).
rule8:-of(dept(ode),5),of(pc(mac_pro),5),perm(dept(ode),5),perm(pc(mac_pro),5).
rule9:-of(dept(fin),1),of(drink(tea),1),perm(dept(fin),1),perm(drink(tea),1).
rule9:-of(dept(fin),2),of(drink(tea),2),perm(dept(fin),2),perm(drink(tea),2).
rule9:-of(dept(fin),3),of(drink(tea),3),perm(dept(fin),3),perm(drink(tea),3).
rule9:-of(dept(fin),4),of(drink(tea),4),perm(dept(fin),4),perm(drink(tea),4).
rule9:-of(dept(fin),5),of(drink(tea),5),perm(dept(fin),5),perm(drink(tea),5).
rule10:-of(cell(iphone),1),of(pc(mac_air),1),perm(cell(iphone),1),perm(pc(mac_air),1).
rule10:-of(cell(iphone),2),of(pc(mac_air),2),perm(cell(iphone),2),perm(pc(mac_air),2).
rule10:-of(cell(iphone),3),of(pc(mac_air),3),perm(cell(iphone),3),perm(pc(mac_air),3).
rule10:-of(cell(iphone),4),of(pc(mac_air),4),perm(cell(iphone),4),perm(pc(mac_air),4).
rule10:-of(cell(iphone),5),of(pc(mac_air),5),perm(cell(iphone),5),perm(pc(mac_air),5).
rule11:-of(cell(nokia),1),of(pc(netbook),2),perm(cell(nokia),1),perm(pc(netbook),2).
rule11:-of(cell(nokia),2),of(pc(netbook),3),perm(cell(nokia),2),perm(pc(netbook),3).
rule11:-of(cell(nokia),3),of(pc(netbook),4),perm(cell(nokia),3),perm(pc(netbook),4).
rule11:-of(cell(nokia),4),of(pc(netbook),5),perm(cell(nokia),4),perm(pc(netbook),5).
rule11:-of(cell(nokia),2),of(pc(netbook),1),perm(cell(nokia),2),perm(pc(netbook),1).
rule11:-of(cell(nokia),3),of(pc(netbook),2),perm(cell(nokia),3),perm(pc(netbook),2).
rule11:-of(cell(nokia),4),of(pc(netbook),3),perm(cell(nokia),4),perm(pc(netbook),3).
rule11:-of(cell(nokia),5),of(pc(netbook),4),perm(cell(nokia),5),perm(pc(netbook),4).
rule12:-of(cell(android),1),of(drink(orangejuice),1),perm(cell(android),1),perm(drink(orangejuice),1).
rule12:-of(cell(android),2),of(drink(orangejuice),2),perm(cell(android),2),perm(drink(orangejuice),2).
rule12:-of(cell(android),3),of(drink(orangejuice),3),perm(cell(android),3),perm(drink(orangejuice),3).
rule12:-of(cell(android),4),of(drink(orangejuice),4),perm(cell(android),4),perm(drink(orangejuice),4).
rule12:-of(cell(android),5),of(drink(orangejuice),5),perm(cell(android),5),perm(drink(orangejuice),5).
rule13:-of(dept(supplies),1),of(cell(ericsson),1),perm(dept(supplies),1),perm(cell(ericsson),1).
rule13:-of(dept(supplies),2),of(cell(ericsson),2),perm(dept(supplies),2),perm(cell(ericsson),2).
rule13:-of(dept(supplies),3),of(cell(ericsson),3),perm(dept(supplies),3),perm(cell(ericsson),3).
rule13:-of(dept(supplies),4),of(cell(ericsson),4),perm(dept(supplies),4),perm(cell(ericsson),4).
rule13:-of(dept(supplies),5),of(cell(ericsson),5),perm(dept(supplies),5),perm(cell(ericsson),5).

然后将它们全部提供给一个谓词 'office' 如果它们可以一起应用则应该为真,以便正确形成 table。

office:-rule1,rule2,rule3,rule4...

我将perm(X,Y)定义为当属性X被允许捕获特定点Y时变为真的术语,这意味着X没有捕获另一个一个,因此是允许的。

perm(X,1):-not(of(X,2)),not(of(X,3)),not(of(X,4)),not(of(X,5)).
perm(X,2):-not(of(X,1)),not(of(X,3)),not(of(X,4)),not(of(X,5)).
perm(X,3):-....

最后,如果地点 Y 尚未被另一个 X 占用,则 of(X,Y) 应该变为真。 例如,如果没有其他颜色捕获点 3,of(color(red),3) 应该为真。

of(color(red),Y):-not(of(color(green),Y)),not(of(color(pink),Y)),
        not(of(color(blue),Y)),not(of(color(yellow),Y)).
of(color(green),Y):-not(of(color(red),Y)),not(of(color(pink),Y)),
        not(of(color(blue),Y)),not(of(color(yellow),Y)).
...
of(dept(cs),Y):-not(of(dept(ode),Y)),not(of(dept(supplies),Y)),
        not(of(dept(fin),Y)),not(of(dept(relations),Y)).
of(dept(ode),Y):-not(of(dept(cs),Y)),not(of(dept(supplies),Y)),
        not(of(dept(fin),Y)),not(of(dept(relations),Y)).
...

因此,perm(X,Y)of(X,Y) 应该在每个规则中同时为真。

它不起作用,可能是因为它确实有任何具体的开始。不知道什么时候

not(of(color(green),1))

会是真的。但是我被困住了,想不出一种方法来定义特定的开始。任何人都可以帮助解决如何让它工作吗?

你的方法永远行不通,因为它内置了无限递归。它的逻辑是循环的。它永远不会停止。仅限于两种颜色,你的定义是

of(color(red),Y):- not(of(color(green),Y)).
of(color(green),Y):- not(of(color(red),Y)).

你只会在消极方面积累知识。您应该以某种方式在某个地方掌握积极的知识:"the office 1 is pink",而不是 "the office 1 is not green nor yellow etc"。 (1)

你只有五种颜色可供选择;如果它是四个中的 none,它必然是第五个。

perm 似乎完全是多余的。如果您现在从您的谓词中得到关于办公室颜色的正面知识,那么如果它是红色,它就是红色。

而"next-to"表示,(例如"the man from public relations department is next to the blue office."),

of(dept(pubrel),X), of(color(blue),Y),
of(position(I),X), of(position(J),Y), 1 is abs (I-J).

of 应该编码,以便它以某种方式记录知识。在链接的答案中或标签 下有很多关于如何实现该目标的示例。


(1) (更新:) 另一方面,你可以写

    of(Rec1,color(not_green_nor_yellow),Y,RecN):- 
        not_of(Rec1,color(green),Y,Rec2),
        not_of(Rec2,color(yellow),Y,RecN).

想法是从记录所有可能性开始,然后将它们从记录中逐一删除(通过 not_of/4 谓词),直到只剩下一种可能性(如数独)。也许这就是你的想法。你仍然会保持你的积极知识(即使是消极地表达它),这是每个办公室当前所有可能选择的列表。

所以在一种范例下我们添加选择(因此在同一个办公室中同时添加黄色和绿色是矛盾的——一个办公室只能有一种颜色),而在另一种范例下我们移除它们(同时移除黄色和绿色是矛盾的)非常好,除非这会清空该办公室的剩余选择列表 - 办公室必须有某种颜色,一种颜色)。