Skip to content

Instantly share code, notes, and snippets.

@amalloy
Created October 9, 2011 20:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amalloy/1274144 to your computer and use it in GitHub Desktop.
Save amalloy/1274144 to your computer and use it in GitHub Desktop.
(defn fitness-fn [target]
(fn [data]
(Math/abs (- target data))))
(defn evolve [sources fitness]
(let [next-gen (for [source sources
x (range 5)] ;; try 5 mutations for each source
(+ source (- 20 (rand-int 40))) ; go up or down by up to 20
)]
(take (count sources) (sort-by fitness next-gen))))
(defn start [seeds goal]
(let [fitness (fitness-fn goal)
num-tries 50]
(nth (iterate #(evolve % fitness) seeds)
num-tries)))
(start (range 10) 1000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment