this repo has no description
at develop 1.9 kB view raw
1int: n; 2set of int: PERSON = 1..n; 3enum GENDER = { M, F, O }; 4array[PERSON] of GENDER: g; 5set of int: POSN = 1..n; 6array[int] of int: pos; 7 8array[POSN] of var PERSON: who; 9int: _objective; 10 11test alldifferent(array[int] of int: x) = 12 forall(i,j in index_set(x) where i < j)(x[i] != x[j]); 13 14include "inverse.mzn"; 15constraint if alldifferent(pos) then inverse(pos,who) 16 else forall(i in 1..n)(who[i] = 1) endif; 17 18output [if 19 check_array(pos, n, POSN, "pos") /\ 20 check_alldifferent(pos,"pos") /\ 21 check_array(fix(pos), n, PERSON, "who") /\ 22 forall(i in 1..n-2) 23 (check(g[fix(who[i])] != g[fix(who[i+1])] \/ 24 g[fix(who[i+1])] != g[fix(who[i+2])], 25 "three people of the same gender \(g[fix(who[i])]) in positions \(i)..\(i+2)\n")) /\ 26 let { int: obj = sum(i in 1..n-1)(abs(pos[i] - pos[i+1])); } in 27 check(obj = _objective, "calculated objective \(obj) does not agree with objective from the model (\(_objective))\n") 28 then 29 "CORRECT: All constraints hold" 30 else 31 "INCORRECT" 32 endif]; 33 34test check(bool: b,string: s) = 35 if b then true else trace_stdout("ERROR: "++s++"\n",false) endif; 36 37test check_alldifferent(array[int] of int: x, string: name) = 38 forall(i, j in index_set(x) where i < j) 39 (check(x[i] != x[j], name ++ "[\(i)] = \(x[i]) = " ++ 40 name ++ "[\(j)] " ++ 41 "when they should be different\n")); 42 43test check_int(int: x, set of int: vals, string: name) = 44 check(x in vals, "integer \(name) is not in values \(vals)\n"); 45 46function bool: check_array(array[int] of int: x, int: length, set of int: vals, string: name) = 47 check(length(x) = length, "array \(name) is not of length \(length)\n") /\ 48 forall(i in index_set(x)) 49 (check(x[i] in vals, "element \(i) of array \(name), \(x[i]) is not in values \(vals)\n"));