home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Simtel MSDOS 1992 December
/
simtel1292_SIMTEL_1292_Walnut_Creek.iso
/
msdos
/
turbopas
/
spoc88.arc
/
SEARCH.ARC
/
SEARCH.PRO
< prev
Wrap
Text File
|
1988-05-31
|
3KB
|
139 lines
/* Graph Searching */
domains
node = integer /* Modify this to fit the problem. */
pointer = ptr(node,node)
pointers = pointer*
path = node*
database
mark(node)
/* Predicates defining the search space */
/* Change these to fit the problem. */
predicates
start_node(node)
goal_node(node)
arcc(node,node)
clauses
start_node(0).
goal_node(13). goal_node(14). goal_node(15).
arcc(0,5). arcc(0,6). arcc(0,7). arcc(3,1).
arcc(4,2). arcc(5,8). arcc(6,9). arcc(7,10).
arcc(8,3). arcc(8,9). arcc(9,11). arcc(9,12).
arcc(9,14). arcc(10,4). arcc(10,9). arcc(11,13).
arcc(11,14). arcc(12,14). arcc(12,15).
/* General purpose predicates */
predicates
member(pointer,pointers)
member(node,path)
append(path,path,path)
my_retractall(string)
clauses
member(H,[H|_]).
member(H,[_|T]) :-
member(H,T).
append([],L,L).
append([H|T],L,[H|T1]) :-
append(T,L,T1).
my_retractall(mark) :-
retract(mark(_)),
my_retractall(mark).
my_retractall(_).
/* The search mechanism */
predicates
better(node,node) /* Modify this to suit the problem. */
unmarked_successor(node,node)
search(string,path,pointers,node,path)
continue_search(string,path,pointers,node,path,path)
insert(node,path,path)
merge(string,path,path,path)
findpath(pointers,path,path)
markall(path)
update_pointers(node,path,pointers,pointers)
clauses
better(X,Y) :-
X >= Y.
unmarked_successor(N,M) :-
arcc(N,M),
not(mark(M)).
search(_,[TheGoal | _],P,TheGoal,Path) :-
goal_node(TheGoal),
findpath(P,[TheGoal],Path),
!.
search(Type,[N | R],P,TheGoal,Path) :-
findall(X,unmarked_successor(N,X),New),
continue_search(Type,[N|R],P,TheGoal,Path,New).
continue_search(_,[N | _],P,TheGoal,Path,New) :-
member(TheGoal,New),
goal_node(TheGoal),
findpath(P,[N,TheGoal],Path),
!.
continue_search(Type,[N|R],P,TheGoal,Path,New) :-
markall(New),
merge(Type,New,R,NewL),
update_pointers(N,New,P,NewP),
search(Type,NewL,NewP,TheGoal,Path).
findpath(P,[H|T],Path) :-
member(ptr(X,H),P),
!,
findpath(P,[X,H|T],Path).
findpath(_,Path,Path).
markall([H|T]) :-
assert(mark(H)),
markall(T).
markall([]).
insert(Node,[H|T],[Node,H|T]) :-
better(Node,H),
!.
insert(Node,[H|T],[H|NewT]) :-
insert(Node,T,NewT).
insert(Node,[],[Node]).
merge("depth",New,R,NewL) :-
append(New,R,NewL).
merge("breadth",New,R,NewL) :-
append(R,New,NewL).
merge("best",[H|T],L,NewL) :-
insert(H,L,TempL),
merge("best",T,TempL,NewL).
merge("best",[],NewL,NewL).
update_pointers(N,[H|T],P,NewP) :-
update_pointers(N,T,[ptr(N,H) | P],NewP).
update_pointers(_,_,NewP,NewP).
goal
makewindow(1,7,7,"",5,5,10,65),
my_retractall(mark),
write("\n\tWhat type of search (depth, breadth, best)? "),
readln(Type),
clearwindow,
start_node(S),
search(Type,[S],[],TheGoal,Path),
write("\n\tThe goal ",TheGoal,
" was reached via a ",Type,"-first search."),
nl,
nl,
write("\tA path leading from a start node to this goal is:\n\n\t",
Path),
nl,nl.