/* Teenage romance in Picat. Problem from https://swish.swi-prolog.org/p/TeenageRomance.swinb """ Teenage Romance Suzy wants to flirt with Nathan But not when her old boyfriend John is around Suzy, Nathan, and John all must take classes 1..6 How can Suzy arrange her schedule so she can flirt in at least 3 classes? """ Via Playing with Prolog "Teenage Romance - Constraint Programming and True Love" https://www.youtube.com/watch?v=jzHFDe6fsnM Note: Compared to the Prolog program, this model optimize the number of flirt periods instead of having exactly three flirt periods. Solution: john = [5,4,3,1,2,6] nathan = [6,2,3,1,5,4] z = 4 suzy = [6,2,1,3,5,4] flirtPeriods = [1,2,5,6] 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. % % Given schedules for John and Nathan. % go => john(John), nathan(Nathan), N = John.len, Suzy = new_list(N), teenage_romance(N,Suzy,John,Nathan, Z), print_solution(N,Suzy,John,Nathan,Z), nl. % % If Suzy freely can plan the schedules of all three then all periods are flirt periods. % Solution, example: % % Z = 6 % john = [2,1,4,3,6,5] % nathan = [1,2,3,4,5,6] % suzy = [1,2,3,4,5,6] % flirtPeriods = [1,2,3,4,5,6] % go2 => N = 6, John = new_list(N), Nathan = new_list(N), Suzy = new_list(N), teenage_romance(N,Suzy,John,Nathan, Z), print_solution(N,Suzy,John,Nathan,Z), nl. print_solution(N,Suzy,John,Nathan,Z) => println(z=Z), println('john '=John), println('nathan'=Nathan), println('suzy '=Suzy), println(flirtPeriods=[I : I in 1..N, Suzy[I] == Nathan[I], Suzy[I] != John[I]]). teenage_romance(N,Suzy,John,Nathan, Z) => Suzy :: 1..N, John :: 1..N, Nathan :: 1..N, all_different(Suzy), all_different(John), all_different(Nathan), % Number of classes where Suzy and Nathan are together without John Z #= sum([(Suzy[I] #= Nathan[I])*(Suzy[I] #!= John[I]) : I in 1..N ]), Vars = Suzy ++ Nathan ++ John, solve($[max(Z)],Vars). john(John) => John =[5,4,3,1,2,6]. nathan(Nathan) => Nathan = [6,2,3,1,5,4].