/* Matchmaker problem (PuzzlOR) in Picat. http://puzzlor.editme.com/Matchmaker: """ The Matchmaker was an honored profession in past cultures, serving the valuable purpose of pairing off men and women in hopes of a long and successful relationship. The Matchmaker would carefully consider the characteristics of each partner to determine which pairs would be compatible. Figure 1 shows six men and six women each with varying hair color and eye color. A man or woman will only accept a partner that has at least one of these traits in common. For example, Man A and Woman 5 would make a matching pair because they have at least one trait in common (same hair color). However Man A and Woman 1 would not make a matching pair because they do not have any traits in common. Question: What pairings of men and women allow for everyone to have a partner with at least one trait in common? (There are several correct answers.) """ Women Hair color Eye color 1 Yellow Brown 2 Red Green 3 Black Blue 4 Black Brown 5 Red Brown 6 Black Brown Men Hair color Eye color A Red Green B Yellow Blue C Yellow Brown D Red Green E Black Green F Yellow Blue There are 8 optimal solutions with Total = 7 common traits. Here is one of them: total = 7 x = [2,1,6,3,4,5] numMatches = [1,2,1,1,1,1] [women,1,match,man,B,common = [hair = [Yellow,Yellow],eyes = [Brown,Blue]],numMatches = 1] [women,2,match,man,A,common = [hair = [Red,Red],eyes = [Green,Green]],numMatches = 2] [women,3,match,man,F,common = [hair = [Black,Yellow],eyes = [Blue,Blue]],numMatches = 1] [women,4,match,man,C,common = [hair = [Black,Yellow],eyes = [Brown,Brown]],numMatches = 1] [women,5,match,man,D,common = [hair = [Red,Red],eyes = [Brown,Green]],numMatches = 1] [women,6,match,man,E,common = [hair = [Black,Black],eyes = [Brown,Green]],numMatches = 1] Here are all 8 solutions: [women,1,match,man,B] [women,2,match,man,A] [women,3,match,man,F] [women,4,match,man,C] [women,5,match,man,D] [women,6,match,man,E] [women,1,match,man,B] [women,2,match,man,A] [women,3,match,man,F] [women,4,match,man,E] [women,5,match,man,D] [women,6,match,man,C] [women,1,match,man,B] [women,2,match,man,D] [women,3,match,man,F] [women,4,match,man,C] [women,5,match,man,A] [women,6,match,man,E] [women,1,match,man,B] [women,2,match,man,D] [women,3,match,man,F] [women,4,match,man,E] [women,5,match,man,A] [women,6,match,man,C] [women,1,match,man,F] [women,2,match,man,A] [women,3,match,man,B] [women,4,match,man,C] [women,5,match,man,D] [women,6,match,man,E] [women,1,match,man,F] [women,2,match,man,A] [women,3,match,man,B] [women,4,match,man,E] [women,5,match,man,D] [women,6,match,man,C] [women,1,match,man,F] [women,2,match,man,D] [women,3,match,man,B] [women,4,match,man,C] [women,5,match,man,A] [women,6,match,man,E] [women,1,match,man,F] [women,2,match,man,D] [women,3,match,man,B] [women,4,match,man,E] [women,5,match,man,A] [women,6,match,man,C] 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 = 6, Colors = [Yellow,Brown,Red,Green,Black,Blue], Colors = 1..Colors.len, ColorStr = ["Yellow", "Brown", "Red", "Green", "Black", "Blue"], Women = { % Hair Eyes {Yellow, Brown}, % 1 {Red, Green}, % 2 {Black, Blue}, % 3 {Black, Brown}, % 4 {Red, Brown}, % 5 {Black, Brown} % 6 }, Men = { % Hair Eyes {Red, Green}, % A {Yellow, Blue}, % B {Yellow, Brown}, % C {Red, Green}, % D {Black, Green}, % E {Yellow, Blue} % F }, MenS = ["A","B","C","D","E","F"], % Here we try to match the women 1..6 with some of the men A..F X = new_list(N), X :: 1..N, % total matchings (to maximize) Total :: 0..2*N, % for presentation NumMatches = new_list(N), NumMatches :: 1..2, all_different(X), foreach(I in 1..N) % count the matches matrix_element(Men,X[I],1,MenXI1), matrix_element(Men,X[I],2,MenXI2), NumMatches[I] #= (Women[I,1] #= MenXI1) + (Women[I,2] #= MenXI2) end, Total #= sum(NumMatches), % Total #= 7, Vars = X ++ NumMatches, solve($[max(Total)],Vars), % solve($[],Vars), println(total=Total), println(x=X), println(numMatches=NumMatches), foreach(I in 1..N) Common = [hair=[ColorStr[Women[I,1]],ColorStr[Men[X[I],1]]],eyes=[ColorStr[Women[I,2]],ColorStr[Men[X[I],2]]]], println([women,I,match,man,MenS[X[I]],common=Common,numMatches=NumMatches[I]]) % println([women,I,match,man,MenS[X[I]]]) end, nl, fail, nl. go => true.