Skip to content

Instantly share code, notes, and snippets.

@msramalho
Last active January 9, 2018 10:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save msramalho/a5836c88748925d56fcf3897c6257a25 to your computer and use it in GitHub Desktop.
Save msramalho/a5836c88748925d56fcf3897c6257a25 to your computer and use it in GitHub Desktop.
rage PoC on the last exercise on the second 17/18 PLOG minitest - motivação: POC
:-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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment