Extract column as set from a matrix

535 views Asked by At

In MiniZinc, I need to extract a column from a matrix, in order to have a set composed by the values of the column. For example if I have a table like that:

1 0 0
6 4 6
3 5 8

I would like to extract the set of int 1 6 3, in order to have an index that can flow in this set. Is there a command to do this?

1

There are 1 answers

0
hakank On BEST ANSWER

You can use the col() function in combination with array2set(). Here's a simple model using a fixed matrix "a":

int n: 3;
array[int,int] of int: a = 
    array2d(1..n,1..n,
         [
           1, 0, 0,
           6, 4, 6,
           3, 5, 8,
    ]);

set of int: s1 = array2set(col(a,1));

solve satisfy;
constraint true;
output [
       "col(a,1): \(col(a,1))\n",
       "s1: \(s1)\n"
];

The output is then the set "{1,3,6}".