我正在 Prolog 中编写目标搜索代理。我有两个谓词名为search
. 一个有问题Type == explore
,另一个在哪里Type == climb
。
请注意,使用的深度是一个常数 = 10。该程序为我所有的测试用例提供了正确的路径,但是它们并不是最短的,所以从这里我得到了使用length(Actions,_)
.
它适用于第一个search
谓词,但是当我尝试为后一个搜索谓词添加类似的Type == climb
内容时,它会进入无限循环/花费太长时间。这就是我所拥有的:
search(Problem,Actions):-
Problem = [Goal-A-X,Y-Depth],
Goal = [Type,_,_],
Type == climb,
length(List,_),
depthfirst([A-X,Y],List,Depth),!,concat(List,[climb],Actions).
为什么会这样?
这是完整的代码:
search(Problem,Actions):-
Problem = [Goal-A-X,Y-Depth],
Goal = [Type,_,_],
Type == explore,
length(Actions,_),
depthfirst([A-X,Y],Actions,Depth),!.
search(Problem,Actions):-
Problem = [Goal-A-X,Y-Depth],
Goal = [Type,_,_],
Type == climb,
depthfirst([A-X,Y],List,Depth),concat(List,[climb],Actions).
depthfirst([A-X,Y],[],_):-
goal([_,Gx,Gy]),
Gx == X,
Gy == Y.
depthfirst([A-X,Y],[Action|Sol],Maxdepth):-
Maxdepth > 0,
s([A-X,Y],[A1-X1,Y1],Action),
Max1 is Maxdepth - 1,
depthfirst([A1-X1,Y1],Sol,Max1).
编辑:
好的,现在我知道它与指定的“深度”有关。MaxDepth
我从中删除了约束depthfirst
,它现在正在工作。然而,最初的 depthfirst 是在给定的 Depth 中找到解决方案,那么为什么它不能使用迭代深化来做到这一点 - 按照我指定的方式完成?