Created
March 25, 2015 21:37
-
-
Save cshepp/f56e6452d37a8e827bf0 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
;; single-generation cycle: select -> crossover -> mutate | |
(defn next-generation [population] | |
"" | |
(->> (sort-by calculate-fitness population) | |
vec | |
roulette-select | |
pair | |
(map cycle-crossover) | |
(reduce into) | |
(map (fn [x] (if (should-mutate?) | |
(mutate x) | |
x))))) | |
;; full evolution cycle (many generations) | |
(defn evolve [max-gen gen-size draw-chan] | |
"performs the generation cycle for | |
[max-gen] number of generations" | |
(loop [population (create-population gen-size num-cities) | |
cur-gen 0] | |
(if (< cur-gen max-gen) | |
(let [ngen (next-generation population) | |
gen-num (inc cur-gen) | |
best-fit (calculate-fitness (first (sort-by calculate-fitness ngen))) | |
avg-fit (float (/ (reduce + (map calculate-fitness ngen)) (count ngen)))] | |
(do | |
(go | |
(>! draw-chan (mapv calculate-fitness (sort-by calculate-fitness ngen)))) | |
(recur ngen gen-num))) | |
population))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment