arrays - Minizinc 阵列套装

标签 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)];

最佳答案

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

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

关于arrays - Minizinc 阵列套装,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44644128/

相关文章:

javascript - 在 JavaScript 中解析一个 JSON 对象生成一个数组

java - 将 int 转换为字节数组并使用 System.out.write 方法打印到控制台

java - 如何将集合排序为列表

constraint-programming - MiniZinc 中的基数约束

java - 查找数组中的重复值

javascript - JS 按特定排序顺序排序

Java 分配接口(interface)集

php - mysql查询INSERT INTO和SET问题

logic - 约束编程:按照模式规则用颜色填充网格

multidimensional-array - Minizinc:用一维数组初始化二维数组的每一行