/* Global constraint max_index (and the extension max_index_val) in Picat. From Global Constraint Catalogue http://www.emn.fr/x-info/sdemasse/gccat/Cmax_index.html """ max_index(MAX_INDEX,VARIABLES) Purpose MAX_INDEX is the index of the variables corresponding to the maximum value of the collection of variables VARIABLES. Example ( 3, < index-1 var-3, index-2 var-2, index-3 var-7, index-4 var-2, index-5 var-6 > ) The attribute var=7 of the third item of the collection VARIABLES is the maximum value over values 3,2,7,2,6. Consequently, the max_index constraint holds since its first argument MAX_INDEX is set to 3. """ This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import cp. main => go. go ?=> N = 5, Variables = new_list(N), Variables :: 1..7, MaxInd :: 1..N, MaxVal :: 1..7, % Variables = [3,2,7,2,6], MaxInd #= 3, MaxVal #= 7, % max_index(MaxInd, Variables), max_index_val(MaxInd, Variables, MaxVal), Vars = Variables ++ [MaxInd,MaxVal], solve(Vars), println(variables=Variables), println(maxInd=MaxInd), println(maxVal=MaxVal), nl, fail, nl. go => true. % I assume that the indices are ordered and unique max_index(MI, X) => sum([X[I] #= max(X) #/\ MI #= I : I in 1..X.len]) #>= 1. % % An extension of max_index: also returns the maximum value % max_index_val(MI, X, Val) => sum([X[I] #= max(X) #/\ MI #= I #/\ Val #= X[I] : I in 1..X.len]) #>= 1.