Reduce code duplication; additions before/after each gene; deletion runs on result of addition; tweak default parameters

This commit is contained in:
Lee Spector 2020-06-20 00:05:09 -04:00
parent ef6899d690
commit eed44cfc06
3 changed files with 33 additions and 52 deletions

View File

@ -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

View File

@ -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))))))))
#(new-individual evaluated-pop argmap))))))))

View File

@ -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))))})