在JIProlog中使用冻结 [英] Using freeze in JIProlog
问题描述
我想用JIProlog做图搜索。下面的例子工作正常,没有 memberchk
,但它然后返回带有循环的路径,这是我不想要的。但是,当我包含它时,Prolog会冻结,可能是因为无限搜索。
I want to do a graph search with JIProlog. The below example works fine without the memberchk
, but then it returns paths with cycles, which I don't want. When I do include it, however, Prolog freezes, probably because of the infinite search.
connected(ac,a,b). connected(ac,a,c). connected(ac,b,c).
connected(ac,b,a). connected(ac,c,a). connected(ac,c,b).
path(A,B,[AB]) :- connected(AB,A,B).
path(A,C,[H|T]) :- connected(H,A,B), path(B,C,T), \+ memberchk(H,T).
在 freeze / 2 ) code>)。但是, freeze / 2
在JIProlog中不起作用,这正是我正在使用的。任何人都可以帮助我找到另一种解决方案吗?
In this answer I found why (the list of edges is not yet instantiated) and a hint to a solution (using freeze/2
). However, freeze/2
doesn't work in JIProlog, which is what I'm using. Can anyone help me to an alternative solution?
编辑:我知道图的一般情况,这将是解决方案, ,例如在这个示例中,但对于我的特定应用程序,节点也可以在 边上,这就是为什么我要检查被访问的边缘而不是被访问的节点的原因。
I know for graphs in general it would be a solution to keep track of nodes instead, such as in this example, but for my particular application the "nodes" can also be on an edge, which is why I want to check for edges that were visited rather than nodes that were visited.
推荐答案
我不确定要了解您的要求。
I'm not sure to understand your requirement. I would try to adapt the suggested answer.
% get a path from start to end
path(Start, End, Path) :-
path(Start, End, [], Path).
path(A, B, Visited, [AB|Visited]) :- connected(AB, A, B).
path(A, C, Visited, Path) :-
connected(AB, A, B),
\+ memberchk(AB, Visited),
path(B, C, [AB|Visited], Path).
注意:未经测试的代码....
beware: untested code....
这篇关于在JIProlog中使用冻结的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!