/* Global constraint cond_lex_cost in Picat. From Global Constraint Catalogue http://www.emn.fr/x-info/sdemasse/gccat/Ccond_lex_cost.html """ cond_lex_cost(VECTOR, PREFERENCE_TABLE, COST) Purpose VECTOR is assigned to the COSTth item of the collection PREFERENCE_TABLE. Example ( <0, 1>, < tuple-〈1, 0>, tuple-〈0, 1>, tuple-〈0, 0>, tuple-〈1, 1> >, 2 ) The cond_lex_cost constraint holds since VECTOR is assigned to the second item of the collection PREFERENCE_TABLE. """ 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, M = 2, Arr = new_array(N,M), Arr :: 0..1, V1 = new_list(M), V1 :: 0..1, Pos :: 1..N, % V1 = [0,1], Arr = {{1,0}, {0,1}, {0,0}, {1,1}}, cond_lex_cost(V1, Arr, Pos), Vars = Arr.vars ++ V1 ++ [Pos], solve(Vars), println(arr=Arr), println(v1=V1), println(pos=Pos), nl, fail, nl. go => true. % % Instead of COST, I call the third parameter Ix (position). % cond_lex_cost(V, A, Ix) => member(I,1..A.len), Ix #= I, foreach(J in 1..V.len) A[I,J] #= V[J] end.