Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with a breadth first search

Status
Not open for further replies.

kevsmith38

Technical User
Feb 25, 2008
1
GB
Hi can some body help me with the following:

I am trying to make a prolog file that does a breadth first search through a maze. The maze is made up of rules eg:

path(start, tree).
path(start, car).
path(tree, park).
etc

each node also has a noise level attached to it, the higher the noise the closer to the exit ( a tower) the solution is:
noise_level(start,2).
noise_level(tree,4).
noise_level(park,2).
noise_level(tower,100).
etc

this is the code I have so far (excluding the snippets from above):

Code:
sortPath([], []).
sortPath([X], [X]).
sortPath([X,Y], [X,Y]) :- X =< Y.
sortPath([X | Tail], Result) :- sortM(Tail, NewVar),
				insertInOrder(X, NewVar, Result).

insertInOrder(X, [], [X]).

insertInOrder(X, [Y], [X,Y]) :- X =< Y, !.
insertInOrder(X, [Y], [Y,X]) :- Y < X, !.

insertInOrder(X, [Y | Tail], [X, Y| Tail]) :- X =< Y,!.

insertInOrder(X, [Y | Tail], [Y| Tail1]) :- Y < X,!,
						insertInOrder(X,Tail,Tail1).


there_is_a_way(Start, Start).

there_is_a_way(Start, Finish) :-
	path(Start, Finish,P).

there_is_a_way(Start, Finish):-
	path(Start, Intermediate,P),
		there_is_a_way(Intermediate, Finish).

dfsWay([], Finish, Finish).
dfsWay([Next_State | Way], State, Finish) :-
	path(State, Next_State, Pr),
	dfsWay(way, Next_State, Finish).

smartter_depthfirst(By, Start, Finish) :-
	way(By, [Start], Start, Finish).
way([], _, Finish, Finish).
way([Next_state|Way], Avoiding, State, Finish) :-
	path(State, Next_State, Pr),
	not_member(Next_State, Avoiding),
way(Way, [Next_State|Avoiding], Next_State, Finish).

not_member(X, [A | B ]):- member(X, [A | B ]), !, fail.

not_member(X, [A|B]).

member(X, [X]).
member(X, [X | Y]).
member(X, [A | Y]) :-member(X,Y).

solve(Start, Finish, Solution) :-
	breadthfirst([[Start]], Finish, Solution).
	breadthfirst([[Finish | Path ]], Finish, [Finish|Path]).

breadthfirst([Path | Paths], Finish, Solution) :-
		extend(Path, NewPaths),
		sortPath(NewPaths, Results),
	append(Paths, Results, Paths1),
	breadthfirst(Paths1, Finish, Solution).


extend([Node | Path], NewPaths) :-
	findall([NewNode, Node |Path], 
	(path(Node, NewNode, Path),
	not(member(NewNode, [Node | Path]))), NewPaths), !,
	extend(Path ,[]).

findall(X,Goal,Xlist) :-
call(Goal),
assertz(queue(X)),
fail;
assertz(queue(bottom)).

When I try to find a soloution by calling solve(start,tower,L). I get an error
"uncaught exception: error(existence_error(procedure,path/3),findall/3)"

I know I get the error because I need to write the findall method but I have been trying for a few days with no luck. Can somebody tell me how I would write such a method?


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top