Last active
August 29, 2015 14:17
-
-
Save cshepp/f8e68984d3c490a0a2a3 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
(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