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 262 263
|
/* Part of SWI-Prolog
Author: Markus Triska
E-mail: triska@gmx.at
WWW: http://www.swi-prolog.org
Copyright (c) 2005-2011, Markus Triska
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
:- module(clp_distinct,
[
vars_in/2,
vars_in/3,
all_distinct/1
]).
:- use_module(library(lists)).
/** <module> Weak arc consistent all_distinct/1 constraint
@deprecated Superseded by library(clpfd)'s all_distinct/1.
@author Markus Triska
*/
% For details, see Neng-Fa Zhou, 2005:
% "Programming Finite-Domain Constraint Propagators in Action Rules"
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
This library uses the following attribute value:
dom_neq(Domain, Left, Right)
Domain is an unbounded (GMP) integer representing the domain as a
bit-vector, meaning N is in the domain iff 0 =\= Domain /\ (1<<N).
Left and Right are both lists of lists of variables. Each of those lists
corresponds to one all_distinct constraint the variable is involved in,
and "left" and "right" means literally which variables are to the left,
and which to the right in the first, second etc. of those constraints.
all_distinct([A,B,C,D]), all_distinct([X,Y,C,F,E]) causes the following
attributes for "C":
Left: [[A,B],[X,Y]]
Right: [[D],[F,E]]
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
vars_in(Xs, From, To) :-
Bitvec is (1<<(To+1)) - (1<<From),
vars_in_(Xs, Bitvec).
vars_in(Xs, Dom) :-
domain_bitvector(Dom, 0, Bitvec),
vars_in_(Xs, Bitvec).
vars_in_([], _).
vars_in_([V|Vs], Bitvec) :-
( var(V) ->
( get_attr(V, clp_distinct, dom_neq(VBV,VLeft,VRight)) ->
Bitvec1 is VBV /\ Bitvec,
Bitvec1 =\= 0,
( popcount(Bitvec1) =:= 1 ->
V is msb(Bitvec1)
;
put_attr(V, clp_distinct, dom_neq(Bitvec1,VLeft,VRight))
)
;
( popcount(Bitvec) =:= 1 ->
V is msb(Bitvec)
;
put_attr(V, clp_distinct, dom_neq(Bitvec, [], []))
)
)
;
0 =\= Bitvec /\ (1<<V)
),
vars_in_(Vs, Bitvec).
domain_bitvector([], Bitvec, Bitvec).
domain_bitvector([D|Ds], Bitvec0, Bitvec) :-
Bitvec1 is Bitvec0 \/ (1 << D),
domain_bitvector(Ds, Bitvec1, Bitvec).
all_distinct(Ls) :-
all_distinct(Ls, []),
outof_reducer(Ls).
outof_reducer([]).
outof_reducer([X|Xs]) :-
( var(X) ->
get_attr(X, clp_distinct, dom_neq(Dom,Lefts,Rights)),
outof_reducer(Lefts, Rights, X, Dom)
;
true
),
outof_reducer(Xs).
all_distinct([], _).
all_distinct([X|Right], Left) :-
\+ list_contains(Right, X),
outof(X, Left, Right),
all_distinct(Right, [X|Left]).
outof(X, Left, Right) :-
( var(X) ->
get_attr(X, clp_distinct, dom_neq(Dom, XLefts, XRights)),
put_attr(X, clp_distinct, dom_neq(Dom, [Left|XLefts], [Right|XRights]))
;
exclude_fire([Left], [Right], X)
).
exclude_fire(Lefts, Rights, E) :-
Mask is \ ( 1 << E),
exclude_fire(Lefts, Rights, E, Mask).
exclude_fire([], [], _, _).
exclude_fire([Left|Ls], [Right|Rs], E, Mask) :-
exclude_list(Left, E, Mask),
exclude_list(Right, E, Mask),
exclude_fire(Ls, Rs, E, Mask).
exclude_list([], _, _).
exclude_list([V|Vs], Val, Mask) :-
( var(V) ->
get_attr(V, clp_distinct, dom_neq(VDom0,VLefts,VRights)),
VDom1 is VDom0 /\ Mask,
VDom1 =\= 0,
( popcount(VDom1) =:= 1 ->
V is msb(VDom1)
;
put_attr(V, clp_distinct, dom_neq(VDom1,VLefts,VRights))
)
;
V =\= Val
),
exclude_list(Vs, Val, Mask).
attr_unify_hook(dom_neq(Dom,Lefts,Rights), Y) :-
( ground(Y) ->
Dom /\ (1 << Y) =\= 0,
exclude_fire(Lefts, Rights, Y)
;
\+ lists_contain(Lefts, Y),
\+ lists_contain(Rights, Y),
( get_attr(Y, clp_distinct, dom_neq(YDom0,YLefts0,YRights0)) ->
YDom1 is YDom0 /\ Dom,
YDom1 =\= 0,
( popcount(YDom1) =:= 1 ->
Y is msb(YDom1)
;
append(YLefts0, Lefts, YLefts1),
append(YRights0, Rights, YRights1),
put_attr(Y, clp_distinct, dom_neq(YDom1,YLefts1,YRights1))
)
;
put_attr(Y, clp_distinct, dom_neq(Dom,Lefts,Rights))
)
).
lists_contain([X|Xs], Y) :-
( list_contains(X, Y) ->
true
;
lists_contain(Xs, Y)
).
list_contains([X|Xs], Y) :-
( X == Y ->
true
;
list_contains(Xs, Y)
).
outof_reducer([], [], _, _).
outof_reducer([L|Ls], [R|Rs], Var, Dom) :-
append(L, R, Others),
N is popcount(Dom),
num_subsets(Others, Dom, 0, Num),
( Num >= N ->
fail
; Num =:= (N - 1) ->
reduce_from_others(Others, Dom)
;
true
),
outof_reducer(Ls, Rs, Var, Dom).
reduce_from_others([], _).
reduce_from_others([X|Xs], Dom) :-
( var(X) ->
get_attr(X, clp_distinct, dom_neq(XDom,XLeft,XRight)),
( is_subset(Dom, XDom) ->
true
;
NXDom is XDom /\ \Dom,
NXDom =\= 0,
( popcount(NXDom) =:= 1 ->
X is msb(NXDom)
;
put_attr(X, clp_distinct, dom_neq(NXDom,XLeft,XRight))
)
)
;
true
),
reduce_from_others(Xs, Dom).
num_subsets([], _Dom, Num, Num).
num_subsets([S|Ss], Dom, Num0, Num) :-
( var(S) ->
get_attr(S, clp_distinct, dom_neq(SDom,_,_)),
( is_subset(Dom, SDom) ->
Num1 is Num0 + 1
;
Num1 = Num0
)
;
Num1 = Num0
),
num_subsets(Ss, Dom, Num1, Num).
% true iff S is a subset of Dom - should be a GMP binding (subsumption)
is_subset(Dom, S) :-
S \/ Dom =:= Dom.
attr_portray_hook(dom_neq(Dom,_,_), _) :-
Max is msb(Dom),
Min is lsb(Dom),
write(Min-Max).
|