160 likes | 307 Views
Mode-Directed Tabling for Dynamic Programming, Machine Learning, and Constraint Solving. Neng-Fa Zhou (City Univ. of New York) Yoshitaka Kameya and Taisuke Sato (Tokyo Inst. of Technology). What is Tabling?.
E N D
Mode-Directed Tabling for Dynamic Programming, Machine Learning, and Constraint Solving Neng-Fa Zhou (City Univ. of New York)Yoshitaka Kameya and Taisuke Sato (Tokyo Inst. of Technology) ICTAI'10, Zhou&Kameya&Sato
What is Tabling? The idea of tabling is to memorize answers to tabled subgoals and use the answers to resolve subsequent variant or subsumed subgoals. • Eliminate infinite loops • Reduce redundancy :-table path/2.path(X,Y):-edge(X,Y).path(X,Y):- edge(X,Z), path(Z,Y). :-table fib/2.fib(N,F):-N=<1,!,F=1.fib(N,F):- N1 is N-1, fib(N1,F1), N2 is N-2, fib(N2,F2), F is F1+F2. ICTAI'10, Zhou&Kameya&Sato
The Table-All Approach • Characteristics • All the arguments of a tabled subgoal are used in variant or subsumption checking • All answers are tabled • Problems • The number of answers may be too large or even infinite for DP and ML problems • When computing aggregates, tabling noncontributing answers is a waste ICTAI'10, Zhou&Kameya&Sato
Problems of Table-All (Ex-1) • It’s infeasible to table all the answers because the number of paths is infinite. :-table path/3.path(X,Y,[(X,Y)]):-edge(X,Y).path(X,Y,[(X,Z)|P]):- edge(X,Z), path(Z,Y,P). ICTAI'10, Zhou&Kameya&Sato
Problems of Table-All (Ex-2)(From the PRISM User’s Manual) proj([],L-L). proj([X|Xs],L0-L1):- pcfg(X,L0-L2), proj(Xs,L2-L1). pcfg(L):- pcfg(s,L-[]). pcfg(LHS,L0-L1):- ( nonterminal(LHS)-> msw(LHS,RHS), proj(RHS,L0-L1) ; L0 = [LHS|L1] ). • It is infeasible to table all the answers of pcfg/2 if the number of parse trees is huge. ICTAI'10, Zhou&Kameya&Sato
Mode-Directed Tabling in B-Prolog • Table mode declaration • C: Cardinality limit • Modes • +: input, used in variant checking • -: output, not used in variant checking • nt : not tabled, not used in variant checking or answer tabling • min: output, table answers with minimum values • max: output, table answers with maximum values :-table p(M1,...,Mn):C. ICTAI'10, Zhou&Kameya&Sato
Ex-1: Shortest Path Problem • sp(X,Y,P,W) • P is a shortest path between X and Y with weight W. :-table sp(+,+,-,min). sp(X,Y,[(X,Y)],W) :- edge(X,Y,W). sp(X,Y,[(X,Z)|Path],W) :- edge(X,Z,W1), sp(Z,Y,Path,W2), W is W1+W2. ICTAI'10, Zhou&Kameya&Sato
Ex-2: Knapsack Problem :- table knapsack(+,+,-,max). knapsack(_,0,[],0). knapsack([_|L],K,Selected,V):- knapsack(L,K,Selected,V). knapsack([F|L],K,[F|Selected],V):- K1 is K - F, K1 >= 0, knapsack(L,K1,Selected,V1), V is V1 + 1. • knapsack(L,K,Selected,V) • L: the list of items • K: the total capacity • Selected: the list of selected items • V: the length of Selected ICTAI'10, Zhou&Kameya&Sato
Mode-Directed Tabling for Dynamic Programming • General approach • Use recursion to generate tuples of a relation • Use mode-directed tabling to guide indexing subgoals and tabling answers • More examples • Second ASP solver competition(http://www.sci.brooklyn.cuny.edu/~zhou/) • How to Solve it With B-Prolog(17th Prolog Programming Contest) ICTAI'10, Zhou&Kameya&Sato
PRISM Prolog + Probabilistic reasoning and learning • Probability distributions and switches • Sample execution: sample(Goal) • Probability calculation: prob(Goal,P) • Learning: learn(Facts) values(coin,[head:0.5,tail:0.5]). direction(D):- msw(coin,Face), (Face==head->D=left;D=right). ICTAI'10, Zhou&Kameya&Sato
An Example in PRISM hmm(L) :- msw(init,Si), hmm(Si,L). hmm(S,[]). hmm(S,[C|L]) :- msw(out(S),C), msw(tr(S),NextS), hmm(NextS,L). values(init,[s0,s1]). values(out(_),[a,b]). values(tr(_),[s0,s1]). hmm([a,b,a]) hmm(s0,[a,b,a]) hmm(s1,[a,b,a]) hmm(s0,[b,a]) hmm(s1,[b,a]) hmm(s0,[a]) hmm(s1,[a]) hmm(s0,[]) hmm(s1,[]) ICTAI'10, Zhou&Kameya&Sato
Use of Tabling in PRISM :-table expl_hmm/2. expl_hmm(S,[]). expl_hmm(S,[CL]) :- expl_msw(out(S),C), expl_msw(tr(S),NextS), expl_hmm(NextS,L), add_explanation(path(hmm(S,[CL]), [hmm(NextS,L)], [msw(out(S),C), msw(tr(S),NextS)])). ICTAI'10, Zhou&Kameya&Sato
Use of Mode-Directed Tabling in PRISM :- table viterbi_hmm(+,-,-,max):3. viterbi_hmm(Os,E0,E1,W) :- str_length(L), viterbi_msw(init,S,W1), viterbi_hmm(1,L,S,Os,E2,E1,W2), W is W1+W2, E0=[node(hmm(Os), [path([hmm(1,L,S,Os)], [msw(init,S)])])|E2]. • Perform partial Viterbi computation by declaring a cardinality limit ICTAI'10, Zhou&Kameya&Sato
Performance Improvement ICTAI'10, Zhou&Kameya&Sato
Mode-Directed Tabling for Evaluating ASP Programs • Use tabling for programs with stratified negation • Use propagation and labeling to handle non-stratified negation • Use mode-directed tabling in seeking supports ICTAI'10, Zhou&Kameya&Sato
Conclusion • Mode-directed tabling is useful • For dynamic programming problems • For machine learning applications • For evaluating ASP programs • More information • B-Prolog version 7.4 & up (bprolog.com) ICTAI'10, Zhou&Kameya&Sato