在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(()),如果没有,则不会给出结果。但是它没有给我Z.I的不同评估结果,我也想知道路线,例如:A B-C-D
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。还是应该使用聚合函数?我不太明白我怎么能用得着.
提前谢谢。
发布于 2013-11-04 18:29:12
您需要一个带有变量的谓词,该变量包含两个端点之间的节点。您可以使用以下路由()定义:
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以来就支持列表。
https://stackoverflow.com/questions/19773176
复制相似问题