/* (Maximally) Diverse solutions in Picat. From Numberjack model examples/DiverseSolution.py """ This example demonstrates finding maximally different solutions. Each iteration forbids any previous solution and minimises similarity to the previous solution. The example model simply asks for N Boolean variables, with (N/2) of them set to 1, but any other satisfaction model could be used also. """ 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 = 9, PreviousSolutions = [], Sol = true, while (Sol) DecisionVars = new_list(N), DecisionVars :: 0..1, sum(DecisionVars) #= N div 2, if PreviousSolutions != [] then % Forbid all previous solutions foreach(P in PreviousSolutions) sum([DecisionVars[I] #!= P[I] : I in 1..N]) #> 0 end, % Minimise the similarity to the last solution Last = last(PreviousSolutions), Z #= sum([DecisionVars[I] #= Last[I] : I in 1..N]), if solve($[degree,split,min(Z)],DecisionVars) then true else Sol := false, println("No more solutions.") end else % first solution solve([degree,split],DecisionVars) end, if Sol then println(DecisionVars), PreviousSolutions := PreviousSolutions ++ [DecisionVars] end end, printf("Found a total of %d solutions.", PreviousSolutions.len), nl.