Skip to content

Instantly share code, notes, and snippets.

@topher200
Created March 13, 2011 07:10
Show Gist options
  • Save topher200/867944 to your computer and use it in GitHub Desktop.
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
(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