2

假设有五个办公室的表格。每个办公室都有颜色、部门、电脑、饮料、手机和从 1 到 5 的特定位置:

桌子在这里

我试图为 Prolog 提供以下线索,以便它可以正确填写表格:

  1. 喝牛奶的人在 3 号办公室。
  2. 在公共关系('pubrel')部门工作的人在办公室#1。
  3. 公关部的人在蓝色办公室旁边。
  4. 绿色办公室在粉红色办公室的右侧。
  5. 绿色办公室的人正在喝咖啡。
  6. 黄色办公室里的人有一部黑莓手机,黄色办公室就在那个拥有windows7 pc的人的办公室旁边。
  7. 在红色办公室工作的人来自'cs'部门。
  8. 'ode' 部门的人有 mac_pro 电脑。
  9. 财务('fin')部门的人正在喝茶。
  10. 拥有 iphone 的人也拥有 mac_air 电脑。
  11. 拿着诺基亚手机的男人旁边是有上网本的男人。
  12. 拥有安卓手机的男人正在喝橙汁。
  13. 供应部门的人有一部爱立信电话。

我在用着

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

我将这些线索定义为“规则”,它们都应该同时起作用。

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

最后,of(X,Y)如果该位置Y尚未被另一个X. 例如,of(color(red),3)如果没有其他颜色捕获点 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))

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

4

1 回答 1

1

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

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

你只会在消极的情况下建立你的知识。您应该以某种方式在某个地方持有积极的知识:“办公室 1 是粉红色的”,而不是“办公室 1 既不是绿色也不是黄色等”。 (1)

您只有五种颜色可供使用;如果不是这四个中的任何一个,它必然是第五个。

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

而“next-to”的意思是,(例如“公关部的人在蓝色办公室旁边。”),

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

of应该编码,以便它以某种方式记录知识。在链接的答案或标签下有很多例子来说明如何实现这一点。


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

    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谓词),直到只剩下一种可能性(如数独)。也许这就是你的想法。您仍然会保持您的积极知识(即使表达消极),这是每个办公室当前所有可能选择的列表。

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

于 2016-02-03T01:16:39.617 回答