more pfl changes.
This commit is contained in:
parent
8c2dd1f847
commit
c5f42cd7eb
@ -31,6 +31,7 @@ Va <- P*X1*Y1 + Q*X2*Y2 + ...
|
|||||||
[dist/4,
|
[dist/4,
|
||||||
get_dist_domain/2,
|
get_dist_domain/2,
|
||||||
get_dist_domain_size/2,
|
get_dist_domain_size/2,
|
||||||
|
get_dist_all_sizes/2,
|
||||||
get_dist_params/2
|
get_dist_params/2
|
||||||
]).
|
]).
|
||||||
|
|
||||||
@ -54,6 +55,10 @@ Va <- P*X1*Y1 + Q*X2*Y2 + ...
|
|||||||
|
|
||||||
:- use_module(library(rbtrees)).
|
:- use_module(library(rbtrees)).
|
||||||
|
|
||||||
|
:- use_module(library(bhash)).
|
||||||
|
|
||||||
|
:- use_module(library(matrix)).
|
||||||
|
|
||||||
:- dynamic network_counting/1.
|
:- dynamic network_counting/1.
|
||||||
|
|
||||||
:- attribute order/1.
|
:- attribute order/1.
|
||||||
@ -120,15 +125,16 @@ get_vars_info([_|MoreVs], Vs0, VsF, Ps0, PsF, VarsInfo, Lvs, Outs) :-
|
|||||||
%
|
%
|
||||||
% let's have some fun with avg
|
% let's have some fun with avg
|
||||||
%
|
%
|
||||||
get_var_info(V, avg(Domain), Parents0, Vs, Vs2, Ps, Ps, Lvs, Outs, DIST) :- !,
|
get_var_info(V, avg(Domain), Parents, Vs, Vs2, Ps, Ps, Lvs, Outs, DIST) :- !,
|
||||||
reorder_vars(Parents0, Parents),
|
|
||||||
length(Domain, DSize),
|
length(Domain, DSize),
|
||||||
run_though_avg(V, DSize, Domain, Parents, Vs, Vs2, Lvs, Outs, DIST).
|
% run_though_avg(V, DSize, Domain, Parents, Vs, Vs2, Lvs, Outs, DIST).
|
||||||
|
top_down_with_tabling(V, DSize, Domain, Parents, Vs, Vs2, Lvs, Outs, DIST).
|
||||||
% bup_avg(V, DSize, Domain, Parents, Vs, Vs2, Lvs, Outs, DIST).
|
% bup_avg(V, DSize, Domain, Parents, Vs, Vs2, Lvs, Outs, DIST).
|
||||||
% standard random variable
|
% standard random variable
|
||||||
get_var_info(V, DistId, Parents, Vs, Vs2, Ps, Ps1, Lvs, Outs, DIST) :-
|
get_var_info(V, DistId, Parents0, Vs, Vs2, Ps, Ps1, Lvs, Outs, DIST) :-
|
||||||
% clpbn:get_atts(V, [key(K)]), writeln(V:K:DistId:Parents),
|
% clpbn:get_atts(V, [key(K)]), writeln(V:K:DistId:Parents),
|
||||||
check_p(DistId, Parms, _ParmVars, Ps, Ps1),
|
reorder_vars(Parents0, Parents, Map),
|
||||||
|
check_p(DistId, Map, Parms, _ParmVars, Ps, Ps1),
|
||||||
unbound_parms(Parms, ParmVars),
|
unbound_parms(Parms, ParmVars),
|
||||||
check_v(V, DistId, DIST, Vs, Vs1),
|
check_v(V, DistId, DIST, Vs, Vs1),
|
||||||
DIST = info(V, Tree, Ev, Values, Formula, ParmVars, Parms),
|
DIST = info(V, Tree, Ev, Values, Formula, ParmVars, Parms),
|
||||||
@ -139,26 +145,34 @@ get_var_info(V, DistId, Parents, Vs, Vs2, Ps, Ps1, Lvs, Outs, DIST) :-
|
|||||||
get_evidence(V, Tree, Ev, Formula0, Formula, Lvs, Outs).
|
get_evidence(V, Tree, Ev, Formula0, Formula, Lvs, Outs).
|
||||||
%, (numbervars(Formula,0,_),writeln(formula:Formula), fail ; true)
|
%, (numbervars(Formula,0,_),writeln(formula:Formula), fail ; true)
|
||||||
|
|
||||||
reorder_vars(Vs, OVs) :-
|
%
|
||||||
add_pos(Vs, PVs),
|
% reorder all variables and make sure we get a
|
||||||
|
% map of how the transfer was done.
|
||||||
|
%
|
||||||
|
% position zero is output
|
||||||
|
%
|
||||||
|
reorder_vars(Vs, OVs, Map) :-
|
||||||
|
add_pos(Vs, 1, PVs),
|
||||||
keysort(PVs, SVs),
|
keysort(PVs, SVs),
|
||||||
remove_key(SVs, OVs).
|
remove_key(SVs, OVs, Map).
|
||||||
|
|
||||||
add_pos([], []).
|
add_pos([], _, []).
|
||||||
add_pos([V|Vs], [K-V|PVs]) :-
|
add_pos([V|Vs], I0, [K-(I0,V)|PVs]) :-
|
||||||
get_atts(V,[order(K)]),
|
get_atts(V,[order(K)]),
|
||||||
add_pos(Vs, PVs).
|
I is I0+1,
|
||||||
|
add_pos(Vs, I, PVs).
|
||||||
|
|
||||||
remove_key([], []).
|
remove_key([], [], []).
|
||||||
remove_key([_-V|SVs], [V|OVs]) :-
|
remove_key([_-(I,V)|SVs], [V|OVs], [I|Map]) :-
|
||||||
remove_key(SVs, OVs).
|
remove_key(SVs, OVs, Map).
|
||||||
|
|
||||||
%%%%%%%%%%%%%%%%%%%%%%%%%
|
%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||||
%
|
%
|
||||||
% use top-down to generate average
|
% use top-down to generate average
|
||||||
%
|
%
|
||||||
run_though_avg(V, 3, Domain, Parents, Vs, Vs2, Lvs, Outs, DIST) :-
|
run_though_avg(V, 3, Domain, Parents0, Vs, Vs2, Lvs, Outs, DIST) :-
|
||||||
check_v(V, avg(Domain,Parents), DIST, Vs, Vs1),
|
reorder_vars(Parents0, Parents, _Map),
|
||||||
|
check_v(V, avg(Domain,Parents0), DIST, Vs, Vs1),
|
||||||
DIST = info(V, Tree, Ev, [V0,V1,V2], Formula, [], []),
|
DIST = info(V, Tree, Ev, [V0,V1,V2], Formula, [], []),
|
||||||
get_parents(Parents, PVars, Vs1, Vs2),
|
get_parents(Parents, PVars, Vs1, Vs2),
|
||||||
length(Parents, N),
|
length(Parents, N),
|
||||||
@ -229,17 +243,81 @@ not_satisf(I0, I1, I2, IR, N0, N1, N2, R, Exp) :-
|
|||||||
|
|
||||||
%%%%%%%%%%%%%%%%%%%%%%%%%
|
%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||||
%
|
%
|
||||||
% use bottom-up dynamic programming to generate average
|
% use top-down to generate average
|
||||||
%
|
%
|
||||||
bup_avg(V, Size, Domain, Parents, Vs, Vs2, Lvs, Outs, DIST) :-
|
top_down_with_tabling(V, Size, Domain, Parents0, Vs, Vs2, Lvs, Outs, DIST) :-
|
||||||
|
reorder_vars(Parents0, Parents, _Map),
|
||||||
check_v(V, avg(Domain,Parents), DIST, Vs, Vs1),
|
check_v(V, avg(Domain,Parents), DIST, Vs, Vs1),
|
||||||
DIST = info(V, Tree, Ev, OVs, Formula, [], []),
|
DIST = info(V, Tree, Ev, OVs, Formula, [], []),
|
||||||
get_parents(Parents, PVars, Vs1, Vs2),
|
get_parents(Parents, PVars, Vs1, Vs2),
|
||||||
% generate_sums(PVars, Size, Max, Sums, F0),
|
|
||||||
bin_sums(PVars, Sums, F00),
|
|
||||||
reverse(F00,F0),
|
|
||||||
length(Parents, N),
|
length(Parents, N),
|
||||||
Max is (Size-1)*N, % This should be true
|
Max is (Size-1)*N, % This should be true
|
||||||
|
avg_borders(0, Size, Max, Borders),
|
||||||
|
b_hash_new(H0),
|
||||||
|
avg_trees(0, Max, PVars, Size, F1, 0, Borders, OVs, Ev, H0, H),
|
||||||
|
generate_avg_code(H, Formula, F),
|
||||||
|
% Formula0 = [V0=F0*Ev0,V2=F2*Ev2,V1=not(F0+F2)*Ev1],
|
||||||
|
% Ev = [Ev0,Ev1,Ev2],
|
||||||
|
get_evidence(V, Tree, Ev, F1, F, Lvs, Outs).
|
||||||
|
|
||||||
|
avg_trees(Size, _, _, Size, F0, _, F0, [], [], H, H) :- !.
|
||||||
|
avg_trees(I0, Max, PVars, Size, [V=O*E|F0], Im, [IM|Borders], [V|OVs], [E|Ev], H0, H) :-
|
||||||
|
I is I0+1,
|
||||||
|
avg_tree(PVars, 0, Max, Im, IM, Size, O, H0, HI),
|
||||||
|
Im1 is IM+1,
|
||||||
|
avg_trees(I, Max, PVars, Size, F0, Im1, Borders, OVs, Ev, HI, H).
|
||||||
|
|
||||||
|
avg_tree( _PVars, P, _, Im, IM, _Size, O, H0, H0) :-
|
||||||
|
b_hash_lookup(k(P,Im,IM), O=_Exp, H0), !.
|
||||||
|
avg_tree([], _P, _Max, _Im, _IM, _Size, 1, H, H).
|
||||||
|
avg_tree([Vals|PVars], P, Max, Im, IM, Size, O, H0, HF) :-
|
||||||
|
b_hash_insert(H0, k(P,Im,IM), O=Simp, HI),
|
||||||
|
MaxI is Max-(Size-1),
|
||||||
|
avg_exp(Vals, PVars, 0, P, MaxI, Size, Im, IM, HI, HF, Exp),
|
||||||
|
simplify_exp(Exp, Simp).
|
||||||
|
|
||||||
|
avg_exp([], _, _, _P, _Max, _Size, _Im, _IM, H, H, 0).
|
||||||
|
avg_exp([Val|Vals], PVars, I0, P0, Max, Size, Im, IM, HI, HF, O) :-
|
||||||
|
(Vals = [] -> O=O1 ; O = Val*O1+not(Val)*O2 ),
|
||||||
|
Im1 is max(0, Im-I0),
|
||||||
|
IM1 is IM-I0,
|
||||||
|
( IM1 < 0 -> O1 = 0, H2 = HI; /* we have exceed maximum */
|
||||||
|
Im1 > Max -> O1 = 0, H2 = HI; /* we cannot make to minimum */
|
||||||
|
Im1 = 0, IM1 > Max -> O1 = 1, H2 = HI; /* we cannot exceed maximum */
|
||||||
|
P is P0+1,
|
||||||
|
avg_tree(PVars, P, Max, Im1, IM1, Size, O1, HI, H2)
|
||||||
|
),
|
||||||
|
I is I0+1,
|
||||||
|
avg_exp(Vals, PVars, I, P0, Max, Size, Im, IM, H2, HF, O2).
|
||||||
|
|
||||||
|
generate_avg_code(H, Formula, Formula0) :-
|
||||||
|
b_hash_to_list(H,L),
|
||||||
|
sort(L, S),
|
||||||
|
strip_and_add(S, Formula0, Formula).
|
||||||
|
|
||||||
|
strip_and_add([], F, F).
|
||||||
|
strip_and_add([_-Exp|S], F0, F) :-
|
||||||
|
strip_and_add(S, [Exp|F0], F).
|
||||||
|
|
||||||
|
%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||||
|
%
|
||||||
|
% use bottom-up dynamic programming to generate average
|
||||||
|
%
|
||||||
|
bup_avg(V, Size, Domain, Parents0, Vs, Vs2, Lvs, Outs, DIST) :-
|
||||||
|
reorder_vars(Parents0, Parents, _),
|
||||||
|
check_v(V, avg(Domain,Parents), DIST, Vs, Vs1),
|
||||||
|
DIST = info(V, Tree, Ev, OVs, Formula, [], []),
|
||||||
|
get_parents(Parents, PVars, Vs1, Vs2),
|
||||||
|
length(Parents, N),
|
||||||
|
Max is (Size-1)*N, % This should be true
|
||||||
|
ArraySize is Max+1,
|
||||||
|
functor(Protected, protected, ArraySize),
|
||||||
|
avg_domains(0, Size, 0, Max, LDomains),
|
||||||
|
Domains =.. [d|LDomains],
|
||||||
|
Reach is (Size-1),
|
||||||
|
generate_sums(PVars, Size, Max, Reach, Protected, Domains, ArraySize, Sums, F0),
|
||||||
|
% bin_sums(PVars, Sums, F00),
|
||||||
|
% reverse(F00,F0),
|
||||||
% easier to do recursion on lists
|
% easier to do recursion on lists
|
||||||
Sums =.. [_|LSums],
|
Sums =.. [_|LSums],
|
||||||
generate_avg(0, Size, 0, Max, LSums, OVs, Ev, F1, []),
|
generate_avg(0, Size, 0, Max, LSums, OVs, Ev, F1, []),
|
||||||
@ -247,9 +325,11 @@ bup_avg(V, Size, Domain, Parents, Vs, Vs2, Lvs, Outs, DIST) :-
|
|||||||
get_evidence(V, Tree, Ev, F1, F2, Lvs, Outs),
|
get_evidence(V, Tree, Ev, F1, F2, Lvs, Outs),
|
||||||
append(RF0, F2, Formula).
|
append(RF0, F2, Formula).
|
||||||
|
|
||||||
|
%
|
||||||
|
% use binary approach, like what is standard
|
||||||
|
%
|
||||||
bin_sums(Vs, Sums, F) :-
|
bin_sums(Vs, Sums, F) :-
|
||||||
vs_to_sums(Vs, Sums0),
|
vs_to_sums(Vs, Sums0),
|
||||||
writeln(init:Sums0),
|
|
||||||
bin_sums(Sums0, Sums, F, []).
|
bin_sums(Sums0, Sums, F, []).
|
||||||
|
|
||||||
vs_to_sums([], []).
|
vs_to_sums([], []).
|
||||||
@ -258,15 +338,21 @@ vs_to_sums([V|Vs], [Sum|Sums0]) :-
|
|||||||
vs_to_sums(Vs, Sums0).
|
vs_to_sums(Vs, Sums0).
|
||||||
|
|
||||||
bin_sums([Sum], Sum) --> !.
|
bin_sums([Sum], Sum) --> !.
|
||||||
bin_sums(LSums, Sums) -->
|
bin_sums(LSums, Sum) -->
|
||||||
pack_bins(LSums, Sums1),
|
{ halve(LSums, Sums1, Sums2) },
|
||||||
bin_sums(Sums1, Sums).
|
bin_sums(Sums1, Sum1),
|
||||||
|
bin_sums(Sums2, Sum2),
|
||||||
|
sum(Sum1, Sum2, Sum).
|
||||||
|
|
||||||
pack_bins([], []) --> [].
|
halve(LSums, Sums1, Sums2) :-
|
||||||
pack_bins([Sum], [Sum]) --> [].
|
length(LSums, L),
|
||||||
pack_bins([Sum1,Sum2|LSums], [Sum|NSums]) -->
|
Take is L div 2,
|
||||||
sum(Sum1, Sum2, Sum),
|
head(Take, LSums, Sums1, Sums2).
|
||||||
pack_bins(LSums, NSums).
|
|
||||||
|
head(0, L, [], L) :- !.
|
||||||
|
head(Take, [H|L], [H|Sums1], Sum2) :-
|
||||||
|
Take1 is Take-1,
|
||||||
|
head(Take1, L, Sums1, Sum2).
|
||||||
|
|
||||||
sum(Sum1, Sum2, Sum) -->
|
sum(Sum1, Sum2, Sum) -->
|
||||||
{ functor(Sum1, _, M1),
|
{ functor(Sum1, _, M1),
|
||||||
@ -278,44 +364,73 @@ sum(Sum1, Sum2, Sum) -->
|
|||||||
Sum1 =.. [_|PVals] },
|
Sum1 =.. [_|PVals] },
|
||||||
expand_sums(PVals, 0, Max0, Max1, M2, Sum2, Sum).
|
expand_sums(PVals, 0, Max0, Max1, M2, Sum2, Sum).
|
||||||
|
|
||||||
generate_sums([PVals], Size, Max, Sum, []) :- !,
|
%
|
||||||
|
% bottom up step by step
|
||||||
|
%
|
||||||
|
%
|
||||||
|
generate_sums([PVals], Size, Max, _, _Protected, _Domains, _, Sum, []) :- !,
|
||||||
Max is Size-1,
|
Max is Size-1,
|
||||||
Sum =.. [sum|PVals].
|
Sum =.. [sum|PVals].
|
||||||
generate_sums([PVals|Parents], Size, Max, NewSums, F) :-
|
generate_sums([PVals|Parents], Size, Max, Reach, Protected, Domains, ASize, NewSums, F) :-
|
||||||
generate_sums(Parents, Size, Max0, Sums, F0),
|
NewReach is Reach+(Size-1),
|
||||||
|
generate_sums(Parents, Size, Max0, NewReach, Protected, Domains, ASize, Sums, F0),
|
||||||
Max is Max0+(Size-1),
|
Max is Max0+(Size-1),
|
||||||
Max1 is Max+1,
|
Max1 is Max+1,
|
||||||
functor(NewSums, sum, Max1),
|
functor(NewSums, sum, Max1),
|
||||||
expand_sums(PVals, 0, Max0, Max1, Size, Sums, NewSums, F, F0).
|
protect_avg(0, Max0, Protected, Domains, ASize, Reach),
|
||||||
|
expand_sums(PVals, 0, Max0, Max1, Size, Sums, Protected, NewSums, F, F0).
|
||||||
|
|
||||||
|
protect_avg(Max0,Max0,_Protected, _Domains, _ASize, _Reach) :- !.
|
||||||
|
protect_avg(I0, Max0, Protected, Domains, ASize, Reach) :-
|
||||||
|
I is I0+1,
|
||||||
|
Top is I+Reach,
|
||||||
|
( Top > ASize ;
|
||||||
|
arg(I, Domains, CD),
|
||||||
|
arg(Top, Domains, CD)
|
||||||
|
), !,
|
||||||
|
arg(I, Protected, yes),
|
||||||
|
protect_avg(I, Max0, Protected, Domains, ASize, Reach).
|
||||||
|
protect_avg(I0, Max0, Protected, Domains, ASize, Reach) :-
|
||||||
|
I is I0+1,
|
||||||
|
protect_avg(I, Max0, Protected, Domains, ASize, Reach).
|
||||||
|
|
||||||
|
|
||||||
%
|
%
|
||||||
% outer loop: generate array of sums at level j= Sum[j0...jMax]
|
% outer loop: generate array of sums at level j= Sum[j0...jMax]
|
||||||
%
|
%
|
||||||
expand_sums(_Parents, Max, _, Max, _Size, _Sums, _NewSums, F0, F0) :- !.
|
expand_sums(_Parents, Max, _, Max, _Size, _Sums, _P, _NewSums, F0, F0) :- !.
|
||||||
expand_sums(Parents, I0, Max0, Max, Size, Sums, NewSums, [O=SUM|F], F0) :-
|
expand_sums(Parents, I0, Max0, Max, Size, Sums, Prot, NewSums, [O=SUM|F], F0) :-
|
||||||
I is I0+1,
|
I is I0+1,
|
||||||
|
arg(I, Prot, P),
|
||||||
|
var(P), !,
|
||||||
arg(I, NewSums, O),
|
arg(I, NewSums, O),
|
||||||
sum_all(Parents, 0, I0, Max0, Sums, List),
|
sum_all(Parents, 0, I0, Max0, Sums, List),
|
||||||
to_disj(List, SUM),
|
to_disj(List, SUM),
|
||||||
expand_sums(Parents, I, Max0, Max, Size, Sums, NewSums, F, F0).
|
expand_sums(Parents, I, Max0, Max, Size, Sums, Prot, NewSums, F, F0).
|
||||||
|
expand_sums(Parents, I0, Max0, Max, Size, Sums, Prot, NewSums, F, F0) :-
|
||||||
|
I is I0+1,
|
||||||
|
arg(I, Sums, O),
|
||||||
|
arg(I, NewSums, O),
|
||||||
|
expand_sums(Parents, I, Max0, Max, Size, Sums, Prot, NewSums, F, F0).
|
||||||
|
|
||||||
%
|
%
|
||||||
%inner loop: find all parents that contribute to A_ji,
|
%inner loop: find all parents that contribute to A_ji,
|
||||||
% that is generate Pk*Sum_(j-1)l and k+l st k+l = i
|
% that is generate Pk*Sum_(j-1)l and k+l st k+l = i
|
||||||
%
|
%
|
||||||
sum_all([], _, _, _, _, []).
|
sum_all([], _, _, _, _, []).
|
||||||
sum_all([V|Vs], Pos, I, Max0, Sums, [V*S0|List]) :-
|
sum_all([V|Vs], Pos, I, Max0, Sums, [O|List]) :-
|
||||||
J is I-Pos,
|
J is I-Pos,
|
||||||
J >= 0,
|
J >= 0,
|
||||||
J =< Max0, !,
|
J =< Max0, !,
|
||||||
J1 is J+1,
|
J1 is J+1,
|
||||||
arg(J1, Sums, S0),
|
arg(J1, Sums, S0),
|
||||||
|
( J < I -> O = V*S0 ; O = S0*V ),
|
||||||
Pos1 is Pos+1,
|
Pos1 is Pos+1,
|
||||||
sum_all(Vs, Pos1, I, Max0, Sums, List).
|
sum_all(Vs, Pos1, I, Max0, Sums, List).
|
||||||
sum_all([_V|Vs], Pos, I, Max0, Sums, List) :-
|
sum_all([_V|Vs], Pos, I, Max0, Sums, List) :-
|
||||||
Pos1 is Pos+1,
|
Pos1 is Pos+1,
|
||||||
sum_all(Vs, Pos1, I, Max0, Sums, List).
|
sum_all(Vs, Pos1, I, Max0, Sums, List).
|
||||||
|
|
||||||
gen_arg(J, Sums, Max, S0) :-
|
gen_arg(J, Sums, Max, S0) :-
|
||||||
gen_arg(0, Max, J, Sums, S0).
|
gen_arg(0, Max, J, Sums, S0).
|
||||||
|
|
||||||
@ -330,6 +445,26 @@ gen_arg(I0, Max, J, Sums, S) :-
|
|||||||
gen_arg(I, Max, J, Sums, S0).
|
gen_arg(I, Max, J, Sums, S0).
|
||||||
|
|
||||||
|
|
||||||
|
avg_borders(Size, Size, _Max, []) :- !.
|
||||||
|
avg_borders(I0, Size, Max, [J|Vals]) :-
|
||||||
|
I is I0+1,
|
||||||
|
Border is (I*Max)/Size,
|
||||||
|
J is integer(round(Border)),
|
||||||
|
avg_borders(I, Size, Max, Vals).
|
||||||
|
|
||||||
|
avg_domains(Size, Size, _J, _Max, []).
|
||||||
|
avg_domains(I0, Size, J0, Max, Vals) :-
|
||||||
|
I is I0+1,
|
||||||
|
Border is (I*Max)/Size,
|
||||||
|
fetch_domain_for_avg(J0, Border, J, I0, Vals, ValsI),
|
||||||
|
avg_domains(I, Size, J, Max, ValsI).
|
||||||
|
|
||||||
|
fetch_domain_for_avg(J, Border, J, _, Vals, Vals) :-
|
||||||
|
J > Border, !.
|
||||||
|
fetch_domain_for_avg(J0, Border, J, I0, [I0|LVals], RLVals) :-
|
||||||
|
J1 is J0+1,
|
||||||
|
fetch_domain_for_avg(J1, Border, J, I0, LVals, RLVals).
|
||||||
|
|
||||||
generate_avg(Size, Size, _J, _Max, [], [], [], F, F).
|
generate_avg(Size, Size, _J, _Max, [], [], [], F, F).
|
||||||
generate_avg(I0, Size, J0, Max, LSums, [O|OVs], [Ev|Evs], [O=Ev*Disj|F], F0) :-
|
generate_avg(I0, Size, J0, Max, LSums, [O|OVs], [Ev|Evs], [O=Ev*Disj|F], F0) :-
|
||||||
I is I0+1,
|
I is I0+1,
|
||||||
@ -359,18 +494,25 @@ to_disj2([V,V1|Vs], V0, Out) :-
|
|||||||
% look for parameters in the rb-tree, or add a new.
|
% look for parameters in the rb-tree, or add a new.
|
||||||
% distid is the key
|
% distid is the key
|
||||||
%
|
%
|
||||||
check_p(DistId, Parms, ParmVars, Ps, Ps) :-
|
check_p(DistId, Map, Parms, ParmVars, Ps, Ps) :-
|
||||||
rb_lookup(DistId, theta(Parms, ParmVars), Ps), !.
|
rb_lookup(DistId-Map, theta(Parms, ParmVars), Ps), !.
|
||||||
check_p(DistId, Parms, ParmVars, Ps, PsF) :-
|
check_p(DistId, Map, Parms, ParmVars, Ps, PsF) :-
|
||||||
get_dist_params(DistId, Parms0),
|
get_dist_params(DistId, Parms0),
|
||||||
length(Parms0, L0),
|
get_dist_all_sizes(DistId, Sizes),
|
||||||
|
swap_parms(Parms0, Sizes, [0|Map], Parms1),
|
||||||
|
length(Parms1, L0),
|
||||||
get_dist_domain_size(DistId, Size),
|
get_dist_domain_size(DistId, Size),
|
||||||
L1 is L0 div Size,
|
L1 is L0 div Size,
|
||||||
L is L0-L1,
|
L is L0-L1,
|
||||||
initial_maxes(L1, Multipliers),
|
initial_maxes(L1, Multipliers),
|
||||||
copy(L, Multipliers, NextMults, NextMults, Parms0, Parms, ParmVars),
|
copy(L, Multipliers, NextMults, NextMults, Parms1, Parms, ParmVars),
|
||||||
%writeln(t:Size:Parms0:Parms:ParmVars),
|
%writeln(t:Size:Parms0:Parms:ParmVars),
|
||||||
rb_insert(Ps, DistId, theta(Parms, ParmVars), PsF).
|
rb_insert(Ps, DistId-Map, theta(Parms, ParmVars), PsF).
|
||||||
|
|
||||||
|
swap_parms(Parms0, Sizes, Map, Parms1) :-
|
||||||
|
matrix_new(floats, Sizes, Parms0, T0),
|
||||||
|
matrix_shuffle(T0,Map,TF),
|
||||||
|
matrix_to_list(TF, Parms1).
|
||||||
|
|
||||||
%
|
%
|
||||||
% we are using switches by two
|
% we are using switches by two
|
||||||
@ -387,18 +529,19 @@ copy(N, D.Ds, ND.NDs, New, El.Parms0, NEl.Parms, V.ParmVars) :-
|
|||||||
N1 is N-1,
|
N1 is N-1,
|
||||||
(El == 0.0 ->
|
(El == 0.0 ->
|
||||||
NEl = 0,
|
NEl = 0,
|
||||||
ND = D,
|
V = NEl,
|
||||||
V = NEl
|
ND = D
|
||||||
;El == 1.0 ->
|
;El == 1.0 ->
|
||||||
NEl = 1,
|
NEl = 1,
|
||||||
ND = 0.0,
|
V = NEl,
|
||||||
V = NEl
|
ND = 0.0
|
||||||
;El == 0 ->
|
;El == 0 ->
|
||||||
NEl = 0,
|
NEl = 0,
|
||||||
ND = D,
|
V = NEl,
|
||||||
V = NEl
|
ND = D
|
||||||
;El =:= 1 ->
|
;El =:= 1 ->
|
||||||
NEl = 1,
|
NEl = 1,
|
||||||
|
V = NEl,
|
||||||
ND = 0.0,
|
ND = 0.0,
|
||||||
V = NEl
|
V = NEl
|
||||||
;
|
;
|
||||||
@ -585,52 +728,6 @@ eval_outs((V=F).Outs) :-
|
|||||||
V = NF,
|
V = NF,
|
||||||
eval_outs(Outs).
|
eval_outs(Outs).
|
||||||
|
|
||||||
%simplify_exp(V,V) :- !.
|
|
||||||
simplify_exp(V,V) :- var(V), !.
|
|
||||||
simplify_exp(S1+S2,NS) :- !,
|
|
||||||
simplify_exp(S1, SS1),
|
|
||||||
simplify_exp(S2, SS2),
|
|
||||||
simplify_sum(SS1, SS2, NS).
|
|
||||||
simplify_exp(S1*S2,NS) :- !,
|
|
||||||
simplify_exp(S1, SS1),
|
|
||||||
simplify_exp(S2, SS2),
|
|
||||||
simplify_prod(SS1, SS2, NS).
|
|
||||||
simplify_exp(not(S),NS) :- !,
|
|
||||||
simplify_exp(S, SS),
|
|
||||||
simplify_not(SS, NS).
|
|
||||||
simplify_exp(S,S).
|
|
||||||
|
|
||||||
simplify_sum(V1, V2, O) :-
|
|
||||||
( var(V1) ->
|
|
||||||
( var(V2) ->
|
|
||||||
( V1 == V2 -> O = V1 ; O = V1+V2 ) ; /* var(V1) , var(V2) */
|
|
||||||
( V2 == 0 -> O = V1 ; V2 == 1 -> O = 1 ; O = V1+V2 ) /* var(V1) , nonvar(V2) */
|
|
||||||
) ;
|
|
||||||
( var(V2) ->
|
|
||||||
( V1 == 0 -> O = V2 ; V1 == 1 -> O = 1 ; O = V1+V2 ) ; /* nonvar(V1) , var(V2) */
|
|
||||||
( V2 == 0 -> O = V1 ; V2 == 1 -> O = 1 ; V1 == 0 -> O = V2 ; V1 == 1 -> O = 1; O = V1+V2 ) /* nonvar(V1) , nonvar(V2) */
|
|
||||||
)
|
|
||||||
).
|
|
||||||
|
|
||||||
simplify_prod(V1, V2, O) :-
|
|
||||||
( var(V1) ->
|
|
||||||
( var(V2) ->
|
|
||||||
( V1 == V2 -> O = V1 ; O = V1*V2 ) ; /* var(V1) , var(V2) */
|
|
||||||
( V2 == 0 -> O = 0 ; V2 == 1 -> O = V1 ; O = V1*V2 ) /* var(V1) , nonvar(V2) */
|
|
||||||
) ;
|
|
||||||
( var(V2) ->
|
|
||||||
( V1 == 0 -> O = 0 ; V1 == 1 -> O = V2 ; O = V1*V2 ) ; /* nonvar(V1) , var(V2) */
|
|
||||||
( V2 == 0 -> O = 0 ; V2 == 1 -> O = V1 ; V1 == 0 -> O = 0 ; V1 == 1 -> O = V2; V1 == V2 -> O = V1 ; O = V1*V2 ) /* nonvar(V1) , nonvar(V2) */
|
|
||||||
)
|
|
||||||
).
|
|
||||||
|
|
||||||
|
|
||||||
simplify_not(V, not(V)) :- var(V), !.
|
|
||||||
simplify_not(0, 1) :- !.
|
|
||||||
simplify_not(1, 0) :- !.
|
|
||||||
simplify_not(SS, not(SS)).
|
|
||||||
|
|
||||||
|
|
||||||
run_bdd_solver([[V]], LPs, bdd(Term, _Leaves, Nodes)) :-
|
run_bdd_solver([[V]], LPs, bdd(Term, _Leaves, Nodes)) :-
|
||||||
build_out_node(Nodes, Node),
|
build_out_node(Nodes, Node),
|
||||||
findall(Prob, get_prob(Term, Node, V, Prob),TermProbs),
|
findall(Prob, get_prob(Term, Node, V, Prob),TermProbs),
|
||||||
@ -658,9 +755,9 @@ get_prob(Term, Node, V, SP) :-
|
|||||||
build_bdd(Bindings, NVs, VTheta, Theta, Bdd) :-
|
build_bdd(Bindings, NVs, VTheta, Theta, Bdd) :-
|
||||||
bdd_from_list(Bindings, NVs, Bdd),
|
bdd_from_list(Bindings, NVs, Bdd),
|
||||||
bdd_size(Bdd, Len),
|
bdd_size(Bdd, Len),
|
||||||
% number_codes(Len,Codes),
|
number_codes(Len,Codes),
|
||||||
% atom_codes(Name,Codes),
|
atom_codes(Name,Codes),
|
||||||
% bdd_print(Bdd, Name),
|
bdd_print(Bdd, Name),
|
||||||
writeln(length=Len),
|
writeln(length=Len),
|
||||||
VTheta = Theta.
|
VTheta = Theta.
|
||||||
|
|
||||||
|
@ -57,7 +57,7 @@
|
|||||||
]).
|
]).
|
||||||
|
|
||||||
|
|
||||||
call_bp_ground(QueryKeys, AllKeys, Factors, Evidence, Output) :-
|
call_bp_ground(QueryKeys, AllKeys, Factors, Evidence, _Output) :-
|
||||||
b_hash_new(Hash0),
|
b_hash_new(Hash0),
|
||||||
keys_to_ids(AllKeys, 0, Hash0, Hash),
|
keys_to_ids(AllKeys, 0, Hash0, Hash),
|
||||||
get_factors_type(Factors, Type),
|
get_factors_type(Factors, Type),
|
||||||
@ -74,7 +74,7 @@ call_bp_ground(QueryKeys, AllKeys, Factors, Evidence, Output) :-
|
|||||||
%set_vars_information(AllKeys, StatesNames),
|
%set_vars_information(AllKeys, StatesNames),
|
||||||
run_solver(ground(Network,Hash), QueryKeys, Solutions),
|
run_solver(ground(Network,Hash), QueryKeys, Solutions),
|
||||||
writeln(answer:Solutions),
|
writeln(answer:Solutions),
|
||||||
%clpbn_bind_vals([QueryKeys], Solutions, Output).
|
%clpbn_bind_vals([QueryKeys], Solutions, _Output).
|
||||||
free_ground_network(Network).
|
free_ground_network(Network).
|
||||||
|
|
||||||
|
|
||||||
@ -146,7 +146,7 @@ bp([QueryVars], AllVars, Output) :-
|
|||||||
|
|
||||||
init_bp_solver(_, AllVars0, _, bp(BayesNet, DistIds)) :-
|
init_bp_solver(_, AllVars0, _, bp(BayesNet, DistIds)) :-
|
||||||
%check_for_agg_vars(AllVars0, AllVars),
|
%check_for_agg_vars(AllVars0, AllVars),
|
||||||
get_vars_info(AllVars, VarsInfo, DistIds0),
|
get_vars_info(AllVars0, VarsInfo, DistIds0),
|
||||||
sort(DistIds0, DistIds),
|
sort(DistIds0, DistIds),
|
||||||
create_ground_network(VarsInfo, BayesNet),
|
create_ground_network(VarsInfo, BayesNet),
|
||||||
true.
|
true.
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
:- clpbn_horus:set_solver(fove).
|
:- clpbn_horus:set_solver(fove).
|
||||||
%:- clpbn_horus:set_solver(hve).
|
%:- clpbn_horus:set_solver(hve).
|
||||||
%:- clpbn_horus:set_solver(bp).
|
:- clpbn_horus:set_solver(bp).
|
||||||
%:- clpbn_horus:set_solver(cbp).
|
%:- clpbn_horus:set_solver(cbp).
|
||||||
|
|
||||||
|
|
||||||
|
@ -15,6 +15,7 @@
|
|||||||
get_dist_domain_size/2,
|
get_dist_domain_size/2,
|
||||||
get_dist_params/2,
|
get_dist_params/2,
|
||||||
get_dist_key/2,
|
get_dist_key/2,
|
||||||
|
get_dist_all_sizes/2,
|
||||||
get_evidence_position/3,
|
get_evidence_position/3,
|
||||||
get_evidence_from_position/3,
|
get_evidence_from_position/3,
|
||||||
dist_to_term/2,
|
dist_to_term/2,
|
||||||
@ -177,21 +178,21 @@ add_dist(Domain, Type, CPT, Parents, Key, Id) :-
|
|||||||
length(CPT, CPTSize),
|
length(CPT, CPTSize),
|
||||||
length(Domain, DSize),
|
length(Domain, DSize),
|
||||||
new_id(Id),
|
new_id(Id),
|
||||||
record_parent_sizes(Parents, Id, PSizes, [DSize|PSizes]),
|
find_parent_sizes(Parents, Id, PSizes, [DSize|PSizes]),
|
||||||
recordz(clpbn_dist_db,db(Id, Key, CPT, Type, Domain, CPTSize, DSize),_).
|
recordz(clpbn_dist_db,db(Id, Key, CPT, Type, Domain, CPTSize, DSize),_).
|
||||||
|
|
||||||
|
|
||||||
record_parent_sizes([], Id, [], DSizes) :-
|
find_parent_sizes([], Id, [], DSizes) :-
|
||||||
recordz(clpbn_dist_psizes,db(Id, DSizes),_).
|
recordz(clpbn_dist_psizes,db(Id, DSizes),_).
|
||||||
record_parent_sizes([P|Parents], Id, [Size|Sizes], DSizes) :-
|
find_parent_sizes([P|Parents], Id, [Size|Sizes], DSizes) :-
|
||||||
integer(P), !,
|
integer(P), !,
|
||||||
Size = P,
|
Size = P,
|
||||||
record_parent_sizes(Parents, Id, Sizes, DSizes).
|
find_parent_sizes(Parents, Id, Sizes, DSizes).
|
||||||
record_parent_sizes([P|Parents], Id, [Size|Sizes], DSizes) :-
|
find_parent_sizes([P|Parents], Id, [Size|Sizes], DSizes) :-
|
||||||
clpbn:get_atts(P,dist(Dist,_)), !,
|
clpbn:get_atts(P,dist(Dist,_)), !,
|
||||||
get_dist_domain_size(Dist, Size),
|
get_dist_domain_size(Dist, Size),
|
||||||
record_parent_sizes(Parents, Id, Sizes, DSizes).
|
find_parent_sizes(Parents, Id, Sizes, DSizes).
|
||||||
record_parent_sizes([_|_], _, _, _).
|
find_parent_sizes([_|_], _, _, _).
|
||||||
|
|
||||||
%
|
%
|
||||||
% Often, * is used to code empty in HMMs.
|
% Often, * is used to code empty in HMMs.
|
||||||
@ -228,6 +229,9 @@ get_dsizes([P|Parents], [Sz|Sizes], Sizes0) :-
|
|||||||
get_dist_params(Id, Parms) :-
|
get_dist_params(Id, Parms) :-
|
||||||
recorded(clpbn_dist_db, db(Id, _, Parms, _, _, _, _), _).
|
recorded(clpbn_dist_db, db(Id, _, Parms, _, _, _, _), _).
|
||||||
|
|
||||||
|
get_dist_all_sizes(Id, DSizes) :-
|
||||||
|
recorded(clpbn_dist_psizes,db(Id, DSizes),_).
|
||||||
|
|
||||||
get_dist_domain_size(DistId, DSize) :-
|
get_dist_domain_size(DistId, DSize) :-
|
||||||
use_parfactors(on), !,
|
use_parfactors(on), !,
|
||||||
pfl:get_pfl_parameters(DistId, Dist),
|
pfl:get_pfl_parameters(DistId, Dist),
|
||||||
|
@ -425,7 +425,7 @@ registration(r65,c22,s20).
|
|||||||
registration(r66,c43,s20).
|
registration(r66,c43,s20).
|
||||||
registration(r67,c17,s21).
|
registration(r67,c17,s21).
|
||||||
registration(r68,c34,s21).
|
registration(r68,c34,s21).
|
||||||
registration(r69,c0,s21).
|
%registration(r69,c0,s21).
|
||||||
registration(r70,c42,s22).
|
registration(r70,c42,s22).
|
||||||
registration(r71,c7,s22).
|
registration(r71,c7,s22).
|
||||||
registration(r72,c46,s22).
|
registration(r72,c46,s22).
|
||||||
@ -515,7 +515,7 @@ registration(r155,c57,s46).
|
|||||||
registration(r156,c25,s46).
|
registration(r156,c25,s46).
|
||||||
registration(r157,c46,s46).
|
registration(r157,c46,s46).
|
||||||
registration(r158,c15,s46).
|
registration(r158,c15,s46).
|
||||||
registration(r159,c0,s47).
|
%registration(r159,c0,s47).
|
||||||
registration(r160,c33,s47).
|
registration(r160,c33,s47).
|
||||||
registration(r161,c30,s47).
|
registration(r161,c30,s47).
|
||||||
registration(r162,c55,s47).
|
registration(r162,c55,s47).
|
||||||
@ -544,7 +544,7 @@ registration(r184,c50,s54).
|
|||||||
registration(r185,c43,s54).
|
registration(r185,c43,s54).
|
||||||
registration(r186,c55,s54).
|
registration(r186,c55,s54).
|
||||||
registration(r187,c14,s55).
|
registration(r187,c14,s55).
|
||||||
registration(r188,c0,s55).
|
%registration(r188,c0,s55).
|
||||||
registration(r189,c31,s55).
|
registration(r189,c31,s55).
|
||||||
registration(r190,c47,s55).
|
registration(r190,c47,s55).
|
||||||
registration(r191,c50,s56).
|
registration(r191,c50,s56).
|
||||||
@ -600,7 +600,7 @@ registration(r240,c20,s71).
|
|||||||
registration(r241,c18,s71).
|
registration(r241,c18,s71).
|
||||||
registration(r242,c38,s71).
|
registration(r242,c38,s71).
|
||||||
registration(r243,c37,s72).
|
registration(r243,c37,s72).
|
||||||
registration(r244,c0,s72).
|
%registration(r244,c0,s72).
|
||||||
registration(r245,c62,s72).
|
registration(r245,c62,s72).
|
||||||
registration(r246,c47,s73).
|
registration(r246,c47,s73).
|
||||||
registration(r247,c53,s73).
|
registration(r247,c53,s73).
|
||||||
|
@ -27,6 +27,9 @@
|
|||||||
[clpbn_flag/2 as pfl_flag,
|
[clpbn_flag/2 as pfl_flag,
|
||||||
set_clpbn_flag/2 as set_pfl_flag]).
|
set_clpbn_flag/2 as set_pfl_flag]).
|
||||||
|
|
||||||
|
:- reexport(library(clpbn/horus),
|
||||||
|
[set_solver/1]).
|
||||||
|
|
||||||
:- ( % if clp(bn) has done loading, we're top-level
|
:- ( % if clp(bn) has done loading, we're top-level
|
||||||
predicate_property(set_pfl_flag(_,_), imported_from(clpbn))
|
predicate_property(set_pfl_flag(_,_), imported_from(clpbn))
|
||||||
->
|
->
|
||||||
|
Reference in New Issue
Block a user