I would like to shorten the following program. Just imaging there are tens of variables instead of just X
and Y
. The problem is that I need to define domain for each variable separately. I don't like it because it makes my program much longer and less transparent.
Input:
?- Dom1 in 0..2, Dom2 in 0..2, global_cardinality([X,Y], [0-Dom1,1-Dom2]), labeling([],[X,Y]).
Results:
X = 0,
Y = 0,
Dom1 = 2,
Dom2 = 0 ? ;
X = 0,
Y = 1,
Dom1 = 1,
Dom2 = 1 ? ;
X = 1,
Y = 0,
Dom1 = 1,
Dom2 = 1 ? ;
X = 1,
Y = 1,
Dom1 = 0,
Dom2 = 2 ? ;
no
At first I thought that I would solve it simply by writing:
?- Dom1 in 0..2, global_cardinality([X,Y], [0-Dom1,1-Dom1]), labeling([],[X,Y]).
but it does not work because Dom1 unifies (is this the proper term for what happens in clpfd?) with one value and therefore the only results are:
X = 0,
Y = 1,
Dom1 = 1 ? ;
X = 1,
Y = 0,
Dom1 = 1 ? ;
no
Thanks!
Suppose that L = [X1,...,Xn] and you want every variable to be in 1..10.
Alternative 1, works for intervals only:
?- domain(L, 1, 10).
Alternative 2, works for domains that are not intervals too:
?- (foreach(X,L) do X in 1..10).