|
34 | 34 | % |
35 | 35 | % |
36 | 36 |
|
37 | | -predicate table_int(array[int] of var int: x, array[int, int] of int: t); |
| 37 | +predicate gecode_table_int(array[int] of var int: x, array[int, int] of int: t); |
| 38 | + |
| 39 | +predicate table_int(array[int] of var int: x, array[int, int] of int: t) = |
| 40 | + assert (index_set_2of2(t) == index_set(x), |
| 41 | + "The second dimension of the table must equal the number of variables " |
| 42 | + ++ "in the first argument", |
| 43 | + gecode_table_int(x,t); |
| 44 | + |
| 45 | +predicate table_int_reif(array[int] of var int: x, array[int, int] of int: t, |
| 46 | + var bool: b) = |
| 47 | + |
| 48 | + let { int: n_vars = length(x) } |
| 49 | + in |
| 50 | + |
| 51 | + assert(n_vars in 1..5, |
| 52 | + "'table' constraints in a reified context " ++ |
| 53 | + "are only supported for 1..5 variables.", |
| 54 | + |
| 55 | + if n_vars = 1 then |
| 56 | + |
| 57 | + x[1] in { t[it,1] | it in index_set_1of2(t) } <-> b |
| 58 | + |
| 59 | + else |
| 60 | + |
| 61 | + let { set of int: ix = index_set(x), |
| 62 | + set of int: full_size = 1..product(i in ix)( dom_size(x[i]) ), |
| 63 | + array[full_size, 1..n_vars + 1] of int: t_b = |
| 64 | + array2d(full_size, 1..n_vars + 1, |
| 65 | + |
| 66 | + if n_vars = 2 then |
| 67 | + |
| 68 | + [ let { array[ix] of int: tpl = [i1,i2] } in |
| 69 | + (tpl ++ [bool2int(aux_is_in_table(tpl,t))])[p] |
| 70 | + | i1 in dom(x[1]), |
| 71 | + i2 in dom(x[2]), |
| 72 | + p in 1..n_vars + 1 ] |
| 73 | + |
| 74 | + else if n_vars = 3 then |
| 75 | + |
| 76 | + [ let { array[ix] of int: tpl = [i1,i2,i3] } in |
| 77 | + (tpl ++ [bool2int(aux_is_in_table(tpl,t))])[p] |
| 78 | + | i1 in dom(x[1]), |
| 79 | + i2 in dom(x[2]), |
| 80 | + i3 in dom(x[3]), |
| 81 | + p in 1..n_vars + 1 ] |
| 82 | + |
| 83 | + else if n_vars = 4 then |
| 84 | + |
| 85 | + [ let { array[ix] of int: tpl = [i1,i2,i3,i4] } |
| 86 | + in |
| 87 | + (tpl ++ [bool2int(aux_is_in_table(tpl,t))])[p] |
| 88 | + | i1 in dom(x[1]), |
| 89 | + i2 in dom(x[2]), |
| 90 | + i3 in dom(x[3]), |
| 91 | + i4 in dom(x[4]), |
| 92 | + p in 1..n_vars + 1 ] |
| 93 | + |
| 94 | + else % if n_vars = 5 then |
| 95 | + |
| 96 | + [ let { array[ix] of int: tpl = [i1,i2,i3,i4,i5] } in |
| 97 | + (tpl ++ [bool2int(aux_is_in_table(tpl,t))])[p] |
| 98 | + | i1 in dom(x[1]), |
| 99 | + i2 in dom(x[2]), |
| 100 | + i3 in dom(x[3]), |
| 101 | + i4 in dom(x[4]), |
| 102 | + i5 in dom(x[5]), |
| 103 | + p in 1..n_vars + 1 ] |
| 104 | + |
| 105 | + endif endif endif ) } |
| 106 | + in |
| 107 | + table_int(x ++ [bool2int(b)], t_b) |
| 108 | + |
| 109 | + endif |
| 110 | + ); |
| 111 | + |
| 112 | +test aux_is_in_table(array[int] of int: e, array[int, int] of int: t) = |
| 113 | + exists(i in index_set_1of2(t))( |
| 114 | + forall(j in index_set(e))( t[i,j] = e[j] ) |
| 115 | + ); |
0 commit comments