0

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?

Rahn
  • 4,787
  • 4
  • 31
  • 57
digvijay91
  • 697
  • 1
  • 6
  • 21
  • 1
    Can ypu show what you typed in for a query which led to the infinite loop? – lurker Mar 23 '14 at 00:36
  • @mbratch I call it simply search(Problem,Action) after assigning a releveant goal and putting it as the Problem. Problem = [Goal-A-X,Y-Depth], where Goal is = [,Goal X coordinate, Goal Y coordinate], A is one of 4 angles of the agent, X,Y are the coordinates, and Depth is the max depth depth first search should go till. The s() predicate used in depthfirst is the successor function (linked to the knowledge base of the agent, which is asserted else where). – digvijay91 Mar 24 '14 at 18:47
  • @mbratch Here is some extra info: I tried to print the length of the List by writting the following code:`length(List,_),length(List,N),write(N),depthfirst([A-X,Y],List,Depth),!` and it seemed pretty much to write all numbers on the screen, without stopping. – digvijay91 Mar 24 '14 at 18:47
  • `length(List, _)` or `length(List, N)` will just generate lists of length 0, 1, 2, ... *ad infinitum* if `List` is a variable. So that behavior is expected. But if your predicate is behaving, I expect that construct to allow you to try lists of progressively longer length until you hit a solution. – lurker Mar 24 '14 at 19:04
  • It still seems like you must type in something specific for `Problem` on the query. If both `Problem` and `Action` are unbound in the `search(Problem, Action)` query, then `Type == climb` would always fail since `Type` would then be unbound, and `==` fails if one argument is bound and the other not. So it would help if, in your problem statement, you showed the simplest example that causes a problem you want help on. – lurker Mar 24 '14 at 19:06
  • @mbratch Ok, I look through the test cases, and see exactly which one is causing the porblem. Btw, did you look at the edit, I have added in the question? – digvijay91 Mar 31 '14 at 05:46

0 Answers0