Aaysha
Aaysha

Reputation: 23

Minizinc - Constraint that checks element match

If I have a 2D decision variable array and a 1D array of set, how do I add constraint to check that values in decision variables should match from 1D array?

For example,

array[1..3, 1..2] of var 1..10: x;
array[1..3] of set of 1..10: y;

And y is = [{2, 6, 7, 8},
 {1, 2, 3},
 {1, 2, 7, 8, 1, 5, 1},
];

I want to add constraint that ensures that x keeps only values contained in y. For example, the rule is that values of x0, x1 should be one among first set of values of y {2, 6, 7, 8}. Values of x2, x3 should be amonst the second set of values of y {1, 2, 3} and so on.

Thanks

Upvotes: 1

Views: 183

Answers (1)

hakank
hakank

Reputation: 6854

If y is defined as parameter (i.e. not defined with var) as in your example, then you can extract the values of y as a set (vals) like this:

array[1..3] of set of 1..10: y = [{2, 6, 7, 8},
 {1, 2, 3},
 {1, 2, 7, 8, 1, 5, 1},
];

% convert the values 
set of int: vals = { val | row in y, val in row  };
        
array[1..3, 1..2] of var vals : x;

output [
  "vals:\(vals)\nx:\(x)\n"
];

Update: Here's a suggestion for the updated version of the question. Here we constrain the x variables in the i'th to values be only the i'th row in y, i.e. the i'th set in y. As before I assume that the values in y are plain integers (not decision variables):

array[1..3] of set of 1..10: y = [{2, 6, 7, 8},
 {1, 2, 3},
 {1, 2, 7, 8, 1, 5, 1},
];

array[1..3, 1..2] of var int : x;

% Ensure that the i'th row in x only contains values 
% that are in the i'th row/set of y
constraint
  forall(i in 1..3) (
     forall(j in 1..2) (
        x[i,j] in y[i]
     )
  )
;

output [
"x:\(x)\n",
];


Upvotes: 1

Related Questions