Guest User

Untitled

a guest
Jan 17th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. :-use_module(library(clpfd)).
  2. :-use_module(library(lists)).
  3. :-use_module(library(between)).
  4.  
  5. % emparelhar homens e mulheres, respeitar o delta de diferença máxima de altura, maximizar pares,
  6. % homem é sempre mais alto que mulher
  7.  
  8. optimal_gym_pairs(MenHeights, WomenHeights, Delta, Pairs):-
  9. same_length(Matrix, MenHeights),
  10. maplist(same_length(WomenHeights), Matrix),
  11. maplist(domainAndConstrain, Matrix),
  12. transpose(Matrix, TMatrix),
  13. maplist(domainAndConstrain, TMatrix),
  14. scanlist(heightRules(MenHeights, WomenHeights, Delta), Matrix, 1, _),
  15. scanlist(sumLine, Matrix, 0, CountPairs),
  16. append(Matrix, Vars),
  17. labeling([maximize(CountPairs),down], Vars),
  18. findall(H-M, (nth1(H, Matrix, Line), nth1(M, Line, 1)), Pairs).
  19.  
  20. heightRules(MenHeights, WomenHeights, Delta, Line, Hi, NextHi):-
  21. NextHi #= Hi + 1,
  22. scanlist(heightRuleCell(MenHeights, WomenHeights, Delta, Hi), Line, 1, _).
  23.  
  24. heightRuleCell(MenHeights, WomenHeights, Delta, Hi, Cell, Mi, NextMi):-
  25. NextMi #= Mi + 1,
  26. element(Hi, MenHeights, H),
  27. element(Mi, WomenHeights, M),
  28. Cell #=> H #> M #/\ H - M #=< Delta.
  29.  
  30. domainAndConstrain(Line):-
  31. domain(Line, 0, 1),
  32. count(1, Line, #=, Match),
  33. Match in 0..1. %single match
  34.  
  35. sumLine(Line, Prev, Sum):- sum(Line, #=, Acc), Sum #= Prev + Acc.
  36.  
  37. % | ?- optimal_gym_pairs([75, 85, 68, 70], [65, 76, 60, 70], 10, Pairs).
  38. % Pairs = [1-4,2-2,3-1,4-3] ? ;
  39. % no
  40.  
  41. % | ?- optimal_gym_pairs([75, 85, 68, 70], [65, 76, 60, 80], 10, Pairs).
  42. % Pairs = [1-1,2-2,3-3] ? ;
  43. % no
Add Comment
Please, Sign In to add comment