Created
November 22, 2016 20:28
-
-
Save sarcilav/c143f9469acae9b1e375d11338b44c8f to your computer and use it in GitHub Desktop.
This file contains 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
(defn permutations [s] | |
(lazy-seq | |
(if (seq (rest s)) | |
(apply concat (for [x s] | |
(map #(cons x %) (permutations (remove #{x} s))))) | |
[s]))) | |
(defn cost [proposoals s] | |
{:permutation s | |
:cost (apply + (map-indexed (fn [project candidate-str] | |
(let [[candidate _] (clojure.string/split candidate-str #"-")] | |
(get-in proposoals [candidate project]))) | |
s))}) | |
(defn basic-seq [candidates max-per] | |
(let [opts (range 0 max-per)] | |
(flatten (map (fn [c] | |
(map (fn [opt] (str c "-" opt)) opts)) | |
candidates)))) | |
(defn mini [proposoals max-per] | |
(apply min-key :cost (map (partial cost proposoals) (permutations (basic-seq (keys proposoals) max-per))))) | |
(mini {"c1" [1 2 30 4] "c2" [4 3 200 1]} 2) |
Author
sarcilav
commented
Nov 22, 2016
(mini {"candidato 1" [,10,22,10,11,28,40,53,75,,]
"candidato 2" [,12,22,12,12,39,41,54,80,,]
"candidato 3" [,9,24,10,11,33,44,50,81,,]
"candidato 4" [,14,20,14,15,33,40,56,78,,]} 2)
{:permutation ("candidato 3-1" "candidato 4-1" "candidato 2-1" "candidato 2-0" "candidato 1-1" "candidato 4-0" "candidato 3-0" "candidato 1-0"), :cost 246}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment