给定一组不同大小的集合,生成一组集合,这些集合是给定集合的组合。尝试了各种理解安排。错误消息有时是“不是整数表达式”
int: n = 4; % number of groups
set of int: Grp = 1..n;
array[Grp] of int: m = [3,2,2,3]; % sizes of groups
int: t = sum(i in Grp)(m[i]); % Total number of members
set of int: PRSN = 1..t; % a unique id for each member
% An array of sets of groups.
array[1..n] of set of int: GrpSets = [{1,2,3}, {4,5}, {6,7}, {8,9,10}];
% COMBINED GROUPS
int: r = 2; % number of combines
set of int: Cmb = 1..r;
array[Cmb] of Grp: cg1 = [1,2];
array[Cmb] of Grp: cg2 = [3,4]; % gc1[1] to be combined with gc2[1] ...
% generate array of combined sets. Both versions fails.
%array[PRSN] of set of int: CmbSets = [ {GrpSets[cg1[k]] union GrpSets[cg2[k]]}| k in 1..r];
array[PRSN] of set of int: CmbSets = [{GrpSets[cg1[1]] union GrpSets[cg2[1]]}, {GrpSets[cg1[2]] union GrpSets[cg2[2]]}];
% Expected outcome CmbSets = [{1,2,3,6,7}, {4,5,8,9,10}]
solve satisfy;
output ["CmbSets = ", show(CmbSets),
";\n" ];
答案 0 :(得分:1)
您对CmbSets
的两个定义存在一些问题:
array[PRSN] of set of int: CmbSets = [ {GrpSets[cg1[k]] union GrpSets[cg2[k]]}| k in 1..r];
array[PRSN] of set of int: CmbSets = [{GrpSets[cg1[1]] union GrpSets[cg2[1]]}, {GrpSets[cg1[2]] union GrpSets[cg2[2]]}];
1)集合PRSN
应为1..2
(不是1..10
),因为数组CmbSets
中只有两个元素。也许你的意思是1..r
?
2)使用union
时,不应包含{...}
的表达式。这就是为什么你得到 - 有点不清楚 - 错误消息"not an integer expression"
以下是两种工作变体:
array[1..r] of set of int: CmbSets = [ GrpSets[cg1[k]] union GrpSets[cg2[k]]| k in 1..r];
和
array[1..r] of set of int: CmbSets = [GrpSets[cg1[1]] union GrpSets[cg2[1]], GrpSets[cg1[2]] union GrpSets[cg2[2]]];