Created
March 13, 2011 07:10
-
-
Save topher200/867944 to your computer and use it in GitHub Desktop.
My Clojure main function for my blog, source available: https://github.com/topher200/genetic-hello-world-clojure
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 generate-selected | |
"The selected group is generated by taking the best 10 chromos (elitism), | |
then repeated calling tourny-select on the sample to generate 90 more." | |
[sample] | |
(concat | |
(take 10 sample) | |
(take 90 (repeatedly #(tourny-select-chromo sample))))) | |
(defn generate-solution | |
"Generates the solution set by repeatedly selecting two chromos (at random) | |
from selected and breeding them" | |
[selected] | |
(repeatedly 1000 | |
(fn [] (breed-wrapper (rand-nth selected) (rand-nth selected))))) | |
(defn run-generation | |
[sample] | |
(generate-solution (generate-selected sample))) | |
(defn run | |
([] | |
(run (generate-random-sample))) | |
([sample] | |
(let [solution (sort-by-fitness (run-generation sample))] | |
(do | |
(println (fitness (first solution)) (first solution)) | |
(if (= (fitness (first solution)) 0) | |
solution | |
(run solution)))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment