Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- :-use_module(library(clpfd)).
- :-use_module(library(lists)).
- :-use_module(library(between)).
- % emparelhar homens e mulheres, respeitar o delta de diferença máxima de altura, maximizar pares,
- % homem é sempre mais alto que mulher
- optimal_gym_pairs(MenHeights, WomenHeights, Delta, Pairs):-
- same_length(Matrix, MenHeights),
- maplist(same_length(WomenHeights), Matrix),
- maplist(domainAndConstrain, Matrix),
- transpose(Matrix, TMatrix),
- maplist(domainAndConstrain, TMatrix),
- scanlist(heightRules(MenHeights, WomenHeights, Delta), Matrix, 1, _),
- scanlist(sumLine, Matrix, 0, CountPairs),
- append(Matrix, Vars),
- labeling([maximize(CountPairs),down], Vars),
- findall(H-M, (nth1(H, Matrix, Line), nth1(M, Line, 1)), Pairs).
- heightRules(MenHeights, WomenHeights, Delta, Line, Hi, NextHi):-
- NextHi #= Hi + 1,
- scanlist(heightRuleCell(MenHeights, WomenHeights, Delta, Hi), Line, 1, _).
- heightRuleCell(MenHeights, WomenHeights, Delta, Hi, Cell, Mi, NextMi):-
- NextMi #= Mi + 1,
- element(Hi, MenHeights, H),
- element(Mi, WomenHeights, M),
- Cell #=> H #> M #/\ H - M #=< Delta.
- domainAndConstrain(Line):-
- domain(Line, 0, 1),
- count(1, Line, #=, Match),
- Match in 0..1. %single match
- sumLine(Line, Prev, Sum):- sum(Line, #=, Acc), Sum #= Prev + Acc.
- % | ?- optimal_gym_pairs([75, 85, 68, 70], [65, 76, 60, 70], 10, Pairs).
- % Pairs = [1-4,2-2,3-1,4-3] ? ;
- % no
- % | ?- optimal_gym_pairs([75, 85, 68, 70], [65, 76, 60, 80], 10, Pairs).
- % Pairs = [1-1,2-2,3-3] ? ;
- % no
Add Comment
Please, Sign In to add comment