I am writing a goal searching agent in Prolog. I have two predicates named search
. One where the Problem has Type == explore
and in the other Type == climb
.
Note the Depth used is a constant = 10. The program was giving correct paths for all my test cases, however they were not the shortest, so from here I got the idea of using length(Actions,_)
.
It works great for the first search
predicate but when I try to add something similar for the latter search predicate satisfying Type == climb
, it goes into an infinite loop/taking too long a time.
This is what I had for that:
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).
Why is this happening?
Here is the full code:
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).
EDIT:
Ok, now I know it is related to the "Depth" being specified. I removed the constraint MaxDepth
from depthfirst
, and it is working now. However the original depthfirst was finding a solution in the given Depth, then how come it is not able to do it using Iterative Deepening - done the way I specified?