Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Cardinality & Sorting Networks Cardinality constraint Appears in many practical problems: scheduling, timetabling etc’. Also takes place in the Max-Sat problem. Has the form: x1 x2 ... xn k where the symbol is one of {, , , } , variables are boolean. Cardinality constraint As an example, the car sequencing problem (from last week) defines the number of cars to be manufactured, per model. If we agree that a Boolean variable xi is true iff the i‘th car of the resulting sequence belongs to some model M, then the constraint x1 x2 ... xn k enforces the existence of k cars from that model to be in the sequence (where the sequence size is n). Cardinality constraint car sequence instance: 1 2 3 4 5 6 7 8 9 10 0 1 5 2 4 3 3 4 2 5 0 0 1 0 0 0 0 0 1 0 Each Boolean variable below the sequence is 1 iff a “yellow” model appears in the corresponding position. Cardinality constraint In the Max-Sat problem we seek an assignment which satisfies the maximal number of clauses in a propositional formula {C1 , C2 ,..., Cn } One approach to solve the problem is to add “fresh” blocking variables to each clause, giving ' {C1 x1 , C2 x2 ,..., Cn xn } . We then search for a minimal k such that '{x1 x2 x3 ... xn k} is satisfied. Cardinality constraint There are several ways to encode a cardinality constraint as propositional formula. In this presentation we consider encoding which is based on sorting networks. Sorting networks A (Boolean) sorting network is a circuit that receives n Boolean inputs x1 , x2 ,..., xn and permutes them to obtain the sorted outputs y1 , y2 ,..., yn . The network is composed of “wired” comparators. Each comparator has two inputs u1 ,u2 and two outputs v1 , v2 . The upper output, v1 , receives the maximal input value, where the lower output,v2 , receives the minimal input value. The network computation is performed in parallel. The 0-1 principle Theorem: If a sorting network sorts every sequence of 0's and 1's, then it sorts every arbitrary sequence of values. Sorting networks 0 1 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 1 0 0 Sorting networks it’s not! Is it a No sorting network ? 0 1 0 1 1 0 1 0 Sorting network encoding A comparator comparator(a, b, c1 , c2 ) can be encoded into CNF by the following 6 clauses: a c1 , b c1 , a b c2 , c2 a, c2 b, c1 a b Given a set of comparators of any sorting network, it is straightforward to construct a CNF which is a conjunction of the encoded comparators. Sorting network encoding For any assignment on the sequence input, a complete satisfying assignment of the CNF, yields a sorted output assignment. We can use this property to check whether a given network is a sorting network. This could be done by adding constraint which is satisfied only if the output is not sorted. A satisfying assignment for the CNF and the above constraint means that the network does not sort. Cardinality constraint encoding The cardinality constraint, x1 x2 ... xn k over the input sequence x1 , x2 ,..., xn is obtained by setting the k’th largest output to 0. It implies that all outputs from position k are zero. Hence, there are less than k ones amongst the input values. If the propositional formula describes the relation between the input x1 , x2 ,..., xn and the output y1 , y2 ,..., yn of a sorting network, we search a satisfying assignment for yk Cardinality constraint encoding 8 x i 1 i 4 x1 x2 x3 x4 x5 x6 x7 x8 Sorter(8) y1 y2 y3 y4 y5 y6 y7 y8 Cardinality constraint encoding 8 x i 1 i 4 x1 x2 x3 x4 x5 x6 x7 x8 Sorter(8) y1 y2 y3 0 y5 y6 y7 y8 Cardinality constraint encoding 8 x i 1 i 4 x1 x2 x3 x4 x5 x6 x7 x8 Sorter(8) 8 y i 1 i 4 y1 y2 y3 0 0 0 0 0 The odd-even sorting network Batcher’s odd-even network is a classic sorting network. It was devised back in 68’. It uses the divide and conquer design. The approach is similar to the merge-sort algorithm: for sorting a list of 2n inputs, partition the list into two sub lists, with n values each. Recursively sort these two lists, and finally merge them. Network’s size: O(n log 2 n) , depth: O(log 2 n). The odd-even sorting network Sorter(2n) The odd-even sorting network Sorter(n) Sorter(n) Merger(2n) The odd-even sorting network Sorter(n/2) Sorter(n/2) Sorter(n/2) Sorter(n/2) Merger(n) Merger(n) Merger(2n) The odd-even merger The odd even merger uses the divide and conquer design, as well. It is assumed that its two input sequences: a1 , a2 ,..., an and b1 , b2 ,..., bn are already sorted. The procedure divides each input into odd and even sequences, namely: a1 , a2 ,..., an 1and a2 , a4 ,..., an for the a’s. b1 , b2 ,..., bn1 and b2 , b4 ,..., bn for the b’s. The even and the odd sequences are merged recursively. The merged sequences are combined by comparing each ( yi , yi 1 ), i {1,3,5..n 1} in the result outcome The odd-even merger a1 a2 a3 a4 b1 b 2 b 3 b 4 Merger(n) The odd-even merger Merger(n/2) Merger(n/2) a1 a2 a3 a4 b1 b 2 b 3 b 4 The odd-even merger 1 1 0 1 1 1 0 1 1 Merger(n/2) Merger(n/2) 1 1 1 The odd-even merger 0 1 0 1 0 0 Merger(n/2) Merger(n/2) 1 1 Merger(n/2) Merger(n/2) The odd-even merger 1 1 1 1 1 0 1 0 1 1 1 1 1 1 0 0 Unit Propagation If a set of propositional clauses contains a unit clause l, the other clauses are simplified. This process is called unit propagation. For a comparator comparator(a, b, c1 , c2 ) : a comparator( a, b, c1 , c2 ) up c2 b c1 b comparator ( a, b, c1 , c2 ) up c2 a c1 c1 comparator(a, b, c1 , c2 ) up a b c2 If the cardinality constraint parameter k is known a priory, we can use the above process in order to design a simplified network. Odd-Even sorting network properties If there are p input variables that are set to 1, then by unit propagation the first p output variables are set to 1, as well. If there are p input variables that are set to 1, and the output variable in the p+1 position is set to 0, then by unit propagation the reminder of the input variables are set to 0. Odd-Even sorting network properties 8 x i 1 i 4 x11 x3 x41x6 1 1 Sorter(8) y1 y2 y3 y4 y5 y6 y7 y8 Odd-Even sorting network properties 8 x i 1 i 4 x11 x3 x41x6 1 1 Sorter(8) 8 y i 1 i 4 1 1 1 1 y5 y6 y7 y8 Odd-Even sorting network properties 8 x i 1 i 4 x11 x3 x41x6 1 1 Sorter(8) 8 y i 1 i 4 y1 y2 y3 y4 0 y6 y7 y8 Odd-Even sorting network properties 0 1 0 0 1 0 11 x 4 8 i 1 i Sorter(8) 8 y i 1 i 4 y1 y2 y3 y4 0 y6 y7 y8 The pairwise sorting network Devised in 94’ by Ian Parberry Takes a different form of the odd-even sorting network. The pairwise sorting network Has the same size, depth and properties of the odd- even sorting network. Better unit propagation. The simplified CNF of the corresponding cardinality network is significantly smaller. Simple recursive definition of the corresponding cardinality network when k is known a priory. Experiments have shown better performance for structured problems. Cardinality (0,1) matrix constraint A cardinality (0,1) matrix constraint is a constraint C defined on a Matrix M=x[i,j] of boolean variables. Every row i is associated with two positive integers lr[i] and ur[i], such that lr[i] ≤ ur[i]. Every column j is associated with two positive integers lc[j] and uc[j], such that lc[j] ≤ uc[j]. Each row and column of variables in the matrix M is constrained by: i Row ( M ) : lr [i ] x[i, j ] ur[i] jCol(M) j Col ( M ) : lc[ j ] x[i, j ] uc[ j ] iRow(M) Cardinality (0,1) matrix constraint The following Boolean martix is a solution instance for the cardinality (0,1) matrix, where each sum of row and column is bounded by 3 from bottom, and by 4 from top. 1 1 0 0 1 1 0 1 1 0 1 1 1 0 0 1 0 0 1 1 0 1 1 1 1 Exercise You are required to solve n cardinality (0,1) matrix constraint instances. The boolean matrix size for each problem instance is n×n, choose significant size n. For each problem instance i [1..n] the sum of each row and column is exactly i. Use a sorting network for the cardinality encoding. Measure the solving time of each problem instance. Explain your results.