-1

I have an 2D array like that :

array[1..3,1..3] of var 1..3: t;

how can I write a constraint to check if there is a table row that matches |1,2,3| (for example) ?

I wrote it like that but it doesn't work that return a type error :

constraint [1,2,3] in t;

1 Answers1

2

Here is one way to check that [1,2,3] is in t:

array[1..3,1..3] of var 1..3: t;
var bool: check;

constraint
  exists(i in 1..3) (
    t[i,..] == [1,2,3] 
  ) <-> check
;

Some solutions:

...
t = 
[| 3, 2, 3
 | 3, 3, 3
 | 3, 3, 3
 |];
check = false;
----------
t = 
[| 1, 2, 3
 | 3, 3, 3
 | 3, 3, 3
 |];
check = true;
----------
t = 
[| 2, 1, 2
 | 3, 3, 3
 | 3, 3, 3
 |];
check = false;
----------
...

If you want to ensure that [1,2,3] is in t, you can skip the check variable:

array[1..3,1..3] of var 1..3: t;
constraint
  exists(i in 1..3) (
    t[i,..] == [1,2,3] 
  )
;

Some solutions:

t = 
[| 1, 2, 3
 | 3, 3, 3
 | 3, 3, 3
 |];
----------
t = 
[| 1, 2, 3
 | 2, 3, 3
 | 3, 3, 3
 |];
----------
t = 
[| 1, 2, 3
 | 1, 3, 3
 | 3, 3, 3
 |];
----------
...
hakank
  • 6,629
  • 1
  • 17
  • 27