290 likes | 429 Views
Pairwise Cardinality Networks Michael Codish and Moshe Zazon-Ivry Department of Computer Science B en-Gurion University. Cardinality constraints in a nutshell. Satisfiability problems of the form: - conjunction of clauses - conjunction of constraints of the form
E N D
Pairwise Cardinality Networks Michael Codish and Moshe Zazon-Ivry Department of Computer Science Ben-Gurion University
Cardinality constraints in a nutshell • Satisfiability problems of the form: • - conjunction of clauses • - conjunction of constraints of the form • is one of • Typically • Applications: optimization problems, Max-Sat,scheduling problems, and more. • How to solve? • Dedicated solvers (SMT solvers) • Sat encoding • BDD-like structure • Sorting networks (unary ) • Networks of adders (binary)
Focus for this talk Encoding Cardinality Constraints to SAT, using sorting networks Input Output x1 y1 x2 y2 x3 y3 Sorter 0 x4 x5 y5 MiniSAT+, Barcelogic (and others) adapt this approach applying Batcher's Odd Even Sorting Network (from the 60’s) x6 y6 x7 y7 x8 y8 We propose an alternative choice of Sorting Network
Outline of the talk • Sorting networks • Batcher’s Odd-Even (OE) Network • An Alternative - Parberry's Pairwise (PW) Network • Why an Alternative (I) - constraint propagation • Understanding OE vs. PW • Why an Alternative (II) - partial evaluation • Conclusion
Sorting networks A Comparator is a device with two inputs, a and b, and two outputs, c and d c=max(a,b) a comparator d=min(a,b) b b1 a1 A Sorting network is a composition of wires and comparators that sorts its inputs a2 b2 b3 a3 a4 b4
Sorting networks x1 y1 x2 y2 There are “handcrafted” sorting networks x3 y3 y4 x4 x5 y5 x6 y6 x7 y7 x8 y8 x9 y9 Custom sorting network, n=9 x1 y1 And there are sorting networks based on your favorite sorting algorithm x2 y2 x3 y3 y4 x4 x5 y5 x6 y6 “Bubble sort” network
Sat encoding - comparators a c b d The SAT encoding of a comparator comparator(a, b, c, d) is:
Sat encoding – sorting networks t1 a1 b1 t2 t5 a2 b2 t3 t6 b3 a3 t4 a4 b4 The SAT encoding of a sorting network is the conjunction of the encodings of its comparators.
Sat encoding – cardinality constraints x1 y1 Given a cardinality constraint x1+ x2+…+ xn< k x2 y2 x3 y3 Sorter(8) 0 x4 Set the k’th output variable to zero. 0 x5 0 x6 0 x7 0 x8
The odd-even (OE) sorting network x1 y1 Sorter(n/2) Sorter(n/4) x2 y2 OE-Merger(n/2) x3 y3 Sorter(n/4) Sorter(n) x4 y4 OE-Merger(n) x5 y5 Sorter(n/2) Sorter(n/4) x6 y6 OE-Merger(n/2) Sorter(n/4) x7 y7 x8 y8
It’s all in the (OE) merger Given two sorted input sequences x1 y1 x2 y2 sorted OE Merger(8) x3 y3 Merge the odd input sequences OE-Merger(4) OE-Merger(4) x4 y4 x5 y5 x6 y6 sorted Merge the even input sequences x7 y7 x8 y8
It’s all in the (OE) merger Given two sorted input sequences x1 y1 x2 y2 sorted x3 y3 Merge the odd input sequences OE-Merger(4) OE-Merger(4) x4 y4 x5 y5 x6 y6 sorted Merge the even input sequences x7 y7 x8 y8 Combine the outputs into a sorted output
And Then There Was … Ian Parberry devised the pairwise (PW) sorting network in 1992 It has the exact same size & depth as the OE sorting network, but it is not isomorphic ,… Ian Parberry “The value of the pairwise sorting network is not that it is superior to the odd-even sorting network in any sense, but that it is the first serious rival to appear in over 20 years”
The pairwise sorting network A pairwisesplit is placed before the recursive calls and divides the network into “upper” and “lower” sub networks . Starting from the OE sorting network a1 x1 y1 “upper” Sorter(4) a2 x2 y2 a3 x3 y3 The inputs to the merger from the “upper” network have more (or equal) ones than those from the “lower” network. a4 x4 y4 OE-Merger(8) Splitter(8) PW-Merger(8) b1 x5 y5 “lower” Sorter(4) b2 x6 y6 b3 x7 y7 This property enables Parberry to simplify the specification of the merger b4 x8 y8 ai bi
k/2 k And More! When the k’th merger output is set to zero, then the k/2 ‘th output from the “lower“ network is also set to zero. “upper” Sorter(n/2) 0 PW-Merger(n) Splitter(n) 0 “lower” Sorter(n/2) Constraint propagation Because
The pairwise (PW) sorting network Parberry’s sorting network unfolds to a network of splitters followed by simplified mergers Sorter(n/2) PW-Merger(n) Splitter(n) Sorter(n/2)
What is the pairwise merger? The pairwise merger is constructed by layers of comparators x1 y1 x2 y2 x3 y3 y4 x4 x5 y5 x6 y6 Each layer reduces the “amplitude” of the sequence by half x7 y7 x8 y8 y9 x9 x10 y10 x11 y11 y12 x12 y13 x13 Check the Parberry paper, it’s a bit technical y14 x14 y15 x15 x16 y16
Our take on the pairwise merger Sorter(n/2) Sorter(n/2) OE-Merger(n) PW-Merger(n) Splitter(n) Sorter(n/2) Sorter(n/2) We observe that theoe-mergeris exactly the composition of a splitter and a pw-merger OE-Merger(n) Splitter PW-Merger(n)
PW vs. OE sorting network In the PW network splitters precede pw mergers. In the OE network splitters and pw mergers alternate Both sorting networks are composed from the same components OE-Sorting Network PW-Sorting Network x1 y1 x1 y1 x2 y2 x2 y2 x3 y3 x3 y3 x4 y4 x4 y4 x5 y5 x5 y5 x6 y6 x6 y6 x7 y7 x7 y7 x8 y8 x8 y8
Pairwise Cardinality Network 0 Advance knowledge of comparator’s input/output facilitates partial evaluation c=max(a,b) a 0 d=min(a,b) b Splitters before mergers gives more partial evaluation Comparators may become obsolete
Pairwise Cardinality Network k=1 k=2 k=4 Partial evaluation propagates through the network. x1 y1 Sorter(8) x2 y2 x3 y3 0 x4 0 x5 0 0 x6 0 0 x7 0 Splitter(16) 0 x8 0 x9 0 Sorter(8) Sorter(4) 0 x10 0 0 The simplified network is of size . It can be constructed directly. x11 0 0 x12 0 Splitter(8) PW-Merger(8) 0 0 x13 0 Sorter(4) 0 0 x14 0 0 0 x15 0 0 0 x16 0
Comapring potential for P.E. PW cardinality networkn=128,k=8 OE cardinality networkn=128,k=8 massive reduction of comparators in the “lower” halves
Pairwise Cardinality Network Number of comparators after partial evaluation In the worst case no big difference. Before p.e.
Experiments Boolean Matrix M n=5, k1=1 & k2=4 ongoing... 1 1 0 0 0 1 1 0 1 0 In the cardinality matrix problem the sum of each row and column is restricted between k1 and k2 0 3= 0 1 0 1 0 1 0 0 1 1 0 0 1 1 =2
Experiments For a Boolean Matrix with n=100 Cardinality Network proposed by Asinetal [1]
Conclusion • Propose cardinality networks based on pairwise sorting networks instead of odd-even networks • Better constraint propagation • Better partial evaluation • Improve understanding on the relation between pairwise and odd-even networks • Pairwise Networks are better for cardinality constraints and should be considered in tools (MiniSAT+, etc).