diff --git a/cplint/lpadclpbn.pl b/cplint/lpadclpbn.pl index e55c8ab51..e8fa8e75c 100644 --- a/cplint/lpadclpbn.pl +++ b/cplint/lpadclpbn.pl @@ -8,7 +8,7 @@ */ :- module(lpadclpbn, [p/1, - s/2,sc/3]). + s/2,sc/3,s/6,sc/7]). :-dynamic rule/4,def_rule/2,setting/2. @@ -35,10 +35,22 @@ not appearing in the head, the body represents an existential event */ /* end of list of parameters */ -/* s(GoalsLIst,Prob) compute the probability of a list of goals -GoalsLis can have variables, s returns in backtracking all the solutions with their -corresponding probability */ +/* s(GoalsList,Prob) compute the probability of a list of goals +GoalsLis can have variables, s returns in backtracking all the solutions with +their corresponding probability */ +s(GL,P):- + setof(Deriv,find_deriv(GL,Deriv),LDup), + append_all(LDup,[],L), + remove_head(L,L1), + remove_duplicates(L1,L2), + build_ground_lpad(L2,0,CL), + convert_to_clpbn(CL,GL,LV,P). +/* sc(GoalsList,EvidenceList,Prob) compute the probability of a list of goals +GoalsList given EvidenceList. Both lists can have variables, sc returns in +backtracking all the solutions with their corresponding probability +Time1 is the time for performing resolution +Time2 is the time for performing bayesian inference */ sc(GL,GLC,P):- setof(Deriv,find_deriv(GL,Deriv),LDup), setof(Deriv,find_deriv(GLC,Deriv),LDupC), @@ -52,13 +64,56 @@ sc(GL,GLC,P):- convert_to_clpbn(CL,GL,LV,P,GLC). -s(GL,P):- + +/* s(GoalsList,Prob,Time1,Time2) compute the probability of a list of goals +GoalsLis can have variables, s returns in backtracking all the solutions with +their corresponding probability +Time1 is the time for performing resolution +Time2 is the time for performing bayesian inference */ +s(GL,P,Time1,Time2):- + statistics(cputime,[_,_]), + statistics(walltime,[_,_]), setof(Deriv,find_deriv(GL,Deriv),LDup), append_all(LDup,[],L), remove_head(L,L1), remove_duplicates(L1,L2), + statistics(cputime,[_,CT1]), + CPUTime1 is CT1/1000, + statistics(walltime,[_,WT1]), + WallTime1 is WT1/1000, build_ground_lpad(L2,0,CL), - convert_to_clpbn(CL,GL,LV,P). + convert_to_clpbn(CL,GL,LV,P), + statistics(cputime,[_,CT2]), + CPUTime2 is CT2/1000, + statistics(walltime,[_,WT2]), + WallTime2 is WT2/1000. + +/* sc(GoalsList,EvidenceList,Prob) compute the probability of a list of goals +GoalsList given EvidenceList. Both lists can have variables, sc returns in +backtracking all the solutions with their corresponding probability */ + +sc(GL,GLC,P,CPUTime1,CPUTime2,WallTime1,WallTime2):- + statistics(cputime,[_,_]), + statistics(walltime,[_,_]), + setof(Deriv,find_deriv(GL,Deriv),LDup), + setof(Deriv,find_deriv(GLC,Deriv),LDupC), + append_all(LDup,[],L), + remove_head(L,L1), + append_all(LDupC,[],LC), + remove_head(LC,LC1), + append(L1,LC1,LD), + remove_duplicates(LD,LD1), + statistics(cputime,[_,CT1]), + CPUTime1 is CT1/1000, + statistics(walltime,[_,WT1]), + WallTime1 is WT1/1000, + build_ground_lpad(LD1,0,CL), + convert_to_clpbn(CL,GL,LV,P,GLC), + statistics(cputime,[_,CT2]), + CPUTime2 is CT2/1000, + statistics(walltime,[_,WT2]), + WallTime2 is WT2/1000. + remove_head([],[]). diff --git a/cplint/lpadsld.pl b/cplint/lpadsld.pl index cc89f6ca1..34ea40f75 100644 --- a/cplint/lpadsld.pl +++ b/cplint/lpadsld.pl @@ -46,6 +46,43 @@ solve(GoalsList,Prob):- solve(GoalsList,0):- \+ find_deriv(GoalsList,_Deriv). +/* s(GoalsList,Prob,CPUTime1,CPUTime2,WallTime1,WallTime2) compute the probability of a list of goals +GoalsLis can have variables, s returns in backtracking all the solutions with +their corresponding probability +CPUTime1 is the cpu time for performing resolution +CPUTime2 is the cpu time for elaborating the BDD +WallTime1 is the wall time for performing resolution +WallTime2 is the wall time for elaborating the BDD */ +*/ + +s(GoalsList,Prob,CPUTime1,CPUTime2,WallTime1,WallTime2):- + solve(GoalsList,Prob,CPUTime1,CPUTime2,WallTime1,WallTime2). + + +solve(GoalsList,Prob,CPUTime1,CPUTime2,WallTime1,WallTime2):- + statistics(cputime,[_,_]), + statistics(walltime,[_,_]), + setof(Deriv,find_deriv(GoalsList,Deriv),LDup), + rem_dup_lists(LDup,[],L), + statistics(cputime,[_,CT1]), + CPUTime1 is CT1/1000, + statistics(walltime,[_,WT1]), + WallTime1 is WT1/1000, + build_formula(L,Formula,[],Var), + var2numbers(Var,0,NewVar), + (setting(save_dot,true)-> + format("Variables: ~p~n",[Var]), + compute_prob(NewVar,Formula,Prob,1) + ; + compute_prob(NewVar,Formula,Prob,0) + ), + statistics(cputime,[_,CT2]), + CPUTime2 is CT2/1000, + statistics(walltime,[_,WT2]), + WallTime2 is WT2/1000,!. + + + find_deriv(GoalsList,Deriv):- solve(GoalsList,[],DerivDup), remove_duplicates(DerivDup,Deriv). @@ -56,29 +93,85 @@ same clause when instantiated */ Goals given the list of goals Evidence Goals and Evidence can have variables, sc returns in backtracking all the solutions with their corresponding probability -if it fails, the conditional probability is undefined */ sc(Goals,Evidence,Prob):- solve_cond(Goals,Evidence,Prob). solve_cond(Goals,Evidence,Prob):- - setof(DerivE,find_deriv(Evidence,DerivE),LDupE), - rem_dup_lists(LDupE,[],LE), - build_formula(LE,FormulaE,[],VarE), - var2numbers(VarE,0,NewVarE), - compute_prob(NewVarE,FormulaE,ProbE,0), - solve_cond_goals(Goals,LE,ProbGE), - Prob is ProbGE/ProbE. + (setof(DerivE,find_deriv(Evidence,DerivE),LDupE)-> + rem_dup_lists(LDupE,[],LE), + (setof(DerivGE,find_deriv_GE(LE,Goals,DerivGE),LDupGE)-> + rem_dup_lists(LDupGE,[],LGE), + build_formula(LE,FormulaE,[],VarE), + var2numbers(VarE,0,NewVarE), + build_formula(LGE,FormulaGE,[],VarGE), + var2numbers(VarGE,0,NewVarGE), + compute_prob(NewVarE,FormulaE,ProbE,0), + call_compute_prob(NewVarGE,FormulaGE,ProbGE), + Prob is ProbGE/ProbE + ; + Prob=0.0 + ) + ; + Prob=undefined + ). -solve_cond_goals(Goals,LE,ProbGE):- - setof(DerivGE,find_deriv_GE(LE,Goals,DerivGE),LDupGE), - rem_dup_lists(LDupGE,[],LGE), - build_formula(LGE,FormulaGE,[],VarGE), - var2numbers(VarGE,0,NewVarGE), - call_compute_prob(NewVarGE,FormulaGE,ProbGE). +/* sc(Goals,Evidence,Prob,Time1,Time2) compute the conditional probability of the list of goals +Goals given the list of goals Evidence +Goals and Evidence can have variables, sc returns in backtracking all the solutions with their +corresponding probability +Time1 is the time for performing resolution +Time2 is the time for elaborating the two BDDs +*/ +sc(Goals,Evidence,Prob,CPUTime1,CPUTime2,WallTime1,WallTime2):- + solve_cond(Goals,Evidence,Prob,CPUTime1,CPUTime2,WallTime1,WallTime2). -solve_cond_goals(Goals,LE,0):- - \+ find_deriv_GE(LE,Goals,_DerivGE). +solve_cond(Goals,Evidence,Prob,CPUTime1,CPUTime2,WallTime1,WallTime2):- + statistics(cputime,[_,_]), + statistics(walltime,[_,_]), + (setof(DerivE,find_deriv(Evidence,DerivE),LDupE)-> + rem_dup_lists(LDupE,[],LE), + (setof(DerivGE,find_deriv_GE(LE,Goals,DerivGE),LDupGE)-> + rem_dup_lists(LDupGE,[],LGE), + statistics(cputime,[_,CT1]), + CPUTime1 is CT1/1000, + statistics(walltime,[_,WT1]), + WallTime1 is WT1/1000, + build_formula(LE,FormulaE,[],VarE), + var2numbers(VarE,0,NewVarE), + build_formula(LGE,FormulaGE,[],VarGE), + var2numbers(VarGE,0,NewVarGE), + compute_prob(NewVarE,FormulaE,ProbE,0), + call_compute_prob(NewVarGE,FormulaGE,ProbGE), + Prob is ProbGE/ProbE, + statistics(cputime,[_,CT2]), + CPUTime2 is CT2/1000, + statistics(walltime,[_,WT2]), + WallTime2 is WT2/1000 + ; + Prob=0.0, + statistics(cputime,[_,CT1]), + CPUTime1 is CT1/1000, + statistics(walltime,[_,WT1]), + WallTime1 is WT1/1000, + CPUTime2=0.0, + WallTime2=0.0 + ) + ; + Prob=undefined, + statistics(cputime,[_,CT1]), + CPUTime1 is CT1/1000, + statistics(walltime,[_,WT1]), + WallTime1 is WT1/1000, + CPUTime2=0.0, + WallTime2=0.0 + ). + +solve_cond_goals(Goals,LE,0,Time1,0):- + statistics(runtime,[_,_]), + \+ find_deriv_GE(LE,Goals,_DerivGE), + statistics(runtime,[_,T1]), + Time1 is T1/1000. call_compute_prob(NewVarGE,FormulaGE,ProbGE):- (setting(save_dot,true)->