Simplifying Dynamic Programming via Tabling HaiFeng Guo University
Simplifying Dynamic Programming via Tabling Hai-Feng Guo University of Nebraska at Omaha, USA Gopal Gupta University of Texas at Dallas, USA
Tabled Logic Programming n A tabled logic programming system n n terminate more often by computing fixed points avoid redundant computation by memoing the computed answers Keeps the declarative and procedural semantics consistent for any definite logic programs. Tabled resolution schemes n OLDT, SLG, SLS, SLDT, DRA
Reachability : - table reach/2 reach(X, Y) : - reach(X, Z), arc(Z, Y). reach(X, Y) : - arc(X, Y). arc(a, b). arc(b, a). arc(b, c). a b c : - table reach/3 reach(X, Y, E) : reach(X, Z, E 1), arc(Z, Y, E 2), append(E 1, E 2, E). reach(X, Y, E) : - arc(X, Y, E). arc(a, b, [(a, b)]). arc(b, a, [(b, a)]). arc(b, c, [(b, c)]). Table Space will be increased dramatically due to the extra argument to record the path.
How tabled answers are collected? n n When an answer to a tabled call is generated, variant checking is used to check whether it has been already tabled. Observation: for collecting paths for the reachability problem, we need only one simple path for each pair of nodes. A second possible path for the same pair of nodes could be thought of as a variant answer.
Indexed / Non-indexed n n n The arguments of each tabled predicate are divided into indexed and nonindexed ones. Only indexed arguments are used for variant checking for collecting tabled answers. Non-indexed arguments are treated as no difference.
Mode Declaration for Tabled Predicates : - table_mode p(a 1, …, an). n p/n is a predicate name, n > 0; n each ai has one of the following forms: n n n + denotes that this indexed argument is used for variant checking; denotes that this non-indexed arguments is not be used for variant checking; * denotes that they are always bound before a call to the tabled predicate p/n is invoked.
: - table_mode reach(+, +, )
Aggregate Declaration n n Associate a non-indexed argument of a tabled predicate with some optimum constraint, e. g. minimum or maximum. The argument mode also includes: n n 0 denotes that this argument is a minimum; 9 denotes that this argument is a maximum.
Dynamic Programming n n Dynamic programming is typically used for solving optimization problems. A recursive strategy: the value of an optimal solution is recursively defined in terms of optimal solutions to subproblems.
Dynamic Programming with Mode declaration n n Optimization = Problem + Aggregation With mode declaration, defining a general solution suffices.
Matrix-Chain Multiplication Without Mode Declaration With Mode Declaration
Matrix-Chain Multiplication : - table scalar_cost/4. : - table_mode scalar_cost(+, 0, , ). scalar_cost([P 1, P 2], 0, P 1, P 2). scalar_cost([P 1, P 2, P 3 | Pr], V, PL 1, PL 2) : break([P 1, P 2, P 3 | Pr], PL 1, PL 2, Pk), scalar_cost(PL 1, V 1, Pk), scalar_cost(PL 2, V 2, Pk, Pn), V is V 1 + V 2 + P 1 * Pk * Pn.
Running Time Comparison (Seconds) Without Evidence Construction Benchmark matrix lcs obst apsp knap Without mode 2. 18 0. 94 0. 90 4. 17 54. 59 With mode 1. 14 0. 43 0. 32 2. 90 40. 64 With Evidence Construction Benchmark matrix lcs obst apsp knap Without mode 3. 09 5. 93 11. 69 6. 70 140. 46 With mode 2. 27 0. 67 0. 73 3. 10 41. 77 • The programs with mode declaration run 1. 34 to 16. 0 times faster than those without mode declaration.
Scalability Without Evidence With Evidence
Running Space Comparison (Megabytes) Without Evidence Construction Benchmark matrix lcs obst apsp knap Without mode 4. 98 78. 75 2. 65 20. 17 222. 65 With mode 0. 57 23. 44 0. 25 14. 60 14. 86 • Without evidence construction, the programs with mode declaration consumes 1. 4 to 15. 0 times less space than those without mode declaration.
Running Space Comparison (Megabytes) With Evidence Construction Benchmark matrix lcs obst apsp knap Without mode 9. 22 92. 99 4. 44 29. 90 399. 95 With mode 11. 64 44. 24 17. 46 21. 39 305. 19 • With evidence construction, space performance can be better or worse depending on the programs. • The programs without mode explicitly generate all possible answers and then table the optimal one; • The programs with mode implicitly generate all possible answers and selectively table the better answers until the optimal one is found.
Conclusion n n A new mode declaration for tabled predicates is introduced to aggregate information dynamically recorded in the table; A tabled predicate can be regarded as a function in which non-indexed arguments (outputs) are uniquely defined by the indexed arguments (inputs); The new mode declaration scheme, coupled with recursion, provides an elegant method for solving optimization problems; The efficiency of tabled resolution is improved since only indexed arguments are involved in variant checking.
- Slides: 17