diff --git a/packages/CLPBN/clpbn.yap b/packages/CLPBN/clpbn.yap index 11e57eb45..0d8733be9 100644 --- a/packages/CLPBN/clpbn.yap +++ b/packages/CLPBN/clpbn.yap @@ -26,7 +26,7 @@ :- dynamic user:term_expansion/2. -:- attribute key/1, dist/2, evidence/1, starter/0. +:- attribute key/1, dist/2, evidence/1. :- use_module('clpbn/ve', @@ -163,14 +163,18 @@ clpbn_flag(parameter_softening,Before,After) :- % % make sure a query variable is reachable by the garbage collector. % +% we use a mutable variable to avoid unnecessary trailing. +% store_var(El) :- - catch(b_getval(clpbn_qvars,Q.Tail), _, init_clpbn_vars(El, Q, Tail)), - Tail = [El|NewTail], - b_setval(clpbn_qvars, [Q|NewTail]). + nb_current(clpbn_qvars, Mutable), !, + get_mutable(Tail, Mutable), + update_mutable(El.Tail, Mutable). +store_var(El) :- + init_clpbn_vars(El). -init_clpbn_vars(El, Q, Tail) :- - Q = [El|Tail], - b_setval(clpbn_qvars, [Q|Tail]). +init_clpbn_vars(El) :- + create_mutable(El, Mutable), + b_setval(clpbn_qvars, Mutable). check_constraint(Constraint, _, _, Constraint) :- var(Constraint), !. check_constraint((A->D), _, _, (A->D)) :- var(A), !. @@ -273,17 +277,12 @@ get_bnode(Var, Goal) :- get_dist(Dist,_,Domain,CPT), (Parents = [] -> X = tab(Domain,CPT) ; X = tab(Domain,CPT,Parents)), dist_goal(X, Key, Goal0), - include_evidence(Var, Goal0, Key, Goali), - include_starter(Var, Goali, Key, Goal). + include_evidence(Var, Goal0, Key, Goal). include_evidence(Var, Goal0, Key, ((Key:-Ev),Goal0)) :- get_atts(Var, [evidence(Ev)]), !. include_evidence(_, Goal0, _, Goal0). -include_starter(Var, Goal0, Key, ((:-Key),Goal0)) :- - get_atts(Var, [starter]), !. -include_starter(_, Goal0, _, Goal0). - dist_goal(Dist, Key, (Key=NDist)) :- term_variables(Dist, DVars), process_vars(DVars, DKeys), diff --git a/packages/ProbLog/problog.yap b/packages/ProbLog/problog.yap index 8f6fd06ea..218403d28 100644 --- a/packages/ProbLog/problog.yap +++ b/packages/ProbLog/problog.yap @@ -2,8 +2,8 @@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % -% $Date: 2011-04-08 19:30:08 +0200 (Fri, 08 Apr 2011) $ -% $Revision: 5887 $ +% $Date: 2011-09-02 11:23:22 +0200 (Fri, 02 Sep 2011) $ +% $Revision: 6475 $ % % This file is part of ProbLog % http://dtai.cs.kuleuven.be/problog @@ -229,7 +229,10 @@ problog_kbest/4, problog_kbest_save/6, problog_max/3, + problog_kbest_explanations/3, problog_exact/3, + problog_all_explanations/2, + problog_all_explanations_unsorted/2, problog_exact_save/5, problog_montecarlo/3, problog_dnf_sampling/3, @@ -302,7 +305,7 @@ :- yap_flag(unknown,error). % general yap modules -:- use_module(library(lists), [append/3,member/2,memberchk/2,reverse/2,select/3,nth1/3,nth1/4,nth0/4]). +:- use_module(library(lists), [append/3,member/2,memberchk/2,reverse/2,select/3,nth1/3,nth1/4,nth0/4,sum_list/2]). :- use_module(library(terms), [variable_in_term/2,variant/2] ). :- use_module(library(random), [random/1]). :- use_module(library(system), [tmpnam/1,shell/2,delete_file/1,delete_file/2]). @@ -1274,6 +1277,8 @@ print_ad_intern((A1;B1),[A2|B2],Mass,Handle) :- print_ad_intern(_::Fact,[],Mass,Handle) :- P2 is 1.0 - Mass, format(Handle,'~10f :: ~q',[P2,Fact]). +print_ad_intern(P::A1,[A2],Mass,Handle) :- + once(print_ad_intern_one(P::A1,A2,Mass,_NewMass,Handle)). print_ad_intern_one(_::Fact,_::AuxFact,Mass,NewMass,Handle) :- % ask problog to get the fact_id once(probabilistic_fact(P,AuxFact,_FactID)), @@ -2099,6 +2104,40 @@ init_problog_low(Threshold) :- nb_setval(problog_completed_proofs, Trie_Completed_Proofs), init_problog(Threshold). + +% generalizing problog_max to return all explanations, sorted by non-increasing probability +problog_all_explanations(Goal,Expl) :- + problog_all_explanations_unsorted(Goal,Unsorted), + keysort(Unsorted,Decreasing), + reverse(Decreasing,Expl). + +problog_all_explanations_unsorted(Goal, _) :- + init_problog_low(0.0), + problog_control(off, up), + timer_start(sld_time), + problog_call(Goal), + add_solution, + fail. +problog_all_explanations_unsorted(_,Expl) :- + timer_stop(sld_time,SLD_Time), + problog_var_set(sld_time, SLD_Time), + nb_getval(problog_completed_proofs, Trie_Completed_Proofs), + explanations_from_trie(Trie_Completed_Proofs,Expl). + +% catch basecases +explanations_from_trie(Trie,[]) :- + empty_ptree(Trie),!. +explanations_from_trie(Trie,[1.0-[]]) :- + traverse_ptree(Trie,[true]),!. +explanations_from_trie(Trie_Completed_Proofs,Expl) :- + findall(Prob-Facts, + (traverse_ptree(Trie_Completed_Proofs,L), + findall(P,(member(A,L),get_fact_log_probability(A,P)),Ps), + sum_list(Ps,LS), + Prob is exp(LS), + get_fact_list(L,Facts) + ),Expl). + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % approximate inference: bounds by iterative deepening up to interval width Delta % problog_delta(+Goal,+Delta,-LowerBound,-UpperBound,-Status) @@ -2351,6 +2390,15 @@ problog_kbest(Goal, K, Prob, Status) :- eval_dnf(Trie_Completed_Proofs,Prob,Status), delete_ptree(Trie_Completed_Proofs). +% generalizes problog_max to return the k best explanations +problog_kbest_explanations(Goal, K, Explanations) :- + problog_flag(first_threshold,InitT), + init_problog_kbest(InitT), + problog_control(off,up), + problog_kbest_id(Goal, K), + retract(current_kbest(_,ListFound,_NumFound)), + to_external_format_with_reverse(ListFound,Explanations). + problog_real_kbest(Goal, K, Prob, Status) :- problog_flag(first_threshold,InitT), init_problog_kbest(InitT), @@ -2463,6 +2511,15 @@ take_k_best(In,K,OutOf,Out) :- take_k_best(R,K,OutOf2,Out) ). +to_external_format_with_reverse(Intern,Extern) :- + to_external_format_with_reverse(Intern,[],Extern). +to_external_format_with_reverse([],Extern,Extern). +to_external_format_with_reverse([LogP-FactIDs|Intern],Acc,Extern) :- + Prob is exp(LogP), + ( FactIDs = [_|_] -> get_fact_list(FactIDs, Facts); + Facts = FactIDs), + to_external_format_with_reverse(Intern,[Prob-Facts|Acc],Extern). + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % exact probability % problog_exact(+Goal,-Prob,-Status) @@ -3073,6 +3130,7 @@ problog_bdd_forest(Goals) :- unrequire(keep_ground_ids), reset_non_ground_facts, bdd_par_file(BDDParFile), +% format('Vars: ~w~n',[Vars]), tell(BDDParFile), bdd_vars_script(Vars), flush_output, % isnt this called by told/0? @@ -3089,8 +3147,8 @@ problog_bdd_forest(Goals) :- problog_bdd_forest_supported :- build_trie_supported. % Iterate over all Goals, write BDD scripts and collect variables used. -write_bdd_forest([],VarsTot,VarsTot,_). -write_bdd_forest([Goal|Rest],VarsAcc,VarsTot,N):- +write_bdd_forest([],AtomsTot,AtomsTot,_). +write_bdd_forest([Goal|Rest],AtomsAcc,AtomsTot,N) :- build_trie(Goal, Trie), write_nth_bdd_struct_script(N, Trie, Vars), (problog_flag(verbose, true)-> @@ -3100,9 +3158,15 @@ write_bdd_forest([Goal|Rest],VarsAcc,VarsTot,N):- ), delete_ptree(Trie), N2 is N+1, - list_to_ord_set(Vars,VarsSet), - ord_union(VarsAcc,VarsSet,VarsAcc2), - once(write_bdd_forest(Rest,VarsAcc2,VarsTot,N2)). + % map 'not id' to id in Vars + findall(ID,(member((not ID),Vars)) ,NegativeAtoms), + findall(ID,(member(ID,Vars),ID \= (not _)),PositiveAtoms), +% format('PositiveAtoms: ~w~n',[PositiveAtoms]), +% format('NegativeAtoms: ~w~n',[NegativeAtoms]), + append(PositiveAtoms,NegativeAtoms,Atoms), + list_to_ord_set(Atoms,AtomsSet), + ord_union(AtomsAcc,AtomsSet,AtomsAcc2), + once(write_bdd_forest(Rest,AtomsAcc2,AtomsTot,N2)). % Write files write_nth_bdd_struct_script(N,Trie,Vars) :- diff --git a/packages/ProbLog/problog/ad_converter.yap b/packages/ProbLog/problog/ad_converter.yap index 6f6fac997..20d3b7f3f 100644 --- a/packages/ProbLog/problog/ad_converter.yap +++ b/packages/ProbLog/problog/ad_converter.yap @@ -2,8 +2,8 @@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % -% $Date: 2011-04-26 15:48:52 +0200 (Tue, 26 Apr 2011) $ -% $Revision: 6371 $ +% $Date: 2011-08-19 13:13:56 +0200 (Fri, 19 Aug 2011) $ +% $Revision: 6471 $ % % This file is part of ProbLog % http://dtai.cs.kuleuven.be/problog @@ -210,15 +210,15 @@ ]). % general yap modules -:- use_module(library(lists),[reverse/2,member/2,memberchk/2,append/3]). - +:- use_module(library(lists),[member/2,append/3]). :- use_module(flags). :- style_check(all). :- yap_flag(unknown,error). :- discontiguous user:(<--)/2, problog:(<--)/2. - +:- discontiguous user:myclause/1, problog:myclause/1. % notation of ADs in LFI-ProbLog + :- op( 550, yfx, :: ). % for annotated disjunctions @@ -230,48 +230,70 @@ -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -term_expansion_intern_ad( (Head<--Body),Module,Mode,Result) :- - problog_flag(ad_cpl_semantics,AD_CPL_Semantics), +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% term_expansion_intern_ad( +AD, +Module, +Mode, -ListOfAtoms) +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +:- bb_put(ad_converter_unique_id,1). + +term_expansion_intern_ad((Head<--Body), Module, Mode, [user:ad_intern((Head<--Body),ID,Aux_Facts)|Result]) :- + % the internal ID for the annotated disjunction + bb_get(ad_converter_unique_id,ID), + ID2 is ID+1, + bb_put(ad_converter_unique_id,ID2), + + % if CPL semantics is on we need to add all body variables to the + % auxilliary probabilistic facts to ensure that each grounding + % of an AD "triggers" a new CP event ( - proper_tunable_annotated_disjunction(Head) - -> - compile_tunable_annotated_disjunction(Head,Body,Facts,Bodies,ID,AD_CPL_Semantics,Mode); - ( - proper_annotated_disjunction(Head,Sum_of_P_in_Head) - -> - compile_annotated_disjunction(Head,Body,Facts,Bodies,ID,AD_CPL_Semantics,Mode,Sum_of_P_in_Head); - throw(error(invalid_annotated_disjunction,(Head<--Body))) - ) + problog_flag(ad_cpl_semantics,true) -> + term_variables(Body,Body_Vars) + ; + Body_Vars=[] ), + % construct the auxilliary facts we need to represent the AD + ( + % if it's a tunable AD create tunable auxilliary facts + proper_tunable_ad_head(Head) -> + create_tunable_ad_aux_facts(Head,Body_Vars,ID,1,Aux_Facts) + ; + % if it's a regular AD create auxilliary facts + proper_ad_head(Head,0.0) -> + create_ad_aux_facts(Head,Body_Vars,ID,1,0.0,Aux_Facts) + ; + % neither nor, let's complain + throw(error(invalid_annotated_disjunction,(Head<--Body))) + ), + + % call term_expansion for the aux facts, this has the same effect + % as if the use had defined the facts in the original file findall(problog:Atom,( - member(F,Facts), + member(F,Aux_Facts), once(problog:term_expansion_intern(F,Module,Atom)) ),Result_Atoms), + % construct the auxilliary clauses + + create_aux_bodies(Head,Body_Vars,Body,ID,1,Aux_Facts,Mode,Aux_Clauses), + ( - Mode==lfi_learning - -> - findall(Module:myclause(H,B),member((H:-B),Bodies),Result_Bodies); - findall(Module:B,member(B,Bodies),Result_Bodies) + Mode==lfi_learning -> + findall(Module:myclause(H,B),member((H:-B),Aux_Clauses),Result,Result_Atoms) + ; + findall(Module:B,member(B,Aux_Clauses),Result,Result_Atoms) ), - append(Result_Atoms,Result_Bodies,Result), - - problog_flag(show_ad_compilation,Show_AD_compilation), ( - Show_AD_compilation==true + problog_flag(show_ad_compilation,true) -> ( format('Compiling the annotated disjunction~n ~q~ninto the following code~n',[(Head<--Body)]), format('================================================~n',[]), - forall(member(F,Facts),format(' ~q.~n',[F])), + forall(member(F,Aux_Facts),format(' ~q.~n',[F])), format(' - - - - - - - - - - - - - - - - - - - - - - ~n',[]), - forall(member(B,Bodies),format(' ~q.~n',[B])), + forall(member(B,Aux_Clauses),format(' ~q.~n',[B])), format('================================================~2n',[]) ); true @@ -283,167 +305,155 @@ term_expansion_intern_ad( (Head<--Body),_,_) :- fail. + + + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% +% proper_ad_head(+Head, +Acc) +% +% this predicate succeeds if Head is valid disjunction +% of probabilistic facts as used in the head of an AD +% in particular, it checks that all probabilities are +% valid and the sum does not exceed 1.0 +% +% if will throw an exception if any of the probabilties P +% P::A +% can not be evaluated using is/2 +% +% ?- proper_ad_head( 0.1::a, 0.1). +% yes +% ?- proper_ad_head( (0.1::a,0.8::b), 0.1). +% no +% ?- proper_ad_head( (0.1::a;0.8::b), 0.1). +% yes +% ?- proper_ad_head( (0.1::a;0.8::b;0.2::c), 0.1). +% no +% ?- proper_ad_head( (0.1::a;0.4::true), 0.1). +% no +% ?- ad_converter:proper_ad_head( (1/2::a;0.4::foo(X)), 0.1). +% true +% ?- ad_converter:proper_ad_head( (goo::a;0.4::foo(X)), 0.1). +% ERROR at clause 2 of ad_converter:proper_ad_head/2 !! +% TYPE ERROR %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -get_next_unique_id(ID) :- + +proper_ad_head( P :: A, Acc) :- + P>=0.0, + P+Acc=<1.0, + \+ var(A), + \+ system_predicate(_,A), + once((atom(A);compound(A))). + +proper_ad_head((P :: A;T),Acc) :- + \+ var(A), + \+ system_predicate(_,A), + once((atom(A);compound(A))), + P>=0.0, + Acc2 is P+Acc, + proper_ad_head(T,Acc2). + + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% proper_tunable_ad_head(+Head) +% +% this predicate succeeds if Head is valid disjunction of +% tunable probabilistic facts as used in the head of an AD +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +proper_tunable_ad_head( t(_)::A ) :- + \+ var(A), + \+ system_predicate(_,A), + once((atom(A);compound(A))). + +proper_tunable_ad_head( ( t(_)::A ;T) ) :- + \+ var(A), + \+ system_predicate(_,A), + once((atom(A);compound(A))), + proper_tunable_ad_head(T). + + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% create_mws_atom(+Atom,+Body_Vars,+ID,+Pos,-A2) +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +create_mws_atom(A,Body_Vars,ID,Pos,A2) :- + A =.. [_F|Args], + append(Args,Body_Vars,Args2), + atomic_concat([mvs_fact_,ID,'_',Pos],F2), + A2 =.. [F2|Args2]. + + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% create_ad_aux_facts(+Head,+Vars,+ID,+POS,+Acc,-Facts) +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +create_ad_aux_facts(P::_, _, _, _, Acc, []) :- + % if the probabilities in the head of the AD + % sum up to 1.0 drop the last aux fact + abs(Acc+P-1.0) < 0.0000001, + !. +create_ad_aux_facts(P::Atom, Body_Vars, ID, Pos, Acc, [P1::ProbFact]) :- + create_mws_atom(Atom,Body_Vars,ID,Pos,ProbFact), ( - bb_get(mvs_unique_id,ID) - -> - true; - ID=1 - ), - ID2 is ID+1, - bb_put(mvs_unique_id,ID2). - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -proper_annotated_disjunction(AD,Sum) :- - proper_annotated_disjunction(AD,0.0,Sum), - Sum=<1. - -proper_annotated_disjunction( P :: _, OldSum,NewSum) :- - % evaluate P - P2 is P, - P2>=0, - P2=<1, - NewSum is OldSum+P. -proper_annotated_disjunction((X;Y),OldSum,Sum) :- - proper_annotated_disjunction(X,OldSum,NewSum), - proper_annotated_disjunction(Y,NewSum,Sum). - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - -proper_tunable_annotated_disjunction( t(_) :: _). -proper_tunable_annotated_disjunction((X;Y)) :- - proper_tunable_annotated_disjunction(X), - proper_tunable_annotated_disjunction(Y). - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - -compile_tunable_annotated_disjunction(Head,Body,Facts2,Bodies2,Extra_ID,AD_CPL_Semantics,Mode) :- - get_next_unique_id(Extra_ID), - - ( - AD_CPL_Semantics==true - -> - term_variables(Body,Body_Vars); - Body_Vars=[] - ), - - - - convert_a_tunable(Head,Extra_ID,[],Facts0,Body_Vars), - - problog_flag(ad_sumto1_learning,AD_SumTo1_Learning), - ( - AD_SumTo1_Learning==true - -> - Facts0=[_|Facts1]; - Facts1=Facts0 - ), - - reverse(Facts1,Facts2), - convert_b(Head,Body,_NewBody,Extra_ID,[],Bodies,Body_Vars,Mode,Facts2), - - - reverse(Bodies,Bodies2). -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -compile_annotated_disjunction(Head,Body,Facts2,Bodies2,Extra_ID,AD_CPL_Semantics,Mode,ProbSum) :- - get_next_unique_id(Extra_ID), - - ( - AD_CPL_Semantics==true - -> - term_variables(Body,Body_Vars); - Body_Vars=[] - ), - - convert_a(Head,0.0,_Acc,Extra_ID,[],Facts0,Body_Vars), - - ( - abs(ProbSum-1.0) < 0.0000001 - -> - Facts0=[_|Facts1]; - Facts1=Facts0 - ), - - - reverse(Facts1,Facts2), - convert_b(Head,Body,_NewBody,Extra_ID,[],Bodies,Body_Vars,Mode,Facts2), - - - reverse(Bodies,Bodies2). - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -convert_a((X;Y),OldAcc,Acc,Extra_ID,OldFacts,Facts,Body_Vars) :- - convert_a(X,OldAcc,NewAcc,Extra_ID,OldFacts,NewFacts,Body_Vars), - convert_a(Y,NewAcc,Acc,Extra_ID,NewFacts,Facts,Body_Vars). -convert_a(P::Atom,OldAcc,NewAcc,Extra_ID,OldFacts,[P1::ProbFact|OldFacts],Body_Vars) :- - Atom =.. [Functor|AllArguments], - append(AllArguments,Body_Vars,NewAllArguments), - length(AllArguments,Arity), - - atomic_concat([mvs_fact_,Functor,'_',Arity,'_',Extra_ID],NewAtom), - - ProbFact =.. [NewAtom|NewAllArguments], - ( - (P=:=0; OldAcc=:=0) - -> - P1 is P; - P1 is min(P/(1-OldAcc),1.0) - ), - NewAcc is OldAcc+P. -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -convert_a_tunable((X;Y),Extra_ID,OldFacts,Facts,Body_Vars) :- - convert_a_tunable(X,Extra_ID,OldFacts,NewFacts,Body_Vars), - convert_a_tunable(Y,Extra_ID,NewFacts,Facts,Body_Vars). -convert_a_tunable(t(_)::Atom,Extra_ID,OldFacts,[t(_)::ProbFact|OldFacts],Body_Vars) :- - Atom =.. [Functor|AllArguments], - append(AllArguments,Body_Vars,NewAllArguments), - length(AllArguments,Arity), - - atomic_concat([mvs_fact_,Functor,'_',Arity,'_',Extra_ID],NewAtom), - - ProbFact =.. [NewAtom|NewAllArguments]. -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -convert_b((X;Y),OldBody,Body,ExtraID,OldBodies,Bodies,Body_Vars,Mode,Facts) :- - convert_b(X,OldBody,NewBody,ExtraID,OldBodies,NewBodies,Body_Vars,Mode,Facts), - convert_b(Y,NewBody,Body,ExtraID,NewBodies,Bodies,Body_Vars,Mode,Facts). -convert_b(_::Atom,OldBody,NewBody,Extra_ID,OldBodies,[(Atom:-ThisBody)|OldBodies],Body_Vars,Mode,Facts) :- - Atom =.. [Functor|AllArguments], - append(AllArguments,Body_Vars,NewAllArguments), - - length(AllArguments,Arity), - atomic_concat([mvs_fact_,Functor,'_',Arity,'_',Extra_ID],NewFunctor), - - ProbFact =.. [NewFunctor|NewAllArguments], - ( - memberchk(_::ProbFact,Facts) - -> - tuple_append(OldBody,ProbFact,ThisBody); - ThisBody=OldBody - ), - - ( - Mode==lfi_learning - -> - tuple_append(OldBody,\+ProbFact,NewBody); - tuple_append(OldBody,problog_not(ProbFact),NewBody) + (P=:=0; Acc=:=0)-> + P1 is P + ; + P1 is min(P/(1-Acc),1.0) ). +create_ad_aux_facts((P::Atom;T), Body_Vars, ID, Pos, Acc, [P1::ProbFact|T2]) :- + create_mws_atom(Atom,Body_Vars,ID,Pos,ProbFact), + ( + (P=:=0; Acc=:=0)-> + P1 is P + ; + P1 is min(P/(1-Acc),1.0) + ), + Acc2 is Acc+P, + Pos2 is Pos+1, + create_ad_aux_facts(T,Body_Vars,ID,Pos2,Acc2,T2). + + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +create_tunable_ad_aux_facts(t(_)::_,_,_,Pos,[]) :- + Pos>1, + problog_flag(ad_sumto1_learning,true), + !. +create_tunable_ad_aux_facts(t(_)::Atom,Body_Vars,ID,Pos,[t(_)::ProbFact]) :- + create_mws_atom(Atom,Body_Vars,ID,Pos,ProbFact). +create_tunable_ad_aux_facts((t(_)::Atom;T),Body_Vars,ID,Pos,[t(_)::ProbFact|T2]) :- + create_mws_atom(Atom,Body_Vars,ID,Pos,ProbFact), + Pos2 is Pos+1, + create_tunable_ad_aux_facts(T,Body_Vars,ID,Pos2,T2). + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +create_aux_bodies(_::Atom, Body_Vars, Body, ID, Pos, Aux_Facts , _, [(Atom:-Body2)]) :- + create_mws_atom(Atom,Body_Vars,ID,Pos,ProbFact), + ( + member(_::ProbFact,Aux_Facts)-> + tuple_append(Body,ProbFact,Body2) + ; + Body2=Body + ). + +create_aux_bodies((_::Atom; T), Body_Vars, Body, ID, Pos, Aux_Facts , Mode, [(Atom:-Body2)|T2]) :- + create_mws_atom(Atom,Body_Vars,ID,Pos,ProbFact), + tuple_append(Body,ProbFact,Body2), + ( + Mode==lfi_learning -> + tuple_append(Body,\+ProbFact,Body3) + ; + tuple_append(Body,problog_not(ProbFact),Body3) + ), + + Pos2 is Pos+1, + create_aux_bodies(T,Body_Vars,Body3,ID,Pos2,Aux_Facts,Mode,T2). + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % diff --git a/packages/cplint/Makefile.in b/packages/cplint/Makefile.in index d55e3ffc3..8dd0398a5 100644 --- a/packages/cplint/Makefile.in +++ b/packages/cplint/Makefile.in @@ -70,7 +70,8 @@ CPLINT_PROGRAMS= \ $(CPLINT_SRCDIR)/approx/params.pl \ $(CPLINT_SRCDIR)/approx/parsing.pl \ $(CPLINT_SRCDIR)/approx/tptreefile.pl \ - $(CPLINT_SRCDIR)/approx/utility.pl + $(CPLINT_SRCDIR)/approx/utility.pl \ + $(CPLINT_SRCDIR)/approx/tptree_lpad.pl CPLINT_SEMANTICS_PROGRAMS= \ $(CPLINT_SRCDIR)/semlpadsld.pl \ @@ -143,6 +144,10 @@ CPLINT_DOCS=\ $(CPLINT_DOCDIR)/manual0x.png \ $(CPLINT_DOCDIR)/Makefile +CPLINT_LEARNING_PROGRAMS=\ + $(CPLINT_SRCDIR)/em \ + $(CPLINT_SRCDIR)/rib + all: $(SOBJS) @@ -171,6 +176,8 @@ install: all for h in $(CPLINT_SEMANTICS_PROGRAMS); do $(INSTALL_DATA) $$h $(DESTDIR)$(SHAREDIR); done $(INSTALL_PROGRAM) $(SOBJS) $(DESTDIR)$(YAPLIBDIR) $(INSTALL_PROGRAM) approx/simplecuddLPADs/LPADBDD $(SHAREDIR) + cp -R $(CPLINT_LEARNING_PROGRAMS) $(DESTDIR)$(SHAREDIR)/cplint + installcheck: for h in ${CPLINT_TEST_PROGRAMS}; do echo "t. halt." | $(BINDIR)/yap -l $$h; done diff --git a/packages/cplint/doc/manual.css b/packages/cplint/doc/manual.css index b108289a8..1f45d11a5 100644 --- a/packages/cplint/doc/manual.css +++ b/packages/cplint/doc/manual.css @@ -9,6 +9,7 @@ .cmr-12{font-size:120%;} .cmtt-10{font-family: monospace;} .cmtt-10{font-family: monospace;} +.cmbx-10{ font-weight: bold;} .cmti-10{ font-style: italic;} p.noindent { text-indent: 0em } td p.noindent { text-indent: 0em; margin-top:0em; } diff --git a/packages/cplint/doc/manual.html b/packages/cplint/doc/manual.html index 16d034d2c..572d6cf81 100644 --- a/packages/cplint/doc/manual.html +++ b/packages/cplint/doc/manual.html @@ -1,13 +1,13 @@ -cplint Version 2.0 Manual +cplint Manual - + @@ -15,44 +15,176 @@ -

cplint Version 2.0 Manual

+

cplint Manual

Fabrizio Riguzzi
fabrizio.riguzzi@unife.it

July 27, 2010
+class="cmr-12">July 25, 2011

1 Introduction

cplint is a suite of programs for reasoning with LPADs [1112] and CP-logic -programs [1013]. -

It consists of three Prolog modules for answering queries using goal-oriented -procedures plus three Prolog modules for answering queries using the definition of the -semantics of LPADs and CP-logic. -

The modules for answering queries using using goal-oriented procedures are +class="cmtt-10">cplint is a suite of programs for reasoning with ICL [12], LPADs [2122] and +CP-logic programs [1920]. It contains programs both for inference and +learning. +

+

2 Installation

+

cplint is distributed in source code in the CVS version of Yap. It +includes Prolog and C files. Download it by following the instruction in +http://www.ncc.up.pt/ vsc/Yap/downloads.html . +

cplint requires CUDD and glib-2.0. You can download CUDD from +ftp://vlsi.colorado.edu/pub/cudd-2.4.2.tar.gz . You can download glib-2.0 (version lpadsld.pl, lpad.pl and cpl.pl: +class="cmsy-10">≥ 2.0) from http://www.gtk.org/ . This is a standard GNU package so it is easy to +install it using the package management software of your Linux or Cygwin +distribution. +

Compile CUDD: +

    +
  1. decompress cudd-2.4.2.tar.gz +
  2. +
  3. cd cudd-2.4.2 +
  4. +
  5. see the README file for instructions on compilation
+

Install Yap together with cplint: when compiling Yap following the instruction of +the INSTALL file in the root of the Yap folder, use + +

+configure --enable-cplint=DIR +
+

where DIR is the directory where CUDD is, i.e., the directory ending with +cudd-2.4.2. Under Windows, you have to use Cygwin (CUDD does not compile +under MinGW), so
+ +

+configure --enable-cplint=DIR --enable-cygwin +
+

+

After having performed make install you can do make installcheck that will +execute a suite of tests of the various programs. If no error is reported you have a +working installation of cplint. +

+

3 Syntax

+

LPAD and CP-logic programs consist of a set of annotated disjunctive clauses. +Disjunction in the head is represented with a semicolon and atoms in the head are +separated from probabilities by a colon. For the rest, the usual syntax of Prolog is +used. For example, the CP-logic clause +

+h1 : p1 ∨...∨ hn : pn ← b1,...,bm,¬c1,...,¬cl
is +represented by + +
+h1:p1 ; ... ; hn:pn :- b1,...,bm,\+ c1,....,\+ cl +
+

No parentheses are necessary. The pi are numeric expressions. It is up to the user to +ensure that the numeric expressions are legal, i.e. that they sum up to less than +one. +

If the clause has an empty body, it can be represented like this + +

+h1:p1 ; ... ;hn:pn. +
+

If the clause has a single head with probability 1, the annotation can be omitted and +the clause takes the form of a normal prolog clause, i.e. + +

+h1:- b1,...,bm,\+ c1,...,\+ cl. +
+

stands for + +

+h1:1 :- b1,...,bm,\+ c1,...,\+ cl. +
+

+

The coin example of [22] is represented as (see file coin.cpl) + +

+heads(Coin):1/2 ; tails(Coin):1/2:- + 
     toss(Coin),\+biased(Coin). + 

heads(Coin):0.6 ; tails(Coin):0.4:- + 
     toss(Coin),biased(Coin). + 

fair(Coin):0.9 ; biased(Coin):0.1. + 

toss(coin). +
+

The first clause states that if we toss a coin that is not biased it has equal +probability of landing heads and tails. The second states that if the coin is biased it +has a slightly higher probability of landing heads. The third states that the coin is +fair with probability 0.9 and biased with probability 0.1 and the last clause states +that we toss a coin with certainty. +

Moreover, the bodies of rules can contain the built-in predicates: + +

+is/2, >/2, </2, >=/2 ,=</2, + 
=:=/2, =\=/2, true/0, false/0, + 
=/2, ==/2, \=/2 ,\==/2, length/2 +
+

The bodies can also contain the following library predicates: + +

+member/2, max_list/2, min_list/2 + 
nth0/3, nth/3 +
+

plus the predicate + +

+average/2 +
+

that, given a list of numbers, computes its arithmetic mean. +

The syntax of ICL program is the one used by the AILog 2 system. +

4 Inference

+

cplint contains various modules for answering queries. +

These modules answer queries using using goal-oriented procedures:

-

The modules for answering queries using the definition of the semantics of LPADs -and CP-logic are semlpadsld.pl, semlpad.pl and semcpl.pl: +class="cmtt-10">approx/exact.pl as lpadsld.pl but uses SimplecuddLPADs, a modification + of the Simplecudd instead of the cplint library for building BDDs and + computing the probability. +

These modules answer queries using the definition of the semantics of LPADs and +CP-logic:

-

-

2 Installation

-

cplint is distributed in source code in the git version of Yap. It -includes Prolog and C files. Download it by following the instruction in -http://www.ncc.up.pt/ vsc/Yap/downloads.html . -

cplint requires cudd and glib-2.0. You can download cudd from -http://vlsi.colorado.edu/ fabio/CUDD/ . You can download glib-2.0 (version 2.0) -from http://www.gtk.org/ . This is a standard GNU package so it is easy to -install it using the package management software of your Linux or Cygwin -distribution. -

Compile cudd: -

    -
  1. downlad cudd-2.4.2.tar.gz - -
  2. -
  3. decompress it -
  4. -
  5. cd cudd-2.4.2 -
  6. -
  7. check makefile options -
  8. -
  9. make
-

Install Yap together with cplint: when compiling Yap following the instuction of -the INSTALL file in the root of the Yap folder, use - -

-configure --enable-cplint=DIR -
-

Under Windows, you have to use Cygwin (glu does not compile under MinGW), -so
- -

-configure --enable-cplint=DIR --enable-cygwin -
-

where DIR is the path to the directory cudd-2.4.2 (including cudd-2.4.2). -

After having performed make install you can do make installcheck that will -execute a suite of tests of the various programs. If no error is reported you have a -working installation of cplint. -

-

3 Syntax

-

Disjunction in the head is represented with a semicolon and atoms in the head are -separated from probabilities by a colon. For the rest, the usual syntax of Prolog is -used. For example, the CP-logic clause -

-h1 : p1 ∨...∨ hn : pn ← b1,...,bm,¬c1,...,¬cl
is -represented by - -
-h1:p1 ; ... ; hn:pn :- b1,...,bm,\+ c1,....,\+ cl -
-

No parentheses are necessary. The pi are numeric expressions. It is up to the user to -ensure that the numeric expressions are legal, i.e. that they sum up to less than -one. -

If the clause has an empty body, it can be represented like this - -

-h1:p1 ; ... ;hn:pn. -
-

If the clause has a single head with probability 1, the annotation can be omitted and -the clause takes the form of a normal prolog clause, i.e. - -

-h1:- b1,...,bm,\+ c1,...,\+ cl. -
-

stands for - -

-h1:1 :- b1,...,bm,\+ c1,...,\+ cl. -
-

-

The coin example of [12] is represented as (see file coin.cpl) - -

-heads(Coin):1/2 ; tails(Coin):1/2:- - 
     toss(Coin),\+biased(Coin). - 

heads(Coin):0.6 ; tails(Coin):0.4:- - 
     toss(Coin),biased(Coin). - 

fair(Coin):0.9 ; biased(Coin):0.1. - 

toss(coin). -
-

The first clause states that if we toss a coin that is not biased it has equal -probability of landing heads and tails. The second states that if the coin is biased it -has a slightly higher probability of landing heads. The third states that the coin is -fair with probability 0.9 and biased with probability 0.1 and the last clause states -that we toss a coin with certainty. -

-

4 Commands

-

All six modules accept the same commands for reading in files and answering queries. -The LPAD or CP-logic program must be stored in a text file with extension 20]. +

+

4.1 Commands

+

The LPAD or CP-logic program must be stored in a text file with extension .cpl. Suppose you have stored the example above in file coin.cpl. In order to answer @@ -246,23 +294,23 @@ queries from this program, you have to run Yap, load one of the modules (such as example lpad.pl) by issuing the command -

+
use_module(library(lpad)).
-

at the command prompt. Then you must parse the source file

at the command prompt. Then you must parse the source file coin.cpl with the command -

+
p(coin).
-

if

if coin.cpl is in the current directory, or -

+
p(’path_to_coin/coin’).
-

if

if coin.cpl is in a different directory. At this point you can pose query to the program by using the predicate s/2 (for solve) that takes as its first argument a @@ -271,10 +319,10 @@ as its second argument. For example, the probability of the conjunction head(coin),biased(coin) can be asked with the query -

+
s([head(coin),biased(coin)],P).
-

For computing the probability of a conjunction given another conjunction you can +

For computing the probability of a conjunction given another conjunction you can use the predicate sc/3 (for solve conditional) that take takes as input the query conjunction as its first argument, the evidence conjunction as its second argument @@ -284,22 +332,22 @@ class="cmtt-10">heads(coin) given the evidence biased(coin) can be asked with the query -

+
sc([heads(coin)],[biased(coin)],P).
-

After having parsed a program, in order to read in a new program you must restart +

After having parsed a program, in order to read in a new program you must restart Yap when using semlpadsld.pl and semlpad.pl. With the other modules, you can directly parse a new program. -

When using

When using lpad.pl, the system can print the message “Uunsound program” in the case in which an instance with a three valued well founded model is found. Moreover, it can print the message “It requires the choice of a head atom from a non ground head”: in this case, in order to answer the query, all the groundings of the culprit clause must be generated, which may be impossible for programs with function symbols. -

When using

When using semcpl.pl, you can print the execution process by using the command print. after context is a list of atoms that are true in the context. semcpl.pl can print “Invalid program” in the case in which no execution process exists. -

When using

When using cpl.pl you can print a partial execution model including all the clauses involved in the query issued with print. cpl.pl can print the messages “Uunsound program”, “It requires the choice of a head atom from a non ground head” and “Invalid program”. -

The modules make use of a number of parameters in order to control their -behavior. They that can be set with the command +

For approx/deepit.pl and approx/deepdyn.pl the command -

+
+solve(GoalsList, ProbLow, ProbUp, ResTime, BddTime) +
+

takes as input a list of goals GoalsList and returns a lower bound on the +probability ProbLow, an upper bound on the probability ProbUp, the CPU time spent +on performing resolution ResTime and the CPU time spent on handling BDDs +BddTime. +

For approx/bestk.pl the command + +

+solve(GoalsList, ProbLow,  ResTime, BddTime) +
+

takes as input a list of goals GoalsList and returns a lower bound on the +probability ProbLow, the CPU time spent on performing resolution ResTime and the +CPU time spent on handling BDDs BddTime. +

For approx/bestfirst.pl the command + +

+solve(GoalsList, ProbLow, ProbUp, Count, ResTime, BddTime) +
+

takes as input a list of goals GoalsList and returns a lower bound on the +probability ProbLow, an upper bound on the probability ProbUp, the number of +BDDs generated by the algorithm Count, the CPU time spent on performing +resolution ResTime and the CPU time spent on handling BDDs BddTime. +

For approx/montecarlo.pl the command + +

+solve(GoalsList, Samples, Time, Low, Prob, Up) +
+

takes as input a list of goals GoalsList and returns the number of samples taken +Samples, the time required to solve the problem Time, the lower end of the +confidence interval Lower, the estimated probability Prob and the upper end of the +confidence interval Up. +

For mcintyre.pl: the command + +

+solve(Goals, Samples, CPUTime, WallTime, Lower, Prob, Upper) :- +
+

takes as input a conjunction of goals Goals and returns the number of samples taken +Samples, the CPU time required to solve the problem CPUTime, the wall time +required to solve the problem CPUTime, the lower end of the confidence interval +Lower, the estimated probability Prob and the upper end of the confidence interval +Up. +

For approx/exact.pl the command + +

+solve(GoalsList, Prob, ResTime, BddTime) +
+

takes as input a conjunction of goals Goals and returns the probability Prob, the +CPU time spent on performing resolution ResTime and the CPU time spent on +handling BDDs BddTime. +

+

4.1.1 Parameters
+

The modules make use of a number of parameters in order to control their behavior. +They that can be set with the command + +

set(parameter,value).
-

from the Yap prompt after having loaded the module. The current value can be read +

from the Yap prompt after having loaded the module. The current value can be read with -

+
setting(parameter,Value).
-

from the Yap prompt. The available parameters are: +

from the Yap prompt. The available parameters are:

  • epsilon_parsing (valid for all six modules): if (1 - the sum of the +class="cmtt-10">epsilon_parsing (valid for all modules): if (1 - the sum of the probabilities of all the head atoms) is smaller than epsilon_parsing then m is the number of the binary variable. The correspondenc between variables and clauses can be evinced from the message printed on the screen, such as -
    +
    Variables: [(2,[X=2,X1=1]),(2,[X=1,X1=0]),(1,[])]
    -

    where the first element of each couple is the clause number of the input file +

    where the first element of each couple is the clause number of the input file (starting from 1). In the example above variable X0 corresponds to clause 2 @@ -366,20 +509,20 @@ class="cmtt-10">2 with the substitutions X=1,X1=0 and variable X2 corresponds to clause 1 with the empty - substitution. You can view the graph with graphviz ( www.graphviz.org ) - using the command +class="cmtt-10">1 with the + empty substitution. You can view the graph with graphviz using the + command -

    +
    dotty cpl.dot &
    -

    +

  • ground_body (valid for lpadsld.pl and all semantic modules): determines how +class="cmtt-10">ground_body: (valid for lpadsld.pl and all semantic modules) determines how non ground clauses are treated: if true, ground clauses are obtained from a non ground clause by replacing each variable with a constant, if false, ground clauses are obtained by replacing only variables in the head with a constant. In the case where the body contains variables not in the head, setting it to false means that the body represents an existential - event.
-

-

5 Semantic Modules

-

The three semantic modules need to produce a grounding of the program in order to + event. + +

  • min_error: (valid for approx/deepit.pl, approx/deepdyn.pl, + approx/bestk.pl, approx/bestfirst.pl, approx/montecarlo.pl and + mcintyre.pl) is the threshold under which the difference between + upper and lower bounds on probability must fall for the algorithm to + stop. +
  • +
  • k: maximum number of explanations for approx/bestk.pl and + approx/bestfirst.pl and number of samples to take at each iteration for + approx/montecarlo.pl and mcintyre.pl +
  • +
  • prob_bound: (valid for approx/deepit.pl, approx/deepdyn.pl, + approx/bestk.pl and approx/bestfirst.pl) is the initial bound on the + probability of explanations when iteratively building explanations +
  • +
  • prob_step: (valid for approx/deepit.pl, approx/deepdyn.pl, + approx/bestk.pl and approx/bestfirst.pl) is the increment on the + bound on the probability of explanations when iteratively building + explanations +
  • +
  • timeout: (valid for approx/deepit.pl, approx/deepdyn.pl, approx/bestk.pl, + approx/bestfirst.pl and approx/exact.pl) timeout for builduing + BDDs
  • + +

    +

    4.2 Semantic Modules

    +

    The three semantic modules need to produce a grounding of the program in order to compute the semantics. They require an extra file with extension .uni (for universe) in the same directory where the .cpl file is. -

    There are two ways to specify how to ground a program. The first consists in +

    There are two ways to specify how to ground a program. The first consists in providing the list of constants to which each variable can be instantiated. For example, in our case the current directory will contain a file coin.uni that is a Prolog file containing facts of the form -

    +
    universe(var_list,const_list).
    -

    where

    where var_list is a list of variables names (each must be included in single quotes) and const_list is a list of constants. The semantic modules generate the grounding @@ -415,23 +610,23 @@ class="cmtt-10">var_list with the constants of class="cmtt-10">const_list. Note that the variables are identified by name, so a variable with the same name in two different clauses will be instantiated with the same constants. -

    The other way to specify how to ground a program consists in using mode and +

    The other way to specify how to ground a program consists in using mode and type information. For each predicate, the file .uni must contain a fact of the form -

    +
    mode(predicate(t1,...,tn)).
    -

    that specifies the number and types of each argument of the predicate. Then, the list +

    that specifies the number and types of each argument of the predicate. Then, the list of constants that are in the domain of each type ti must be specified with a fact of the form -

    +
    type(ti,list_of_constants).
    -

    The file

    The file .uni can contain both universe and mode declaration, the ones to be used depend on the value of the parameter grounding: with value variables, the universe declarations are used, with value modes the mode declarations are used. -

    With

    With semcpl.pl only mode declarations can be used. -

    -

    6 Extensions

    -

    In this section we will present the extensions to the syntax of LPADs and CP-logic +

    +

    4.3 Extensions

    +

    In this section we will present the extensions to the syntax of LPADs and CP-logic programs that cplint can handle. -

    The first is the use of some standard Prolog predicates. The bodies can contain -the built-in predicates: - -

    -is/2 - 
    >/2 - 
    </2 - 
    >=/2 - 
    =</2 - 
    =:=/2 - 
    =\=/2 - 
    true/0 - 
    false/0 - 
    =/2 - 
    ==/2 - 
    \=/2 - 
    \==/2 - 
    length/2 -
    -

    The bodies can also contain the following library predicates: - -

    -member/2 - 
    max_list/2 - 
    min_list/2 - 
    nth0/3 - 
    nth/3 -
    -

    plus the predicate - -

    -average/2 -
    -

    that, given a list of numbers, computes its arithmetic mean. -

    When using lpadsld can handle. +

    When using lpadsld.pl, the bodies can contain the predicates setof/3 and bagof/3 with the same meaning as in Prolog. Existential quantifiers are allowed in both, so for example the query -

    +
    setof(Z, (term(X,Y))^foo(X,Y,Z), L).
    -

    returns all the instantiations of

    returns all the instantiations of Z such that there exists an instantiation of X and Y for which foo(X,Y,Z) is true. -

    An example of the use of

    An example of the use of setof and bagof is in the file female.cpl: -

    +
    male(C):M/P ; female(C):F/P:-  
        person(C),  
        setof(Male,known_male(Male),LM), @@ -514,31 +675,27 @@ male(C):M/P ; female(C):F/P:-  
    person(f).  
     
    known_female(a). - 
     
    known_female(b). - 
     
    known_female(c). - 
     
    known_male(d). - 
     
    known_male(e).
    -

    The disjunctive rule expresses the probability of a person of unknown sex of being +

    The disjunctive rule expresses the probability of a person of unknown sex of being male or female depending on the number of males and females that are known. This is an example of the use of expressions in the probabilities in the head that depend on variables in the body. The probabilities are well defined because they always sum to 1 (unless P is 0). -

    Another use of

    Another use of setof and bagof is to have an attribute depend on an aggregate function of another attribute, similarly to what is done in PRM and CLP(BN). -

    So, in the classical school example (available in

    So, in the classical school example (available in student.cpl) you can find the following clauses: -

    +
    student_rank(S,h):0.6 ; student_rank(S,l):0.4:-  
        bagof(G,R^(registr_stu(R,S),registr_gr(R,G)),L),  
        average(L,Av),Av>1.5. @@ -547,7 +704,7 @@ student_rank(S,h):0.6 ; student_rank(S,l):0.4:-  
        bagof(G,R^(registr_stu(R,S),registr_gr(R,G)),L),  
        average(L,Av),Av =< 1.5.
    -

    where

    where registr_stu(R,S) expresses that registration R refers to student S and @@ -557,7 +714,7 @@ class="cmtt-10">R reports grade G which is a natural number. The two clauses express a dependency of the rank of the student from the average of her grades. -

    Another extension can be used with

    Another extension can be used with lpadsld.pl in order to be able to represent reference uncertainty of PRMs. Reference uncertainty means that the link structure of a relational model is not fixed but is uncertain: this is represented by having the @@ -569,25 +726,25 @@ selected uniformly from certain sets, the following clauses can be used (see fil paper_ref_simple.cpl): -

    +
    uniform(cites_cited(C,P),P,L):-  
        bagof(Pap,paper_topic(Pap,theory),L).  
     
    uniform(cites_citing(C,P),P,L):-  
        bagof(Pap,paper_topic(Pap,ai),L).
    -

    The first clauses states that the paper

    The first clauses states that the paper P cited in a citation C is selected uniformly from the set of all papers with topic theory. The second clauses expresses that the citing paper is selected uniformly from the papers with topic ai. -

    These clauses make use of the predicate +

    These clauses make use of the predicate -

    +
    uniform(Atom,Variable,List)
    -

    in the head, where

    in the head, where Atom must contain Variable. The meaning is the following: the set of all the atoms obtained by instantiating ∕N where N is the length of List. -

    A more elaborate example is present in file

    A more elaborate example is present in file paper_ref.cpl: -

    +
    uniform(cites_citing(C,P),P,L):-  
        setof(Pap,paper(Pap),L).  
    @@ -616,19 +773,19 @@ uniform(cites_citing(C,P),P,L):-  
    uniform(cites_cited(C,P),P,L):-  
        cites_cited_group(C,T),bagof(Pap,paper_topic(Pap,T),L).
    -

    where the cited paper depends on the topic of the citing paper. In particular, if the +

    where the cited paper depends on the topic of the citing paper. In particular, if the topic is theory, the cited paper is selected uniformly from the papers about theory with probability 0.9 and from the papers about ai with probability 0.1. if the topic is ai, the cited paper is selected uniformly from the papers about theory with probability 0.01 and from the papers about ai with probability 0.99. -

    PRMs take into account as well existence uncertainty, where the existence of +

    PRMs take into account as well existence uncertainty, where the existence of instances is also probabilistic. For example, in the paper domain, the total number of citations may be unknown and a citation between any two paper may have a probability of existing. For example, a citation between two paper may be more probable if they are about the same topic: -

    +
    cites(X,Y):0.005 :-  
        paper_topic(X,theory),paper_topic(Y,theory).  
    @@ -641,7 +798,7 @@ cites(X,Y):0.005 :-  
    cites(X,Y):0.008 :-  
        paper_topic(X,ai),paper_topic(Y,ai).
    -

    This is an example where the probabilities in the head do not sum up to one so the +

    This is an example where the probabilities in the head do not sum up to one so the null event is automatically added to the head. The first clause states that, if the topic of a paper X is theory and of paper X to Y. The other clauses consider the remaining cases for the topics. -

    -

    7 Additional Files

    -

    In the directory where Yap keeps the library files (usually

    +

    4.4 Files

    +

    In the directory where Yap keeps the library files (usually /usr/local/share/ Yap) you can find the directory cplint that contains the files:

      -
    • testlpadsld_gbtrue.pl, testlpadsld_gbfalse.pl, testlpad.pl, - testcpl.pl, testsemlpadsld.pl, testsemlpad.pl testsemcpl.pl: +
    • testlpadsld_gbtrue.pl, testlpadsld_gbfalse.pl, testlpad.pl, + testcpl.pl, testsemlpadsld.pl, testsemlpad.pl testsemcpl.pl: Prolog programs for testing the modules. They are executed when issuing the command make installcheck during the installation. To execute @@ -679,55 +833,55 @@ class="cmtt-10">examples:
    • alarm.cpl: representation of the Bayesian network in Figure 2 of [12]. +href="#XVenVer04-ICLP04-IC">22].
    • coin.cpl: coin example from [12]. +href="#XVenVer04-ICLP04-IC">22].
    • coin2.cpl: coin example with two coins. -
    • dice.cpl: dice example from [12]. +href="#XVenVer04-ICLP04-IC">22]. +
    • twosideddice.cpl, threesideddice.cpl game with idealized dice with two or three sides. Used in the experiments in [8]. +href="#XRig-RCRA07-IC">14].
    • ex.cpl: first example in [8]. +href="#XRig-RCRA07-IC">14].
    • exapprox.cpl: example showing the problems of approximate inference (see [8]). +href="#XRig-RCRA07-IC">14]).
    • exrange.cpl: example showing the problems with non range restricted programs (see [8]). +href="#XRig-RCRA07-IC">14]).
    • female.cpl: example showing the dependence of probabilities in the head from variables in the body (from [12]). +href="#XVenVer04-ICLP04-IC">22]).
    • mendel.cpl, mendels.cpl: programs describing the Mendelian rules of inheritance, taken from [2]. +href="#XBlo04-ILP04WIP-IC">5].
    • paper_ref.cpl, paper_ref_simple.cpl: paper citations examples, showing reference uncertainty, inspired by [6]. +href="#XGetoor+al:JMLR02">11].
    • paper_ref_not.cpl: paper citations example showing that negation @@ -747,16 +901,16 @@ class="cmtt-10">school.cpl.
    • student.cpl: student example from Figure 1.3 of [5]. +href="#XGetFri01-BC">10].
    • win.cpl, light.cpl, trigger.cpl, throws.cpl, hiv.cpl,
      invalid.cpl: programs taken from [13]. 20]. invalid.cpl is an example of a program that is invalid but sound.
    -

    The files

    The files *.uni that are present for some of the examples are used by the semantical modules. Some of the example files contain in an initial comment some queries together with their result. @@ -764,18 +918,289 @@ class="cmtt-10">*.uni that are present for some of the examples are used

  • Subdirectory doc: contains this manual in latex, html and pdf.
  • -

    -

    8 License

    -

    +

    5 Learning

    +

    cplint contains the following learning algorithms: +

      +
    • CEM (cplint EM): an implementation of EM for learning parameters + that is based on lpadsld.pl [17] +
    • +
    • RIB (Relational Information Bottleneck): an algorithm for learning + parameters based on the Information Bottleneck [17] +
    • +
    • EMBLEM (EM over Bdds for probabilistic Logic programs Efficient + Mining): an implementation of EM for learning parameters that computes + expectations directly on BDDs [23] +
    • +
    • SLIPCASE (Structure LearnIng of ProbabilistiC logic progrAmS with Em + over bdds): an algorithm for learning the structure of program that is + based on EMBLEM [4]
    +

    +

    5.1 Input

    +

    To execute the learning algorithms, prepare four files in the same folder: +

      +
    • <stem>.kb: contains the example interpretations +
    • +
    • <stem>.bg: contains the background knowledge, i.e., knowledge valid for + all interpretations +
    • +
    • <stem>.l: contains language bias information +
    • +
    • <stem>.cpl: contains the LPAD for you which you want to learn the + parameters or the initial LPAD for SLIPCASE
    +

    where <stem> is your dataset name. Examples of these files can be found in the dataset +pages. +

    In <stem>.kb the example interpretations have to be given as a list of Prolog +facts initiated by begin(model(<name>)). and terminated by end(model(<name>)). +as in + +

    +begin(model(b1)). + 
    sameperson(1,2). + 
    movie(f1,1). + 
    movie(f1,2). + 
    workedunder(1,w1). + 
    workedunder(2,w1). + 
    gender(1,female). + 
    gender(2,female). + 
    actor(1). + 
    actor(2). + 
    end(model(b1)). +
    +

    The interpretations may contain a fact of the form + +

    +prob(0.3). +
    +

    assigning a probability (0.3 in this case) to the interpretations. If this is omitted, the +probability of each interpretation is considered equal to 1∕n where n is the total +number of interpretations. prob/1 can be used to set different multiplicity for the +different interpretations. +

    In order for RIB to work, the input interpretations must share the Herbrand +universe. If this is not the case, you have to translate the interpretations in this was, +see for example the sp1 files in RIB’s folder, that are the results of the conversion of +the first fold of the IMDB dataset. +

    <stem>.bg can contain Prolog clauses that can be used to derive additional +conclusions from the atoms in the interpretations. +

    <stem>.l contains the declarations of the input and output predicates, of the +unseen predicates and the commands for setting the algorithms’ parameters. Output +predicates are declared as + +

    +output(<predicate>/<arity>). +
    +

    and define the predicates whose atoms in the input interpretations are used as the +goals for the prediction of which you want to optimize the parameters. Derivations +for these goals are built by the systems. +

    Input predicates are those for the predictions of which you do not want to +optimize the parameters. You can declare closed world input predicates +with + +

    +input_cw(<predicate>/<arity>). +
    +

    For these predicates, the only true atoms are those in the interpretations, the +clauses in the input program are not used to derive atoms not present in the +interpretations. +

    Open world input predicates are declared with + +

    +input(<predicate>/<arity>). +
    +

    In this case, if a subgoal for such a predicate is encountered when deriving the atoms +for the output predicates, both the facts in the interpretations and the clauses of the +input program are used. +

    For RIB, if there are unseen predicates, i.e., predicates that are present in the +input program but not in the interpretations, you have to declare them +with + +

    +unseen(<predicate>/<arity>). +
    +

    +

    For SLIPCASE, you have to specify the language bias by means of mode +declarations in the style of Progol . + +

    +modeh(<recall>,<predicate>(<arg1>,...). +
    +

    specifies the atoms that can appear in the head of clauses, while + +

    +modeb(<recall>,<predicate>(<arg1>,...). +
    +

    specifies the atoms that can appear in the body of clauses. <recall> can be an +integer or * (currently unused). +

    The arguments are of the form + +

    ++<type> +
    +

    for specifying an input variable of type <type>, or + +

    +-<type> +
    +

    for specifying an output variable of type <type>. +

    5.2 Parameters

    +

    In order to set the algorithms’ parameters, you have to insert in <stem>.l commands +of the form + +

    +:- set(<parameter>,<value>). +
    +

    The available parameters are: +

      +
    • depth (values: integer or inf, default value: 3): depth of derivations if + depth_bound is set to true +
    • +
    • single_var (values: {true,false}, default value: false, valid for CEM + only): if set to true, there is a random variable for each clauses, instead + of a separate random variable for each grounding of a clause +
    • +
    • sample_size (values: integer, default value: 1000): total number of + examples in case in which the models in the .kb file contain a prob(P). + fact. In that case, one model corresponds to sample_size*P examples +
    • +
    • epsilon_em (values: real, default value: 0.1, valid for CEM only): if the + difference in the log likelihood in two successive EM iteration is smaller + than epsilon_em, then EM stops +
    • +
    • epsilon_em_fraction (values: real, default value: 0.01, valid for CEM + only): if the difference in the log likelihood in two successive EM iteration + is smaller than epsilon_em_fraction*(-current log likelihood), then EM + stops +
    • +
    • random_restarts_number (values: integer, default value: 1, valid for CEM + only): number of random restarts +
    • +
    • setrand (values: rand(integer,integer,integer)): seed for the random + functions, see Yap manual for allowed values +
    • +
    • minimal_step (values: [0,1], default value: 0.005, valid for RIB only): + minimal increment of γ +
    • +
    • maximal_step (values: [0,1], default value: 0.1, valid for RIB only): + maximal increment of γ + +
    • +
    • logsize_fraction (values: [0,1], default value 0.9, valid for RIB only): + RIB stops when I(CH,T;Y ) is above logsize_fraction times its + maximum value (log |CH,T|, see [9]) +
    • +
    • delta (values: negative integer, default value -10, valid for RIB only): + value assigned to log 0 +
    • +
    • epsilon_fraction (values: integer, default value 100, valid for RIB only): + in the computation of the step, the value of ϵ of [9] is obtained as + log |CH,Tepsilon_fraction +
    • +
    • max_rules (values: integer, default value: 6000, valid for RIB only): + maximum number of ground rules. Used to set the size of arrays for storing + internal statistics. Can be increased as much as memory allows.
    +

    +

    5.3 Commands

    +

    To execute CEM, load em.pl and call: + +

    +?:- em(stem). +
    +

    To execute RIB, load rib.pl and call: + +

    +?:- ib_par(stem). +
    +

    +

    +

    6 License

    +

    cplint, as Yap, follows the Artistic License 2.0 that you can find in Yap CVS root dir. The copyright is by Fabrizio Riguzzi. -

    The program uses the library

    The modules in the approx subdirectory use SimplecuddLPADs, a modification of +the Simplecudd library whose copyright is by Katholieke Universiteit Leuven and +that follows the Artistic License 2.0. +

    Some modules use the library CUDD for manipulating BDDs that is included in glu. For the use of CUDD, the following license must be accepted: -

    Copyright (c) 1995-2004, Regents of the University of Colorado -

    All rights reserved. -

    Redistribution and use in source and binary forms, with or without modification, +

    Copyright (c) 1995-2004, Regents of the University of Colorado +

    All rights reserved. +

    Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

    • Redistributions of source code must retain the above copyright notice, this @@ -788,13 +1213,14 @@ are permitted provided that the following conditions are met:
    • Neither the name of the University of Colorado nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
    -

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS
    AND CONTRIBUTORS ”AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAU-SED
    AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -

    lpad.pl, semlpad.pl and cpl.pl are based on the SLG system by and David Scott Warren , Copyright (C) 1993 Southern Methodist University, 1993 SUNY at Stony Brook, see the file COYPRIGHT_SLG for detailed information on this copyright. -

    References

    + id="x1-150006">References

    [1]   K. R. Apt and M. Bezem. Acyclic programs. New Generation - Comput., 9(3/4):335–364, 1991. + id="XDBLP:journals/ngc/AptB91">K. R. Apt and M. Bezem. Acyclic programs. New Gener. Comput., + 9(3/4):335–364, 1991.

    [2]   Elena Bellodi and Fabrizio Riguzzi. EM over binary decision diagrams + for probabilistic logic programs. In Proceedings of the 26th Italian + Conference on Computational Logic (CILC2011), Pescara, Italy, 31 August + 31-2 September, 2011, 2011. +

    +

    + [3]   Elena Bellodi and Fabrizio Riguzzi. EM over binary decision + diagrams for probabilistic logic programs. Technical Report CS-2011-01, + Dipartimento di Ingegneria, Universitą di Ferrara, Italy, 2011. +

    +

    + [4]   Elena Bellodi and Fabrizio Riguzzi. Learning the structure of + probabilistic logic programs. In Inductive Logic Programming, 21th + International Conference, ILP 2011, London, UK, 31 July-3 August, 2011, + 2011. +

    +

    + [5]   H. Blockeel. Probabilistic logical models for mendel’s experiments: An + exercise. In Inductive Logic Programming (ILP 2004), Work in Progress Track, 2004.

    - [3]      Stefano Bragaglia and Fabrizio Riguzzi. Approximate inference for logic + programs with annotated disjunctions. In Paolo Frasconi and Francesca + Lisi, editors, Inductive Logic Programming 20th International Conference, + ILP 2010, Florence, Italy, June 27-30, 2010. Revised Papers, volume 6489 + of LNCS, pages 30–37. Springer, 2011. +

    +

    + [7]   Weidong Chen and David Scott Warren. Tabled evaluation with delaying for general logic programs. J. ACM, 43(1):20–74, 1996. +class="cmti-10">Journal of the ACM, 43(1):20–74, 1996.

    - [4]   L. De Raedt, A. Kimmig, and H. Toivonen. Problog: A probabilistic - prolog and its application in link discovery. In Proceedings of the 20th + [8]   L. De Raedt, A. Kimmig, and H. Toivonen. ProbLog: A probabilistic + Prolog and its application in link discovery. In International Joint International Joint Conference on Artificial Intelligence, pages 2462–2467, - 2007. +class="cmti-10">Conference on Artificial Intelligence, pages 2462–2467, 2007.

    - [5]      G. Elidan and N. Friedman. Learning hidden variable networks: The + information bottleneck approach. Journal of Machine Learning Research, + 6:81–127, 2005. +

    +

    + [10]   L. Getoor, N. Friedman, D. Koller, and A. Pfeffer. Learning probabilistic relational models. In Saso Dzeroski and Nada Lavrac, editors, Relational Data Mining. Springer-Verlag, Berlin, 2001.

    - [6]      L. Getoor, N. Friedman, D. Koller, and B. Taskar. Learning probabilistic models of relational structure. Journal of Machine Learning @@ -863,70 +1330,92 @@ class="cmti-10">Journal of Machine Learning class="cmti-10">Research, 3:679–707, December 2002.

    - [7]   Fabrizio Riguzzi. A top down interpreter for lpad and cp-logic. In - 10th Congress of the Italian Association for Artificial Intelligence. Springer, - 2007. - http://www.ing.unife.it/docenti/FabrizioRiguzzi/Papers/Rig-AIIA07.pdf - . + [12]   David Poole. The independent choice logic for modelling multiple agents + under uncertainty. Artificial Intelligence, 94(1-2):7–56, 1997.

    - [8]   Fabrizio Riguzzi. A top down interpreter for lpad and - cp-logic. In The 14th RCRA workshop Experimental Evaluation of + [13]   Fabrizio Riguzzi. A top down interpreter for LPAD and CP-logic. In Algorithms for Solving Problems with Combinatorial Explosion, 2007. - http://pst.istc.cnr.it/RCRA07/articoli/P19-riguzzi-RCRA07.pdf . +class="cmti-10">Congress of the Italian Association for Artificial Intelligence, volume 4733 + of LNAI, pages 109–120. Springer, 2007.

    - [9]   V. Santos Costa, D. Page, M. Qazi, and J. Cussens. CLP(BN): + [14]   Fabrizio Riguzzi. A top down interpreter for LPAD and CP-logic. + In Proceedings of the 14th RCRA workshop Experimental Evaluation of + Algorithms for Solving Problems with Combinatorial Explosion, 2007. +

    +

    + [15]   Fabrizio Riguzzi. Extended semantics and inference for the Independent + Choice Logic. Logic Journal of the IGPL, 17(6):589–629, 2009. +

    +

    + [16]   Fabrizio Riguzzi. MCINTYRE: A Monte Carlo algorithm for + probabilistic logic programming. In Proceedings of the 26th Italian + Conference on Computational Logic (CILC2011), Pescara, Italy, 31 + August-2 September, 2011, 2011. +

    +

    + [17]   Fabrizio Riguzzi and Nicola Di Mauro. Applying the information + bottleneck to statistical relational learning. Machine Learning, 2011. To + appear. +

    +

    + [18]   V. Santos Costa, D. Page, M. Qazi, and J. Cussens. CLP(BN): Constraint logic programming for probabilistic knowledge. In Uncertainty in Artificial Intelligence (UAI 2003), 2003. +class="cmti-10">in Artificial Intelligence. Morgan Kaufmann, 2003.

    - [10]      J. Vennekens, M. Denecker, and M. Bruynooghe. Representing causal - information about a probabilistic process. In 10th European Conference on + information about a probabilistic process. In Proceedings of the 10th Logics in Artificial Intelligence, JELIA 2006, LNAI. Springer, September - 2006. +class="cmti-10">European Conference on Logics in Artificial Intelligence, LNAI. Springer, + September 2006. +

    + +

    + [20]   J. Vennekens, Marc Denecker, and Maurice Bruynooghe. CP-logic: + A language of causal probabilistic events and its relation to logic + programming. Theory Pract. Log. Program., 9(3):245–308, 2009.

    - [11]      J. Vennekens and S. Verbaeten. Logic programs with annotated - disjunctions. Technical Report CW386, K. U. Leuven, 2003. - http://www.cs.kuleuven.ac.be/~joost/techrep.ps . + disjunctions. Technical Report CW386, K. U. Leuven, 2003.

    - [12]   J. Vennekens, S. Verbaeten, and M. Bruynooghe. Logic programs with - annotated disjunctions. In The 20th International Conference on Logic + [22]   J. Vennekens, S. Verbaeten, and M. Bruynooghe. Logic programs + with annotated disjunctions. In International Conference on Logic Programming (ICLP 2004), 2004. http://www.cs.kuleuven.ac.be/~joost/ - . -

    -

    - [13]   Joost Vennekens, Marc Denecker, and Maurice Bruynooge. Extending - the role of causality in - probabilistic modeling. http://www.cs.kuleuven.ac.be/~joost/cplogic.pdf - , 2006. +class="cmti-10">Programming, volume 3131 of LNCS, pages 195–209. Springer, 2004.

    diff --git a/packages/cplint/doc/manual.pdf b/packages/cplint/doc/manual.pdf index a37549757..e2491631e 100644 Binary files a/packages/cplint/doc/manual.pdf and b/packages/cplint/doc/manual.pdf differ diff --git a/packages/cplint/doc/manual.tex b/packages/cplint/doc/manual.tex index ef9b79fd7..8effd8554 100644 --- a/packages/cplint/doc/manual.tex +++ b/packages/cplint/doc/manual.tex @@ -1,5 +1,5 @@ \ifnum\pdfoutput>0 % pdflatex compilation -\documentclass[a4paper,12pt]{article} +\documentclass[a4paper,10pt]{article} \usepackage[pdftex]{graphicx} \DeclareGraphicsExtensions{.pdf,.png,.jpg} \RequirePackage[hyperindex]{hyperref} @@ -7,16 +7,16 @@ \documentclass{article} \usepackage{graphicx} \DeclareGraphicsExtensions{.png, .gif, .jpg} -\newcommand{\url}[1]{\Link[#1]{}{} #1 \EndLink} \newcommand{\href}[2]{\Link[#1]{}{} #2 \EndLink} \newcommand{\hypertarget}[2]{\Link[]{}{#1} #2 \EndLink} \newcommand{\hyperlink}[2]{\Link[]{#1}{} #2 \EndLink} +\newcommand{\url}[1]{\Link[#1]{}{} #1 \EndLink} \fi \begin{document} -\title{\texttt{cplint} Version 2.0 Manual} +\title{\texttt{cplint} Manual} \author{Fabrizio Riguzzi\\ @@ -28,69 +28,41 @@ fabrizio.riguzzi@unife.it} \section{Introduction} -\texttt{cplint} is a suite of programs for reasoning with LPADs \cite{VenVer03-TR,VenVer04-ICLP04-IC} and CP-logic programs \cite{VenDenBru-JELIA06,CP-logic-unp}. - -It consists of three Prolog modules for answering queries using goal-oriented procedures plus - three -Prolog modules for answering queries using the definition of the semantics of LPADs and CP-logic. - -The modules for answering queries using using goal-oriented procedures are \texttt{lpadsld.pl}, \texttt{lpad.pl} and -\texttt{cpl.pl}: -\begin{itemize} -\item \texttt{lpadsld.pl}: computes the probability of a query using the top-down procedure described in -in \cite{Rig-AIIA07-IC} and \cite{Rig-RCRA07-IC}. It is based on SLDNF resolution and is an adaptation of the interpreter for ProbLog \cite{DBLP:conf/ijcai/RaedtKT07}. - -It was proved correct \cite{Rig-RCRA07-IC} with respect to the semantics of LPADs for range restricted acyclic programs \cite{DBLP:journals/ngc/AptB91} without function symbols. - -It is also able to deal with extensions of LPADs and CP-logic: the clause bodies can contain \texttt{setof} and \texttt{bagof}, the probabilities in the head may be depend on variables in the body and it is possible to specify a uniform distribution in the head with reference to a \texttt{setof} or \texttt{bagof} operator. These extended features have been introduced in order to represent CLP(BN) \cite{SanPagQaz03-UAI-IC} programs and PRM models \cite{Getoor+al:JMLR02}: -\texttt{setof} and \texttt{bagof} allow to express dependency of an attribute from an aggregate function of another attribute, as in CLP(BN) and PRM, while the possibility of specifying a uniform distribution allows the use of the reference uncertainty feature of PRM. -\item \texttt{lpad.pl}: computes the probability of a query using a top-down procedure based on SLG resolution \cite{DBLP:journals/jacm/ChenW96}. As a consequence, it works for any sound LPADs, i.e., any LPAD such that each of its instances has a two valued well founded model. -\item \texttt{cpl.pl}: computes the probability of a query using a top-down procedure based on SLG resolution and moreover checks that the CP-logic program is valid, i.e., that it has at least an execution model. -\end{itemize} - -The modules for answering queries using the definition of the semantics of LPADs and CP-logic are \texttt{semlpadsld.pl}, \texttt{semlpad.pl} and -\texttt{semcpl.pl}: -\begin{itemize} -\item \texttt{semlpadsld.pl}: given an LPAD $P$, it generates all the instances of $P$. The probability of a query $Q$ is computed by identifying all the instances where $Q$ is derivable by SLDNF resolution. -\item \texttt{semlpad.pl}: given an LPAD $P$, it generates all the instances of $P$. The probability of a query $Q$ is computed by identifying all the instances where $Q$ is derivable by SLG resolution. -\item \texttt{semlcpl.pl}: given an LPAD $P$, it builds an execution model of $P$, i.e., a probabilistic process that satisfy the principles of universal causation, sufficient causation, independent causation, no deus ex machina events and temporal precedence. It uses the definition of the semantics given in \cite{CP-logic-unp}. -\end{itemize} -%For program with function symbols, the semantics of LPADs and CP-logic are not defined. However, the interpreter accepts programs with function symbols and, if it does not go into a loop, it returns an answer. What is the meaning of this answer is subject of current study. +\texttt{cplint} is a suite of programs for reasoning with ICL \cite{DBLP:journals/ai/Poole97}, LPADs \cite{VenVer03-TR,VenVer04-ICLP04-IC} and CP-logic programs \cite{VenDenBru-JELIA06,DBLP:journals/tplp/VennekensDB09}. It contains programs both for inference and learning. \section{Installation} -\texttt{cplint} is distributed in source code in the git version of Yap. It includes Prolog and C files. Download it by following the instruction in \url{http://www.ncc.up.pt/~vsc/Yap/downloads.html}. +\texttt{cplint} is distributed in source code in the CVS version of Yap. It includes Prolog and C files. Download it by following the instruction in \url{http://www.ncc.up.pt/~vsc/Yap/downloads.html}. -\texttt{cplint} requires cudd and glib-2.0. -You can download cudd from \url{http://vlsi.colorado.edu/~fabio/CUDD/}. +\texttt{cplint} requires \href{http://vlsi.colorado.edu/~fabio/CUDD/}{CUDD} and glib-2.0. +You can download CUDD from \url{ftp://vlsi.colorado.edu/pub/cudd-2.4.2.tar.gz}. You can download glib-2.0 (version $\geq 2.0$) from \url{http://www.gtk.org/}. This is a standard GNU package so it is easy to install it using the package management software of your Linux or Cygwin distribution. -Compile cudd: +Compile CUDD: \begin{enumerate} -\item downlad \texttt{cudd-2.4.2.tar.gz} -\item decompress it +\item decompress cudd-2.4.2.tar.gz \item \texttt{cd cudd-2.4.2} -\item check makefile options -\item \texttt{make} +\item see the \texttt{README} file for instructions on compilation \end{enumerate} Install Yap together with \texttt{cplint}: -when compiling Yap following the instuction of the \texttt{INSTALL} file in the root of the Yap folder, use +when compiling Yap following the instruction of the \texttt{INSTALL} file in the root of the Yap folder, use \begin{verbatim} configure --enable-cplint=DIR \end{verbatim} -Under Windows, you have to use Cygwin (glu does not compile under MinGW), so\\ +where \verb|DIR| is the directory where CUDD is, i.e., the directory ending with \texttt{cudd-2.4.2}. +Under Windows, you have to use Cygwin (CUDD does not compile under MinGW), so\\ \begin{verbatim} configure --enable-cplint=DIR --enable-cygwin \end{verbatim} -where \texttt{DIR} is the path to the directory \texttt{cudd-2.4.2} (including \texttt{cudd-2.4.2}). After having performed \texttt{make install} you can do \texttt{make installcheck} that will execute a suite of tests of the various programs. If no error is reported you have a working installation of \texttt{cplint}. \section{Syntax} +LPAD and CP-logic programs consist of a set of annotated disjunctive clauses. Disjunction in the head is represented with a semicolon and atoms in the head are separated from probabilities by a colon. For the rest, the usual syntax of Prolog is used. For example, the CP-logic clause $$h_1:p_1\vee \ldots \vee h_n:p_n\leftarrow b_1,\dots,b_m ,\neg c_1,\ldots,\neg c_l$$ @@ -127,10 +99,68 @@ toss(coin). \end{verbatim} The first clause states that if we toss a coin that is not biased it has equal probability of landing heads and tails. The second states that if the coin is biased it has a slightly higher probability of landing heads. The third states that the coin is fair with probability 0.9 and biased with probability 0.1 and the last clause states that we toss a coin with certainty. +Moreover, the bodies of rules can contain the built-in predicates: +\begin{verbatim} +is/2, >/2, =/2 ,=/2 -=/2 -=.kb}: contains the example interpretations +\item \texttt{.bg}: contains the background knowledge, i.e., knowledge valid for all interpretations +\item \texttt{.l}: contains language bias information +\item \texttt{.cpl}: contains the LPAD for you which you want to learn the parameters or the initial LPAD for SLIPCASE +\end{itemize} +where \texttt{} is your dataset name. Examples of these files can be found in the dataset pages. + +In \texttt{.kb} the example interpretations have to be given as a list of Prolog facts initiated by +\texttt{begin(model()).} and terminated by \texttt{end(model()).} as in +\begin{verbatim} +begin(model(b1)). +sameperson(1,2). +movie(f1,1). +movie(f1,2). +workedunder(1,w1). +workedunder(2,w1). +gender(1,female). +gender(2,female). +actor(1). +actor(2). +end(model(b1)). +\end{verbatim} +The interpretations may contain a fact of the form +\begin{verbatim} +prob(0.3). +\end{verbatim} +assigning a probability (0.3 in this case) to the interpretations. If this is omitted, the probability of each interpretation is considered equal to $1/n$ where $n$ is the total number of interpretations. \verb|prob/1| can be used to set different multiplicity for the different interpretations. + +In order for RIB to work, the input interpretations must share the Herbrand universe. If this is not the case, you have to translate the interpretations in this was, see for example the \texttt{sp1} files in RIB's folder, that are the results of the conversion of the first fold of the IMDB dataset. + +\texttt{.bg} can contain Prolog clauses that can be used to derive additional conclusions from the atoms in +the interpretations. + +\texttt{.l} contains the declarations of the input and output predicates, of the unseen predicates and the commands for setting the algorithms' parameters. +Output predicates are declared as +\begin{verbatim} +output(/). +\end{verbatim} +and define the predicates whose atoms in the input interpretations are used as the goals for the prediction of which you want to optimize the parameters. Derivations for these goals are built by the systems. + +Input predicates are those for the predictions of which you do not want to optimize the parameters. You can declare closed world input predicates with +\begin{verbatim} +input_cw(/). +\end{verbatim} +For these predicates, the only true atoms are those in the interpretations, the clauses in the input program are not used to derive atoms not present in the interpretations. + +Open world input predicates are declared with +\begin{verbatim} +input(/). +\end{verbatim} +In this case, if a subgoal for such a predicate is encountered when deriving the atoms for the output predicates, +both the facts in the interpretations and the clauses of the input program are used. + +For RIB, if there are unseen predicates, i.e., predicates that are present in the input program but not in the interpretations, you have to declare them with +\begin{verbatim} +unseen(/). +\end{verbatim} + +For SLIPCASE, you have to specify the language bias by means of mode declarations in the style of +\href{http://www.doc.ic.ac.uk/~shm/progol.html}{Progol}. +\begin{verbatim} +modeh(,(,...). +\end{verbatim} +specifies the atoms that can appear in the head of clauses, while +\begin{verbatim} +modeb(,(,...). +\end{verbatim} +specifies the atoms that can appear in the body of clauses. +\texttt{} can be an integer or \texttt{*} (currently unused). + +The arguments are of the form +\begin{verbatim} ++ +\end{verbatim} +for specifying an input variable of type \texttt{}, or +\begin{verbatim} +- +\end{verbatim} +for specifying an output variable of type \texttt{}. +\subsection{Parameters} +In order to set the algorithms' parameters, you have to insert in \texttt{.l} commands of the form +\begin{verbatim} +:- set(,). +\end{verbatim} +The available parameters are: +\begin{itemize} +\item \verb|depth| (values: integer or \verb|inf|, default value: 3): depth of derivations if \verb|depth_bound| is set to \verb|true| +\item \verb|single_var| (values: \verb|{true,false}|, default value: \verb|false|, valid for CEM only): if set to \verb|true|, there is a random variable for each clauses, instead of a separate random variable for each grounding of a clause +\item \verb|sample_size| (values: integer, default value: 1000): total number of examples in case in which the models in the \verb|.kb| file contain a \verb|prob(P).| fact. In that case, one model corresponds to \verb|sample_size*P| examples +\item \verb|epsilon_em| (values: real, default value: 0.1, valid for CEM only): if the difference in the log likelihood in two successive EM iteration is smaller +than \verb|epsilon_em|, then EM stops +\item \verb|epsilon_em_fraction| (values: real, default value: 0.01, valid for CEM only): if the difference in the log likelihood in two successive EM iteration is smaller +than \verb|epsilon_em_fraction|*(-current log likelihood), then EM stops +\item \verb|random_restarts_number| (values: integer, default value: 1, valid for CEM only): number of random restarts +\item \verb|setrand| (values: rand(integer,integer,integer)): seed for the random functions, see Yap manual for allowed values +\item \verb|minimal_step| (values: [0,1], default value: 0.005, valid for RIB only): minimal increment of $\gamma$ +\item \verb|maximal_step| (values: [0,1], default value: 0.1, valid for RIB only): maximal increment of $\gamma$ +\item \verb|logsize_fraction| (values: [0,1], default value 0.9, valid for RIB only): RIB stops when $\mathbf{I}(CH,T;Y)$ is above \verb|logsize_fraction| times its maximum value ($\log |CH,T|$, see \cite{DBLP:journals/jmlr/ElidanF05}) +\item \verb|delta| (values: negative integer, default value -10, valid for RIB only): value assigned to $\log 0$ +\item \verb|epsilon_fraction| (values: integer, default value 100, valid for RIB only): in the computation of the step, the value of $\epsilon$ of \cite{DBLP:journals/jmlr/ElidanF05} is obtained as $\log |CH,T|\times$\verb|epsilon_fraction| +\item \verb|max_rules| (values: integer, default value: 6000, valid for RIB only): maximum number of ground rules. Used to set the size of arrays for storing internal statistics. Can be increased as much as memory allows. +\end{itemize} +\subsection{Commands} +To execute CEM, load \texttt{em.pl} and call: +\begin{verbatim} +?:- em(stem). +\end{verbatim} +To execute RIB, load \texttt{rib.pl} and call: +\begin{verbatim} +?:- ib_par(stem). +\end{verbatim} + + \section{License} \label{license} \texttt{cplint}, as Yap, follows the Artistic License 2.0 that you can find in Yap CVS root dir. The copyright is by Fabrizio Riguzzi. - - \vspace{3mm} -The program uses the library \href{http://vlsi.colorado.edu/~fabio/}{CUDD} for manipulating BDDs that is included in glu. +The modules in the approx subdirectory use SimplecuddLPADs, a modification of the \href{www.cs.kuleuven.be/~theo/tools/simplecudd.html}{Simplecudd} library whose copyright is by Katholieke Universiteit Leuven and that follows the Artistic License 2.0. +\vspace{3mm} + +Some modules use the library \href{http://vlsi.colorado.edu/~fabio/}{CUDD} for manipulating BDDs that is included in glu. For the use of CUDD, the following license must be accepted: \vspace{3mm} diff --git a/packages/cplint/lpadsld.pl b/packages/cplint/lpadsld.pl index a4b511ac8..f808876a8 100644 --- a/packages/cplint/lpadsld.pl +++ b/packages/cplint/lpadsld.pl @@ -4,8 +4,11 @@ Goal oriented interpreter for LPADs based on SLDNF Copyright (c) 2007, Fabrizio Riguzzi */ - -:-dynamic rule/4,def_rule/2,setting/2. +%:- set_prolog_flag(debug,on). +%:- set_prolog_flag(discontiguous_warnings,on). +%:- set_prolog_flag(single_var_warnings,on). +%:- source. +:-dynamic rule/5,rule_by_num/8,rule_uniform/8,def_rule/2,setting/2. :-use_module(library(lists)). :-use_module(library(ugraphs)). @@ -16,14 +19,17 @@ set(Parameter,Value) */ setting(epsilon_parsing,0.00001). setting(save_dot,false). -setting(ground_body,false). +setting(ground_body,true). /* available values: true, false if true, both the head and the body of each clause will be grounded, otherwise only the head is grounded. In the case in which the body contains variables not appearing in the head, the body represents an existential event */ setting(min_error,0.01). +setting(initial_depth_bound,4). setting(depth_bound,4). setting(prob_threshold,0.00001). +setting(prob_bound,0.01). + /* end of list of parameters */ /* s(GoalsLIst,Prob) compute the probability of a list of goals @@ -101,39 +107,43 @@ solve(GoalsList,Prob,CPUTime1,CPUTime2,WallTime1,WallTime2):- format(user_error,"~nMemory after inference~n",[]), print_mem.*/ +/* iterative deepening, depth bounded +for negative goals, if their derivation is cut, then they are +added to the head of the list of goals to be resolved at the next depth bound*/ si(GoalsList,ProbL,ProbU,CPUTime):- statistics(cputime,[_,_]), - setting(depth_bound,D), + setting(initial_depth_bound,D), solve_i([(GoalsList,[])],[],D,ProbL,ProbU), statistics(cputime,[_,CT]), CPUTime is CT/1000. - +/* solve_i(L0,Succ,D,ProbL0,ProbU0): L0 is a list of couples (G,Der) where +G is a list of goals to be resolved and Der is an explanation, D is the +current depth, ProbL0 is the lower bound of the prob and ProbU0 is the upper +bound +*/ solve_i(L0,Succ,D,ProbL0,ProbU0):- - (findall((G1,Deriv),(member((G0,C0),L0),solvei(G0,D,C0,Deriv,G1)),L)-> + findall((G1,Deriv),(member((G0,C0),L0),solvei(G0,D,C0,Deriv,G1)),L), % print_mem, - separate_ulbi(L,[],LL0,[],LU,[],Incomplete), - append(Succ,LL0,LL), - compute_prob_deriv(LL,ProbL), - append(Succ,LU,LU1), - compute_prob_deriv(LU1,ProbU), - Err is ProbU-ProbL, - setting(min_error,ME), - (Err - ProbU0=ProbU, - ProbL0=ProbL - ; - setting(depth_bound,DB), - D1 is D+DB, - solve_i(Incomplete,LL,D1,ProbL0,ProbU0) - ) + separate_ulbi(L,[],LL0,[],LU,[],Incomplete), + append(Succ,LL0,LL), + compute_prob_deriv(LL,ProbL), + append(Succ,LU,LU1), + compute_prob_deriv(LU1,ProbU), + Err is ProbU-ProbL, + setting(min_error,ME), + (Err + ProbU0=ProbU, + ProbL0=ProbL ; - % print_mem, - ProbL0=0.0, - ProbU0=0.0 + setting(depth_bound,DB), + D1 is D+DB, + solve_i(Incomplete,LL,D1,ProbL0,ProbU0) ). +/* iterative deepening, problog style: each time +the derivation is restarted from the original goal */ sir(GoalsList,ProbL,ProbU,CPUTime):- statistics(cputime,[_,_]), setting(depth_bound,D), @@ -142,7 +152,11 @@ sir(GoalsList,ProbL,ProbU,CPUTime):- CPUTime is CT/1000. - +/* solveir(GoalsList,D,ProbL0,ProbU0) GoalsLIst is the list +of goals to be derived, D is the depth bound, ProbL0,ProbU0 are the lower +and upper bound. If for a certain depth bound the error is not smaller +than the threshold, the depth bound is increased and the derivation is +restarted from the beginning */ solveir(GoalsList,D,ProbL0,ProbU0):- (setof(Deriv,find_derivr(GoalsList,D,Deriv),LDup)-> rem_dup_lists(LDup,[],L), @@ -166,7 +180,8 @@ solveir(GoalsList,D,ProbL0,ProbU0):- ProbU0=0.0 ). - +/* approximate algorithm cilog2 style: the explanations with a prob below the +threshold are cut */ sic(GoalsList,ProbL,ProbU,CPUTime):- statistics(cputime,[_,_]), setting(depth_bound,D), @@ -174,8 +189,6 @@ sic(GoalsList,ProbL,ProbU,CPUTime):- statistics(cputime,[_,CT]), CPUTime is CT/1000. - - solveic(GoalsList,D,ProbL0,ProbU0):- (setof((Deriv,P,Pruned),solvec(GoalsList,D,[],Deriv,1.0,P,Pruned),L)-> % print_mem, @@ -194,16 +207,24 @@ solveic(GoalsList,D,ProbL0,ProbU0):- ). compute_prob_deriv(LL,ProbL):- - build_formula(LL,FormulaL,[],VarL,0,ConjL), - length(LL,NDL), - length(VarL,NVL), + build_formula(LL,FormulaL,[],VarL,0,_ConjL), + %length(LL,NDL), + %length(VarL,NVL), %format(user_error,"Disjunctions :~d~nConjunctions: ~d~nVariables ~d~n",[NDL,ConjL,NVL]), var2numbers(VarL,0,NewVarL), - (setting(save_dot,true)-> - % format("Variables: ~p~n",[VarL]), - compute_prob(NewVarL,FormulaL,ProbL,1) + (FormulaL=[]-> + ProbL=0.0 ; - compute_prob(NewVarL,FormulaL,ProbL,0) + (FormulaL=[[]|_]-> + ProbL=1.0 + ; + (setting(save_dot,true)-> + % format("Variables: ~p~n",[VarL]), + compute_prob(NewVarL,FormulaL,ProbL,1) + ; + compute_prob(NewVarL,FormulaL,ProbL,0) + ) + ) ). print_mem:- @@ -313,7 +334,7 @@ solve_condi(LGoals,LEvidence,SuccGE,SuccE,D,ProbL0,ProbU0):- ProbU0=undefined ). - +/* iterative deepening, problog style */ scir(Goals,Evidence,ProbL,ProbU,CPUTime):- statistics(cputime,[_,_]), setting(depth_bound,D), @@ -363,6 +384,7 @@ solve_condir(Goals,Evidence,D,ProbL0,ProbU0):- ProbU0=undefined ). +/* approximate algorithm cilog2 style */ scic(Goals,Evidence,ProbL,ProbU,CPUTime):- statistics(cputime,[_,_]), setting(depth_bound,D), @@ -468,11 +490,19 @@ solve_cond_goals(Goals,LE,0,Time1,0):- Time1 is T1/1000. call_compute_prob(NewVarGE,FormulaGE,ProbGE):- - (setting(save_dot,true)-> - format("Variables: ~p~n",[NewVarGE]), - compute_prob(NewVarGE,FormulaGE,ProbGE,1) + (FormulaGE=[]-> + ProbGE=0.0 ; - compute_prob(NewVarGE,FormulaGE,ProbGE,0) + (FormulaGE=[[]|_]-> + ProbGE=1.0 + ; + (setting(save_dot,true)-> + format("Variables: ~p~n",[NewVarGE]), + compute_prob(NewVarGE,FormulaGE,ProbGE,1) + ; + compute_prob(NewVarGE,FormulaGE,ProbGE,0) + ) + ) ). find_deriv_GE(LD,GoalsList,Deriv):- @@ -572,7 +602,7 @@ solvei(G,0,C,C,G):-!. solvei([\+ H |T],DB,CIn,COut,G):-!, list2and(HL,H), (findall((GH,D),solvei(HL,DB,CIn,D,GH),L)-> - separate_ulbi(L,[],LB,[],UB,[],I), + separate_ulbi(L,[],LB,[],_UB,[],I), (I\=[]-> C1=CIn, G=[\+ H|G1] @@ -680,7 +710,6 @@ solvec([H|T],DB,CIn,COut,P0,P1,Pruned):- solve_presc(R,S,N,B,T,DB1,CIn,COut,P,P0,P1,Pruned). - solve_pres(R,S,N,B,T,CIn,COut):- member_eq((N,R,S),CIn),!, append(B,T,NG), @@ -782,16 +811,16 @@ returns the index R of a disjunctive rule resolving with G together with the index N of the resolving head, the substitution S and the Body of the rule */ find_rule(H,(R,S,N),Body,C):- - rule(R,S,_,Head,Body), + rule(H,_P,N,R,S,_,Head,Body), member_head(H,Head,0,N), not_already_present_with_a_different_head(N,R,S,C). find_rule(H,(R,S,Number),Body,C):- - rule(R,S,_,uniform(H:1/_Num,_P,Number),Body), + rule_uniform(H,R,S,_,1/_Num,_L,Number,Body), not_already_present_with_a_different_head(Number,R,S,C). find_rulec(H,(R,S,N),Body,C,P):- - rule(R,S,_,Head,Body), + rule(H,_P,N,R,S,_,Head,Body), member_headc(H,Head,0,N,P), not_already_present_with_a_different_head(N,R,S,C). @@ -864,7 +893,7 @@ choose_clausesc(CIn,[D|T],COut,P0,P1):- new_head(N,R,S,N1), \+ already_present(N1,R,S,CIn), impose_dif_cons(R,S,CIn), - rule(R,S,_Numbers,Head,_Body), + rule_by_num(R,S,_Numbers,Head,_Body), nth0(N1, Head, (_H:P), _Rest), P2 is P0*P, choose_clausesc([(N1,R,S)|CIn],T,COut,P2,P1). @@ -931,7 +960,7 @@ choose_a_headc(N,R,S,[(NH,R,SH)|T],[(NH,R,S),(NH,R,SH)|T],P0,P1):- \+ \+ S=SH, S\==SH, dif(N,NH), dif(S,SH), - rule(R,S,_Numbers,Head,_Body), + rule_by_num(R,S,_Numbers,Head,_Body), nth0(NH, Head, (_H:P), _Rest), P1 is P0*P. @@ -959,13 +988,13 @@ choose_a_head(N,R,S,[H|T],[H|T1]):- /* select a head different from N for rule R with substitution S, return it in N1 */ new_head(N,R,S,N1):- - rule(R,S,Numbers,Head,_Body), + rule_by_num(R,S,Numbers,Head,_Body), Head\=uniform(_,_,_),!, nth0(N, Numbers, _Elem, Rest), member(N1,Rest). new_head(N,R,S,N1):- - rule(R,S,Numbers,uniform(_A:1/Tot,_L,_Number),_Body), + rule_by_num(R,S,Numbers,uniform(_A:1/Tot,_L,_Number),_Body), listN(0,Tot,Numbers), nth0(N, Numbers, _Elem, Rest), member(N1,Rest). @@ -1005,11 +1034,7 @@ member_subset(E,[_H|T]):- member_subset(E,T). separate_ulbi([],L,L,U,U,I,I):-!. -/* -separate_ulb([H|T],L0,L1,U0,[H|U1]):- - member(pruned,H),!, - separate_ulb(T,L0,L1,U0,U1). -*/ + separate_ulbi([([],H)|T],L0,[H|L1],U0,[H|U1],I0,I1):- !, separate_ulbi(T,L0,L1,U0,U1,I0,I1). @@ -1020,11 +1045,7 @@ separate_ulbi([(G,H)|T],L0,L1,U0,[H1|U1],I0,[(G,H)|I1]):- separate_ulb([],L,L,U,U):-!. -/* -separate_ulb([H|T],L0,L1,U0,[H|U1]):- - member(pruned,H),!, - separate_ulb(T,L0,L1,U0,U1). -*/ + separate_ulb([H|T],L0,[H|L1],U0,[H|U1]):- ground(H),!, separate_ulb(T,L0,L1,U0,U1). @@ -1036,21 +1057,20 @@ separate_ulb([H|T],L0,L1,U0,[H1|U1]):- separate_ulbc([],L,L,P,P):-!. -separate_ulbc([(H,P,true)|T],L0,L1,P0,P1):-!, +separate_ulbc([(_H,P,true)|T],L0,L1,P0,P1):-!, P2 is P0+P, separate_ulbc(T,L0,L1,P2,P1). separate_ulbc([(H,_P,false)|T],L0,[H|L1],P0,P1):- separate_ulbc(T,L0,L1,P0,P1). - get_ground([],[]):-!. get_ground([H|T],[H|T1]):- ground(H),!, get_ground(T,T1). -get_ground([H|T],T1):- +get_ground([_H|T],T1):- get_ground(T,T1). @@ -1116,7 +1136,7 @@ var2numbers([(R,S)|T],N,[[N,ValNumber,Probs]|TNV]):- var2numbers(T,N1,TNV). find_probs(R,S,Probs):- - rule(R,S,_N,Head,_Body), + rule_by_num(R,S,_N,Head,_Body), get_probs(Head,Probs). get_probs(uniform(_A:1/Num,_P,_Number),ListP):- @@ -1150,11 +1170,13 @@ parse(File):- open(FilePl,read,S), read_clauses(S,C), close(S), - retractall(rule(_,_,_,_,_)), - retractall(def_rule(_,_)), + retractall(rule_by_num(_,_,_,_,_)), + retractall(rule(_,_,_,_,_,_,_,_)), + retractall(def_rule(_,_)), + retractall(rule_uniform(_,_,_,_,_,_,_,_)), process_clauses(C,1). -process_clauses([(end_of_file,[])],_N). +process_clauses([(end_of_file,[])],_N):-!. process_clauses([((H:-B),V)|T],N):- H=uniform(A,P,L),!, @@ -1163,7 +1185,8 @@ process_clauses([((H:-B),V)|T],N):- remove_vars([P],V1,V2), append(BL,[length(L,Tot),nth0(Number,L,P)],BL1), append(V2,['Tot'=Tot],V3), - assertz(rule(N,V3,_NH,uniform(A:1/Tot,L,Number),BL1)), + assertz(rule_by_num(N,V3,_NH,uniform(A:1/Tot,L,Number),BL1)), + assertz(rule_uniform(A,N,V3,_NH,1/Tot,L,Number,BL1)), N1 is N+1, process_clauses(T,N1). @@ -1175,7 +1198,8 @@ process_clauses([((H:-B),V)|T],N):- process_body(BL,V,V1), length(HL,LH), listN(0,LH,NH), - assertz(rule(N,V1,NH,HL,BL)), + assert_rules(HL,0,HL,BL,NH,N,V1), + assertz(rule_by_num(N,V1,NH,HL,BL)), N1 is N+1, process_clauses(T,N1). @@ -1187,7 +1211,8 @@ process_clauses([((H:-B),V)|T],N):- process_body(BL,V,V1), length(HL,LH), listN(0,LH,NH), - assertz(rule(N,V1,NH,HL,BL)), + assert_rules(HL,0,HL,BL,NH,N,V1), + assertz(rule_by_num(N,V1,NH,HL,BL)), N1 is N+1, process_clauses(T,N1). @@ -1202,7 +1227,8 @@ process_clauses([(H,V)|T],N):- process_head(HL1,HL), length(HL,LH), listN(0,LH,NH), - assertz(rule(N,V,NH,HL,[])), + assert_rules(HL,0,HL,[],NH,N,V), + assertz(rule_by_num(N,V,NH,HL,[])), N1 is N+1, process_clauses(T,N1). @@ -1212,7 +1238,8 @@ process_clauses([(H,V)|T],N):- process_head(HL1,HL), length(HL,LH), listN(0,LH,NH), - assertz(rule(N,V,NH,HL,[])), + assert_rules(HL,0,HL,[],NH,N,V), + assertz(rule_by_num(N,V,NH,HL,[])), N1 is N+1, process_clauses(T,N1). @@ -1220,6 +1247,16 @@ process_clauses([(H,_V)|T],N):- assert(def_rule(H,[])), process_clauses(T,N). +assert_rules([],_Pos,_HL,_BL,_Nh,_N,_V1):-!. + +assert_rules(['':_P],_Pos,_HL,_BL,_Nh,_N,_V1):-!. + +assert_rules([H:P|T],Pos,HL,BL,NH,N,V1):- + assertz(rule(H,P,Pos,N,V1,NH,HL,BL)), + Pos1 is Pos+1, + assert_rules(T,Pos1,HL,BL,NH,N,V1). + + /* if the annotation in the head are not ground, the null atom is not added and the eventual formulas are not evaluated */ diff --git a/packages/cplint/testlpadsldit.pl b/packages/cplint/testlpadsldit.pl index 5a586b0b7..7257ae516 100644 --- a/packages/cplint/testlpadsldit.pl +++ b/packages/cplint/testlpadsldit.pl @@ -120,7 +120,7 @@ test((s([on(4,1)],P),close_to(P,0.0658436213991769)),threesideddice,_). test((sc([on(2,1)],[on(0,1)],P),close_to(P,0.222222222222222)),threesideddice,_). test((sc([on(2,1)],[on(1,1)],P),close_to(P,0.333333333333333)),threesideddice,_). -test((sc([on(4,1)],[on(1,1)],P),close_to(P, 0.148148148148148)),threesideddice,_). +/*test((sc([on(4,1)],[on(1,1)],P),close_to(P, 0.148148148148148)),threesideddice,_). */ test((sc([on(5,1)],[on(2,1)],P),close_to(P, 0.148148148148148)),threesideddice,_). diff --git a/pl/utils.yap b/pl/utils.yap index 90bcc3fbf..a6688898b 100644 --- a/pl/utils.yap +++ b/pl/utils.yap @@ -695,7 +695,7 @@ nth_instance(Key,Index,T,Ref) :- nb_current(GlobalVariable, Val) :- '$nb_current'(GlobalVariable), - nb_getval(GlobalVariable, Val). + '$nb_getval'(GlobalVariable, Val, _). '$getval_exception'(GlobalVariable, Val, Caller) :- user:exception(undefined_global_variable, GlobalVariable, Action),