2

我在 Prolog 中编写的程序的一部分与查找从起始位置到结束位置的所有可能路径选项有关。

这是我到目前为止所拥有的:

findAllRoutes(Start, End, Path) :-
     findAllRoutes(Start, _, End, Path),
     print('Successful route: '), print(Route).

findAllRoutes(End, _, End, [End]). %route was successfully finished

 findAllRoutes(Start, Temp, End, [Start|Rest_of_List]) :-
     path(Start, Temp),
     findAllRoutes(Temp, _, End, Rest).

这是要读入的数据:

%path(Start, End).
path(1, 4). %find all the possible paths from location 1 to location 4.

%paths_in_place[[Start, End, Distance]].
paths_in_place[[1, 2, 250], [2, 4, 250], [1, 3, 400], [3, 4, 300]].

我的问题是,这是一种准确的循环方式,paths_in_place同时还保存从起始位置到结束位置的途中到达的点的顺序?

另外,在没有提及该字段的情况下调用Distance时会发生什么?即使在字段中传递参数,Prolog 是否合法?findAllRoutesDistanceStart, End, Routepaths_in_placeStart, End, Distance

任何帮助深表感谢。如果我需要澄清任何事情,请告诉我。

4

1 回答 1

1

Prolog 数据是象征性的。您可以以米为单位传递距离,并将其视为以英尺为单位的距离。这就是其中一个火星任务坠毁的原因,尽管他们使用了另一种语言。所以这最终取决于你。

至于你的代码,

findAllRoutes(Start, End, Path) :-
     findAllRoutes(Start, _, End, Path),
     print('Successful route: '), print(Route).

Route? 什么Route?这是一个未经实例化的“单例”变量。SWI Prolog 提醒我们注意这一点。也许你的意思是Path

findAllRoutes(End, _, End, [End]).  % route was successfully finished

findAllRoutes(Start, Temp, End, [Start|Rest_of_List]) :-
     path(Start, Temp),
     findAllRoutes(Temp, _, End, Rest).

再次,Rest_of_List并且Rest可能应该具有相同的名称(是相同的逻辑变量)。

否则它看起来没问题,除了它的名字:它在每次调用/回溯时找到一个路径,而不是“AllRoutes”。名称“AllRoutes”表明它找到了所有这些并在其结果参数中返回它们;它不是。

然后是您的数据,它与您的代码不同步:

%path(Start, End).
path(1, 4). %find all the possible paths from location 1 to location 4.

%paths_in_place[[Start, End, Distance]].
paths_in_place([[1, 2, 250], [2, 4, 250], [1, 3, 400], [3, 4, 300]]).
  % ! must be in parentheses !

path(Start, Temp)在代码中的调用建议TempStart. 根据这些数据并非如此。另外,path/2缺少定义。但是你可以只用一堆事实而不是列表来定义数据。

于 2013-10-26T07:41:07.407 回答