/* Global constraint next_greater_element in Picat. From Global Constraint Catalogue http://www.emn.fr/x-info/sdemasse/gccat/Cnext_greater_element.html """ Constraint next_greater_element(VAR1,VAR2,VARIABLES) Purpose VAR2 is the value strictly greater than VAR1 located at the smallest possible entry of the table TABLE. In addition, the variables of the collection VARIABLES are sorted in strictly increasing order. Example (7, 8, <3, 5, 8, 9>) The next_greater_element constraint holds since: * VAR2 is fixed to the first value 8 strictly greater than VAR1=7, * The var attributes of the items of the collection VARIABLES are sorted in strictly increasing order. Usage Originally introduced for modelling the fact that a nucleotide has to be consumed as soon as possible at cycle VAR2 after a given cycle VAR1. Remark Similar to the minimum_greater_than constraint, except for the fact that the var attributes are sorted. """ 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 = 4, Variables = new_list(N), Variables :: 1..9, [MinDom,MaxDom] = fd_min_max_array(Variables), Var1 :: MinDom..MaxDom, Var2 :: MinDom..MaxDom, Variables = [3,5,8,9], Var1 #= 7, % Var2 #= 8, next_greater_element(Var1,Var2,Variables), Vars = Variables ++ [Var1,Var2], solve(Vars), println(variables=Variables), println(var1=Var1), println(var2=Var2), nl, fail, nl. go => true. next_greater_element(Var1,Var2,Variables) => Len = Variables.len, increasing_strict(Variables), sum([ Variables[I] #> Var1 #/\ Variables[I] #= Var2 #/\ % all values before I must be less than Var1, i.e. % Var2 is the smallest possible entry sum([Variables[J] #< Var1 : J in 1..I-1]) #= I-1 : I in 1..Len] ) #= 1. % % Maximum/minimum domain value in array X % fd_max_array(X) = max([fd_max(V) : V in X]). fd_min_array(X) = min([fd_min(V) : V in X]). fd_min_max_array(X) = [min([fd_min(V) : V in X]), max([fd_max(V) : V in X])].