【发布时间】:2013-04-04 09:46:17
【问题描述】:
我目前正在实施一个 prolog 程序来计算两点之间的最短路径。 该框架已存在于 Java 项目中。作为要求,路径必须在 prolog 中实现。 因此我使用 gnu.prolog (http://www.gnu.org/software/gnuprologjava/)
在 java 中我调用 searchPath(1,5,Path) 将返回 Path=[5,4,3,2,1]
这是我的序言代码:
:- dynamic(path/3).
findPath( [Goal | Rest], Goal, Temp, Temp, [Goal | Rest]).
findPath( [A | Rest], Goal, Cost, Temp, Path) :-
path(A,B,C),
\+member(B, [A | Rest]),
NewCosts is (Temp + C),
findPath([B, A | Rest], Goal, Cost, NewCosts, Path).
searchPath(Start,Goal,Path_to_goal) :-
findPath([Start], Goal, Cost1, 0, Path),
findPath([Start], Goal, Cost2, 0, Path2),
Cost1=<Cost2,
Path_to_goal = Path.
我有两个问题:
searchPath方法应该返回最短路径。然而它 不。这导致我的幽灵“决定”切换 在某个点的方向导致鬼从左侧抖动 向右。我的序言代码最多需要 6 秒 才能返回结果。一世 不必告诉你,这是太多的时间。但是有时 prolog 只需要 19ms。我无法弄清楚这取决于哪种情况。例如,一个包含 99 个元素的路径列表需要 19 毫秒的时间来计算,但 6 秒花费在一个仅包含 38 个元素的列表上。
您能提出任何改进建议吗?
提前感谢您的帮助!
【问题讨论】:
标签: performance path prolog shortest-path