Minizinc数组

时间:2017-06-20 04:58:13

标签: arrays set minizinc

在我的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)];

1 个答案:

答案 0 :(得分:1)

您可以不受限制地执行此操作。这是一种方法,虽然有点难看:

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"];