* Add missing file
[chr.git] / a_star.pl
blob41a0c11183761e70457ec31ed5f5b4ff6f9028bf
1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2 % Author: Tom Schrijvers
3 % Email: Tom.Schrijvers@cs.kuleuven.be
4 % Copyright: K.U.Leuven 2004
5 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 :- module(a_star,
8 a_star/4
9 ]).
11 :- use_module(library(lists)).
13 :- use_module(binomialheap).
15 :- use_module(find).
17 :- use_module(hprolog).
19 %% SICStus begin
20 %% :- use_module(library(terms),[term_variables/2]).
21 %% SICStus end
23 a_star(DataIn,FinalData,ExpandData,DataOut) :-
24 a_star_node(DataIn,0,InitialNode),
25 empty_q(NewQueue),
26 insert_q(NewQueue,InitialNode,Queue),
27 a_star_aux(Queue,FinalData,ExpandData,EndNode),
28 a_star_node(DataOut,_,EndNode).
30 a_star_aux(Queue,FinalData,ExpandData,EndNode) :-
31 delete_min_q(Queue,Queue1,Node),
32 ( final_node(FinalData,Node) ->
33 Node = EndNode
35 expand_node(ExpandData,Node,Nodes),
36 insert_list_q(Nodes,Queue1,NQueue),
37 a_star_aux(NQueue,FinalData,ExpandData,EndNode)
40 final_node(D^Call,Node) :-
41 a_star_node(Data,_,Node),
42 term_variables(Call,Vars),
43 chr_delete(Vars,D,DVars),
44 copy_term(D^Call-DVars,Data^NCall-DVars),
45 call(NCall).
47 expand_node(D^Ds^C^Call,Node,Nodes) :-
48 a_star_node(Data,Score,Node),
49 term_variables(Call,Vars),
50 chr_delete(Vars,D,DVars0),
51 chr_delete(DVars0,Ds,DVars1),
52 chr_delete(DVars1,C,DVars),
53 copy_term(D^Ds^C^Call-DVars,Data^EData^Cost^NCall-DVars),
54 term_variables(Node,NVars,DVars),
55 find_with_var_identity(ENode,NVars,(NCall,EScore is Cost + Score,a_star:a_star_node(EData,EScore,ENode)),Nodes).
57 a_star_node(Data,Score,Data-Score).