Search code examples
arrayssetminizinc

Minizinc array of sets combining


Given an array of sets not of the same size, generate an array of sets which are combined pairs of the given sets. Tried various comprehension arrangements. The error message sometimes is "not an integer expression"

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

Solution

  • There is a couple of problems with your two definitions of 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) The set PRSN should be 1..2 (not 1..10) since there are only two elements in the array CmbSets. Perhaps you meant 1..r?

    2) When using union you should not embrace the expressions with {...}. This is why you get the - somewhat unclear - error message "not an integer expression"

    Here are two working variants:

    array[1..r] of set of int: CmbSets = [ GrpSets[cg1[k]] union GrpSets[cg2[k]]| k in 1..r];
    

    and

    array[1..r] of set of int: CmbSets = [GrpSets[cg1[1]] union GrpSets[cg2[1]], GrpSets[cg1[2]] union GrpSets[cg2[2]]];