2

在我的 Minizinc 项目中,我试图生成一个包含 n 个集合的数组。给定一个包含 t 个不同数字的数组,生成 n 个不同的集合,其
基数在数组 m 中给出。例如:t = 10;n = 4; 和 m = [3, 2, 2, 3];我想生成一组集合 x = [1..3, 4..5, 6..7, 8..10];

但是我从下面的代码中得到的是 x = [1..3, 4..5, {6,10}, 7..9]; (我不想使用求解最小化或其他各种求解,因为我的
目的只是生成一个中间集数组。)

int: n = 4;                 % number of groups
array[1..n] of int: m = [3, 2, 2, 3];  % size of each group
int: t = sum(i in 1..n)(m[i]); % total members

array[1..n] of var set of  1..t: x; % the array of sets
constraint forall(i in 1..n-1)(x[i] >  x[i+1]); % SORT .
constraint forall(i in 1..n)(card(x[i] ) = m[i]); % Size of each set
constraint forall(i in 1..n-1)( x[i] intersect x[i+1] = {}); %
% I can't see a way to keep the digits in order
%constraint array_intersect(x) = {}; % this didn't help

solve satisfy;
output [show(x)];
4

1 回答 1

2

您可以不受限制地做到这一点。这是一种方法,虽然有点难看:

int: n = 4; % number of sets
array[1..n] of int: s = [3,2,2,3]; % cardinality of the sets
array[1..n] of set of int: x = [ {k | k in sum([s[j]  | j in 1..i-1])+1..sum([s[j]  | j in 1..i]) } | i in 1..n];

solve satisfy;
constraint  true  ; % just used to run the model
output [  "x: \(x)\n"];
于 2017-06-20T05:29:38.707 回答