Evolutionary algorithm: Difference between revisions

Content added Content deleted
Line 492: Line 492:
(defn perfectly-fit? [s] (= (fitness s) (count target)))
(defn perfectly-fit? [s] (= (fitness s) (count target)))


(defn randc [] (get alphabet (rand-int (count alphabet))))
(defn randc [] (get alphabet (rand-int (count alphabet)))) ; or simply (rand-nth alphabet) in Clojure 1.2
(defn mutate [s] (map #(if (< (rand) p) (randc) %) s))</lang>
(defn mutate [s] (map #(if (< (rand) p) (randc) %) s))</lang>
Finally evolve. At each generation, print the generation number, the parent, and the parent's fitness.
Finally evolve. At each generation, print the generation number, the parent, and the parent's fitness.