Algebraic Dynamic Programming Unit 3.b: Products of Evaluation Algebras Robert Giegerich1 (Lecture) Benedikt Löwes (Exercises) Faculty of Technology Bielefeld University http://www.techfak.uni-bielefeld.de/ags/pi/lehre/ADP Summer 2014 1 robert@techfak.uni-bielefeld.de Robert Giegerich, Benedikt Löwes ADP Lecture Summer 2014 1 / 23 Product Algebras Motivation for a bag-of-surprises You have worked with the ADP examples Relaxed Palindromes NW alignments Nussinow RNA folding using different algebras: minimizing cost, distance maximizing base pairs, similarity counting numbers of candidates enumerating candidate trees or pretty printing So far, we have always used one algebra at a time. Robert Giegerich, Benedikt Löwes ADP Lecture Summer 2014 2 / 23 Product Algebras Algebras in combination In working with applications, often we want several types of information about the candidates in our search space. Optimal solutions under a primary and a secondary objectives (the cheapest of the largest pizzas, or vice versa) the optimal score and the prettyprint of the candidate(s) that achieve(s) it the optial score and the number of co-optial candidates enumeration of candidates in tree and prettyprint form (for debugging) ... or combinations of the above Whenever the desired information can be derived via various evaluation algebras, this amounts to using several algebras simultaneously and in cooperation. Robert Giegerich, Benedikt Löwes ADP Lecture Summer 2014 3 / 23 Product Algebras Ad-hoc combinations of evaluation algebras Given two Σ-algebras A and B, holding (say) functions fA , fB and hA , hB , we can define algebra C operating on value pairs from A and B: fC ((a, b)) = some function using fA (a) and fB (b) hC ([(a, b)]) = some function on lists of pairs, implementing the desired combination of hA and hB This means coding a new algebra (using the available fA , fB ), a new proof of Bellman’s Principle for fC versus hC , and new debugging. There are “standard” cases where we can do better . . . ! Robert Giegerich, Benedikt Löwes ADP Lecture Summer 2014 4 / 23 Product Algebras Algebra products We define several product operations *, ×, ... to combine two algebras in specific ways. Instead of hand-coding algebra C and calling G(C , x) we directly call G(A ∗ B, x) with no coding and debugging effort on our side. The ADP compiler knows the definitin of A ∗ B and constructs and uses a correct implementation of C . Robert Giegerich, Benedikt Löwes ADP Lecture Summer 2014 5 / 23 Product Algebras General scheme of product operations Product algebras A ∗ b – compute answer value pairs (a, b) – using functions fA ∗ fB : hA ∗ hB : component wise dependent on our purpose In fact, there are at least four useful variants of hA ∗ hB . Robert Giegerich, Benedikt Löwes ADP Lecture Summer 2014 6 / 23 Product Algebras The Cartesian product (×) Definition Let A and B be evaluation algebras over Σ. Their Cartesian product algebra C = A × B is an evaluation algebra over Σ and with fC (x1 , . . . , xk ) = (fA (a1 , . . . , ak ), fB (b1 , . . . , bk )) if xi = (xiA , xiB ), then ai = xiA , bi = xiB , if xi ∈ A, then ai = xi = bi for each f in Σ, and with the objective function hC [(a1 , b1 ), ..., (ak , bk )] = [(l, r )| l ∈ hA [a1 , . . . , ak ], r ∈ hB [b1 , ..., bk ] ] Robert Giegerich, Benedikt Löwes ADP Lecture Summer 2014 7 / 23 Product Algebras Meaning of the Cartesian Product The Cartesian product algebra A × B optimizes independently according to A and B returns the Cartesian product of the result sets from A and B a result value (a, b) may or may not be derived from the same candidate This appears too simple to be useful – but be patient. Robert Giegerich, Benedikt Löwes ADP Lecture Summer 2014 8 / 23 Product Algebras Definition of lexicographic product * Definition Let A and B be evaluation algebras over Σ. Their lexicographic product algebra A ∗ B is an evaluation algebra C over Σ with fC defined component-wise (as with ×) for each f in Σ, and with the objective function hA∗B ([(a1 , b1 ), ..., (ak , bk )]) = [(l, r )]| l ← set(hA [a1 , . . . , ak ]), r ← hB ([r |(l 0 , r ) ← [(a1 , b1 ), ..., (ak , bk )], l 0 = l]) where [] denotes a multi-set and the function set returns a multi-set in which all duplicate elements are removed. Robert Giegerich, Benedikt Löwes ADP Lecture Summer 2014 9 / 23 Product Algebras The meaning of G(A ∗ B, x) is actually lready explained in ADP definitions, but still ... Intuitive explanation: Phase 1 evaluates all candidates via functions fA ∗ fB into a gigantic list [..., (ai , bi ), ...] Phase 2 chooses pairs with hA based on the ai component, and Phase 3 chooses from those the final pairs, based on te bi component Thus, if hA maximizes under ordering <A and hB maximizes under ordering <B , the product maximizes under the lexicographic combination of orderinge, (<A , <B ). Robert Giegerich, Benedikt Löwes ADP Lecture Summer 2014 10 / 23 Product Algebras How about Bellman’s Principle? In general, there is a proof obligation with * A ∗ B must satisfy Bellman’s principle . . . because the three phases are not executed separately. Instead, hA∗B is applied at intermediate steps. Bellman’s Principle guarantees that we loose no final result along the way. Robert Giegerich, Benedikt Löwes ADP Lecture Summer 2014 11 / 23 Product Algebras Advantages over hand-coded algebra combinations Benefits of ∗ products: the ADP compiler knows the definition of * and produces automatically code for A ∗ B. the resulting functions fA∗B etc. maybe a little less efficient than in a handcoded product, BUT: products formed with ∗ require no coding and testing Robert Giegerich, Benedikt Löwes ADP Lecture Summer 2014 12 / 23 Product Algebras Versatility of the lexicographic product A ∗ B has a surprising number of uses optimization under a lexicographic ordering class-wise optimization reporting candidates The latter two cases use algebras A or B that do not do optimization. More later ... Robert Giegerich, Benedikt Löwes ADP Lecture Summer 2014 13 / 23 Product Algebras Useful properties of the lexicographic product (1) Theorem (1) Selectivity: Let hA (X ) ⊆ X and hB (X ) ⊆ X . For each value (a, b) computed by hA ∗ hB , there is a candidate t such that (a, b) = (A(t), B(t)). I.e., each returned value pair is backed up by a candidate that evaluates to these two values. Selectivity does not hold e.g. for the counting algebra. Robert Giegerich, Benedikt Löwes ADP Lecture Summer 2014 14 / 23 Product Algebras Useful properties of the lexicographic product (2) Theorem (2) Identity: For any algebras A and B, and multiset X , (idA ∗ idB )(X ) = X . Combining two identity functions gives the identity function on pairs. (On lists, (idA ∗ idB )(X ) is a permutation of X .) Robert Giegerich, Benedikt Löwes ADP Lecture Summer 2014 15 / 23 Product Algebras Useful elementary properties (3) Since A ∗ B is just another algebra, we can make bigger products ... Theorem (3) Associativity: G(A ∗ (B ∗ C ), x) = G((A ∗ B) ∗ C , x) modulo re-grouping of tuples of result values, e.g. (a, (b, c)) = ((a, b), c) = (a, b, c) Hence we can simply write G(A ∗ B ∗ C , x). Robert Giegerich, Benedikt Löwes ADP Lecture Summer 2014 16 / 23 Product Algebras Useful elementary properties (4) Theorem (4) Lexicographic optimization: Let A and B optimize over <A and <B . Then A ∗ B satisfies Bellman’s Principle and optimizes over the lexicographic ordering (<A , <B ). So, for optimizing over two orderings in lexicographic fashion, there is a general proof that Bellman’s Principle is always satisfied. Robert Giegerich, Benedikt Löwes ADP Lecture Summer 2014 17 / 23 Interleaved product The interleaved product: Motivation Let B(k) be an algebra that computes the best k scores under some optimization scheme. Let A be a an algebra that assigns a class attribute to each candidate. With G(A ∗ B(k), x), we obtain the best k for each class. How can we get the best 3 B-values from different A-classes? We first compute B(1) for each A-class, and then choose the classes with the best k B-values. Robert Giegerich, Benedikt Löwes ADP Lecture Summer 2014 18 / 23 Interleaved product The interleaved product: Definition Let A be a Σ- algebra and B(k) a generic Σ-algebra, and note that B(1) is unitary. Definition The interleaved product (A/B)(k) is a generic Σ-algebra and has the functions fA/B = fA×B (1) for each f ∈ Σ, and the objective function h(A/B)(k) [(a1 , b1 ), . . . , (am , bm )] = [ (l, r ) | (l, r ) ← U, p ← V , p = r ] where U = hA∗B(1) [(a1 , b1 ), . . . , (am , bm )] V = set(hB(k) [ v | ( , v ) ← U]) Robert Giegerich, Benedikt Löwes ADP Lecture (2) Summer 2014 19 / 23 Interleaved product Interleaved product (2) In Equation 2, U stores the best results in each A-class, V stores the global rank of all solutions from U, and the main set comprehension filters U to contain only the results from V . This product is useful when B(k) alone produces the k-best-scoring candidates, and A maps candidates to different classes (not making any choices). Then, A/B yields the 1-best-scoring candidates from k different classes. This situation required sophisticated hand-coding in RNA shape analysis in 2004 – it is now simply written as (Shape/Mfe)(k). Robert Giegerich, Benedikt Löwes ADP Lecture Summer 2014 20 / 23 Pareto product Pareto-Optimization Pareto-optimization is a general way of optimizing with two (or more) idependent criteria. The solution set one computes is the Pareto front of the search space X ⊆ A × B. Taking hA and hB as maximization, the Pareto front operator pf is defined as pf(X ) = {(a, b) ∈ X | 6 ∃(a0 , b 0 ) ∈ X \ {(a, b)} with a ≤ a0 , b ≤ b 0 }. In words: No element of the Pareto front is dominated in both dimensions by another element of X . Robert Giegerich, Benedikt Löwes ADP Lecture Summer 2014 21 / 23 Pareto product The Pareto-Product Let A and B be two Σ-algebras which maximize with respect to <A and <B . Definition The Pareto-product AB is a Σ-algebra C with fC defined component-wise from fA and fB , and hC (X ) = pf(X ) with pf as defined above for <A and <B . The Pareto product is a recent development and not yet implemented in Bellman’s GAP. Robert Giegerich, Benedikt Löwes ADP Lecture Summer 2014 22 / 23 Pareto product Conclusion on algebra products A number of algebra product operators have been defined – there may be more. it is faster and safer to use products where suitable than hand-coding a combined evalation algebra. since products yield new algebras, these can be used with further products, as in G((A/B) ∗ (C × D), x), which makes them a powerful programming device on a very high level of abstraction. Understanding the definitions of products is one thing. Using products effectively takes quite some practice!! That’s the topic of our next set of slides. Robert Giegerich, Benedikt Löwes ADP Lecture Summer 2014 23 / 23