Skip to content

Instantly share code, notes, and snippets.

@cshepp
Created March 22, 2015 22:15
Show Gist options
  • Save cshepp/f5ea5f241ffe5e3ab4b5 to your computer and use it in GitHub Desktop.
Save cshepp/f5ea5f241ffe5e3ab4b5 to your computer and use it in GitHub Desktop.
(def fitness-multiplier 100)
(defn roulette-select [population]
"randomly selects solutions -
probability of being selected is
weighted using fitness score"
(let [roulette-wheel (reduce #(conj %1 (+ (* (- 700 %2) fitness-multiplier) (last %1))) [0] (map calculate-fitness population))
total (last roulette-wheel)]
(mapv (fn [x] (nth population (.indexOf roulette-wheel (first (filter #(< (rand-int total) %1) roulette-wheel)))))
(range (count population)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment