/* Global constraint element_matrix in Picat. From Global Constraint Catalogue http://www.emn.fr/x-info/sdemasse/gccat/Celement_matrix.html """ Constraint element_matrix(MAX_I,MAX_J,INDEX_I,INDEX_J,MATRIX,VALUE) Purpose The MATRIX collection corresponds to the two-dimensional matrix MATRIX[1..MAX_I,1..MAX_J]. VALUE is equal to the entry MATRIX[INDEX_I,INDEX_J] of the p revious matrix. Example ( 4,3,1,3,< i-1 j-1 v-4, i-1 j-2 v-1, i-1 j-3 v-7, i-2 j-1 v-1, i-2 j-2 v-0, i-2 j-3 v-8, i-3 j-1 v-3, i-3 j-2 v-2, i-3 j-3 v-1, i-4 j-1 v-0, i-4 j-2 v-0, i-4 j-3 v-6 >,7 ) The element_matrix constraint holds since its last argument VALUE=7 is equal to the v attribute of the kth item of the MATRIX collection such that MATRIX[k].i=INDEX_I=1 and MATRIX[k].j=INDEX_J=3. """ Note that Picat has the built-in matrix_element(Matrix,I,J,Value) which is used here. 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 ?=> MaxI = 4, MaxJ= 3, IndexI :: 1..MaxI, IndexJ :: 1..MaxJ, Matrix = new_array(MaxI,MaxJ), Matrix :: 0..9, Value :: 0..8, Matrix = {{4,1,7}, {1,0,8}, {3,2,1}, {0,0,6}}, % IndexI #= 1, % IndexJ #= 3, % Value #= 7, element_matrix(IndexI,IndexJ, Matrix, Value), Vars = Matrix.vars ++ [IndexI,IndexJ], solve(Vars), println(matrix=Matrix), println([indexI=IndexI,indexJ=IndexJ,value=Value]), nl, fail, nl. go => true. element_matrix(IndexI, IndexJ, Matrix,Value) => % Matrix[IndexI,IndexJ] #= Value. matrix_element(Matrix,IndexI,IndexJ,Value).