Debian Codegraph @

tests/spec/unit/compilation/test_nested_comp.mzn

raw
1 /***
2 !Test
3 solvers: [gecode]
4 expected: !Result
5 solution: !Solution
6 A: [[[0], [0,], [0]], [[3], [3], [3]], [[0], [0], [0]]]
7 T: [[18], [30], [26], [25], [24], [29], [44], [21], [30], [39]]
8 ***/
9
10 % Use this editor as a MiniZinc scratch book
11 % Nested comprehensions + nested let expressions stress test
12 % ----------------------------------------------------------
13 % This model constructs a 3D array using nested comprehensions,
14 % with inner arrays defined via let-in, and then aggregates them.
15
16 int: N = 3;
17 set of int: IDX = 1..N;
18
19 % Some base data to weave through nested lets/comprehensions.
20 array[IDX] of int: base = [2, 3, 5];
21
22 % A 2D array built by a comprehension that uses a nested let,
23 % builds an inner array via another comprehension, and folds it.
24 any: A ::output =
25 array2d(IDX, IDX,
26 [
27 let {
28 int: t = i * j,
29 int: u = sum(k in 1..j) (k), % triangular-ish helper
30 any: helper = % inner array via comp.
31 [ (t + u + base[k],) | k in IDX where (i + k) mod 2 = 1 ]
32 } in
33 % Mix min/max over the inner array (force evaluation of 'helper')
34 (max([h.1 | h in helper]) - min([h.1 | h in helper]),)
35 | i in IDX, j in IDX
36 ]
37 );
38
39 % A 3D array that nests comprehensions deeper and uses multiple lets.
40 any: T ::output = (
41 % array3d(IDX, IDX, IDX,
42 [
43 let {
44 int: s = i + j + k,
45 int: cross = i * j + j * k + k * i,
46 any: inner1 =
47 [ let {
48 int: a = i * j,
49 int: b = j * k
50 } in (a + b + p,)
51 | p in base
52 ],
53 any: inner2 =
54 [ ((inner1[q].1 + s + base[q]) mod 7,)
55 | q in index_set(inner1) % refer to inner array's index set
56 ]
57 } in
58 % Combine both inner arrays; also reference A to force 2D->3D interplay.
59 (s + cross + sum(q in index_set(inner2))(inner2[q].1) + A[i, ((j + k - 1) mod N) + 1].1,)
60 | i in IDX, j in IDX where i != j, k in IDX where (i + j + k) mod 2 = 0
61 ]
62 );