% % Global constraint ith pos different from 0 in MiniZinc. % % From Global Constraint Catalogue % http://www.emn.fr/x-info/sdemasse/gccat/sec4.151.html % """ % ith_pos_different_from_0​(ITH,​POS,​VARIABLES) % % Purpose % POS is the position of the ITHth non-zero item of the sequence of variables % VARIABLES. % % Example % ​(2,​4,​〈3,​0,​0,​8,​6〉)​ % % The ith_pos_different_from_0 constraint holds since 4 corresponds to the % position of the 2th non-zero item of the sequence 3 0 0 8 6. % % """ % This MiniZinc model was created by Hakan Kjellerstrand, hakank@bonetmail.com % See also my MiniZinc page: http://www.hakank.org/minizinc % % include "globals.mzn"; int: n = 5; array[1..n] of var 0..8: x; var 1..n: ith; var 1..n: pos; predicate ith_pos_different_from_0(var int: ith, var int: pos, array[int] of var int: x) = exists(i in 1..length(x)) ( pos = i /\ ith = sum(j in 1..i) (bool2int(x[j] != 0)) /\ x[i] != 0 ) ; solve satisfy; constraint x = [3,0,0,8,6] /\ pos = 4 /\ ith = 2 /\ ith_pos_different_from_0(ith, pos, x) ;