Last active
January 9, 2018 10:45
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
:-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