Skip to content

Instantly share code, notes, and snippets.

@cshepp
Last active August 29, 2015 14:17
Show Gist options
  • Save cshepp/f8e68984d3c490a0a2a3 to your computer and use it in GitHub Desktop.
Save cshepp/f8e68984d3c490a0a2a3 to your computer and use it in GitHub Desktop.
(map (fn [x] (if (should-mutate?)
(mutate x)
x)
population))
(defn mutate [genome]
"swaps two sets of random elements in the given genome"
(let [a (rand-int (count genome))
b (rand-int (count genome))
c (rand-int (count genome))
d (rand-int (count genome))]
(swap (swap genome c d) a b)))
(defn swap [v i1 i2]
"swaps two elements in a vector"
(assoc v i2 (v i1) i1 (v i2)))
(def mutation-chance 15)
(defn should-mutate? []
"determines whether or not a given genome
should be mutated"
(< (rand-int 100) mutation-chance))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment