1.. _sec-sat: 2 3Boolean Satisfiability Modelling in MiniZinc 4============================================ 5 6MiniZinc can be used to model Boolean satisfiability 7problems where the variables are restricted to be Boolean (:mzn:`bool`). 8MiniZinc can be used with efficient Boolean satisfiability 9solvers to solve the resulting models efficiently. 10 11Modelling Integers 12------------------ 13 14Many times although we wish to use a Boolean satisfiability solver we may 15need to model some integer parts of our problem. 16 17There are three common ways of modelling an integer variables 18:math:`I` in the range :math:`0 \dots m` where :math:`m = 2^{k}-1` 19using Boolean 20variables. 21 22- Binary: :math:`I` is represented by :math:`k` binary variables 23 :math:`i_0, \ldots, i_{k-1}` where 24 :math:`I = 2^{k-1} i_{k-1} + 2^{k-2} i_{k-2} + \cdots + 2 i_1 + i_0`. 25 This can be represented in MiniZinc as 26 27 .. code-block:: minizinc 28 29 array[0..k-1] of var bool: i; 30 var 0..pow(2,k)-1: I = sum(j in 0..k-1)(bool2int(i[j])*pow(2,j)); 31 32- Unary: where :math:`I` is represented by :math:`m` binary variables 33 :math:`i_1, \ldots, i_m` 34 and :math:`i = \sum_{j=1}^m \mathtt{bool2int}(i_j)`. Since there is massive redundancy in 35 the unary representation we usually require that 36 :math:`i_j \rightarrow i_{j-1}, 1 < j \leq m`. This can be represented in MiniZinc as 37 38 .. code-block:: minizinc 39 40 array[1..m] of var bool: i; 41 constraint forall(j in 2..m)(i[j] -> i[j-1]); 42 var 0..m: I = sum(j in 1..m)(bool2int(i[j]); 43 44- Value: where :math:`I` is represented by :math:`m+1` binary variables 45 :math:`i_0, \ldots, i_m` where 46 :math:`I = k \Leftrightarrow i_k`, and at most one of :math:`i_0, \ldots, i_m` is true. 47 This can be represented in MiniZinc as 48 49 .. code-block:: minizinc 50 51 array[0..m] of var bool: i; 52 constraint sum(j in 0..m)(bool2int(i[j]) == 1; 53 var 0..m: I; 54 constraint forall(j in 0..m)(I == j <-> i[j]); 55 56There are advantages and disadvantages to each representation. It depends 57on what operations on integers are to required in the model as to which is 58preferable. 59 60Modelling Disequality 61--------------------- 62 63Let us consider modelling a latin squares problem. A latin square 64is an :math:`n \times n` grid of numbers from :math:`1..n` such that 65each number appears exactly once in every row and column. 66An integer model for latin squares is shown in :numref:`ex-latin`. 67 68.. literalinclude:: examples/latin.mzn 69 :language: minizinc 70 :name: ex-latin 71 :caption: Integer model for Latin Squares (:download:`latin.mzn <examples/latin.mzn>`). 72 73The only constraint on the integers is in fact disequality, which is encoded 74in the :mzn:`alldifferent` constraint. 75The value representation is the best way of representing disequality. 76A Boolean only model for latin squares is shown in 77:numref:`ex-latinbool`. 78Note each integer array element :mzn:`a[i,j]` is replaced by an array of 79Booleans. 80We use the :mzn:`exactlyone` predicate to encode that each value is used 81exactly once in every row and every column, as well as to encode that exactly 82one of the Booleans corresponding to integer array element :mzn:`a[i,j]` is true. 83 84.. literalinclude:: examples/latinbool.mzn 85 :language: minizinc 86 :name: ex-latinbool 87 :caption: Boolean model for Latin Squares (:download:`latinbool.mzn <examples/latinbool.mzn>`). 88 89Modelling Cardinality 90--------------------- 91 92Let us consider modelling the Light Up puzzle. The puzzle consists of a 93rectangular grid of squares which are blank, or filled. Every filled square 94may contain a number from 1 to 4, or may have no number. The aim is to place 95lights 96in the blank squares so that 97 98- Each blank square is "illuminated", that is can see a light through an 99 uninterrupted line of blank squares 100- No two lights can see each other 101- The number of lights adjacent to a numbered filled square 102 is exactly the number in the filled square. 103 104An example of a Light Up puzzle is shown in :numref:`fig-lightup` 105with its solution in :numref:`fig-lightup-sol`. 106 107.. _fig-lightup: 108 109.. figure:: figures/lightup.* 110 111 An example of a Light Up puzzle 112 113.. _fig-lightup-sol: 114 115.. figure:: figures/lightup2.* 116 117 The completed solution of the Light Up puzzle 118 119It is natural to model this problem 120using Boolean variables to determine which 121squares contain a light and which do not, but there is some integer 122arithmetic to consider for the filled squares. 123 124.. literalinclude:: examples/lightup.mzn 125 :language: minizinc 126 :name: ex-lightup 127 :caption: SAT Model for the Light Up puzzle (:download:`lightup.mzn <examples/lightup.mzn>`). 128 129A model for the problem is given in :numref:`ex-lightup`. 130A data file for the problem shown in :numref:`fig-lightup` 131is shown in :numref:`fig-lightupdzn`. 132 133.. literalinclude:: examples/lightup.dzn 134 :language: minizinc 135 :name: fig-lightupdzn 136 :caption: Datafile for the Light Up puzzle instance shown in :numref:`fig-lightup`. 137 138The model makes use of a Boolean sum predicate 139 140.. code-block:: minizinc 141 142 predicate bool_sum_eq(array[int] of var bool:x, int:s); 143 144which requires that the sum of an array of Boolean equals some fixed 145integer. There are a number of ways of modelling such 146*cardinality* constraints using Booleans. 147 148- Adder networks: we can use a network of adders to 149 build a binary Boolean representation of the sum of the Booleans 150- Sorting networks: we can use a sorting network to sort 151 the array of Booleans to create a unary representation of the sum 152 of the Booleans 153- Binary decision diagrams: we can create a binary decision diagram 154 (BDD) that encodes the cardinality constraint. 155 156 157.. literalinclude:: examples/bboolsum.mzn 158 :language: minizinc 159 :name: ex-bboolsum 160 :caption: Cardinality constraints by binary adder networks (:download:`bboolsum.mzn <examples/bboolsum.mzn>`). 161 162.. literalinclude:: examples/binarysum.mzn 163 :language: minizinc 164 :name: ex-binarysum 165 :caption: Code for building binary addition networks (:download:`binarysum.mzn <examples/binarysum.mzn>`). 166 167We can implement :mzn:`bool_sum_eq` using binary adder networks 168using the code shown in :numref:`ex-bboolsum`. 169The predicate :mzn:`binary_sum` 170defined in :numref:`ex-binarysum` 171creates a binary representation 172of the sum of :mzn:`x` by splitting the list into two, 173summing up each half to create a binary representation 174and then summing these two binary numbers using :mzn:`binary_add`. 175If the list :mzn:`x` is odd the last bit is saved to use as a carry in 176to the binary addition. 177 178.. \pjs{Add a picture of an adding network} 179 180.. literalinclude:: examples/uboolsum.mzn 181 :language: minizinc 182 :name: ex-uboolsum 183 :caption: Cardinality constraints by sorting networks (:download:`uboolsum.mzn <examples/uboolsum.mzn>`). 184 185.. literalinclude:: examples/oesort.mzn 186 :language: minizinc 187 :name: ex-oesort 188 :caption: Odd-even merge sorting networks (:download:`oesort.mzn <examples/oesort.mzn>`). 189 190We can implement :mzn:`bool_sum_eq` using unary sorting networks 191using the code shown in :numref:`ex-uboolsum`. 192The cardinality constraint is defined by expanding the input 193:mzn:`x` to have length a power of 2, and sorting the resulting bits 194using an odd-even merge sorting network. 195The odd-even merge sorter shown in :mzn:`ex-oesort` works 196recursively by splitting 197the input list in 2, sorting each list and merging the two 198sorted lists. 199 200.. \pjs{Add much more stuff on sorting networks} 201 202 203 204.. \pjs{Add a picture of an adding network} 205 206.. literalinclude:: examples/bddsum.mzn 207 :language: minizinc 208 :name: ex-bddsum 209 :caption: Cardinality constraints by binary decision diagrams (:download:`bddsum.mzn <examples/bddsum.mzn>`). 210 211We can implement :mzn:`bool_sum_eq` using binary decision diagrams 212using the code shown in :mzn:`ex:bddsum`. 213The cardinality constraint is broken into two cases: 214either the first element :mzn:`x[1]` is :mzn:`true`, 215and the sum of the remaining bits 216is :mzn:`s-1`, or :mzn:`x[1]` is :mzn:`false` and the sum of the remaining bits 217is :mzn:`s`. For efficiency this relies on common subexpression elimination 218to avoid creating many equivalent constraints. 219 220 221.. \pjs{Add a picture of a bdd network network} 222 223 224