迷你锌阵列集



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

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

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

最新更新