1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 | /* Set partition problem in SICStus Prolog. Problem formulation from "" " This is a partition problem. Given the set S = {1, 2, ..., n}, it consists in finding two sets A and B such that: - A U B = S,</li> - |A| = |B|,</li> - sum(A) = sum(B),</li> - sum_squares(A) = sum_squares(B). "" " Compare with the following models: * MiniZinc: http://www.hakank.org/minizinc/set_partition.mzn * Gecode/R: http://www.hakank.org/gecode_r/set_partition.rb * ECLiPSe : http://www.hakank.org/eclipse/set_partition.ecl Note: This model uses boolean lists instead of set vars. Model created by Hakan Kjellerstrand, hakank @gmail .com */ % Licenced under CC-BY-4.0 : http://creativecommons.org/licenses/by/4.0/ :-use_module(library(clpfd)). :-use_module(library(lists)). % find all (7) solutions for N = 16. go :- N = 16, NumSets = 2, findall([A,Sums,SumSquared],set_partition(N, NumSets,A,Sums, SumSquared),L), length (L,Len), write (len:Len),nl, ( foreach ([A,Sums,SumSquared],L) do write (a:A),nl, write (sums:Sums),nl, write (sum_squared:SumSquared),nl,nl ), fd_statistics. % % Search for a solution between N = 17 and 32 % go2 :- N in 17..32, indomain(N), NumSets = 2, write (n:N),nl, set_partition(N, NumSets,A,Sums,SumSquared), write (a:A),nl, write (sums:Sums),nl, write (sum_squared:SumSquared),nl, fd_statistics. % % Test for larger N and more sets % go3 :- N in 4..127, % overflow for 127 NumSets in 3..5, indomain(N), indomain(NumSets), write ([n:N,num_sets:NumSets]),nl, set_partition(N, NumSets,A,Sums,SumSquared), write (a:A),nl, write (sums:Sums),nl, write (sum_squared:SumSquared),nl, fd_statistics. % % set partition % set_partition(N,NumSets,ASet,Sums,SumSquared) :- % sanity check Mod is N mod NumSets, ( Mod \= 0 -> format ( 'Error: ~d is not a multiple of ~d\n' , [NumSets,N]), fail ; true ), % list of sets % this corresponds to a list of NumSets sets of 1..N matrix(A,[NumSets,N]), append(A,AList), domain(AList,0,1), % sums length (Sums,NumSets), N2 is N*N, domain(Sums,0,N2), % squared sums length (SumSquared,NumSets), N4 is N2*N2, domain(SumSquared,0,N4), % create the universe for partition_set % and the weights for weight/3 below. length (Weights,N), length (Weights2,N), ( for (I,1,N), foreach (L,Universe), foreach (W, Weights), foreach (W2, Weights2) do L is I, W is I, W2 is I*I ), % same number of elements partition_set(A, Universe), % all sets must have the same cardinality same_cardinality(A), % calculate sums and squared sums for each partition ( for (I,1,NumSets), foreach (SumsI,Sums), foreach (SumSquaredI,SumSquared), foreach (AI,A), param(A,Weights,Weights2) do scalar_product(Weights,AI, #=,SumsI), scalar_product(Weights2,AI, #=,SumSquaredI) ), % all sums and squared sums must be equal same_values(Sums), same_values(SumSquared), % symmetry breaking: 1 should be in the first set nth1(1, A, A1), contains(1,A1), % search append(AList,Sums,Vars1), append(Vars1,SumSquared, Vars), labeling([], Vars), % convert to set representation ( foreach (ARow,A), foreach (SetRow,ASet) do boolean_to_set(ARow,SetRow) ). % Element El is in the set Set contains(El, Set) :- element(El,Set,1). % all elements have the same values same_values(X) :- ( fromto(X, [This,Next | Rest], [Next|Rest],[_]) do This #= Next ). % % Partitions the list of sets S into disjoint sets. % All elements in the universe Universe must be % included in exactly one of the sets. % partition_set(S, _Universe) :- all_disjoint(S).%, % all_union(S,Universe). % Checks that either of the sets contains the value % % Note: This handles both all_disjoint and all_union % in the partition set constraint above. % all_disjoint(Sets) :- ( foreach (S1,Sets), count(I,1,_), param(Sets) do ( foreach (S2,Sets), count(J,1,_), param(S1,I) do I < J -> ( foreach (SS1,S1), foreach (SS2,S2) do SS1 + SS2 #= 1 ) ; true ) ). % all sets have the same cardinality same_cardinality(S) :- ( fromto(S,[S1,S2|Rest],[S2|Rest], [_]) do cardinality(S1,Card), cardinality(S2,Card) ). % another version same_cardinality3([]) :- !. same_cardinality3([_]) :- !. same_cardinality3([S1,S2|Rest]) :- cardinality(S1,Card), cardinality(S2,Card), same_cardinality([S2|Rest]). % the cardinality is simply the sum of a "set" cardinality(Set,Card) :- sum(Set, #=,Card). matrix_element(X, I, J, Val) :- nth1(I, X, Row), element(J, Row, Val). % From Mats Carlsson. matrix(_, []) :- !. matrix(L, [Dim|Dims]) :- length (L, Dim), ( foreach (X,L), param(Dims) do matrix(X, Dims) ). % convert a list of boolean to a "set" % (used after labeling) boolean_to_set(List,Set) :- ( foreach (El,List), count(I,1,_), fromto(Set,Out,In,[]) do El == 1 -> Out = [I|In] ; Out = In ). |