/*
Secret Santa problem II in Picat.
From Maple Primes: "Secret Santa Graph Theory"
http://www.mapleprimes.com/blog/jpmay/secretsantagraphtheory
"""
Every year my extended family does a "secret santa" gift exchange.
Each person draws another person at random and then gets a gift for
them. At first, none of my siblings were married, and so the draw was
completely random. Then, as people got married, we added the restriction
that spouses should not draw each others names. This restriction meant
that we moved from using slips of paper on a hat to using a simple
computer program to choose names. Then people began to complain when
they would get the same person two years in a row, so the program was
modified to keep some history and avoid giving anyone a name in their
recent history. This year, not everyone was participating, and so after
removing names, and limiting the number of exclusions to four per person,
I had data something like this:
Name: Spouse, Recent Picks
Noah: Ava. Ella, Evan, Ryan, John
Ava: Noah, Evan, Mia, John, Ryan
Ryan: Mia, Ella, Ava, Lily, Evan
Mia: Ryan, Ava, Ella, Lily, Evan
Ella: John, Lily, Evan, Mia, Ava
John: Ella, Noah, Lily, Ryan, Ava
Lily: Evan, John, Mia, Ava, Ella
Evan: Lily, Mia, John, Ryan, Noah
"""
Note: I interpret this as the following three constraints:
1) One cannot be a Secret Santa of one's spouse
2) One cannot be a Secret Santa for somebody two years in a row
3) Optimization: maximize the time since the last time
This model also handle single persons, something the original
problem don't mention.
Model created by Hakan Kjellerstrand, hakank@gmail.com
See also my Picat page: http://www.hakank.org/picat/
*/
import cp.
main => go.
go =>
% N = 8, % Without Single person
N = 9, % With a Single person
Noah = 1,
Ava = 2,
Ryan = 3,
Mia = 4,
Ella = 5,
John = 6,
Lily = 7,
Evan = 8,
_Single = 9,
Spouses =
[
Ava, % Noa
Noah, % Ava
Mia, % Rya
Ryan, % Mia
John, % Ella
Ella, % John
Evan, % Lily
Lily % Evan
, 0 % Single has no spouse
],
M = N+1, % "large M" to indicate no earlier history
%
% The matrix version of earlier rounds.
% M means that no earlier Santa.
% Note: Ryan and Mia has the same recipient for years 3 and 4,
% and Ella and John has for year 4.
% This seems to be caused by modification of
% original data.
%
%
% rounds with a single person (fake data)
%
Rounds =
[
%N A R M El J L Ev S
[0, M, 3, M, 1, 4, M, 2, 2], % Noah
[M, 0, 4, 2, M, 3, M, 1, 1], % Ava
[M, 2, 0, M, 1, M, 3, 4, 4], % Ryan
[M, 1, M, 0, 2, M, 3, 4, 3], % Mia
[M, 4, M, 3, 0, M, 1, 2, M], % Ella
[1, 4, 3, M, M, 0, 2, M, M], % John
[M, 3, M, 2, 4, 1, 0, M, M], % Lily
[4, M, 3, 1, M, 2, M, 0, M], % Evan
[1, 2, 3, 4, M, 2, M, M, 0] % Single
],
% decision variables
Santas = new_list(N),
Santas :: 1..N,
Santas2 = new_list(N),
Santas :: 1..N,
SantaDistance = new_list(N),
SantaDistance :: 1..N+1,
Z :: 0..1000, % total distance (to minimize)
% constraints
% Everyone gives and receives a Secret Santa
all_different(Santas),
% no Santa for a spouses
foreach(I in 1..N)
Santas[I] #!= I,
if Spouses[I] > 0 then
Santas[I] #!= Spouses[I]
end
end,
% optimize "distance" to earlier rounds:
foreach(I in 1..N)
% SantaDistance[I] #= Rounds[I,Santas[I]]
matrix_element(Rounds,I,Santas[I],SantaDistance[I])
end,
% Cannot be a Secret Santa for the same person two years in a row.
foreach(I in 1..N)
% Rounds[I,Santas2[I]] #= 1,
matrix_element(Rounds,I,Santas2[I],1),
Santas[I] #!= Santas2[I]
end,
Z #= sum([SantaDistance[I] : I in 1..N]),
solve([$max(Z)], Santas),
writeln(z=Z),
writeln(santas=Santas),
writeln(santaDistance=SantaDistance),
nl.
matrix_element(X, I, J, Val) =>
Row = X[I],
element(J,Row,Val).
% matrix_element(X, I, J, Val) =>
% freeze(I, (element(I, X, Row),freeze(J,element(J,Row,Val)))).