2004-07-15 17:23:44 +01:00
|
|
|
/***********************************
|
|
|
|
|
|
|
|
Variable Elimination in Prolog
|
|
|
|
|
|
|
|
How to do it
|
|
|
|
|
|
|
|
|
|
|
|
Three steps:
|
|
|
|
build the graph:
|
|
|
|
- for all variables, find out
|
|
|
|
all tables they connect to;
|
|
|
|
multiply their size
|
|
|
|
order by size
|
|
|
|
|
|
|
|
*********************************/
|
|
|
|
|
|
|
|
:- module(vel, [vel/3,
|
|
|
|
check_if_vel_done/1]).
|
|
|
|
|
2007-07-07 02:52:57 +01:00
|
|
|
:- attribute size/1, all_diffs/1.
|
2004-07-15 17:23:44 +01:00
|
|
|
|
2004-12-16 06:07:07 +00:00
|
|
|
:- use_module(library(ordsets), [ord_union/3]).
|
2004-07-15 17:23:44 +01:00
|
|
|
|
2004-12-11 19:53:43 +00:00
|
|
|
:- use_module(library('clpbn/xbif'), [clpbn2xbif/3]).
|
|
|
|
|
|
|
|
:- use_module(library('clpbn/graphviz'), [clpbn2gviz/4]).
|
2004-12-08 22:32:34 +00:00
|
|
|
|
2007-07-13 01:52:54 +01:00
|
|
|
:- use_module(library('clpbn/dists'), [
|
|
|
|
get_dist_domain_size/2,
|
|
|
|
get_dist/4]).
|
|
|
|
|
2004-12-16 06:07:07 +00:00
|
|
|
:- use_module(library('clpbn/utils'), [
|
|
|
|
clpbn_not_var_member/2,
|
|
|
|
check_for_hidden_vars/3]).
|
|
|
|
|
2007-07-07 02:52:57 +01:00
|
|
|
:- use_module(library('clpbn/display'), [
|
|
|
|
clpbn_bind_vals/3]).
|
|
|
|
|
2005-04-27 21:09:26 +01:00
|
|
|
:- use_module(library('clpbn/discrete_utils'), [
|
|
|
|
project_from_CPT/3,
|
2007-07-13 01:52:54 +01:00
|
|
|
reorder_CPT/5]).
|
2005-04-27 21:09:26 +01:00
|
|
|
|
2004-07-15 17:23:44 +01:00
|
|
|
:- use_module(library(lists),
|
|
|
|
[
|
2007-07-07 02:52:57 +01:00
|
|
|
append/3
|
2004-07-15 17:23:44 +01:00
|
|
|
]).
|
|
|
|
|
|
|
|
check_if_vel_done(Var) :-
|
|
|
|
get_atts(Var, [size(_)]), !.
|
|
|
|
|
2005-01-15 05:21:36 +00:00
|
|
|
vel(LVs0,Vs0,AllDiffs) :-
|
2005-02-18 21:34:02 +00:00
|
|
|
sort(LVs0,LVs1),
|
|
|
|
get_rid_of_ev_vars(LVs1,LVs),
|
2005-01-15 05:21:36 +00:00
|
|
|
do_vel(LVs,Vs0,AllDiffs).
|
|
|
|
|
|
|
|
do_vel([],_,_) :- !.
|
|
|
|
do_vel(LVs,Vs0,AllDiffs) :-
|
2004-07-15 17:23:44 +01:00
|
|
|
check_for_hidden_vars(Vs0, Vs0, Vs1),
|
|
|
|
sort(Vs1,Vs),
|
2004-12-08 22:32:34 +00:00
|
|
|
find_all_clpbn_vars(Vs, LV0, LVi, Tables0),
|
2004-07-15 17:23:44 +01:00
|
|
|
find_all_table_deps(Tables0, LV0),
|
2004-12-16 06:07:07 +00:00
|
|
|
(clpbn:output(xbif(XBifStream)) -> clpbn2xbif(XBifStream,vel,Vs) ; true),
|
|
|
|
(clpbn:output(gviz(XBifStream)) -> clpbn2gviz(XBifStream,vel,Vs,LVs) ; true),
|
2004-12-08 22:32:34 +00:00
|
|
|
process(LVi, LVs, tab(Dist,_,_)),
|
2004-07-15 17:23:44 +01:00
|
|
|
Dist =.. [_|Ps0],
|
|
|
|
normalise(Ps0,Ps),
|
2007-07-07 02:52:57 +01:00
|
|
|
clpbn_bind_vals(LVs,Ps,AllDiffs).
|
2004-07-15 17:23:44 +01:00
|
|
|
|
2005-01-15 05:21:36 +00:00
|
|
|
%
|
|
|
|
% some variables might already have evidence in the data-base.
|
|
|
|
%
|
|
|
|
get_rid_of_ev_vars([],[]).
|
|
|
|
get_rid_of_ev_vars([V|LVs0],LVs) :-
|
2005-01-15 05:40:38 +00:00
|
|
|
clpbn:get_atts(V, [evidence(Ev)]), !,
|
2007-07-07 02:52:57 +01:00
|
|
|
clpbn_display:put_atts(V, [posterior([],Ev,[],[])]), !,
|
2005-01-15 05:21:36 +00:00
|
|
|
get_rid_of_ev_vars(LVs0,LVs).
|
|
|
|
get_rid_of_ev_vars([V|LVs0],[V|LVs]) :-
|
|
|
|
get_rid_of_ev_vars(LVs0,LVs).
|
|
|
|
|
|
|
|
|
2004-12-08 22:32:34 +00:00
|
|
|
find_all_clpbn_vars([], [], [], []) :- !.
|
2005-02-08 04:05:39 +00:00
|
|
|
find_all_clpbn_vars([V|Vs], [Var|LV], ProcessedVars, [table(I,Table,Parents,Sizes)|Tables]) :-
|
|
|
|
var_with_deps(V, Table, Parents, Sizes, Ev, Vals), !,
|
2004-12-08 22:32:34 +00:00
|
|
|
% variables with evidence should not be processed.
|
|
|
|
(var(Ev) ->
|
2005-02-08 04:05:39 +00:00
|
|
|
Var = var(V,I,Sz,Vals,Parents,Ev,_,_),
|
2005-04-27 21:09:26 +01:00
|
|
|
vel_get_dist_size(V,Sz),
|
2004-12-08 22:32:34 +00:00
|
|
|
ProcessedVars = [Var|ProcessedVars0]
|
|
|
|
;
|
|
|
|
ProcessedVars = ProcessedVars0
|
|
|
|
),
|
|
|
|
find_all_clpbn_vars(Vs, LV, ProcessedVars0, Tables).
|
2004-07-15 17:23:44 +01:00
|
|
|
|
2004-12-05 05:01:45 +00:00
|
|
|
var_with_deps(V, Table, Deps, Sizes, Ev, Vals) :-
|
2007-07-13 01:52:54 +01:00
|
|
|
clpbn:get_atts(V, [dist(Id,Parents)]),
|
|
|
|
get_dist(Id,_,Vals,OTable),
|
2004-12-05 05:01:45 +00:00
|
|
|
( clpbn:get_atts(V, [evidence(Ev)]) -> true ; true),
|
2005-04-27 21:09:26 +01:00
|
|
|
reorder_CPT([V|Parents],OTable,Deps0,Table0,Sizes0),
|
2004-12-08 22:32:34 +00:00
|
|
|
simplify_evidence(Deps0, Table0, Deps0, Sizes0, Table, Deps, Sizes).
|
2004-07-15 17:23:44 +01:00
|
|
|
|
|
|
|
find_all_table_deps(Tables0, LV) :-
|
|
|
|
find_dep_graph(Tables0, DepGraph0),
|
|
|
|
sort(DepGraph0, DepGraph),
|
|
|
|
add_table_deps_to_variables(LV, DepGraph).
|
|
|
|
|
|
|
|
find_dep_graph([], []).
|
|
|
|
find_dep_graph([table(I,Tab,Deps,Sizes)|Tables], DepGraph) :-
|
|
|
|
add_table_deps(Deps, I, Deps, Tab, Sizes, DepGraph0, DepGraph),
|
|
|
|
find_dep_graph(Tables, DepGraph0).
|
|
|
|
|
|
|
|
add_table_deps([], _, _, _, _, DepGraph, DepGraph).
|
|
|
|
add_table_deps([V|Deps], I, Deps0, Table, Sizes, DepGraph0, [V-tab(Table,Deps0,Sizes)|DepGraph]) :-
|
|
|
|
add_table_deps(Deps, I, Deps0, Table, Sizes, DepGraph0, DepGraph).
|
|
|
|
|
|
|
|
add_table_deps_to_variables([], []).
|
2005-02-08 04:05:39 +00:00
|
|
|
add_table_deps_to_variables([var(V,_,_,_,_,_,Deps,K)|LV], DepGraph) :-
|
2004-07-15 17:23:44 +01:00
|
|
|
steal_deps_for_variable(DepGraph, V, NDepGraph, Deps),
|
|
|
|
compute_size(Deps,[],K),
|
2005-12-05 17:16:12 +00:00
|
|
|
% ( clpbn:get_atts(V,[key(Key)]) -> format('~w:~w~n',[Key,K]) ; true),
|
2004-07-15 17:23:44 +01:00
|
|
|
add_table_deps_to_variables(LV, NDepGraph).
|
|
|
|
|
|
|
|
steal_deps_for_variable([V-Info|DepGraph], V0, NDepGraph, [Info|Deps]) :-
|
|
|
|
V == V0, !,
|
|
|
|
steal_deps_for_variable(DepGraph, V0, NDepGraph, Deps).
|
|
|
|
steal_deps_for_variable(DepGraph, _, DepGraph, []).
|
|
|
|
|
|
|
|
compute_size([],Vs,K) :-
|
|
|
|
% use sizes now
|
2004-12-20 21:44:58 +00:00
|
|
|
% length(Vs,K).
|
|
|
|
multiply_sizes(Vs,1,K).
|
2004-07-15 17:23:44 +01:00
|
|
|
compute_size([tab(_,Vs,_)|Tabs],Vs0,K) :-
|
|
|
|
ord_union(Vs,Vs0,VsI),
|
|
|
|
compute_size(Tabs,VsI,K).
|
|
|
|
|
|
|
|
multiply_sizes([],K,K).
|
|
|
|
multiply_sizes([V|Vs],K0,K) :-
|
2005-04-27 21:09:26 +01:00
|
|
|
vel_get_dist_size(V, Sz),
|
2004-07-15 17:23:44 +01:00
|
|
|
KI is K0*Sz,
|
|
|
|
multiply_sizes(Vs,KI,K).
|
|
|
|
|
|
|
|
process(LV0, InputVs, Out) :-
|
2004-12-20 21:44:58 +00:00
|
|
|
find_best(LV0, V0, -1, V, WorkTables, LVI, InputVs),
|
2004-07-15 17:23:44 +01:00
|
|
|
V \== V0, !,
|
2005-08-02 04:09:52 +01:00
|
|
|
%format('1 ~w: ~w~n',[V,WorkTables]),
|
|
|
|
multiply_tables(WorkTables, tab(Tab0,Deps0,_)),
|
|
|
|
Tab0 =.. [_|LTab0],
|
|
|
|
reorder_CPT(Deps0,LTab0,Deps,Tab,Sizes),
|
|
|
|
Table = tab(Tab,Deps,Sizes),
|
|
|
|
%format('2 ~w: ~w~n',[V,Table]),
|
2005-04-27 21:09:26 +01:00
|
|
|
project_from_CPT(V,Table,NewTable),
|
2005-08-02 04:09:52 +01:00
|
|
|
%format('3 ~w: ~w~n',[V,NewTable]),
|
2004-07-15 17:23:44 +01:00
|
|
|
include(LVI,NewTable,V,LV2),
|
|
|
|
process(LV2, InputVs, Out).
|
|
|
|
process(LV0, _, Out) :-
|
|
|
|
fetch_tables(LV0, WorkTables),
|
|
|
|
multiply_tables(WorkTables, Out).
|
|
|
|
|
2005-12-05 17:16:12 +00:00
|
|
|
|
2004-12-20 21:44:58 +00:00
|
|
|
find_best([], V, _TF, V, _, [], _).
|
|
|
|
%:-
|
|
|
|
% clpbn:get_atts(V,[key(K)]), write(chosen:K:TF), nl.
|
2005-02-08 04:05:39 +00:00
|
|
|
% root_with_single_child
|
|
|
|
%find_best([var(V,I,_,_,[],Ev,[Dep],K)|LV], _, _, V, [Dep], LVF, Inputs) :- !.
|
|
|
|
find_best([var(V,I,Sz,Vals,Parents,Ev,Deps,K)|LV], _, Threshold, VF, NWorktables, LVF, Inputs) :-
|
2004-12-20 21:44:58 +00:00
|
|
|
( K < Threshold ; Threshold < 0),
|
2004-12-16 06:07:07 +00:00
|
|
|
clpbn_not_var_member(Inputs, V), !,
|
2004-07-15 17:23:44 +01:00
|
|
|
find_best(LV, V, K, VF, WorkTables,LV0, Inputs),
|
|
|
|
(V == VF ->
|
|
|
|
LVF = LV0, Deps = NWorktables
|
|
|
|
;
|
2005-02-08 04:05:39 +00:00
|
|
|
LVF = [var(V,I,Sz,Vals,Parents,Ev,Deps,K)|LV0], WorkTables = NWorktables
|
2004-07-15 17:23:44 +01:00
|
|
|
).
|
|
|
|
find_best([V|LV], V0, Threshold, VF, WorkTables, [V|LVF], Inputs) :-
|
|
|
|
find_best(LV, V0, Threshold, VF, WorkTables, LVF, Inputs).
|
|
|
|
|
|
|
|
multiply_tables([Table], Table) :- !.
|
|
|
|
multiply_tables([tab(Tab1,Deps1,Szs1), tab(Tab2,Deps2,Sz2)| Tables], Out) :-
|
|
|
|
multiply_table(Tab1, Deps1, Szs1, Tab2, Deps2, Sz2, NTab, NDeps, NSz),
|
|
|
|
multiply_tables([tab(NTab,NDeps,NSz)| Tables], Out).
|
|
|
|
|
|
|
|
|
2004-12-08 22:32:34 +00:00
|
|
|
simplify_evidence([], Table, Deps, Sizes, Table, Deps, Sizes).
|
|
|
|
simplify_evidence([V|VDeps], Table0, Deps0, Sizes0, Table, Deps, Sizes) :-
|
2005-04-27 21:09:26 +01:00
|
|
|
clpbn:get_atts(V, [evidence(_)]), !,
|
|
|
|
project_from_CPT(V,tab(Table0,Deps0,Sizes0),tab(NewTable,Deps1,Sizes1)),
|
2004-12-08 22:32:34 +00:00
|
|
|
simplify_evidence(VDeps, NewTable, Deps1, Sizes1, Table, Deps, Sizes).
|
|
|
|
simplify_evidence([_|VDeps], Table0, Deps0, Sizes0, Table, Deps, Sizes) :-
|
|
|
|
simplify_evidence(VDeps, Table0, Deps0, Sizes0, Table, Deps, Sizes).
|
|
|
|
|
2004-07-15 17:23:44 +01:00
|
|
|
fetch_tables([], []).
|
2005-02-08 04:05:39 +00:00
|
|
|
fetch_tables([var(_,_,_,_,_,_,Deps,_)|LV0], Tables) :-
|
2004-07-15 17:23:44 +01:00
|
|
|
append(Deps,Tables0,Tables),
|
|
|
|
fetch_tables(LV0, Tables0).
|
|
|
|
|
|
|
|
multiply_table(Tab1, Deps1, Szs1, Tab2, Deps2, Szs2, NTab, NDeps, NSzs) :-
|
|
|
|
deps_union(Deps1,Szs1,Fs10,Deps2,Szs2,Fs20,NDeps,NSzs),
|
|
|
|
factors(NSzs, Fs, Total),
|
|
|
|
factors(Fs10, Fs1, _),
|
|
|
|
factors(Fs20, Fs2, _),
|
|
|
|
elements(0, Total, Fs, Fs1, Fs2, Tab1, Tab2, LTab),
|
|
|
|
NTab =.. [t|LTab].
|
|
|
|
|
|
|
|
deps_union([],[],[],[],[],[],[],[]) :- !.
|
|
|
|
deps_union([],[],Fs1,[V2|Deps2],[Sz|Szs2],[Sz|Szs2],[V2|Deps2],[Sz|Szs2]) :- !,
|
|
|
|
mk_zeros([Sz|Szs2],Fs1).
|
|
|
|
deps_union([V1|Deps1],[Sz|Szs1],[Sz|Szs1],[],[],Fs2,[V1|Deps1],[Sz|Szs1]) :- !,
|
|
|
|
mk_zeros([Sz|Szs1],Fs2).
|
|
|
|
deps_union([V1|Deps1],[Sz|Szs1],[Sz|Fs1],[V2|Deps2],[Sz|Szs2],[Sz|Fs2],[V1|NDeps],[Sz|NSzs]) :- V1 == V2, !,
|
|
|
|
deps_union(Deps1,Szs1,Fs1,Deps2,Szs2,Fs2,NDeps,NSzs).
|
|
|
|
deps_union([V1|Deps1],[Sz1|Szs1],[Sz1|Fs1],[V2|Deps2],Szs2,[0|Fs2],[V1|NDeps],[Sz1|NSzs]) :- V1 @< V2, !,
|
|
|
|
deps_union(Deps1,Szs1,Fs1,[V2|Deps2],Szs2,Fs2,NDeps,NSzs).
|
|
|
|
deps_union([V1|Deps1],Szs1,[0|Fs1],[V2|Deps2],[Sz|Szs2],[Sz|Fs2],[V2|NDeps],[Sz|NSzs]) :-
|
|
|
|
deps_union([V1|Deps1],Szs1,Fs1,Deps2,Szs2,Fs2,NDeps,NSzs).
|
|
|
|
|
|
|
|
mk_zeros([],[]).
|
|
|
|
mk_zeros([_|Szs],[0|Fs]) :-
|
|
|
|
mk_zeros(Szs,Fs).
|
|
|
|
|
|
|
|
|
|
|
|
factors([], [], 1).
|
|
|
|
factors([0|Ls], [0|NLs], Prod) :- !,
|
|
|
|
factors(Ls, NLs, Prod).
|
|
|
|
factors([N|Ls], [Prod0|NLs], Prod) :-
|
|
|
|
factors(Ls, NLs, Prod0),
|
|
|
|
Prod is Prod0*N.
|
|
|
|
|
|
|
|
elements(Total, Total, _, _, _, _, _, []) :- !.
|
|
|
|
elements(I, Total, Fs, Fs1, Fs2, Tab1, Tab2, [El|Els]) :-
|
|
|
|
element(Fs, I, 1, Fs1, 1, Fs2, Tab1, Tab2, El),
|
|
|
|
I1 is I+1,
|
|
|
|
elements(I1, Total, Fs, Fs1, Fs2, Tab1, Tab2, Els).
|
|
|
|
|
|
|
|
element([], _, P1, [], P2, [], Tab1, Tab2, El) :-
|
|
|
|
arg(P1, Tab1, El1),
|
|
|
|
arg(P2, Tab2, El2),
|
|
|
|
El is El1*El2.
|
|
|
|
element([F|Fs], I, P1, [F1|Fs1], P2, [F2|Fs2], Tab1, Tab2, El) :-
|
|
|
|
FF is I // F,
|
|
|
|
NP1 is P1+F1*FF,
|
|
|
|
NP2 is P2+F2*FF,
|
|
|
|
NI is I mod F,
|
|
|
|
element(Fs, NI, NP1, Fs1, NP2, Fs2, Tab1, Tab2, El).
|
|
|
|
|
|
|
|
%
|
|
|
|
include([],_,_,[]).
|
2005-02-08 04:05:39 +00:00
|
|
|
include([var(V,P,VSz,D,Parents,Ev,Tabs,Est)|LV],tab(T,Vs,Sz),V1,[var(V,P,VSz,D,Parents,Ev,Tabs,Est)|NLV]) :-
|
2004-12-16 06:07:07 +00:00
|
|
|
clpbn_not_var_member(Vs,V), !,
|
2004-07-15 17:23:44 +01:00
|
|
|
include(LV,tab(T,Vs,Sz),V1,NLV).
|
2005-02-08 04:05:39 +00:00
|
|
|
include([var(V,P,VSz,D,Parents,Ev,Tabs,_)|LV],Table,NV,[var(V,P,VSz,D,Parents,Ev,NTabs,NEst)|NLV]) :-
|
2004-12-20 21:44:58 +00:00
|
|
|
update_tables(Tabs,NTabs,Table,NV),
|
|
|
|
compute_size(NTabs, [], NEst),
|
2004-07-15 17:23:44 +01:00
|
|
|
include(LV,Table,NV,NLV).
|
|
|
|
|
2004-12-20 21:44:58 +00:00
|
|
|
update_tables([],[Table],Table,_).
|
|
|
|
update_tables([tab(Tab0,Vs,Sz)|Tabs],[tab(Tab0,Vs,Sz)|NTabs],Table,V) :-
|
2004-12-16 06:07:07 +00:00
|
|
|
clpbn_not_var_member(Vs,V), !,
|
2004-12-20 21:44:58 +00:00
|
|
|
update_tables(Tabs,NTabs,Table,V).
|
|
|
|
update_tables([_|Tabs],NTabs,Table,V) :-
|
|
|
|
update_tables(Tabs,NTabs,Table,V).
|
2004-07-15 17:23:44 +01:00
|
|
|
|
|
|
|
normalise(Ps0,Ps) :-
|
|
|
|
add_all(Ps0,0.0,Sum),
|
|
|
|
divide_by_sum(Ps0,Sum,Ps).
|
|
|
|
|
|
|
|
add_all([],Sum,Sum).
|
|
|
|
add_all([P|Ps0],Sum0,Sum) :-
|
|
|
|
SumI is Sum0+P,
|
|
|
|
add_all(Ps0,SumI,Sum).
|
|
|
|
|
|
|
|
divide_by_sum([],_,[]).
|
|
|
|
divide_by_sum([P|Ps0],Sum,[PN|Ps]) :-
|
|
|
|
PN is P/Sum,
|
|
|
|
divide_by_sum(Ps0,Sum,Ps).
|
|
|
|
|
2004-12-16 06:07:07 +00:00
|
|
|
|
2005-04-27 21:09:26 +01:00
|
|
|
vel_get_dist_size(V,Sz) :-
|
2004-12-16 06:07:07 +00:00
|
|
|
get_atts(V, [size(Sz)]), !.
|
2005-04-27 21:09:26 +01:00
|
|
|
vel_get_dist_size(V,Sz) :-
|
2007-07-13 01:52:54 +01:00
|
|
|
clpbn:get_atts(V,dist(Id,_)), !,
|
|
|
|
get_dist_domain_size(Id,Sz),
|
2004-12-16 06:07:07 +00:00
|
|
|
put_atts(V, [size(Sz)]).
|
|
|
|
|