, MiniZinc:
int: noOfCollections = 3;
int: noOfDisjoints = 7;
int: noOfSquares = noOfCollections * noOfDisjoints;
set of int: Squares = 1..noOfSquares;
int: maxDim = 10000; % somewhat arbitrary limit!
int: maxLeft = maxDim;
int: maxRight = maxDim;
int: maxTop = maxDim;
int: maxBottom = maxDim;
int: maxHeight = maxBottom - 1;
int: maxWidth = maxRight - 1;
array[Squares] of var 1..maxLeft: Left;
array[Squares] of var 1..maxTop: Top;
array[Squares] of var 1..maxHeight: Height;
array[Squares] of var 1..maxWidth: Width;
array[Squares] of var bool: Representative;
array[Squares] of 1..noOfCollections:
Collection = [1 + (s mod noOfCollections) | s in Squares];
% Squares must fit in the overall frame
constraint
forall(s in Squares)(
(Left[s] + Width[s] - 1 <= maxRight) /\
(Top[s] + Height[s] - 1 <= maxBottom)
);
predicate disjoint(var int: s1, var int: s2) =
(Left[s1] + Width[s1] - 1 < Left[s2]) \/
(Left[s2] + Width[s2] - 1 < Left[s1]) \/
(Top[s1] + Height[s1] - 1 < Top[s2]) \/
(Top[s2] + Height[s2] - 1 < Top[s1]);
% Squares in a collection must be disjoint
constraint
forall(s1 in Squares, s2 in Squares
where (s1 > s2) /\ (Collection[s1] == Collection[s2]))(
disjoint(s1, s2)
);
% Exactly one Representative per Collection
constraint
1 == sum(c in 1..noOfCollections, s in Squares
where c == 1 + (s mod noOfCollections))
(bool2int(Representative[s]));
% Is it possible to select 1 square from each collection such
% that the 3 representatives are interior disjoint?
constraint
forall(s1 in Squares, s2 in Squares, s3 in Squares
where (Collection[s1] == 1) /\
(Collection[s2] == 2) /\
(Collection[s3] == 3))(
disjoint(s1, s2) /\
disjoint(s1, s3) /\
disjoint(s2, s3) /\
Representative[s1] /\
Representative[s2] /\
Representative[s3]
);
solve satisfy;
MiniZinc "UNSAT" 45 .