/* argmax/argmin constraints in Picat. argmax_ge(pos, x): pos is the index x for the maximum value(s) there can be many maximum values argmax_gt(pos, x): pos is the index x for the maximum value there can be only one maximum value argmin_le(pos, x): pos is the index x for the minimum value(s) there can be many minumum values argmin_lt(pos, x): pos is the index x for the minimum value there can be only one maximum value 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, X = new_list(N), X :: 1..N, PosMax :: 1..N, PosMin :: 1..N, all_different(X), argmax_gt(PosMax,X), argmin_lt(PosMin,X), PosMin #> PosMax, Vars = X ++ [PosMax,PosMin], solve(Vars), println(x=X), println(posMax=PosMax), println(posMin=PosMin), nl, fail, nl. go => true. % p is the position of the maximum element argmax_gt(P, X) => foreach(I in 1..X.len) element(P,X,XP), P #!= I #=> (XP #> X[I]) end. % p is the position(s) of the maximum element(s) argmax_ge(P, X) => foreach(I in 1..X.len) element(P,X,XP), XP #>= X[I] end. % p is the position of the minimum element argmin_lt(P, X) => foreach(I in 1..X.len) element(P,X,XP), P #!= I #=> (XP #< X[I]) end. % p is the position(s) of the minimum element(s) argmin_le(P, X) => foreach(I in 1..X.len) element(P,X,XP), XP #<= X[I] end.