CLP(BN) related fixes

git-svn-id: https://yap.svn.sf.net/svnroot/yap/trunk@1914 b08c6af1-5177-4d33-ba66-4b1c6b8b522a
This commit is contained in:
vsc 2007-07-07 01:52:57 +00:00
parent 71eea67bc8
commit 9a730f3b05
8 changed files with 228 additions and 530 deletions

View File

@ -33,6 +33,7 @@ CLPBN_PROGRAMS= \
$(CLPBN_SRCDIR)/aggregates.yap \
$(CLPBN_SRCDIR)/bnt.yap \
$(CLPBN_SRCDIR)/discrete_utils.yap \
$(CLPBN_SRCDIR)/display.yap \
$(CLPBN_SRCDIR)/evidence.yap \
$(CLPBN_SRCDIR)/gibbs.yap \
$(CLPBN_SRCDIR)/graphs.yap \
@ -53,6 +54,7 @@ CLPBN_EXAMPLES= \
$(CLPBN_EXDIR)/School/school_32.yap \
$(CLPBN_EXDIR)/School/school_64.yap \
$(CLPBN_EXDIR)/School/tables.yap \
$(CLPBN_EXDIR)/sprinkler.yap
install: $(CLBN_TOP) $(CLBN_PROGRAMS) $(CLPBN_PROGRAMS)

View File

@ -25,14 +25,14 @@
:- attribute key/1, dist/3, evidence/1, starter/0.
:- use_module('clpbn/bnt', [dump_as_bnt/2,
check_if_bnt_done/1
]).
:- use_module('clpbn/vel', [vel/3,
check_if_vel_done/1
]).
:- use_module('clpbn/bnt', [do_bnt/3,
check_if_bnt_done/1
]).
:- use_module('clpbn/gibbs', [gibbs/3,
check_if_gibbs_done/1
]).
@ -70,6 +70,12 @@ clpbn_flag(output,Before,After) :-
clpbn_flag(solver,Before,After) :-
retract(solver(Before)),
assert(solver(After)).
clpbn_flag(bnt_solver,Before,After) :-
retract(bnt:bnt_solver(Before)),
assert(bnt:bnt_solver(After)).
clpbn_flag(bnt_path,Before,After) :-
retract(bnt:bnt_path(Before)),
assert(bnt:bnt_path(After)).
{Var = Key with Dist} :-
put_atts(El,[key(Key),dist(Domain,Table,Parents)]),
@ -143,8 +149,8 @@ write_out(vel, GVars, AVars, DiffVars) :-
vel(GVars, AVars, DiffVars).
write_out(gibbs, GVars, AVars, DiffVars) :-
gibbs(GVars, AVars, DiffVars).
write_out(bnt, GVars, AVars, _) :-
dump_as_bnt(GVars, AVars).
write_out(bnt, GVars, AVars, DiffVars) :-
do_bnt(GVars, AVars, DiffVars).
write_out(graphs, _, AVars, _) :-
clpbn2graph(AVars).

View File

@ -1,494 +1,225 @@
:- module(bnt, [dump_as_bnt/2,
:- module(bnt, [do_bnt/3,
check_if_bnt_done/1]).
:- use_module(library(matlab), [start_matlab/2,
close_matlab/1,
eval_string/2
:- use_module(library('clpbn/display'), [
clpbn_bind_vals/3]).
:- use_module(library(matlab), [start_matlab/1,
close_matlab/0,
matlab_on/0,
matlab_eval_string/1,
matlab_eval_string/2,
matlab_matrix/4,
matlab_sequence/3,
matlab_initialized_cells/4,
matlab_get_variable/2,
matlab_call/2
]).
:- attribute topord/1, map/1.
:- use_module(library(dgraphs), [dgraph_new/1,
dgraph_add_vertices/3,
dgraph_add_edges/3,
dgraph_top_sort/2,
dgraph_vertices/2,
dgraph_edges/2
]).
:- yap_flag(write_strings,on).
% syntactic sugar for matlab_call.
:- op(800,yfx,<--).
G <-- Y :-
matlab_call(Y,G).
:- attribute bnt_id/1.
:- dynamic bnt/1.
:- dynamic bnt_solver/1, bnt_path/1.
% belprop
bnt_solver(jtree).
% likelihood_weighting
bnt_path('/u/vitor/Yap/CLPBN/FullBNT-1.0.3/BNT').
/*****************************************
BNT uses:
bnet
dag
discrete_nodes: which nodes are discrete (all by now),
node_sizes
engine
evidence
marg
*****************************************/
check_if_bnt_done(Var) :-
get_atts(Var, [map(_)]).
dump_as_bnt(GVars, [_|_]) :-
start_matlab(´-nojvm -nosplash',MatEnv),
get_value(clpbn_key, Key),
eval_string(MatEnv, 'cd /u/vitor/sw/BNT'),
eval_string(MatEnv, 'add_BNT_to_path'),
extract_graph(GVars, Graph),
dgraph_top_sort(Graph, SortedGraph),
number_graph(SortedGraph),
zeros(MatEnv, GLength, GLength, dag),
build_dag(SortedGraph),
dump_cpts(SortedGraph).
do_bnt([], _, _) :- !.
do_bnt(QueryVars, AllVars, AllDiffs) :-
init_matlab,
extract_graph(AllVars, Graph),
dgraph_top_sort(Graph, SortedVertices),
number_graph(SortedVertices, NumberedVertices, 0, Size),
init_bnet(SortedVertices, NumberedVertices, Size),
set_inference,
add_evidence(SortedVertices, Size, NumberedVertices),
marginalize(QueryVars, Ps),
clpbn_bind_vals(QueryVars, Ps, AllDiffs).
% make sure MATLAB works.
init_matlab :-
bnt(on), !.
init_matlab :-
start_matlab,
bnt_path(Path),
atom_concat('cd ', Path, Command),
matlab_eval_string(Command),
matlab_eval_string('add_BNT_to_path',_),
assert(bnt(on)).
start_matlab :-
matlab_on, !.
start_matlab :-
start_matlab('matlab -nojvm -nosplash').
% send_command(CommandStream, Answer, 'cd /home/vitor/Yap/CLPBN/BNT;~n', []),
send_command(CommandStream, Answer, 'add_BNT_to_path;~n', []),
send_command(CommandStream, Answer, '~w = ~w;~n', ['$VAR'(Key), Key]),
send_command(CommandStream, Answer, 'dag = zeros(~w,~w);~n', ['$VAR'(Key), '$VAR'(Key)]),
Key1 is Key-1,
dump_variable_indices(Key1, Vs, Heap, CommandStream, Answer),
write_deps(Heap, CommandStream, Answer),
find_observables(GVars, Observables),
mknet(Vs, Key, Observables, CommandStream, Answer),
dump_cpds(Vs, CommandStream, Answer),
inf_engine(Vs, Key, CommandStream, Answer),
output_answer(Observables, CommandStream, Answer),
close(CommandStream),
close(Answer).
dump_variable_indices(I, LF, HF, CommandStream, Answer) :-
all_vars_with_deps(I, LDeps, LNoDeps),
sort(LNoDeps, S0),
empty_heap(H0),
topsort(LDeps, S0, LNoDeps, LF, H0, HF),
reverse(LF, Vs),
dump_vlist(Vs, 0, CommandStream, Answer).
all_vars_with_deps(0, LDeps, LNoDeps) :- !,
var_with_deps(0, LDeps, [], LNoDeps, []).
all_vars_with_deps(I, LDeps, LNoDeps) :-
var_with_deps(I, LDeps, LDeps0, LNoDeps, LNoDeps0),
I1 is I-1,
all_vars_with_deps(I1, LDeps0, LNoDeps0).
var_with_deps(Indx, Deps, Deps0, NoDeps, NoDeps0) :-
array_element(clpbn, Indx, V),
clpbn:get_atts(V, [dist(_,_,VDeps)]), !,
(VDeps = [] ->
NoDeps = [V|NoDeps0], Deps = Deps0 ;
sort(VDeps,SVDeps),
Deps = [[V|SVDeps]|Deps0], NoDeps = NoDeps0 ).
extract_graph(AllVars, Graph) :-
dgraph_new(Graph0),
dgraph_add_vertices(AllVars, Graph0, Graph1),
get_edges(AllVars,Edges),
dgraph_add_edges(Edges, Graph1, Graph).
%
% this is a silly quadratic algorithm for topological sorting.
% it will have to do for now.
%
% to speedup things a bit I keep a sorted and unsorted version
% of the variables sorted so far.
%
topsort([], _, S, S, H, H) :- !.
topsort(LDeps, Sorted0, S0, SF, H, HF) :- !,
delete_all(LDeps, Sorted0, S0, LI, SI, Sorted, H, HI),
topsort(LI, Sorted, SI, SF, HI, HF).
get_edges([],[]).
get_edges([V|AllVars],Edges) :-
clpbn:get_atts(V, [dist(_,_,Parents)]),
add_parent_child(Parents,V,Edges,Edges0),
get_edges(AllVars,Edges0).
delete_all([], SS, S, [], S, SS, H, H).
delete_all([[V|VDeps]|LDeps], SS, S, LF, SF, SSF, H0, HF) :-
ord_subtract(VDeps, SS, VDepsI),
ord_subtract(VDeps, VDepsI, Parents),
add_parents_to_heap(Parents, V, H0, HI),
( VDepsI = [] ->
LF = LI, ord_add_element(SS,V,SSI), SI = [V|S];
LF = [[V|VDepsI]|LI], SI = S, SSI = SS),
delete_all(LDeps, SSI, SI, LI, SF, SSF, HI, HF).
add_parent_child([],_,Edges,Edges).
add_parent_child([P|Parents],V,[P-V|Edges],Edges0) :-
add_parent_child(Parents,V,Edges,Edges0).
add_parents_to_heap([], _, H, H).
add_parents_to_heap([P|Parents], V, H0, HF) :-
add_to_heap(H0, P, V, HI), % if I put a HF here I get the debugger to loop
add_parents_to_heap(Parents, V, HI, HF).
dump_vlist([], _, _, _).
dump_vlist([V|Vs], I, CommandStream, Answer) :-
I1 is I+1,
clpbn:get_atts(V,[key(_)]),
send_command(CommandStream, Answer, '~w = ~w;~n', ['$VAR'(I), I1]),
put_atts(V, [topord(I)]),
I1 is I+1,
dump_vlist(Vs, I1, CommandStream, Answer).
write_deps(H, CommandStream, Answer) :-
heap_to_list(H,L),
write_list_deps(L, CommandStream, Answer).
write_list_deps([], _, _).
write_list_deps([V-A|L], CommandStream, Answer) :-
fetch_same_key(L, V, SK, LN),
write_dep_relation([A|SK], V, CommandStream, Answer),
write_list_deps(LN, CommandStream, Answer).
fetch_same_key([], _, [], []) :- !.
fetch_same_key([V1-A|L], V, [A|SK], LN) :- V1 == V, !,
fetch_same_key(L, V, SK, LN).
fetch_same_key(L, _, [], L).
write_dep_relation([], _, _) :- !.
write_dep_relation([V], D, CommandStream, Answer) :- !,
get_atts(V, [topord(IV)]),
get_atts(D, [topord(ID)]),
send_command(CommandStream, Answer, "dag(~w,~w) = 1;~n", ['$VAR'(ID),'$VAR'(IV)]).
write_dep_relation(Vs, D, CommandStream, Answer) :-
get_atts(D, [topord(ID)]),
my_format(CommandStream, "dag(~w,[",['$VAR'(ID)]),
write_anc_list(Vs, start, CommandStream),
send_command(CommandStream, Answer, "]) = 1;~n", []).
write_anc_list([], _, _).
write_anc_list([V|Vs], start, CommandStream) :- !,
get_atts(V, [topord(I)]),
my_format(CommandStream, "~w",['$VAR'(I)]),
write_anc_list(Vs, cont, CommandStream).
write_anc_list([V|Vs], cont, CommandStream) :-
get_atts(V, [topord(I)]),
my_format(CommandStream, " ~w",['$VAR'(I)]),
write_anc_list(Vs, cont, CommandStream).
mknet(Vs, Key, Observables, CommandStream, Answer) :-
mknet_all_discrete(Vs, Key, Observables, CommandStream, Answer).
mknet_all_discrete(Vs, Key, Observables, CommandStream, Answer) :-
send_command(CommandStream, Answer, "discrete_nodes = 1:~w;~n",['$VAR'(Key)]),
my_format(CommandStream, "ns = [", []),
reverse(Vs, RVs),
send_var_sizes(RVs, CommandStream),
send_command(CommandStream, Answer, "];~n", []),
my_format(CommandStream, "onodes = [", []),
dump_observables(Observables, start, CommandStream),
send_command(CommandStream, Answer, "];~n", []),
send_command(CommandStream, Answer, "bnet = mk_bnet(dag, ns, 'discrete', discrete_nodes, 'observed', onodes);~n", []).
send_var_sizes([V], CommandStream) :- !,
clpbn:get_atts(V, [dist(_,Tab,_)]),
length(Tab, Sz),
my_format(CommandStream, "~w", [Sz]).
send_var_sizes([V|Vs], CommandStream) :-
clpbn:get_atts(V, [dist(_,Tab,_)]),
length(Tab, Sz),
my_format(CommandStream, "~w ", [Sz]),
send_var_sizes(Vs, CommandStream).
dump_observables([], _, _) :- !.
dump_observables([Observable|Observables], start, CommandStream) :- !,
get_atts(Observable, [topord(I)]),
my_format(CommandStream, "~w",['$VAR'(I)]),
dump_observables(Observables, mid, CommandStream).
dump_observables([Observable|Observables], mid, CommandStream) :-
get_atts(Observable, [topord(I)]),
my_format(CommandStream, " ~w",['$VAR'(I)]),
dump_observables(Observables, mid, CommandStream).
dump_cpds([], _, _).
dump_cpds([V|Vs], CommandStream, Answer) :-
clpbn:get_atts(V, [dist(Domain,_,_)]),
dump_cpds(Vs, CommandStream, Answer),
dump_dist(Domain, V, CommandStream, Answer).
%
% this is a discrete distribution
%
dump_dist(Vs, average, Ss, V, CommandStream, Answer) :- !,
vals_map(Vs, 1, Map),
get_atts(V, [topord(I)]),
my_format(CommandStream, "bnet.CPD{~w} = deterministic_CPD(bnet, ~w, inline('round(mean([",['$VAR'(I),'$VAR'(I)]),
put_atts(V, [map(Map)]),
length(Ss, Len),
dump_indices(0,Len,CommandStream),
send_command(CommandStream, Answer, "]))'));~n",[]).
dump_dist(Vs, sum, Ss, V, CommandStream, Answer) :- !,
vals_map(Vs, 1, Map),
get_atts(V, [topord(I)]),
my_format(CommandStream, "bnet.CPD{~w} = deterministic_CPD(bnet, ~w, inline('sum([",['$VAR'(I),'$VAR'(I)]),
put_atts(V, [map(Map)]),
length(Ss, Len),
dump_indices(0,Len,CommandStream),
send_command(CommandStream, Answer, "])'));~n",[]).
dump_dist(Vs, normalised_average(N), Ss, V, CommandStream, Answer) :- !,
vals_map(Vs, 1, Map),
get_atts(V, [topord(I)]),
my_format(CommandStream, "bnet.CPD{~w} = deterministic_CPD(bnet, ~w, inline('round((sum([",['$VAR'(I),'$VAR'(I)]),
put_atts(V, [map(Map)]),
length(Ss, Len),
dump_indices(0,Len,CommandStream),
N2 is N//2,
send_command(CommandStream, Answer, "])+~d)/~d)'));~n",[N2,N]).
dump_dist(Vs,Ds,Ss0, V, CommandStream, Answer) :- !,
vals_map(Vs, 1, Map),
get_atts(V, [topord(I)]),
my_format(CommandStream, "bnet.CPD{~w} = tabular_CPD(bnet, ~w, [ ",['$VAR'(I),'$VAR'(I)]),
put_atts(V, [map(Map)]),
reverse([V|Ss0], Ss),
get_numbers_for_vars(Ss, Ns),
calculate_new_numbers(Ds,Ns,0,KDs0),
keysort(KDs0,KDs),
dump_elements(KDs, CommandStream),
send_command(CommandStream, Answer, "]);~n",[]).
vals_map([], _, []).
vals_map([V|Vs], I, [[V|I]|Map]) :-
I1 is I+1,
vals_map(Vs, I1, Map).
get_numbers_for_vars(Ss, Ns) :-
numb_vars(Ss, 0, 1, VPs0),
keysort(VPs0, VPs),
compute_new_factors(VPs, 1, Int0),
keysort(Int0, Int),
select_factors(Int, [], Ns).
numb_vars([], _, _, []).
numb_vars([V|Vs], I, A0, [T-p(I,A0,L)|VPs]) :-
get_atts(V, [map(Map),topord(T)]),
length(Map,L),
I1 is I+1,
Ai is A0*L,
numb_vars(Vs, I1, Ai, VPs).
compute_new_factors([], _, []).
compute_new_factors([_-p(I,Siz,L)|VPs], Div, [I-f(Siz,Div)|Os]) :-
NDiv is Div*L,
compute_new_factors(VPs, NDiv, Os).
select_factors([], L, L).
select_factors([_-Fac|Int], Ns0, Nsf) :-
select_factors(Int, [Fac|Ns0], Nsf).
calculate_new_numbers([],_, _,[]).
calculate_new_numbers([P|Ps], Ls, I0, [Pos-P|KDs]) :-
compute_new_position(Ls, I0, 0, Pos),
number_graph([], [], I, I).
number_graph([V|SortedGraph], [I|Is], I0, IF) :-
I is I0+1,
calculate_new_numbers(Ps, Ls, I, KDs).
compute_new_position([], _, P, P).
compute_new_position([f(Siz,Div)|Ls], I0, P0, Pf) :-
A is I0 // Siz,
I1 is I0 mod Siz,
B is A*Div,
Pi is P0+B,
compute_new_position(Ls, I1, Pi, Pf).
dump_indices(Len,Len,_) :- !.
dump_indices(I0,Len,CommandStream) :-
I is I0+1,
my_format(CommandStream, "x(~d) ",[I]),
dump_indices(I,Len,CommandStream).
dump_elements([], _).
dump_elements([_-P|KDs], CommandStream) :-
my_format(CommandStream, "~w~n",[P]),
dump_elements(KDs, CommandStream).
dump_problist([], _).
dump_problist([P|KDs], CommandStream) :-
my_format(CommandStream, "~w~n",[P]),
dump_problist(KDs, CommandStream).
dump_dlist((D1;D2), Start, CommandStream) :- !,
dump_dlist(D1, Start, CommandStream),
dump_dlist(D2, mid, CommandStream).
dump_dlist((_ = V), Pos, CommandStream) :- !,
dump_dlist(V, Pos, CommandStream).
dump_dlist((_ -> V), Pos, CommandStream) :- !,
dump_dlist(V, Pos, CommandStream).
dump_dlist(V, start, CommandStream) :- !,
my_format(CommandStream, "~w~n",[V]).
dump_dlist(V, mid, CommandStream) :-
my_format(CommandStream, "~w~n",[V]).
find_map((D1;D2), I0, N, LF, L0) :- !,
find_map(D1, I0, I, LF, LI),
find_map(D2, I, N, LI, L0).
find_map((M->_), I, I1, [[M|I]|L0], L0) :-
I1 is I+1.
put_atts(V, [bnt_id(I)]),
number_graph(SortedGraph, Is, I, IF).
inf_engine(Vs, Key, CommandStream, Answer) :-
send_command(CommandStream, Answer, "engine = jtree_inf_engine(bnet)~n", []),
% send_command(CommandStream, Answer, "engine = var_elim_inf_engine(bnet)~n", []),
send_command(CommandStream, Answer, "evidence = cell(1,~w)~n", ['$VAR'(Key)]),
dump_evidence(Vs, CommandStream, Answer),
send_command(CommandStream, Answer, "[engine, loglik] = enter_evidence(engine, evidence)~n",[]).
init_bnet(SortedGraph, NumberedGraph, Size) :-
build_dag(SortedGraph, Size),
matlab_sequence(1,Size,discrete_nodes),
mksizes(SortedGraph, Size),
bnet <-- mk_bnet(dag, node_sizes, \discrete, discrete_nodes),
dump_cpts(SortedGraph, NumberedGraph).
dump_evidence([], _, _).
dump_evidence([V|Vs], CommandStream, Answer) :-
clpbn:get_atts(V, [evidence(Ev)]), !,
get_atts(V, [topord(I),map(M)]), !,
follow_map(M,Ev,NEv),
send_command(CommandStream, Answer, "evidence{~w} = ~w~n", ['$VAR'(I),NEv]),
dump_evidence(Vs, CommandStream, Answer).
dump_evidence([_|Vs], CommandStream, Answer) :-
dump_evidence(Vs, CommandStream, Answer).
build_dag(SortedVertices, Size) :-
get_numbered_edges(SortedVertices, Edges),
mkdag(Size, Edges).
follow_map([[K|V]|_], K, V) :- !.
follow_map([_|Map], K, V) :- !,
follow_map(Map, K, V).
get_numbered_edges([], []).
get_numbered_edges([V|SortedVertices], Edges) :-
clpbn:get_atts(V, [dist(_,_,Ps)]),
v2number(V,N),
add_numbered_edges(Ps, N, Edges, Edges0),
get_numbered_edges(SortedVertices, Edges0).
find_observables([], []).
find_observables([Var|GVars], [Var|Observables]) :-
clpbn:get_atts(Var, [dist(_,_,_)]), !,
find_observables(GVars, Observables).
find_observables([_|GVars], Observables) :-
find_observables(GVars, Observables).
add_numbered_edges([], _, Edges, Edges).
add_numbered_edges([P|Ps], N, [PN-N|Edges], Edges0) :-
v2number(P,PN),
add_numbered_edges(Ps, N, Edges, Edges0).
output_answer(Observables, CommandStream, Answer) :-
split_by_cliques(Observables, Cliques),
output_cliques(Cliques, CommandStream, Answer).
v2number(V,N) :-
get_atts(V,[bnt_id(N)]).
split_by_cliques([], []).
split_by_cliques([V|Vs], Cliques) :-
split_by_cliques(Vs, Cliques0),
add_to_cliques(Cliques0, V, Cliques).
mkdag(N,Els) :-
Tot is N*N,
functor(Dag,dag,Tot),
add_els(Els,N,Dag),
Dag=..[_|L],
addzeros(L),
matlab_matrix(N,N,L,dag).
add_to_cliques([], V, [[V]]).
add_to_cliques([Cl|L], V, [[V|Cl]|L]) :-
in_clique(Cl,V), !.
add_to_cliques([Cl|L], V, [Cl|LN]) :-
add_to_cliques(L, V, LN).
add_els([],_,_).
add_els([X-Y|Els],N,Dag) :-
Pos is (X-1)*N+Y,
arg(Pos,Dag,1),
add_els(Els,N,Dag).
in_clique([], _).
in_clique([V1|L], V) :-
child(V, V1), !,
in_clique(L, V).
in_clique([V1|L], V) :-
child(V1, V),
in_clique(L,V).
addzeros([]).
addzeros([0|L]) :- !,
addzeros(L).
addzeros([1|L]) :-
addzeros(L).
child(V,V1) :-
clpbn:get_atts(V, [dist(_,_,LVs)]),
varmember(LVs, V1).
mksizes(SortedVertices, Size) :-
get_szs(SortedVertices,Sizes),
matlab_matrix(1,Size,Sizes,node_sizes).
varmember([H|_], V1) :- H == V1, !.
varmember([_|L], V1) :-
varmember(L, V1).
get_szs([],[]).
get_szs([V|SortedVertices],[LD|Sizes]) :-
clpbn:get_atts(V, [dist(Dom,_,_)]),
length(Dom,LD),
get_szs(SortedVertices,Sizes).
output_cliques([], _, _).
output_cliques([Observables|Cliques], CommandStream, Answer) :-
marginal(Observables, CommandStream, Answer),
read_answer(Answer, -1, MargDis),
parse_observables(Observables, MargDis),
output_cliques(Cliques, CommandStream, Answer).
dump_cpts([], []).
dump_cpts([V|SortedGraph], [I|Is]) :-
clpbn:get_atts(V, [dist(_,CPT,_)]),
mkcpt(bnet,I,CPT),
dump_cpts(SortedGraph, Is).
marginal(Margs, CommandStream, Answer) :-
my_format(CommandStream, "marg = marginal_nodes(engine, ", []),
write_margs(Margs, CommandStream),
send_command(CommandStream, Answer, ")~n", []),
my_format(CommandStream, "p = marg.T~n", []).
mkcpt(BayesNet, V, Tab) :-
(BayesNet.'CPD'({V})) <-- tabular_CPD(BayesNet,V,Tab).
set_inference :-
bnt_solver(Solver),
init_solver(Solver).
write_margs([], _) :- !.
write_margs([V], CommandStream) :- !,
get_atts(V, [topord(IV)]),
my_format(CommandStream, "~w", ['$VAR'(IV)]).
write_margs(Vs, CommandStream) :-
my_format(CommandStream, "[", []),
write_anc_list(Vs, start, CommandStream),
my_format(CommandStream, "]", []).
init_solver(jtree) :-
engine <-- jtree_inf_engine(bnet).
init_solver(belprop) :-
engine <-- belprop_inf_engine(bnet).
init_solver(likelihood_weighting) :-
engine <-- likelihood_weighting_inf_engine(bnet).
init_solver(enumerative) :-
engine <-- enumerative_inf_engine(bnet).
init_solver(gibbs) :-
engine <-- gibbs_inf_engine(bnet).
init_solver(global_joint) :-
engine <-- global_joint_inf_engine(bnet).
init_solver(pearl) :-
engine <-- pearl_inf_engine(bnet).
init_solver(var_elim) :-
engine <-- var_elim_inf_engine(bnet).
add_evidence(Graph, Size, Is) :-
mk_evidence(Graph, Is, LN),
matlab_initialized_cells( 1, Size, LN, evidence),
[engine, loglik] <-- enter_evidence(engine, evidence).
mk_evidence([], [], []).
mk_evidence([V|L], [I|Is], [ar(1,I,Val)|LN]) :-
clpbn:get_atts(V, [evidence(Ev),dist(Domain,_,_)]), !,
evidence_val(Ev,1,Domain,Val),
mk_evidence(L, Is, LN).
mk_evidence([_|L], [_|Is], LN) :-
mk_evidence(L, Is, LN).
read_answer(Answer, C0, [C1|L]) :-
get0(Answer, C1),
put(user_error, C1),
( (( C0 = 10 ; C0 = 85) ,C1 = 62) ->
L = []
;
read_answer(Answer, C1, L)
).
evidence_val(Ev,Val,[Ev|_],Val) :- !.
evidence_val(Ev,I0,[_|Domain],Val) :-
I1 is I0+1,
evidence_val(Ev,I1,Domain,Val).
wait_for_matlab_prompt(Answer) :-
fetch_prompt(Answer, -1).
fetch_prompt(Answer, C0) :-
get0(Answer, C1),
put(user_error, C1),
( ((C0 = 62 ; C0 = 85) ,C1 = 62) ->
true
;
fetch_prompt(Answer, C1)
).
send_command(OStream, IStream, String, Args) :-
my_format(OStream, String, Args),
wait_for_matlab_prompt(IStream).
parse_observables([Obs], MargDis) :- !,
get_atts(Obs, [map(Map)]),
skip_to_eq(MargDis, L1),
fetch_map(Map, L1, Out),
clpbn:get_atts(Obs, [key(Key)]),
Obs = {Key:Out}.
parse_observables(LObs, MargDis) :-
joint_map(LObs, Map),
skip_to_eq(MargDis, L1),
fetch_maps(Map, L1, Out),
bind_lobs(LObs, Key, Key, Out).
fetch_map([[Name|_]], L, (Name -> P)) :- !,
get_next_float(L, P, _).
fetch_map([[Name|_]|Names], L0, (Name->P ; Rest)) :-
get_next_float(L0, P, Lf),
fetch_map(Names, Lf, Rest).
get_next_float(L0, P, Lf) :-
skip_spaces(L0, Li),
fetch_float(Li,Ls, Lf),
number_codes(P, Ls).
skip_to_eq([61|L], L) :- !.
skip_to_eq([_|L], LF) :-
skip_to_eq(L, LF).
skip_spaces([10|L], LF) :- !, skip_spaces(L, LF).
skip_spaces([32|L], LF) :- !, skip_spaces(L, LF).
skip_spaces(L, L).
fetch_float([10|L], [], L) :- !.
fetch_float([32|L], [], L) :- !.
fetch_float([C|Li], [C|Ls], Lf) :-
fetch_float(Li, Ls, Lf).
joint_map(Vars,FMap) :-
fetch_maps(Vars,Maps),
join_maps(Maps, FMap).
fetch_maps([], []).
fetch_maps([V|Vs], [M|Ms]) :-
get_atts(V, [map(M)]),
fetch_maps(Vs, Ms).
join_maps([], [[]]).
join_maps([Map|Maps], Rf) :-
join_maps(Maps, R1),
add(Map, R1, Rf).
add([], _, []).
add([[Name|_]|R], R1, RsF) :-
add_head(R1, Name, RsF, Rs0),
add(R, R1, Rs0).
add_head([], _, Rs, Rs).
add_head([H|L], A, [[A|H]|Rs], Rs0) :-
add_head(L, A, Rs, Rs0).
fetch_maps([Name1], L, (Name2 -> P)) :- !,
generate_name(Name1, Name2),
get_next_float(L, P, _).
fetch_maps([Name1|Names], L0, (Name2->P ; Rest)) :-
generate_name(Name1, Name2),
get_next_float(L0, P, Lf),
fetch_maps(Names, Lf, Rest).
generate_name([Name], Name) :- !.
generate_name([Name|Names], (Name,New)) :-
generate_name(Names, New).
bind_lobs([Obs], Key, FullKey, Out) :- !,
clpbn:get_atts(Obs, [key(Key)]),
Obs = {FullKey:Out}.
bind_lobs([Obs|Lobs], (Key,Rest), FullKey, Out) :-
clpbn:get_atts(Obs, [key(Key)]),
Obs = {FullKey:Out},
bind_lobs(Lobs, Rest, FullKey, Out).
my_format(Stream, String, Args) :-
format(user_error, String, Args),
format(Stream, String, Args).
marginalize([V], Ps) :- !,
v2number(V,Pos),
marg <-- marginal_nodes(engine, Pos),
matlab_get_variable( marg.'T', Ps).

View File

@ -1,3 +1,4 @@
%
% adapted from Hendrik Blockeel's ILP04 paper.
%

View File

@ -17,7 +17,7 @@
:- module(vel, [vel/3,
check_if_vel_done/1]).
:- attribute size/1, posterior/4, all_diffs/1.
:- attribute size/1, all_diffs/1.
:- use_module(library(ordsets), [ord_union/3]).
@ -29,6 +29,9 @@
clpbn_not_var_member/2,
check_for_hidden_vars/3]).
:- use_module(library('clpbn/display'), [
clpbn_bind_vals/3]).
:- use_module(library('clpbn/discrete_utils'), [
project_from_CPT/3,
reorder_CPT/5,
@ -36,8 +39,7 @@
:- use_module(library(lists),
[
append/3,
member/2
append/3
]).
check_if_vel_done(Var) :-
@ -59,7 +61,7 @@ do_vel(LVs,Vs0,AllDiffs) :-
process(LVi, LVs, tab(Dist,_,_)),
Dist =.. [_|Ps0],
normalise(Ps0,Ps),
bind_vals(LVs,Ps,AllDiffs).
clpbn_bind_vals(LVs,Ps,AllDiffs).
%
% some variables might already have evidence in the data-base.
@ -67,7 +69,7 @@ do_vel(LVs,Vs0,AllDiffs) :-
get_rid_of_ev_vars([],[]).
get_rid_of_ev_vars([V|LVs0],LVs) :-
clpbn:get_atts(V, [evidence(Ev)]), !,
put_atts(V, [posterior([],Ev,[],[])]), !,
clpbn_display:put_atts(V, [posterior([],Ev,[],[])]), !,
get_rid_of_ev_vars(LVs0,LVs).
get_rid_of_ev_vars([V|LVs0],[V|LVs]) :-
get_rid_of_ev_vars(LVs0,LVs).
@ -252,32 +254,6 @@ update_tables([tab(Tab0,Vs,Sz)|Tabs],[tab(Tab0,Vs,Sz)|NTabs],Table,V) :-
update_tables([_|Tabs],NTabs,Table,V) :-
update_tables(Tabs,NTabs,Table,V).
bind_vals([],_,_) :- !.
% simple case, we want a distribution on a single variable.
%bind_vals([V],Ps) :- !,
% clpbn:get_atts(V, [dist(Vals,_,_)]),
% put_atts(V, posterior([V], Vals, Ps)).
% complex case, we want a joint distribution, do it on a leader.
% should split on cliques ?
bind_vals(Vs,Ps,AllDiffs) :-
get_all_combs(Vs, Vals),
Vs = [V|_],
put_atts(V, posterior(Vs, Vals, Ps, AllDiffs)).
get_all_combs(Vs, Vals) :-
get_all_doms(Vs,Ds),
findall(L,ms(Ds,L),Vals).
get_all_doms([], []).
get_all_doms([V|Vs], [D|Ds]) :-
clpbn:get_atts(V, [dist(D,_,_)]),
get_all_doms(Vs, Ds).
ms([], []).
ms([H|L], [El|Els]) :-
member(El,H),
ms(L, Els).
normalise(Ps0,Ps) :-
add_all(Ps0,0.0,Sum),
divide_by_sum(Ps0,Sum,Ps).
@ -293,31 +269,6 @@ divide_by_sum([P|Ps0],Sum,[PN|Ps]) :-
divide_by_sum(Ps0,Sum,Ps).
%
% what is actually output
%
attribute_goal(V, G) :-
get_atts(V, [posterior(Vs,Vals,Ps,AllDiffs)]),
massage_out(Vs, Vals, Ps, G, AllDiffs, V).
massage_out([], Ev, _, V=Ev, _, V) :- !.
massage_out(Vs, [D], [P], p(CEqs)=P, AllDiffs, _) :- !,
gen_eqs(Vs,D,Eqs),
add_alldiffs(AllDiffs,Eqs,CEqs).
massage_out(Vs, [D|Ds], [P|Ps], (p(CEqs)=P,G) , AllDiffs, V) :-
gen_eqs(Vs,D,Eqs),
add_alldiffs(AllDiffs,Eqs,CEqs),
massage_out(Vs, Ds, Ps, G, AllDiffs, V).
gen_eqs([V], [D], (V=D)) :- !.
gen_eqs([V], D, (V=D)) :- !.
gen_eqs([V|Vs], [D|Ds], ((V=D),Eqs)) :-
gen_eqs(Vs,Ds,Eqs).
add_alldiffs([],Eqs,Eqs).
add_alldiffs(AllDiffs,Eqs,(Eqs/alldiff(AllDiffs))).
vel_get_dist_size(V,Sz) :-
get_atts(V, [size(Sz)]), !.
vel_get_dist_size(V,Sz) :-

View File

@ -16,6 +16,8 @@
<h2>Yap-5.1.3:</h2>
<ul>
<li> FIXED: CLP(BN) with BNT.</li>
<li> FIXED: allow dgraphs with graphs of vars.</li>
<li> FIXED: add matrix library documentation.</li>
<li> NEW: start atom_gc by default.</li>
<li> FIXED: array add was not working for dyamic arrays (obs from Rui

View File

@ -85,7 +85,8 @@ all_vertices_in_edges([V1-V2|Edges],[V1,V2|Vertices]) :-
all_vertices_in_edges(Edges,Vertices).
edges2graphl([], [], []).
edges2graphl([V|Vertices], [V-V1|SortedEdges], [V-[V1|Children]|GraphL]) :- !,
edges2graphl([V|Vertices], [VV-V1|SortedEdges], [V-[V1|Children]|GraphL]) :-
V == VV, !,
get_extra_children(SortedEdges,V,Children,RemEdges),
edges2graphl(Vertices, RemEdges, GraphL).
edges2graphl([V|Vertices], SortedEdges, [V-[]|GraphL]) :-
@ -101,7 +102,7 @@ dgraph_add_edges([V|Vs],Es) --> !,
dgraph_update_vertex(V,[]),
dgraph_add_edges(Vs,Es).
get_extra_children([V-C|Es],V,[C|Children],REs) :- !,
get_extra_children([V-C|Es],VV,[C|Children],REs) :- V == VV, !,
get_extra_children(Es,V,Children,REs).
get_extra_children(Es,_,[],Es).
@ -318,14 +319,14 @@ dup([_|AllVs], [_|Q]) :-
dup(AllVs, Q).
start_queue([], [], RQ, RQ).
start_queue([V|AllVs], [V-e(S,B,S,E)|InvertedEdges], Q, RQ) :- !,
link_edges(InvertedEdges, V, B, S, E, RemainingEdges),
start_queue([V|AllVs], [VV-e(S,B,S,E)|InvertedEdges], Q, RQ) :- V == VV, !,
link_edges(InvertedEdges, VV, B, S, E, RemainingEdges),
start_queue(AllVs, RemainingEdges, Q, RQ).
start_queue([V|AllVs], InvertedEdges, [V|Q], RQ) :-
start_queue(AllVs, InvertedEdges, Q, RQ).
link_edges([V-e(A,B,S,E)|InvertedEdges], V, A, S, E, RemEdges) :- !,
link_edges(InvertedEdges, V, B, S, E, RemEdges).
link_edges([V-e(A,B,S,E)|InvertedEdges], VV, A, S, E, RemEdges) :- V == VV, !,
link_edges(InvertedEdges, VV, B, S, E, RemEdges).
link_edges(RemEdges, _, A, _, A, RemEdges).
continue_queue([], _, []).

View File

@ -19,7 +19,8 @@
matlab_item/4,
matlab_item1/3,
matlab_item1/4,
matlab_sequence/3]).
matlab_sequence/3,
matlab_call/2]).
:- ensure_loaded(library(lists)).
@ -74,12 +75,14 @@ build_outputs([Out|Outs],[Out,' '|L],L0) :-
build_outputs(Outs,L,L0).
build_args([],L,L).
build_args([Arg],Lf,L0) :-
build_args([Arg],Lf,L0) :- !,
build_arg(Arg,Lf,[')'|L0]).
build_args([Arg|Args],L,L0) :-
build_arg(Arg,L,[', '|L1]),
build_args(Args,L1,L0).
build_arg(V,_,_) :- var(V), !,
throw(error(instantiation_error)).
build_arg(Arg,[Arg|L],L) :- atomic(Arg), !.
build_arg(\S0,['\'',S0,'\''|L],L) :-
atom(S0), !.
@ -96,7 +99,8 @@ build_arg(F,[N,'{'|L],L0) :- %N({A}) = N{A}
F=..[N,{A}], !,
build_arg(A,L,['}'|L0]).
build_arg(F,[N,'('|L],L0) :-
build_args(A,L,L0).
F=..[N|As],
build_args(As,L,L0).
build_arglist([A],L,L0) :- !,
build_arg(A,L,[' ]'|L0]).