/* Fairies problem in Picat. From Martin J. Chlond: Puzzle—O.R. with the Fairies Informs, Transactions on Education Volume 8, Issue 2 January 2008 http://www.informs.org/site/ITE/article.php?id=58 PDF: http://www.informs.org/site/ITE/downloadfile.php?i=3416a75f4cea9109507cacd8e2f2aefc The original MathProg model had the following note: Model name : fairies.mod Description : Color Fairies puzzle Source : Puzzling Adventures, Dennis E. Shasha Date written : 21/12/06 Written by : Martin Chlond, Lancashire Business School Email : mchlond@uclan.ac.uk 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 ?=> F = 5, % fairy C = 9, % color H = 3, % cHild N = 5, % night % star(j,k) = 1 if child j has star color k, else 0 Star = [[0, 0, 0, 1, 1, 0, 1, 0, 0], [0, 1, 0, 0, 0, 1, 1, 0, 0], [0, 1, 0, 0, 0, 0, 1, 1, 0]], % pearl(j,m) = no of pearls received by child j during night m Pearl = [[1, 0, 0, 1, 1], [1, 2, 1, 1, 2], [2, 2, 2, 0, 1]], % visit(i,m) = 1 if fairy i visits children on night m, else 0 Visit = [[1, 0, 0, 1, 1], [0, 0, 1, 1, 0], [0, 1, 0, 1, 1], [1, 1, 1, 0, 1], [1, 1, 1, 0, 0]], Fairy = 1..F, % Cloe, Ariana, Oliviana, Anya, Caroline Child = 1..H, % Tyler, Jordan, David Color = 1..C, % silver, sage, gold, rose, turquoise, ivory, violet, emerald, earth Night = 1..N, % decision variables % x(i,k) = 1 if fairy i attracted to color k, else 0 X = new_array(F,C), X :: 0..1, % pearls received by each child on each night consistent with visits and stars foreach(J in Child, M in Night) sum([Visit[I,M]*Star[J,K]*X[I,K] : I in Fairy, K in Color]) #= Pearl[J,M] end, % each fairy attracted to a single color foreach(I in Fairy) sum([X[I,K] : K in Color]) #= 1 end, % at least one fairy attracted to turquoise sum([X[I,5] : I in Fairy]) #>= 1, % one fairy attracted to earth sum([X[I,9] : I in Fairy]) #= 1, solve(X), foreach(Row in X) println(Row) end, fail, nl. go => true.