Merge branch 'master' of ssh://yap.git.sourceforge.net/gitroot/yap/yap-6.3
This commit is contained in:
@@ -53,6 +53,10 @@ Va <- P*X1*Y1 + Q*X2*Y2 + ...
|
||||
|
||||
:- use_module(library(bdd)).
|
||||
|
||||
:- use_module(library(ddnnf)).
|
||||
|
||||
:- use_module(library(simpbool)).
|
||||
|
||||
:- use_module(library(rbtrees)).
|
||||
|
||||
:- use_module(library(bhash)).
|
||||
@@ -63,6 +67,10 @@ Va <- P*X1*Y1 + Q*X2*Y2 + ...
|
||||
|
||||
:- attribute order/1.
|
||||
|
||||
:- dynamic bdds/1.
|
||||
%bdds(ddnnf).
|
||||
bdds(bdd).
|
||||
|
||||
check_if_bdd_done(_Var).
|
||||
|
||||
bdd([[]],_,_) :- !.
|
||||
@@ -74,7 +82,8 @@ bdd([QueryVars], AllVars, AllDiffs) :-
|
||||
|
||||
init_bdd_solver(_, AllVars0, _, bdd(Term, Leaves, Tops)) :-
|
||||
% check_for_agg_vars(AllVars0, AllVars1),
|
||||
sort_vars(AllVars0, AllVars, Leaves),
|
||||
AllVars0 = AllVars1,
|
||||
sort_vars(AllVars1, AllVars, Leaves),
|
||||
order_vars(AllVars, 0),
|
||||
rb_new(Vars0),
|
||||
rb_new(Pars0),
|
||||
@@ -128,8 +137,8 @@ get_vars_info([_|MoreVs], Vs0, VsF, Ps0, PsF, VarsInfo, Lvs, Outs) :-
|
||||
get_var_info(V, avg(Domain), Parents, Vs, Vs2, Ps, Ps, Lvs, Outs, DIST) :- !,
|
||||
length(Domain, DSize),
|
||||
% 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).
|
||||
% top_down_with_tabling(V, DSize, Domain, Parents, Vs, Vs2, Lvs, Outs, DIST).
|
||||
bup_avg(V, DSize, Domain, Parents, Vs, Vs2, Lvs, Outs, DIST).
|
||||
% standard random variable
|
||||
get_var_info(V, DistId, Parents0, Vs, Vs2, Ps, Ps1, Lvs, Outs, DIST) :-
|
||||
% clpbn:get_atts(V, [key(K)]), writeln(V:K:DistId:Parents),
|
||||
@@ -271,7 +280,7 @@ 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),
|
||||
b_hash_insert(H0, k(P,Im,IM), O=Simp*1, HI),
|
||||
MaxI is Max-(Size-1),
|
||||
avg_exp(Vals, PVars, 0, P, MaxI, Size, Im, IM, HI, HF, Exp),
|
||||
simplify_exp(Exp, Simp).
|
||||
@@ -399,7 +408,7 @@ protect_avg(I0, Max0, Protected, Domains, ASize, Reach) :-
|
||||
% outer loop: generate array of sums at level j= Sum[j0...jMax]
|
||||
%
|
||||
expand_sums(_Parents, Max, _, Max, _Size, _Sums, _P, _NewSums, F0, F0) :- !.
|
||||
expand_sums(Parents, I0, Max0, Max, Size, Sums, Prot, NewSums, [O=SUM|F], F0) :-
|
||||
expand_sums(Parents, I0, Max0, Max, Size, Sums, Prot, NewSums, [O=SUM*1|F], F0) :-
|
||||
I is I0+1,
|
||||
arg(I, Prot, P),
|
||||
var(P), !,
|
||||
@@ -466,7 +475,7 @@ fetch_domain_for_avg(J0, Border, J, I0, [I0|LVals], RLVals) :-
|
||||
fetch_domain_for_avg(J1, Border, J, I0, LVals, RLVals).
|
||||
|
||||
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=Disj*Ev|F], F0) :-
|
||||
I is I0+1,
|
||||
Border is (I*Max)/Size,
|
||||
fetch_for_avg(J0, Border, J, LSums, MySums, RSums),
|
||||
@@ -732,7 +741,6 @@ run_bdd_solver([[V]], LPs, bdd(Term, _Leaves, Nodes)) :-
|
||||
build_out_node(Nodes, Node),
|
||||
findall(Prob, get_prob(Term, Node, V, Prob),TermProbs),
|
||||
sumlist(TermProbs, Sum),
|
||||
writeln(TermProbs:Sum),
|
||||
normalise(TermProbs, Sum, LPs).
|
||||
|
||||
build_out_node([_Top], []).
|
||||
@@ -744,7 +752,12 @@ build_out_node2([T,T1|Tops], T*Top) :-
|
||||
build_out_node2(T1.Tops, Top).
|
||||
|
||||
|
||||
get_prob(Term, _Node, V, SP) :-
|
||||
bdds(ddnnf), !,
|
||||
all_cnfs(Term, CNF, IVs, Indics, V, AllParms, AllParmValues),
|
||||
build_cnf(CNF, IVs, Indics, AllParms, AllParmValues, SP).
|
||||
get_prob(Term, Node, V, SP) :-
|
||||
bdds(bdd), !,
|
||||
bind_all(Term, Node, Bindings, V, AllParms, AllParmValues),
|
||||
% reverse(AllParms, RAllParms),
|
||||
term_variables(AllParms, NVs),
|
||||
@@ -754,25 +767,25 @@ get_prob(Term, Node, V, SP) :-
|
||||
|
||||
build_bdd(Bindings, NVs, VTheta, Theta, Bdd) :-
|
||||
bdd_from_list(Bindings, NVs, Bdd),
|
||||
bdd_size(Bdd, Len),
|
||||
number_codes(Len,Codes),
|
||||
atom_codes(Name,Codes),
|
||||
bdd_print(Bdd, Name),
|
||||
writeln(length=Len),
|
||||
% bdd_size(Bdd, Len),
|
||||
% number_codes(Len,Codes),
|
||||
% atom_codes(Name,Codes),
|
||||
% bdd_print(Bdd, Name),
|
||||
% writeln(length=Len),
|
||||
VTheta = Theta.
|
||||
|
||||
bind_all([], End, End, _V, [], []).
|
||||
bind_all(info(V, _Tree, Ev, _Values, Formula, ParmVars, Parms).Term, End, BindsF, V0, ParmVars.AllParms, Parms.AllTheta) :-
|
||||
bind_all([info(V, _Tree, Ev, _Values, Formula, ParmVars, Parms)|Term], End, BindsF, V0, ParmVars.AllParms, Parms.AllTheta) :-
|
||||
V0 == V, !,
|
||||
set_to_one_zeros(Ev),
|
||||
bind_formula(Formula, BindsF, BindsI),
|
||||
bind_all(Term, End, BindsI, V0, AllParms, AllTheta).
|
||||
bind_all(info(_V, _Tree, Ev, _Values, Formula, ParmVars, Parms).Term, End, BindsF, V0, ParmVars.AllParms, Parms.AllTheta) :-
|
||||
bind_all([info(_V, _Tree, Ev, _Values, Formula, ParmVars, Parms)|Term], End, BindsF, V0, ParmVars.AllParms, Parms.AllTheta) :-
|
||||
set_to_ones(Ev),!,
|
||||
bind_formula(Formula, BindsF, BindsI),
|
||||
bind_all(Term, End, BindsI, V0, AllParms, AllTheta).
|
||||
% evidence: no need to add any stuff.
|
||||
bind_all(info(_V, _Tree, _Ev, _Values, Formula, ParmVars, Parms).Term, End, BindsF, V0, ParmVars.AllParms, Parms.AllTheta) :-
|
||||
bind_all([info(_V, _Tree, _Ev, _Values, Formula, ParmVars, Parms)|Term], End, BindsF, V0, ParmVars.AllParms, Parms.AllTheta) :-
|
||||
bind_formula(Formula, BindsF, BindsI),
|
||||
bind_all(Term, End, BindsI, V0, AllParms, AllTheta).
|
||||
|
||||
@@ -800,3 +813,103 @@ normalise(P.TermProbs, Sum, NP.LPs) :-
|
||||
|
||||
finalize_bdd_solver(_).
|
||||
|
||||
all_cnfs([], [], [], [], _V, [], []).
|
||||
all_cnfs([info(V, Tree, Ev, Values, Formula, ParmVars, Parms)|Term], BindsF, IVars, Indics, V0, AllParmsF, AllThetaF) :-
|
||||
%writeln(f:Formula),
|
||||
V0 == V, !,
|
||||
set_to_one_zeros(Ev),
|
||||
all_indicators(Values, BindsF, Binds0),
|
||||
indicators(Values, [], Ev, IVars, IVarsI, Indics, IndicsI, Binds0, Binds1),
|
||||
parms( ParmVars, Parms, AllParmsF, AllThetaF, AllParms, AllTheta),
|
||||
parameters(Formula, Tree, Binds1, BindsI),
|
||||
all_cnfs(Term, BindsI, IVarsI, IndicsI, V0, AllParms, AllTheta).
|
||||
all_cnfs([info(_V, Tree, Ev, Values, Formula, ParmVars, Parms)|Term], BindsF, IVars, Indics, V0, AllParmsF, AllThetaF) :-
|
||||
set_to_ones(Ev),!,
|
||||
all_indicators(Values, BindsF, Binds0),
|
||||
indicators(Values, [], Ev, IVars, IVarsI, Indics, IndicsI, Binds0, Binds1),
|
||||
parms( ParmVars, Parms, AllParmsF, AllThetaF, AllParms, AllTheta),
|
||||
parameters(Formula, Tree, Binds1, BindsI),
|
||||
all_cnfs(Term, BindsI, IVarsI, IndicsI, V0, AllParms, AllTheta).
|
||||
% evidence: no need to add any stuff.
|
||||
all_cnfs([info(_V, Tree, Ev, Values, Formula, ParmVars, Parms)|Term], BindsF, IVars, Indics, V0, AllParmsF, AllThetaF) :-
|
||||
all_indicators(Values, BindsF, Binds0),
|
||||
indicators(Values, [], Ev, IVars, IVarsI, Indics, IndicsI, Binds0, Binds1),
|
||||
parms( ParmVars, Parms, AllParmsF, AllThetaF, AllParms, AllTheta),
|
||||
parameters(Formula, Tree, Binds1, BindsI),
|
||||
all_cnfs(Term, BindsI, IVarsI, IndicsI, V0, AllParms, AllTheta).
|
||||
|
||||
all_indicators(Values) -->
|
||||
{ values_to_disj(Values, Disj) },
|
||||
[Disj].
|
||||
|
||||
values_to_disj([V], V) :- !.
|
||||
values_to_disj([V|Values], V+Disj) :-
|
||||
values_to_disj(Values, Disj).
|
||||
|
||||
indicators([V|Vars], SeenVs, [E|Ev], [V|IsF], IsI, [E|Inds], Inds0) -->
|
||||
generate_exclusions(SeenVs, V),
|
||||
indicators(Vars, [V|SeenVs], Ev, IsF, IsI, Inds, Inds0).
|
||||
indicators([], _SeenVs, [], IsF, IsF, Inds, Inds) --> [].
|
||||
|
||||
parms([], [], AllParms, AllTheta, AllParms, AllTheta).
|
||||
parms([V|ParmVars], [P|Parms], [V|AllParmsF], [P|AllThetaF], AllParms, AllTheta) :-
|
||||
parms( ParmVars, Parms, AllParmsF, AllThetaF, AllParms, AllTheta).
|
||||
|
||||
parameters([], _) --> [].
|
||||
% ignore disj, only useful to BDDs
|
||||
parameters([(T=_)|Formula], Tree) -->
|
||||
{ Tree == T }, !,
|
||||
parameters(Formula, Tree).
|
||||
parameters([(V0=Disj*_I0)|Formula], Tree) -->
|
||||
conj(Disj, V0),
|
||||
parameters(Formula, Tree).
|
||||
|
||||
% transform V0<- A*B+C*(D+not(E))
|
||||
% [V0+not(A)+not(B),V0+not(C)+not(D),V0+not(C)+E]
|
||||
conj(Disj, V0) -->
|
||||
{ conj2(Disj, [[V0]], LVs) },
|
||||
to_disjs(LVs).
|
||||
|
||||
conj2(A, L0, LF) :- var(A), !,
|
||||
add(not(A), L0, LF).
|
||||
conj2((A*B), L0, LF) :-
|
||||
conj2(A, L0, LI),
|
||||
conj2(B, LI, LF).
|
||||
conj2((A+B), L0, LF) :-
|
||||
conj2(A, L0, L1),
|
||||
conj2(B, L0, L2),
|
||||
append(L1, L2, LF).
|
||||
conj2(not(A), L0, LF) :-
|
||||
add(A, L0, LF).
|
||||
|
||||
add(_, [], []).
|
||||
add(Head, [H|L], [[Head|H]|NL]) :-
|
||||
add(Head, L, NL).
|
||||
|
||||
to_disjs([]) --> [].
|
||||
to_disjs([[H|L]|LVs]) -->
|
||||
mkdisj(L, H),
|
||||
to_disjs(LVs).
|
||||
|
||||
mkdisj([], Disj) --> [Disj].
|
||||
mkdisj([H|L], Disj) -->
|
||||
mkdisj(L, (H+Disj)).
|
||||
|
||||
%
|
||||
% add formula for V \== V0 -> V or V0 and not(V) or not(V0)
|
||||
%
|
||||
generate_exclusions([], _V) --> [].
|
||||
generate_exclusions([V0|SeenVs], V) -->
|
||||
[(not(V0)+not(V))],
|
||||
generate_exclusions(SeenVs, V).
|
||||
|
||||
build_cnf(CNF, IVs, Indics, AllParms, AllParmValues, Val) :-
|
||||
%(numbervars(CNF,1,_), writeln(cnf_to_ddnnf(CNF, Vars, IVs, [], F)), fail ; true ),
|
||||
cnf_to_ddnnf(CNF, AllParms, F),
|
||||
AllParms = AllParmValues,
|
||||
IVs = Indics,
|
||||
term_variables(CNF, Extra),
|
||||
set_to_ones(Extra),
|
||||
ddnnf_is(F, Val).
|
||||
|
||||
|
||||
|
@@ -244,7 +244,9 @@ get_dist_domain_size(Id, DSize) :-
|
||||
recorded(clpbn_dist_db, db(Id, _, _, _, _, _, DSize), _).
|
||||
|
||||
get_dist_domain(Id, Domain) :-
|
||||
recorded(clpbn_dist_db, db(Id, _, _, _, Domain, _, _), _).
|
||||
recorded(clpbn_dist_db, db(Id, _, _, _, Domain, _, _), _), !.
|
||||
get_dist_domain(avg(Domain), Domain) :-
|
||||
recorded(clpbn_dist_db, db(Id, _, _, _, Domain, _, _), _), !.
|
||||
|
||||
get_dist_key(Id, Key) :-
|
||||
use_parfactors(on), !,
|
||||
|
@@ -425,7 +425,7 @@ registration(r65,c22,s20).
|
||||
registration(r66,c43,s20).
|
||||
registration(r67,c17,s21).
|
||||
registration(r68,c34,s21).
|
||||
%registration(r69,c0,s21).
|
||||
registration(r69,c0,s21).
|
||||
registration(r70,c42,s22).
|
||||
registration(r71,c7,s22).
|
||||
registration(r72,c46,s22).
|
||||
@@ -515,7 +515,7 @@ registration(r155,c57,s46).
|
||||
registration(r156,c25,s46).
|
||||
registration(r157,c46,s46).
|
||||
registration(r158,c15,s46).
|
||||
%registration(r159,c0,s47).
|
||||
registration(r159,c0,s47).
|
||||
registration(r160,c33,s47).
|
||||
registration(r161,c30,s47).
|
||||
registration(r162,c55,s47).
|
||||
@@ -544,7 +544,7 @@ registration(r184,c50,s54).
|
||||
registration(r185,c43,s54).
|
||||
registration(r186,c55,s54).
|
||||
registration(r187,c14,s55).
|
||||
%registration(r188,c0,s55).
|
||||
registration(r188,c0,s55).
|
||||
registration(r189,c31,s55).
|
||||
registration(r190,c47,s55).
|
||||
registration(r191,c50,s56).
|
||||
@@ -600,7 +600,7 @@ registration(r240,c20,s71).
|
||||
registration(r241,c18,s71).
|
||||
registration(r242,c38,s71).
|
||||
registration(r243,c37,s72).
|
||||
%registration(r244,c0,s72).
|
||||
registration(r244,c0,s72).
|
||||
registration(r245,c62,s72).
|
||||
registration(r246,c47,s73).
|
||||
registration(r247,c53,s73).
|
||||
@@ -619,7 +619,7 @@ registration(r259,c2,s76).
|
||||
registration(r260,c7,s77).
|
||||
registration(r261,c3,s77).
|
||||
registration(r262,c63,s77).
|
||||
%registration(r263,c0,s78).
|
||||
registration(r263,c0,s78).
|
||||
registration(r264,c43,s78).
|
||||
registration(r265,c57,s78).
|
||||
registration(r266,c46,s79).
|
||||
@@ -648,7 +648,7 @@ registration(r288,c45,s86).
|
||||
registration(r289,c17,s86).
|
||||
registration(r290,c2,s86).
|
||||
registration(r291,c48,s86).
|
||||
%registration(r292,c0,s86).
|
||||
registration(r292,c0,s86).
|
||||
registration(r293,c40,s87).
|
||||
registration(r294,c44,s87).
|
||||
registration(r295,c41,s87).
|
||||
@@ -663,7 +663,7 @@ registration(r303,c50,s90).
|
||||
registration(r304,c26,s90).
|
||||
registration(r305,c58,s90).
|
||||
registration(r306,c45,s90).
|
||||
%registration(r307,c0,s91).
|
||||
registration(r307,c0,s91).
|
||||
registration(r308,c35,s91).
|
||||
registration(r309,c4,s91).
|
||||
registration(r310,c4,s92).
|
||||
@@ -677,7 +677,7 @@ registration(r317,c1,s94).
|
||||
registration(r318,c18,s94).
|
||||
registration(r319,c35,s94).
|
||||
registration(r320,c3,s95).
|
||||
%registration(r321,c0,s95).
|
||||
registration(r321,c0,s95).
|
||||
registration(r322,c38,s95).
|
||||
registration(r323,c1,s96).
|
||||
registration(r324,c30,s96).
|
||||
@@ -763,7 +763,7 @@ registration(r403,c49,s119).
|
||||
registration(r404,c61,s119).
|
||||
registration(r405,c38,s120).
|
||||
registration(r406,c8,s120).
|
||||
%registration(r407,c0,s120).
|
||||
registration(r407,c0,s120).
|
||||
registration(r408,c60,s121).
|
||||
registration(r409,c45,s121).
|
||||
registration(r410,c28,s121).
|
||||
@@ -782,7 +782,7 @@ registration(r422,c29,s125).
|
||||
registration(r423,c54,s125).
|
||||
registration(r424,c28,s126).
|
||||
registration(r425,c22,s126).
|
||||
%registration(r426,c0,s126).
|
||||
registration(r426,c0,s126).
|
||||
registration(r427,c61,s127).
|
||||
registration(r428,c7,s127).
|
||||
registration(r429,c28,s127).
|
||||
@@ -1124,7 +1124,7 @@ registration(r764,c10,s227).
|
||||
registration(r765,c3,s228).
|
||||
registration(r766,c47,s228).
|
||||
registration(r767,c54,s228).
|
||||
%registration(r768,c0,s229).
|
||||
registration(r768,c0,s229).
|
||||
registration(r769,c10,s229).
|
||||
registration(r770,c37,s229).
|
||||
registration(r771,c62,s230).
|
||||
@@ -1140,7 +1140,7 @@ registration(r780,c51,s233).
|
||||
registration(r781,c8,s233).
|
||||
registration(r782,c58,s233).
|
||||
registration(r783,c14,s234).
|
||||
%registration(r784,c0,s234).
|
||||
registration(r784,c0,s234).
|
||||
registration(r785,c23,s234).
|
||||
registration(r786,c59,s234).
|
||||
registration(r787,c5,s235).
|
||||
@@ -1161,7 +1161,7 @@ registration(r801,c45,s239).
|
||||
registration(r802,c47,s239).
|
||||
registration(r803,c7,s240).
|
||||
registration(r804,c4,s240).
|
||||
%registration(r805,c0,s240).
|
||||
registration(r805,c0,s240).
|
||||
registration(r806,c54,s240).
|
||||
registration(r807,c9,s240).
|
||||
registration(r808,c11,s241).
|
||||
@@ -1169,7 +1169,7 @@ registration(r809,c29,s241).
|
||||
registration(r810,c45,s241).
|
||||
registration(r811,c58,s241).
|
||||
registration(r812,c48,s242).
|
||||
%registration(r813,c0,s242).
|
||||
registration(r813,c0,s242).
|
||||
registration(r814,c51,s242).
|
||||
registration(r815,c12,s243).
|
||||
registration(r816,c24,s243).
|
||||
@@ -1212,6 +1212,6 @@ registration(r852,c7,s254).
|
||||
registration(r853,c61,s254).
|
||||
registration(r854,c60,s255).
|
||||
registration(r855,c48,s255).
|
||||
%registration(r856,c0,s255).
|
||||
registration(r856,c0,s255).
|
||||
|
||||
|
||||
|
@@ -30,10 +30,12 @@ BIBTEX=bibtex
|
||||
|
||||
PROGRAMS= \
|
||||
$(srcdir)/problog.yap \
|
||||
$(srcdir)/problog_lbdd.yap \
|
||||
$(srcdir)/problog_lfi.yap \
|
||||
$(srcdir)/dtproblog.yap \
|
||||
$(srcdir)/aproblog.yap \
|
||||
$(srcdir)/problog_learning.yap
|
||||
$(srcdir)/problog_learning.yap\
|
||||
$(srcdir)/problog_learning_lbdd.yap
|
||||
|
||||
PROBLOG_PROGRAMS= \
|
||||
$(srcdir)/problog/extlists.yap \
|
||||
@@ -56,6 +58,7 @@ PROBLOG_PROGRAMS= \
|
||||
$(srcdir)/problog/version_control.yap \
|
||||
$(srcdir)/problog/nestedtries.yap \
|
||||
$(srcdir)/problog/utils.yap \
|
||||
$(srcdir)/problog/utils_lbdd.yap \
|
||||
$(srcdir)/problog/ad_converter.yap \
|
||||
$(srcdir)/problog/termhandling.yap \
|
||||
$(srcdir)/problog/completion.yap \
|
||||
|
@@ -233,6 +233,8 @@
|
||||
problog_max/3,
|
||||
problog_kbest_explanations/3,
|
||||
problog_exact/3,
|
||||
problog_fl_bdd/2,
|
||||
problog_kbest_bdd/4,
|
||||
problog_all_explanations/2,
|
||||
problog_all_explanations_unsorted/2,
|
||||
problog_exact_save/5,
|
||||
@@ -625,14 +627,14 @@ term_expansion_intern((Head<--Body), Module, C):-
|
||||
|
||||
% converts ?:: prefix to ? :: infix, as handled by other clause
|
||||
term_expansion_intern((Annotation::Fact), Module, ExpandedClause) :-
|
||||
Annotation == '?',
|
||||
term_expansion_intern((? :: Fact :- true), Module, ExpandedClause).
|
||||
Annotation == ( '?' ),
|
||||
term_expansion_intern(((?) :: Fact :- true), Module, ExpandedClause).
|
||||
|
||||
|
||||
% handles decision clauses
|
||||
term_expansion_intern((Annotation :: Head :- Body), Module, problog:ExpandedClause) :-
|
||||
(
|
||||
Annotation == '?' ->
|
||||
Annotation == ('?') ->
|
||||
% It's a decision with a body
|
||||
(decision_fact(_,Head) ->
|
||||
throw(error('New decision unifies with already defined decision!', (Head))) ; true
|
||||
@@ -650,7 +652,7 @@ term_expansion_intern((Annotation :: Head :- Body), Module, problog:ExpandedClau
|
||||
(problog_control(check,internal_strategy) ->
|
||||
dtproblog:strategy_log(ID,Head,LProb)
|
||||
;
|
||||
LProb = '?'
|
||||
LProb = ('?')
|
||||
)
|
||||
),
|
||||
assertz(dynamic_probability_fact(ID)),
|
||||
@@ -1029,7 +1031,7 @@ prove_problog_fact(ClauseID,GroundID,Prob) :-
|
||||
(problog_control(check,find_decisions) ->
|
||||
signal_decision(ClauseID,GroundID)
|
||||
;
|
||||
(Prob = '?' ->
|
||||
(Prob = ('?') ->
|
||||
add_to_proof(GroundID,0) % 0 is log(1)!
|
||||
;
|
||||
% Checks needed for LeDTProbLog
|
||||
@@ -1053,7 +1055,7 @@ prove_problog_fact_negated(ClauseID,GroundID,Prob) :-
|
||||
(problog_control(check,find_decisions) ->
|
||||
signal_decision(ClauseID,GroundID)
|
||||
;
|
||||
(Prob = '?' ->
|
||||
(Prob = ('?') ->
|
||||
add_to_proof_negated(GroundID,-inf) % 0 is log(1)!
|
||||
;
|
||||
% Checks needed for LeDTProbLog
|
||||
@@ -1187,7 +1189,7 @@ get_fact_probability(ID,Prob) :-
|
||||
get_internal_fact(ID,ProblogTerm,_ProblogName,ProblogArity)
|
||||
),
|
||||
arg(ProblogArity,ProblogTerm,Log),
|
||||
(Log = '?' ->
|
||||
(Log = ('?') ->
|
||||
throw(error('Why do you want to know the probability of a decision?')) %fail
|
||||
; ground(Log) ->
|
||||
Prob is exp(Log)
|
||||
@@ -1206,7 +1208,7 @@ get_fact_log_probability(ID,Prob) :-
|
||||
get_internal_fact(ID,ProblogTerm,_ProblogName,ProblogArity)
|
||||
),
|
||||
arg(ProblogArity,ProblogTerm,Prob),
|
||||
Prob \== '?'.
|
||||
Prob \== ('?').
|
||||
get_fact_log_probability(ID,Prob) :-
|
||||
get_fact_probability(ID,Prob1),
|
||||
Prob is log(Prob1).
|
||||
@@ -2145,6 +2147,7 @@ init_problog_low(Threshold) :-
|
||||
nb_setval(problog_completed_proofs, Trie_Completed_Proofs),
|
||||
init_problog(Threshold).
|
||||
|
||||
:- include(problog_lbdd).
|
||||
|
||||
% generalizing problog_max to return all explanations, sorted by non-increasing probability
|
||||
problog_all_explanations(Goal,Expl) :-
|
||||
@@ -2427,7 +2430,7 @@ problog_kbest(Goal, K, Prob, Status) :-
|
||||
problog_kbest_id(Goal, K),
|
||||
retract(current_kbest(_,ListFound,_NumFound)),
|
||||
build_prefixtree(ListFound),
|
||||
nb_getval(problog_completed_proofs, Trie_Completed_Proofs),
|
||||
nb_getval(problog_completed_proofs, Trie_Completed_Proofs),
|
||||
eval_dnf(Trie_Completed_Proofs,Prob,Status),
|
||||
delete_ptree(Trie_Completed_Proofs).
|
||||
|
||||
|
@@ -343,6 +343,12 @@ learning_init_handler(validating, (_,_,_,_,_)).
|
||||
learning_init_handler(validated, _Value).
|
||||
learning_init_handler(stored, _Value).
|
||||
|
||||
learning_libdd_init_handler(message, '(Q,BDD,Query)').
|
||||
learning_libdd_init_handler(validating, (_,_,_)).
|
||||
%learning_init_handler(validate, V_).
|
||||
learning_libdd_init_handler(validated, _Value).
|
||||
learning_libdd_init_handler(stored, _Value).
|
||||
|
||||
learning_prob_init_handler(message, '(0,1] or uniform(l,h) ').
|
||||
learning_prob_init_handler(validating, uniform(Low,High)) :-
|
||||
number(Low),
|
||||
|
235
packages/ProbLog/problog/utils_lbdd.yap
Normal file
235
packages/ProbLog/problog/utils_lbdd.yap
Normal file
@@ -0,0 +1,235 @@
|
||||
%%% -*- Mode: Prolog; -*-
|
||||
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
%
|
||||
% $Date: 2011-11-28 14:41:26 +0100 (Mon, 28 Nov 2011) $
|
||||
% $Revision: 6764 $
|
||||
%
|
||||
% This file is part of ProbLog
|
||||
% http://dtai.cs.kuleuven.be/problog
|
||||
%
|
||||
% ProbLog was developed at Katholieke Universiteit Leuven
|
||||
%
|
||||
% Copyright 2008, 2009, 2010
|
||||
% Katholieke Universiteit Leuven
|
||||
%
|
||||
% Main authors of this file:
|
||||
% Bernd Gutmann, Vitor Santos Costa
|
||||
%
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
%
|
||||
% Artistic License 2.0
|
||||
%
|
||||
% Copyright (c) 2000-2006, The Perl Foundation.
|
||||
%
|
||||
% Everyone is permitted to copy and distribute verbatim copies of this
|
||||
% license document, but changing it is not allowed. Preamble
|
||||
%
|
||||
% This license establishes the terms under which a given free software
|
||||
% Package may be copied, modified, distributed, and/or
|
||||
% redistributed. The intent is that the Copyright Holder maintains some
|
||||
% artistic control over the development of that Package while still
|
||||
% keeping the Package available as open source and free software.
|
||||
%
|
||||
% You are always permitted to make arrangements wholly outside of this
|
||||
% license directly with the Copyright Holder of a given Package. If the
|
||||
% terms of this license do not permit the full use that you propose to
|
||||
% make of the Package, you should contact the Copyright Holder and seek
|
||||
% a different licensing arrangement. Definitions
|
||||
%
|
||||
% "Copyright Holder" means the individual(s) or organization(s) named in
|
||||
% the copyright notice for the entire Package.
|
||||
%
|
||||
% "Contributor" means any party that has contributed code or other
|
||||
% material to the Package, in accordance with the Copyright Holder's
|
||||
% procedures.
|
||||
%
|
||||
% "You" and "your" means any person who would like to copy, distribute,
|
||||
% or modify the Package.
|
||||
%
|
||||
% "Package" means the collection of files distributed by the Copyright
|
||||
% Holder, and derivatives of that collection and/or of those files. A
|
||||
% given Package may consist of either the Standard Version, or a
|
||||
% Modified Version.
|
||||
%
|
||||
% "Distribute" means providing a copy of the Package or making it
|
||||
% accessible to anyone else, or in the case of a company or
|
||||
% organization, to others outside of your company or organization.
|
||||
%
|
||||
% "Distributor Fee" means any fee that you charge for Distributing this
|
||||
% Package or providing support for this Package to another party. It
|
||||
% does not mean licensing fees.
|
||||
%
|
||||
% "Standard Version" refers to the Package if it has not been modified,
|
||||
% or has been modified only in ways explicitly requested by the
|
||||
% Copyright Holder.
|
||||
%
|
||||
% "Modified Version" means the Package, if it has been changed, and such
|
||||
% changes were not explicitly requested by the Copyright Holder.
|
||||
%
|
||||
% "Original License" means this Artistic License as Distributed with the
|
||||
% Standard Version of the Package, in its current version or as it may
|
||||
% be modified by The Perl Foundation in the future.
|
||||
%
|
||||
% "Source" form means the source code, documentation source, and
|
||||
% configuration files for the Package.
|
||||
%
|
||||
% "Compiled" form means the compiled bytecode, object code, binary, or
|
||||
% any other form resulting from mechanical transformation or translation
|
||||
% of the Source form.
|
||||
%
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
%
|
||||
% Permission for Use and Modification Without Distribution
|
||||
%
|
||||
% (1) You are permitted to use the Standard Version and create and use
|
||||
% Modified Versions for any purpose without restriction, provided that
|
||||
% you do not Distribute the Modified Version.
|
||||
%
|
||||
% Permissions for Redistribution of the Standard Version
|
||||
%
|
||||
% (2) You may Distribute verbatim copies of the Source form of the
|
||||
% Standard Version of this Package in any medium without restriction,
|
||||
% either gratis or for a Distributor Fee, provided that you duplicate
|
||||
% all of the original copyright notices and associated disclaimers. At
|
||||
% your discretion, such verbatim copies may or may not include a
|
||||
% Compiled form of the Package.
|
||||
%
|
||||
% (3) You may apply any bug fixes, portability changes, and other
|
||||
% modifications made available from the Copyright Holder. The resulting
|
||||
% Package will still be considered the Standard Version, and as such
|
||||
% will be subject to the Original License.
|
||||
%
|
||||
% Distribution of Modified Versions of the Package as Source
|
||||
%
|
||||
% (4) You may Distribute your Modified Version as Source (either gratis
|
||||
% or for a Distributor Fee, and with or without a Compiled form of the
|
||||
% Modified Version) provided that you clearly document how it differs
|
||||
% from the Standard Version, including, but not limited to, documenting
|
||||
% any non-standard features, executables, or modules, and provided that
|
||||
% you do at least ONE of the following:
|
||||
%
|
||||
% (a) make the Modified Version available to the Copyright Holder of the
|
||||
% Standard Version, under the Original License, so that the Copyright
|
||||
% Holder may include your modifications in the Standard Version. (b)
|
||||
% ensure that installation of your Modified Version does not prevent the
|
||||
% user installing or running the Standard Version. In addition, the
|
||||
% modified Version must bear a name that is different from the name of
|
||||
% the Standard Version. (c) allow anyone who receives a copy of the
|
||||
% Modified Version to make the Source form of the Modified Version
|
||||
% available to others under (i) the Original License or (ii) a license
|
||||
% that permits the licensee to freely copy, modify and redistribute the
|
||||
% Modified Version using the same licensing terms that apply to the copy
|
||||
% that the licensee received, and requires that the Source form of the
|
||||
% Modified Version, and of any works derived from it, be made freely
|
||||
% available in that license fees are prohibited but Distributor Fees are
|
||||
% allowed.
|
||||
%
|
||||
% Distribution of Compiled Forms of the Standard Version or
|
||||
% Modified Versions without the Source
|
||||
%
|
||||
% (5) You may Distribute Compiled forms of the Standard Version without
|
||||
% the Source, provided that you include complete instructions on how to
|
||||
% get the Source of the Standard Version. Such instructions must be
|
||||
% valid at the time of your distribution. If these instructions, at any
|
||||
% time while you are carrying out such distribution, become invalid, you
|
||||
% must provide new instructions on demand or cease further
|
||||
% distribution. If you provide valid instructions or cease distribution
|
||||
% within thirty days after you become aware that the instructions are
|
||||
% invalid, then you do not forfeit any of your rights under this
|
||||
% license.
|
||||
%
|
||||
% (6) You may Distribute a Modified Version in Compiled form without the
|
||||
% Source, provided that you comply with Section 4 with respect to the
|
||||
% Source of the Modified Version.
|
||||
%
|
||||
% Aggregating or Linking the Package
|
||||
%
|
||||
% (7) You may aggregate the Package (either the Standard Version or
|
||||
% Modified Version) with other packages and Distribute the resulting
|
||||
% aggregation provided that you do not charge a licensing fee for the
|
||||
% Package. Distributor Fees are permitted, and licensing fees for other
|
||||
% components in the aggregation are permitted. The terms of this license
|
||||
% apply to the use and Distribution of the Standard or Modified Versions
|
||||
% as included in the aggregation.
|
||||
%
|
||||
% (8) You are permitted to link Modified and Standard Versions with
|
||||
% other works, to embed the Package in a larger work of your own, or to
|
||||
% build stand-alone binary or bytecode versions of applications that
|
||||
% include the Package, and Distribute the result without restriction,
|
||||
% provided the result does not expose a direct interface to the Package.
|
||||
%
|
||||
% Items That are Not Considered Part of a Modified Version
|
||||
%
|
||||
% (9) Works (including, but not limited to, modules and scripts) that
|
||||
% merely extend or make use of the Package, do not, by themselves, cause
|
||||
% the Package to be a Modified Version. In addition, such works are not
|
||||
% considered parts of the Package itself, and are not subject to the
|
||||
% terms of this license.
|
||||
%
|
||||
% General Provisions
|
||||
%
|
||||
% (10) Any use, modification, and distribution of the Standard or
|
||||
% Modified Versions is governed by this Artistic License. By using,
|
||||
% modifying or distributing the Package, you accept this license. Do not
|
||||
% use, modify, or distribute the Package, if you do not accept this
|
||||
% license.
|
||||
%
|
||||
% (11) If your Modified Version has been derived from a Modified Version
|
||||
% made by someone other than you, you are nevertheless required to
|
||||
% ensure that your Modified Version complies with the requirements of
|
||||
% this license.
|
||||
%
|
||||
% (12) This license does not grant you the right to use any trademark,
|
||||
% service mark, tradename, or logo of the Copyright Holder.
|
||||
%
|
||||
% (13) This license includes the non-exclusive, worldwide,
|
||||
% free-of-charge patent license to make, have made, use, offer to sell,
|
||||
% sell, import and otherwise transfer the Package with respect to any
|
||||
% patent claims licensable by the Copyright Holder that are necessarily
|
||||
% infringed by the Package. If you institute patent litigation
|
||||
% (including a cross-claim or counterclaim) against any party alleging
|
||||
% that the Package constitutes direct or contributory patent
|
||||
% infringement, then this Artistic License to you shall terminate on the
|
||||
% date that such litigation is filed.
|
||||
%
|
||||
% (14) Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT
|
||||
% HOLDER AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED
|
||||
% WARRANTIES. THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
||||
% PARTICULAR PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT
|
||||
% PERMITTED BY YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT
|
||||
% HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
% INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE
|
||||
% OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
%
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
|
||||
:- module(utils_libbdd, [empty_bdd_nbs/0,
|
||||
create_bdd_nb/3,
|
||||
create_bdd_input_nb/2,
|
||||
create_bdd_output_nb/4]).
|
||||
|
||||
|
||||
:- reexport(utils_learning, [
|
||||
empty_output_directory/0,
|
||||
create_known_values_file_name/2,
|
||||
create_factprobs_file_name/2,
|
||||
create_test_predictions_file_name/2,
|
||||
create_training_predictions_file_name/2]).
|
||||
|
||||
%========================================================================
|
||||
%=
|
||||
%=
|
||||
%========================================================================
|
||||
|
||||
empty_bdd_nbs.
|
||||
|
||||
|
||||
%========================================================================
|
||||
%=
|
||||
%=
|
||||
%========================================================================
|
||||
|
||||
create_bdd_nb(QueryID,ClusterID,NB_Name) :-
|
||||
atomic_concat(['query_',QueryID,'_cluster_',ClusterID],NB_Name).
|
||||
|
@@ -277,14 +277,8 @@ create_known_values_file_name(_,_) :-
|
||||
%=
|
||||
%========================================================================
|
||||
|
||||
create_bdd_file_name(QueryID,ClusterID,Absolute_File_Name) :-
|
||||
problog_flag(bdd_directory,Path),
|
||||
!,
|
||||
|
||||
atomic_concat(['query_',QueryID,'_cluster_',ClusterID],File_Name),
|
||||
concat_path_with_filename(Path,File_Name,Absolute_File_Name).
|
||||
create_bdd_file_name(_,_,_) :-
|
||||
throw(error(problog_flag_does_not_exist(bdd_directory))).
|
||||
create_bdd_nb_name(QueryID,ClusterID,NB_Name) :-
|
||||
atomic_concat(['query_',QueryID,'_cluster_',ClusterID],NB_Name).
|
||||
|
||||
%========================================================================
|
||||
%=
|
||||
|
101
packages/ProbLog/problog_examples/learn_graph_lbdd.pl
Normal file
101
packages/ProbLog/problog_examples/learn_graph_lbdd.pl
Normal file
@@ -0,0 +1,101 @@
|
||||
%%% -*- Mode: Prolog; -*-
|
||||
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
% ProbLog program describing a probabilistic graph
|
||||
% (running example from ProbLog presentations)
|
||||
% $Id: learn_graph.pl 4875 2010-10-05 15:28:35Z theo $
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
% example for parameter learning with LeProbLog
|
||||
%
|
||||
% training and test examples are included at the end of the file
|
||||
% query ?- do_learning(20).
|
||||
% will run 20 iterations of learning with default settings
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
|
||||
%:- use_module(library(problog)).
|
||||
:- use_module(library(problog_learning_lbdd)).
|
||||
|
||||
%%%%
|
||||
% background knowledge
|
||||
%%%%
|
||||
% definition of acyclic path using list of visited nodes
|
||||
path(X,Y) :- path(X,Y,[X],_).
|
||||
|
||||
path(X,X,A,A).
|
||||
path(X,Y,A,R) :-
|
||||
X\==Y,
|
||||
edge(X,Z),
|
||||
absent(Z,A),
|
||||
path(Z,Y,[Z|A],R).
|
||||
|
||||
% using directed edges in both directions
|
||||
edge(X,Y) :- dir_edge(Y,X).
|
||||
edge(X,Y) :- dir_edge(X,Y).
|
||||
|
||||
% checking whether node hasn't been visited before
|
||||
absent(_,[]).
|
||||
absent(X,[Y|Z]):-X \= Y, absent(X,Z).
|
||||
|
||||
%%%%
|
||||
% probabilistic facts
|
||||
% - probability represented by t/1 term means learnable parameter
|
||||
% - argument of t/1 is real value (used to compare against in evaluation when known), use t(_) if unknown
|
||||
%%%%
|
||||
t(0.9)::dir_edge(1,2).
|
||||
t(0.8)::dir_edge(2,3).
|
||||
t(0.6)::dir_edge(3,4).
|
||||
t(0.7)::dir_edge(1,6).
|
||||
t(0.5)::dir_edge(2,6).
|
||||
t(0.4)::dir_edge(6,5).
|
||||
t(0.7)::dir_edge(5,3).
|
||||
t(0.2)::dir_edge(5,4).
|
||||
|
||||
%%%%%%%%%%%%%%
|
||||
% training examples of form example(ID,Query,DesiredProbability)
|
||||
%%%%%%%%%%%%%%
|
||||
|
||||
example(1,path(1,2),0.94).
|
||||
example(2,path(1,3),0.81).
|
||||
example(3,path(1,4),0.54).
|
||||
example(4,path(1,5),0.70).
|
||||
example(5,path(1,6),0.87).
|
||||
example(6,path(2,3),0.85).
|
||||
example(7,path(2,4),0.57).
|
||||
example(8,path(2,5),0.72).
|
||||
example(9,path(2,6),0.86).
|
||||
example(10,path(3,4),0.66).
|
||||
example(11,path(3,5),0.80).
|
||||
example(12,path(3,6),0.75).
|
||||
example(13,path(4,5),0.57).
|
||||
example(14,path(4,6),0.51).
|
||||
example(15,path(5,6),0.69).
|
||||
% some examples for learning from proofs:
|
||||
example(16,(dir_edge(2,3),dir_edge(2,6),dir_edge(6,5),dir_edge(5,4)),0.032).
|
||||
example(17,(dir_edge(1,6),dir_edge(2,6),dir_edge(2,3),dir_edge(3,4)),0.168).
|
||||
example(18,(dir_edge(5,3),dir_edge(5,4)),0.14).
|
||||
example(19,(dir_edge(2,6),dir_edge(6,5)),0.2).
|
||||
example(20,(dir_edge(1,2),dir_edge(2,3),dir_edge(3,4)),0.432).
|
||||
|
||||
%%%%%%%%%%%%%%
|
||||
% test examples of form test_example(ID,Query,DesiredProbability)
|
||||
% note: ID namespace is shared with training example IDs
|
||||
%%%%%%%%%%%%%%
|
||||
|
||||
test_example(21,path(2,1),0.94).
|
||||
test_example(22,path(3,1),0.81).
|
||||
test_example(23,path(4,1),0.54).
|
||||
test_example(24,path(5,1),0.70).
|
||||
test_example(25,path(6,1),0.87).
|
||||
test_example(26,path(3,2),0.85).
|
||||
test_example(27,path(4,2),0.57).
|
||||
test_example(28,path(5,2),0.72).
|
||||
test_example(29,path(6,2),0.86).
|
||||
test_example(30,path(4,3),0.66).
|
||||
test_example(31,path(5,3),0.80).
|
||||
test_example(32,path(6,3),0.75).
|
||||
test_example(33,path(5,4),0.57).
|
||||
test_example(34,path(6,4),0.51).
|
||||
test_example(35,path(6,5),0.69).
|
||||
|
52
packages/ProbLog/problog_lbdd.yap
Normal file
52
packages/ProbLog/problog_lbdd.yap
Normal file
@@ -0,0 +1,52 @@
|
||||
%
|
||||
% ProbLog extension to use an YAP BDD interface module, instead of simplecudd.
|
||||
%
|
||||
|
||||
|
||||
:- use_module(library(trie_sp)).
|
||||
:- use_module(library(bdd)).
|
||||
:- use_module(library(bhash)).
|
||||
|
||||
problog_kbest_bdd(Goal, K, Prob, ok) :-
|
||||
problog_kbest_to_bdd(Goal, K, BDD, MapList),
|
||||
bind_maplist(MapList, BoundVars),
|
||||
bdd_to_probability_sum_product(BDD, BoundVars, Prob).
|
||||
|
||||
problog_kbest_as_bdd(Goal, K, bdd(Tree, MapList)) :-
|
||||
problog_kbest_to_bdd(Goal, K, BDD, MapList),
|
||||
bdd_tree(BDD, bdd(_Dir, Tree, _Vars)),
|
||||
bdd_close(BDD).
|
||||
|
||||
problog_kbest_to_bdd(Goal, K, BDD, MapList) :-
|
||||
problog_flag(first_threshold,InitT),
|
||||
init_problog_kbest(InitT),
|
||||
problog_control(off,up),
|
||||
problog_kbest_id(Goal, K),
|
||||
retract(current_kbest(_,ListFound,_NumFound)),
|
||||
build_prefixtree(ListFound),
|
||||
nb_getval(problog_completed_proofs, Trie_Completed_Proofs),
|
||||
trie_to_bdd(Trie_Completed_Proofs, BDD, MapList),
|
||||
delete_ptree(Trie_Completed_Proofs).
|
||||
|
||||
problog_fl_bdd(Goal, _) :-
|
||||
init_problog_low(0.0),
|
||||
problog_control(off, up),
|
||||
timer_start(sld_time),
|
||||
problog_call(Goal),
|
||||
add_solution,
|
||||
fail.
|
||||
problog_fl_bdd(_,Prob) :-
|
||||
timer_stop(sld_time,SLD_Time),
|
||||
problog_var_set(sld_time, SLD_Time),
|
||||
nb_getval(problog_completed_proofs, Trie_Completed_Proofs),
|
||||
tabled_trie_to_bdd(Trie_Completed_Proofs, BDD, MapList),
|
||||
bind_maplist(MapList, BoundVars),
|
||||
bdd_to_probability_sum_product(BDD, BoundVars, Prob),
|
||||
(problog_flag(retain_tables, true) -> retain_tabling; true),
|
||||
clear_tabling.
|
||||
|
||||
bind_maplist([], []).
|
||||
bind_maplist([Node-_|MapList], [ProbFact|BoundVars]) :-
|
||||
get_fact_probability(Node,ProbFact),
|
||||
bind_maplist(MapList, BoundVars).
|
||||
|
@@ -207,6 +207,7 @@
|
||||
|
||||
:- module(learning,[do_learning/1,
|
||||
do_learning/2,
|
||||
set_problog_flag/2,
|
||||
reset_learning/0
|
||||
]).
|
||||
|
||||
@@ -740,7 +741,9 @@ update_query(QueryID,Symbol,What_To_Update) :-
|
||||
' > "',
|
||||
Output_Directory,
|
||||
'values.pl"'],Command),
|
||||
|
||||
shell(Command,Error),
|
||||
%shell('cat /home/vsc/Yap/bins/devel/outputvalues.pl',_),
|
||||
|
||||
|
||||
(
|
||||
|
1516
packages/ProbLog/problog_learning_lbdd.yap
Normal file
1516
packages/ProbLog/problog_learning_lbdd.yap
Normal file
File diff suppressed because it is too large
Load Diff
@@ -39,6 +39,8 @@ CWD=$(PWD)
|
||||
|
||||
BDD_PROLOG= \
|
||||
$(srcdir)/bdd.yap \
|
||||
$(srcdir)/simpbool.yap \
|
||||
$(srcdir)/ddnnf.yap \
|
||||
$(srcdir)/trie_sp.yap
|
||||
|
||||
OBJS=cudd.o
|
||||
|
@@ -10,6 +10,7 @@
|
||||
bdd_size/2,
|
||||
bdd_print/2,
|
||||
bdd_to_probability_sum_product/2,
|
||||
bdd_to_probability_sum_product/3,
|
||||
bdd_close/1,
|
||||
mtbdd_close/1]).
|
||||
|
||||
@@ -17,6 +18,8 @@
|
||||
|
||||
:- use_module(library(rbtrees)).
|
||||
|
||||
:- use_module(library(simpbool)).
|
||||
|
||||
tell_warning :-
|
||||
print_message(warning,functionality(cudd)).
|
||||
|
||||
@@ -47,8 +50,8 @@ set_bdd(T, VS, Manager, Cudd) :-
|
||||
|
||||
set_bdd_from_list(T0, VS, Manager, Cudd) :-
|
||||
numbervars(VS,0,_),
|
||||
% generate_releases(T0, Manager, T),
|
||||
T0 = T,
|
||||
generate_releases(T0, Manager, T),
|
||||
% T0 = T,
|
||||
% writeln_list(T),
|
||||
list_to_cudd(T,Manager,_Cudd0,Cudd).
|
||||
|
||||
@@ -127,7 +130,7 @@ bdd_eval(add(M, X, Vars, _), Val) :-
|
||||
mtbdd_eval(add(M,X, Vars, _), Val) :-
|
||||
add_eval(M, X, Vars, Val).
|
||||
|
||||
bdd_tree(cudd(M, X, Vars, _), bdd(Dir, Tree, Vars)) :-
|
||||
bdd_tree(cudd(M, X, Vars, _Vs), bdd(Dir, Tree, Vars)) :-
|
||||
cudd_to_term(M, X, Vars, Dir, Tree).
|
||||
bdd_tree(add(M, X, Vars, _), mtbdd(Tree, Vars)) :-
|
||||
add_to_term(M, X, Vars, Tree).
|
||||
@@ -138,6 +141,9 @@ mtbdd_tree(add(M,X,Vars, _), mtbdd(Dir, Tree, Vars)) :-
|
||||
bdd_to_probability_sum_product(cudd(M,X,_,Probs), Prob) :-
|
||||
cudd_to_probability_sum_product(M, X, Probs, Prob).
|
||||
|
||||
bdd_to_probability_sum_product(cudd(M,X,_,_Probs), Probs, Prob) :-
|
||||
cudd_to_probability_sum_product(M, X, Probs, Prob).
|
||||
|
||||
bdd_close(cudd(M,_,_Vars, _)) :-
|
||||
cudd_die(M).
|
||||
bdd_close(add(M,_,_Vars, _)) :-
|
||||
|
249
packages/bdd/ddnnf.yap
Normal file
249
packages/bdd/ddnnf.yap
Normal file
@@ -0,0 +1,249 @@
|
||||
|
||||
:- module(ddnnf,
|
||||
[cnf_to_ddnnf/3,
|
||||
ddnnf/3,
|
||||
ddnnf_is/2]).
|
||||
|
||||
:- use_module(library(lists)).
|
||||
:- use_module(library(readutil)).
|
||||
:- use_module(library(lineutils)).
|
||||
:- use_module(library(terms)).
|
||||
:- use_module(library(cnf)).
|
||||
:- use_module(library(simpbool)).
|
||||
|
||||
%
|
||||
% convert a CNF as list with Variables Vars and Existential variables
|
||||
% in DDNNF, Exs \in LVars into DDNNF with extra existential vars
|
||||
%
|
||||
cnf_to_ddnnf(CNF0, PVs, DDNNF) :-
|
||||
list2cnf(CNF0, CNF, []),
|
||||
mkddnnf(CNF, PVs, DDNNF).
|
||||
|
||||
mkddnnf(CNF, PVs, DDNNF) :-
|
||||
term_variables(CNF, AllVars),
|
||||
% (numbervars(CNF,1,_), writeln(CNF), fail ; true),
|
||||
open(dimacs, write, S),
|
||||
cnf_to_file(CNF, AllVars, S),
|
||||
close(S),
|
||||
% execute c2d at this point, but we're lazy%
|
||||
% unix(system('c2d -dt_method 3 -in dimacs')),
|
||||
% unix(system('c2d -visualize -in dimacs')),
|
||||
unix(system('dsharp -Fnnf dimacs.nnf dimacs')),
|
||||
open('dimacs.nnf',read,R),
|
||||
SVars =.. [v|AllVars],
|
||||
% ones(LVars),
|
||||
input_ddnnf(R, SVars, PVs, DDNNF),
|
||||
% writeln(DDNNF),
|
||||
close(R).
|
||||
|
||||
list2cnf([]) --> [].
|
||||
list2cnf([(O=A)|Impls]) --> !,
|
||||
{cvt(O,FO,NO),
|
||||
and2cnf(A,Conj,[]) },
|
||||
[[FO|Conj]],
|
||||
disj(A, NO),
|
||||
list2cnf(Impls).
|
||||
list2cnf([CNF|Impls]) -->
|
||||
{ to_format(CNF, Format, []) },
|
||||
[Format],
|
||||
list2cnf(Impls).
|
||||
|
||||
cvt(O,O,-O) :- var(O), !.
|
||||
cvt(not(O),-O,O).
|
||||
|
||||
neg(O,-O) :- var(O), !.
|
||||
neg(-O,O).
|
||||
|
||||
to_format(A) -->
|
||||
{ var(A) },
|
||||
!,
|
||||
[A].
|
||||
to_format(A+B) -->
|
||||
!,
|
||||
to_format(A),
|
||||
to_format(B).
|
||||
to_format(not(A)) -->
|
||||
!,
|
||||
[-A].
|
||||
to_format(A) -->
|
||||
[A].
|
||||
|
||||
|
||||
and2cnf(A) -->
|
||||
{ var(A) },
|
||||
!,
|
||||
[-A].
|
||||
and2cnf(A*B) -->
|
||||
!,
|
||||
and2cnf(A),
|
||||
and2cnf(B).
|
||||
and2cnf(not(A)) -->
|
||||
!,
|
||||
[A].
|
||||
and2cnf(A) -->
|
||||
!,
|
||||
[-A].
|
||||
|
||||
disj(A, NO) -->
|
||||
{ var(A) }, !,
|
||||
[[NO,A]].
|
||||
disj(A*B, NO) --> !,
|
||||
disj(A, NO),
|
||||
disj(B, NO).
|
||||
disj(A, NO) -->
|
||||
[[NO,A]].
|
||||
|
||||
%
|
||||
% convert a boolean expression with Variables Vars and Existential variables
|
||||
% in DDNNF, Exs \in LVars into DDNNF with extra existential vars
|
||||
%
|
||||
% ex: (A*B+not(B))*(C=B) into something complicated
|
||||
%
|
||||
ddnnf(List, PVs, DDNNF) :-
|
||||
exps2conj(List, Conj),
|
||||
cnf(Conj, CNF),
|
||||
mkddnnf(CNF, PVs, DDNNF).
|
||||
|
||||
exps2conj((C1,C2), CC1*CC2) :- !,
|
||||
exps2conj(C1, CC1),
|
||||
exps2conj(C2, CC2).
|
||||
exps2conj((Conj), CConj) :-
|
||||
cvt_el(Conj, CConj).
|
||||
|
||||
cvt_el(V, V) :- var(V), !.
|
||||
cvt_el(not(X), -X1) :- !,
|
||||
cvt_el(X, X1).
|
||||
cvt_el(X+Y, X1+Y1) :- !,
|
||||
cvt_el(X, X1),
|
||||
cvt_el(Y, Y1).
|
||||
cvt_el(X*Y, X1*Y1) :- !,
|
||||
cvt_el(X, X1),
|
||||
cvt_el(Y, Y1).
|
||||
cvt_el(X=Y, X1==Y1) :- !,
|
||||
cvt_el(X, X1),
|
||||
cvt_el(Y, Y1).
|
||||
cvt_el(X, X).
|
||||
|
||||
cnf_to_file(List, Vars, S) :-
|
||||
number_ivars(Vars, 1, M),
|
||||
length(List, N),
|
||||
M1 is M-1,
|
||||
format(S,'p cnf ~d ~d~n',[M1,N]),
|
||||
output_list(List, S),
|
||||
fail.
|
||||
cnf_to_file(_List, _Vars, _S).
|
||||
|
||||
number_ivars([], M, M).
|
||||
number_ivars([I0|IVars], I0, M) :-
|
||||
I is I0+1,
|
||||
number_ivars(IVars, I, M).
|
||||
|
||||
output_list([], _S).
|
||||
output_list([CNF|List], S) :-
|
||||
output_cnf(CNF, S),
|
||||
output_list(List, S).
|
||||
|
||||
output_cnf([], S) :-
|
||||
format(S, '0~n', []).
|
||||
output_cnf([-V|CNF], S) :- !,
|
||||
format(S, '-~d ',[V]),
|
||||
output_cnf(CNF, S).
|
||||
output_cnf([V|CNF], S) :-
|
||||
format(S, '~d ',[V]),
|
||||
output_cnf(CNF, S).
|
||||
|
||||
input_ddnnf(Stream, SVars, PVs, ddnnf(Out, SVars, Result)) :-
|
||||
read_line_to_codes(Stream, Header),
|
||||
split(Header, ["nnf",VS,_ES,_NS]),
|
||||
number_codes(NVs, VS),
|
||||
functor(TempResults, nnf, NVs),
|
||||
process_nnf_lines(Stream, SVars, PVs, 1, TempResults, Out, Last),
|
||||
Last1 is Last-1,
|
||||
arg(Last1, TempResults, Result).
|
||||
|
||||
process_nnf_lines(Stream, SVars, PVs, LineNumber, TempResults, O, LL) :-
|
||||
read_line_to_codes(Stream, Codes),
|
||||
( Codes = end_of_file -> O = [], LL = LineNumber ;
|
||||
% (LineNumber > 1 -> N is LineNumber-1, arg(N,TempResults,P), format("~w ",[P]);true),
|
||||
% format("~s~n",[Codes]),
|
||||
arg(LineNumber, TempResults, P),
|
||||
process_nnf_line(SVars, PVs, TempResults, Exp0, Codes, []),
|
||||
simplify_line(P=Exp0, Lines, O),
|
||||
NewLine is LineNumber+1,
|
||||
process_nnf_lines(Stream, SVars, PVs, NewLine, TempResults, Lines, LL)
|
||||
).
|
||||
|
||||
process_nnf_line(SVars, PVs, _TempResults, Exp) --> "L ",
|
||||
nnf_leaf(SVars, PVs, Exp).
|
||||
process_nnf_line(_SVars, _, TempResults, Exp) --> "A ",
|
||||
nnf_and_node(TempResults, Exp).
|
||||
process_nnf_line(_SVars, _, TempResults, Exp) --> "O ",
|
||||
nnf_or_node(TempResults, Exp).
|
||||
|
||||
nnf_leaf(SVars, PVs, Prob, Codes, []) :-
|
||||
number_codes(Number, Codes),
|
||||
Abs is abs(Number),
|
||||
arg(Abs, SVars, Node),
|
||||
(Number < 0 ->
|
||||
(parameter(Node,PVs) -> Prob = 1-Node ; Prob = 1 )
|
||||
;
|
||||
Prob = Node
|
||||
).
|
||||
|
||||
parameter(F,[F1|_Exs]) :- F == F1, !.
|
||||
parameter(F,[_|Exs]) :-
|
||||
parameter(F, Exs).
|
||||
|
||||
nnf_and_node(TempResults, Product, Codes, []) :-
|
||||
split(Codes, [_|NumberAsStrings]),
|
||||
multiply_nodes(NumberAsStrings, TempResults, Product).
|
||||
|
||||
multiply_nodes([], _, 1).
|
||||
multiply_nodes(NumberAsString.NumberAsStrings, TempResults, Product) :-
|
||||
number_codes(Pos, NumberAsString),
|
||||
Pos1 is Pos+1,
|
||||
arg(Pos1, TempResults, P),
|
||||
Product = Product0*P,
|
||||
multiply_nodes(NumberAsStrings, TempResults, Product0).
|
||||
|
||||
nnf_or_node(TempResults, Sum, Codes, []) :-
|
||||
split(Codes, [_J,_C|NumberAsStrings]),
|
||||
add_nodes(NumberAsStrings, TempResults, Sum).
|
||||
|
||||
add_nodes([], _, 0).
|
||||
add_nodes(NumberAsString.NumberAsStrings, TempResults, Product) :-
|
||||
number_codes(Pos, NumberAsString),
|
||||
Pos1 is Pos+1,
|
||||
arg(Pos1, TempResults, P),
|
||||
Product = Product0+P,
|
||||
add_nodes(NumberAsStrings, TempResults, Product0).
|
||||
|
||||
ones([]).
|
||||
ones([1|LVars]) :-
|
||||
ones(LVars).
|
||||
|
||||
simplify_line((A=Exp0), List, Final) :-
|
||||
simplify_exp(Exp0, Exp),
|
||||
propagate_constants(Exp, A, List, Final).
|
||||
|
||||
propagate_constants(Exp, A, Lines, Lines) :- var(Exp), !, A=Exp.
|
||||
propagate_constants(0, 0, Lines, Lines) :- !.
|
||||
propagate_constants(1, 1, Lines, Lines) :- !.
|
||||
propagate_constants(Exp, A, Lines, [(A=Exp)|Lines]).
|
||||
|
||||
%
|
||||
% compute the value of a SP
|
||||
%
|
||||
%
|
||||
ddnnf_is(ddnnf(F, Vs, Out), Out) :-
|
||||
term_variables(Vs,LVs),
|
||||
ones(LVs),
|
||||
%(numbervars(F,1,_),writeln(F),fail;true),
|
||||
ddnnf_is_acc(F).
|
||||
|
||||
%ddnnf_is_acc([H=Exp|_]) :- writeln((H=Exp)),fail.
|
||||
ddnnf_is_acc([]).
|
||||
ddnnf_is_acc([H=Exp|Attrs]) :-
|
||||
H is Exp,
|
||||
%writeln(Exp:H),
|
||||
ddnnf_is_acc(Attrs).
|
54
packages/bdd/simpbool.yap
Normal file
54
packages/bdd/simpbool.yap
Normal file
@@ -0,0 +1,54 @@
|
||||
|
||||
%
|
||||
% simplify a boolean expression of the form A*B+C*D...
|
||||
%
|
||||
|
||||
:- module(simplify_boolean,
|
||||
[simplify_exp/2]).
|
||||
|
||||
%simplify_exp(V,V) :- writeln(V), fail, !.
|
||||
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)).
|
||||
|
||||
|
@@ -12,8 +12,7 @@ trie_to_bdd(Trie, BDD, MapList) :-
|
||||
complex_to_andor(Complex,Map0,Map,Tree),
|
||||
rb_visit(Map, MapList),
|
||||
extract_vars(MapList, Vs),
|
||||
bdd_new(Tree, Vs, BDD),
|
||||
bdd_tree(BDD, bdd(_, L, _)), length(L,Len), writeln(Len).
|
||||
bdd_new(Tree, Vs, BDD).
|
||||
|
||||
tabled_trie_to_bdd(Trie, BDD, MapList) :-
|
||||
trie_to_list(Trie, Complex),
|
||||
@@ -21,7 +20,6 @@ tabled_trie_to_bdd(Trie, BDD, MapList) :-
|
||||
rb_new(Tab0),
|
||||
Complex = [list(Els)],
|
||||
tabled_complex_to_andor(Els,Map0,Map,Tab0,_Tab,Tree),
|
||||
writeln(Complex),
|
||||
rb_visit(Map, MapList),
|
||||
extract_vars(MapList, Vs),
|
||||
bdd_new(Tree, Vs, BDD),
|
||||
@@ -31,6 +29,7 @@ extract_vars([], []).
|
||||
extract_vars((_-V).MapList, V.Vs) :-
|
||||
extract_vars(MapList, Vs).
|
||||
|
||||
complex_to_andor(empty, Map, Map, 0).
|
||||
complex_to_andor([list(Els)], Map0, MapF, Tree) :- !,
|
||||
complex_to_andor(Els, Map0, MapF, Tree).
|
||||
complex_to_andor([El1,El2|Els], Map0, MapF, or(T1,T2)) :- !,
|
||||
|
@@ -9,8 +9,8 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <YapTerm.h>
|
||||
#include <YapTags.h>
|
||||
#include <YapRegs.h>
|
||||
#include <YapTags.h>
|
||||
|
||||
typedef void *SYM_REC_PTR;
|
||||
|
||||
|
@@ -71,7 +71,7 @@ iff((X==Y),Polarity,B,Acc,Cnf) :- !,
|
||||
Cnf = [[-BX,BY,-B],[BX,-BY,-B],[-BX,-BY,B],[BX,BY,B] | AccXY]
|
||||
).
|
||||
|
||||
iff((X xor Y),Polarity,B,Acc,Cnf) :- !,
|
||||
iff((xor(X, Y)),Polarity,B,Acc,Cnf) :- !,
|
||||
iff(X,*,BX,Acc,AccX),
|
||||
iff(Y,*,BY,AccX,AccXY),
|
||||
(
|
||||
|
@@ -125,12 +125,13 @@ void *RtreeUdiSearch (control_t *control)
|
||||
|
||||
/*RTreePrint ((*control)[0].tree);*/
|
||||
|
||||
for (i = 0; i < NARGS && (*control)[i].arg != 0 ; i++)
|
||||
if (YAP_IsAttVar(YAP_A((*control)[i].arg)))
|
||||
for (i = 0; i < NARGS && (*control)[i].arg != 0 ; i++) {
|
||||
YAP_Term t = YAP_A((*control)[i].arg);
|
||||
if (YAP_IsAttVar(t))
|
||||
{
|
||||
|
||||
fprintf(stderr,"i=%ld\n",i);
|
||||
/*get the constraits rect*/
|
||||
Constraints = YAP_AttsOfVar(YAP_A((*control)[i].arg));
|
||||
Constraints = YAP_AttsOfVar(t);
|
||||
/* Yap_DebugPlWrite(Constraints); */
|
||||
r = RectOfTerm(YAP_ArgOfTerm(2,Constraints));
|
||||
|
||||
@@ -155,7 +156,7 @@ void *RtreeUdiSearch (control_t *control)
|
||||
|
||||
return Yap_ClauseListCode(c->cl);
|
||||
}
|
||||
|
||||
}
|
||||
return NULL; /*YAP FALLBACK*/
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user