/* Global constraint elements in Picat. From Global Constraint Catalogue http://www.emn.fr/x-info/sdemasse/gccat/Celements.html """ Constraint elements(ITEMS,TABLE) Restriction(s) required(ITEMS,[index,value]) ITEMS.index>=1 ITEMS.index<=|TABLE| required(TABLE,[index,value]) TABLE.index>=1 TABLE.index<=|TABLE| distinct(TABLE,index) Purpose All the items of ITEMS should be equal to one of the entries of the table TABLE. Example ( , < index-1 value-6, index-2 value-9, index-3 value-2, index-4 value-9 > ) The elements constraint holds since each item of its first argument ITEMS corresponds to an item of the TABLE collection: the first item of ITEMS corresponds to the fourth item of TABLE, while the second item of ITEMS corresponds to the first item of TABLE. Usage Used for replacing several element constraints sharing exactly the same table by one single constraint. """ 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, % length of table M = 2,% length of items ElementTable = new_list(N), ElementTable :: 1..9, Items = new_list(M), Items :: 1..9, ElementTable = [6,9,2,9], Items = [9,6], % Items = [9,5], % false elements(Items, ElementTable), Vars = ElementTable ++ Items, solve(Vars), println(elementTable=ElementTable), println(items=Items), nl, fail, nl. go => true. elements(Items, ElementTable) => foreach(I in 1..Items.len) sum([Items[I] #= ElementTable[J] : J in 1..ElementTable.len]) #>= 1 end.