From 1d12a365666312483e408fb2c239628d59141ca5 Mon Sep 17 00:00:00 2001 From: Mahran-Yousef Date: Thu, 18 Jun 2020 22:38:45 -0400 Subject: [PATCH] Fixing previous issues and adding elitism as an option --- src/propeller/gp.clj | 52 +++++++++++++++++++++++++------------ src/propeller/variation.clj | 21 +++++++++------ 2 files changed, 48 insertions(+), 25 deletions(-) diff --git a/src/propeller/gp.clj b/src/propeller/gp.clj index afe1e09..0088377 100644 --- a/src/propeller/gp.clj +++ b/src/propeller/gp.clj @@ -22,21 +22,39 @@ "Main GP loop." [{:keys [population-size max-generations error-function instructions max-initial-plushy-size] - :as argmap}] + :as argmap}] (println "Starting GP with args:" 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) - (repeatedly population-size - #(new-individual evaluated-pop argmap))))))) \ No newline at end of file + (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) + (repeatedly population-size + #(new-individual evaluated-pop argmap)))))))) \ No newline at end of file diff --git a/src/propeller/variation.clj b/src/propeller/variation.clj index 1d174fb..6c613a1 100644 --- a/src/propeller/variation.clj +++ b/src/propeller/variation.clj @@ -31,7 +31,8 @@ (defn uniform-deletion "Randomly deletes instructions from plushy at some rate." [plushy UMADRate] - (remove (fn [x] (< (rand) (/ 1(+ 1 (/ 1 UMADRate))))) + (remove (fn [x] (< (rand) + (/ 1 (+ 1 (/ 1 UMADRate))))) plushy)) (defn new-individual @@ -41,10 +42,14 @@ {:plushy (let [prob (rand)] (cond - (< prob (:crossover (:variation argmap))) (crossover (:plushy (select-parent pop argmap)) - (:plushy (select-parent pop argmap))) - (< prob (+ (:crossover (:variation argmap)) (/ (:UMAD (:variation argmap)) 2))) (uniform-addition (:plushy (select-parent pop argmap)) - (:instructions argmap) - (:UMADRate argmap)) - :else (uniform-deletion (:plushy (select-parent pop argmap)) - (:UMADRate argmap))))}) \ No newline at end of file + (< prob (:crossover (:variation argmap))) + (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))) + :else (:plushy (select-parent pop argmap))))}) \ No newline at end of file