From eed44cfc065fca2f71c969172b27764402c81186 Mon Sep 17 00:00:00 2001 From: Lee Spector Date: Sat, 20 Jun 2020 00:05:09 -0400 Subject: [PATCH] Reduce code duplication; additions before/after each gene; deletion runs on result of addition; tweak default parameters --- src/propeller/core.clj | 4 +-- src/propeller/gp.clj | 52 ++++++++++++++----------------------- src/propeller/variation.clj | 29 +++++++++------------ 3 files changed, 33 insertions(+), 52 deletions(-) diff --git a/src/propeller/core.clj b/src/propeller/core.clj index 5cbbd1e..30fbeb9 100644 --- a/src/propeller/core.clj +++ b/src/propeller/core.clj @@ -13,8 +13,8 @@ :max-initial-plushy-size 50 :step-limit 100 :parent-selection :lexicase - :tournament-size 20 - :UMADRate 0.2 + :tournament-size 5 + :UMADRate 0.1 :variation {:UMAD 0.5 :crossover 0.5} :elitism false} (apply hash-map diff --git a/src/propeller/gp.clj b/src/propeller/gp.clj index 0088377..37dd33b 100644 --- a/src/propeller/gp.clj +++ b/src/propeller/gp.clj @@ -24,37 +24,23 @@ max-initial-plushy-size] :as argmap}] (println "Starting GP with args:" argmap) - (if - (:elitism argmap) - (loop [generation 0 - population (repeatedly - population-size - #(hash-map :plushy - (make-random-plushy instructions - max-initial-plushy-size)))] - (let [evaluated-pop (sort-by :total-error - (map (partial error-function argmap) - population))] - (report evaluated-pop generation) - (cond - (zero? (:total-error (first evaluated-pop))) (println "SUCCESS") - (>= generation max-generations) nil - :else (recur (inc generation) - (conj (repeatedly (- population-size 1) - #(new-individual evaluated-pop argmap)) (first evaluated-pop)))))) - (loop [generation 0 - population (repeatedly - population-size - #(hash-map :plushy - (make-random-plushy instructions - max-initial-plushy-size)))] - (let [evaluated-pop (sort-by :total-error - (map (partial error-function argmap) - population))] - (report evaluated-pop generation) - (cond - (zero? (:total-error (first evaluated-pop))) (println "SUCCESS") - (>= generation max-generations) nil - :else (recur (inc generation) + (loop [generation 0 + population (repeatedly + population-size + #(hash-map :plushy + (make-random-plushy instructions + max-initial-plushy-size)))] + (let [evaluated-pop (sort-by :total-error + (map (partial error-function argmap) + population))] + (report evaluated-pop generation) + (cond + (zero? (:total-error (first evaluated-pop))) (println "SUCCESS") + (>= generation max-generations) nil + :else (recur (inc generation) + (if (:elitism argmap) + (conj (repeatedly (dec population-size) + #(new-individual evaluated-pop argmap)) + (first evaluated-pop)) (repeatedly population-size - #(new-individual evaluated-pop argmap)))))))) \ No newline at end of file + #(new-individual evaluated-pop argmap)))))))) diff --git a/src/propeller/variation.clj b/src/propeller/variation.clj index 6c613a1..af6972e 100644 --- a/src/propeller/variation.clj +++ b/src/propeller/variation.clj @@ -16,22 +16,18 @@ longer)))) (defn uniform-addition - "Randomly adds new instructions before every instruction (and at the end of - the plushy) with some probability." + "Returns plushy with new instructions possibly added before or after each existing instruction." [plushy instructions UMADRate] - (let [rand-code (repeatedly (inc (count plushy)) - (fn [] - (if (< (rand) UMADRate) - (rand-nth instructions) - :mutation-padding)))] - (remove #(= % :mutation-padding) - (interleave (conj plushy :mutation-padding) - rand-code)))) + (apply concat + (map #(if (< (rand) UMADRate) + (shuffle [% (rand-nth instructions)]) + [%]) + plushy))) (defn uniform-deletion "Randomly deletes instructions from plushy at some rate." [plushy UMADRate] - (remove (fn [x] (< (rand) + (remove (fn [_] (< (rand) (/ 1 (+ 1 (/ 1 UMADRate))))) plushy)) @@ -46,10 +42,9 @@ (crossover (:plushy (select-parent pop argmap)) (:plushy (select-parent pop argmap))) (< prob (+ (:crossover (:variation argmap)) - (/ (:UMAD (:variation argmap)) 2))) - (do (uniform-addition (:plushy (select-parent pop argmap)) - (:instructions argmap) - (:UMADRate argmap)) - (uniform-deletion (:plushy (select-parent pop argmap)) - (:UMADRate argmap))) + (:UMAD (:variation argmap)) 2)) + (uniform-deletion (uniform-addition (:plushy (select-parent pop argmap)) + (:instructions argmap) + (:UMADRate argmap)) + (:UMADRate argmap)) :else (:plushy (select-parent pop argmap))))}) \ No newline at end of file