3

在 PyDatalog 中,我定义了以下断言:

#stations
assert_fact('station', 'A' ,'yellow') 
assert_fact('station', 'B' ,'yellow')
assert_fact('station', 'C' ,'yellow')
assert_fact('station', 'D' ,'yellow')
#sections
assert_fact('stretch', 'A' ,'B')
assert_fact('stretch', 'B' ,'C')
assert_fact('stretch', 'C', 'D')

我想询问数据库是否有办法从 A 到达 D。下面的代码应该可以工作,因为如果有办法,它会回答 set([()]),如果没有,它会回答 None。但这并没有给我对Z的不同评估的结果。我也想知道路线,例如:ABCD

load("""
route(X,Y) <= stretch(X,Y)
route(X,Y) <= stretch(X,Z) & route(Z,Y)
""")

我尝试过使用未绑定的值,但它只给了我第一次迭代的结果:

load("""
route(X,Y,P) <= stretch(X,Y) & (P==Y)
route(X,Y,P) <= stretch(X,P) & route(P,Y,Z)  
""")

我认为问题在于它在第一次迭代中只需要 P 。或者我应该使用聚合函数?我不太明白如何使用 concat ...

提前致谢。

4

1 回答 1

4

您需要一个带有变量的谓词,该变量包含两个端点之间的节点。您可以使用以下 route() 定义:

load("""
route(X,P, Y) <= stretch(X,Y) & (P==[])
route(X,P, Y) <= stretch(X,Z) & route(Z,P1,Y) & ~(Z in P1) & (P==[Z]+P1)
""")

print(pyDatalog.ask("route('A', P, 'D')"))
# prints set([(('B', 'C'),)])

从 pyDatalog 0.13 开始支持列表。

于 2013-11-04T18:29:12.320 回答