Switch to normalized autoconstructive hypervariation; reformat

This commit is contained in:
Lee Spector 2023-10-18 17:05:38 -04:00
parent adf039195c
commit 37df3432fb
3 changed files with 156 additions and 165 deletions

View File

@ -23,7 +23,7 @@
[evaluations pop generation argmap training-data] [evaluations pop generation argmap training-data]
(let [best (first pop)] (let [best (first pop)]
(clojure.pprint/pprint (clojure.pprint/pprint
(merge {:generation generation {:generation generation
:best-plushy (:plushy best) :best-plushy (:plushy best)
:best-program (genome/plushy->push (:plushy best) argmap) :best-program (genome/plushy->push (:plushy best) argmap)
:best-total-error (:total-error best) :best-total-error (:total-error best)
@ -34,18 +34,7 @@
:genotypic-diversity (float (/ (count (distinct (map :plushy pop))) (count pop))) :genotypic-diversity (float (/ (count (distinct (map :plushy pop))) (count pop)))
:behavioral-diversity (float (/ (count (distinct (map :behaviors pop))) (count pop))) :behavioral-diversity (float (/ (count (distinct (map :behaviors pop))) (count pop)))
:average-genome-length (float (/ (reduce + (map count (map :plushy pop))) (count pop))) :average-genome-length (float (/ (reduce + (map count (map :plushy pop))) (count pop)))
:average-total-error (float (/ (reduce + (map :total-error pop)) (count pop)))} :average-total-error (float (/ (reduce + (map :total-error pop)) (count pop)))})
(if (> (or (:ah-umad (:variation argmap)) 0) 0) ;; using autoconstructive hypervariability
{:average-hypervariability
(let [variabilities (map (fn [i]
(let [p (:plushy i)]
(if (empty? p)
0
(/ (reduce + (variation/ah-rates p 0 1))
(count p)))))
pop)]
(float (/ (reduce + variabilities) (count variabilities))))}
{})))
(println))) (println)))
(defn gp (defn gp

View File

@ -303,21 +303,20 @@
:training-data (:train train-and-test-data) :training-data (:train train-and-test-data)
:testing-data (:test train-and-test-data) :testing-data (:test train-and-test-data)
:max-generations 1000 :max-generations 1000
:population-size 100 :population-size 1000
:max-initial-plushy-size 100 :max-initial-plushy-size 100
:step-limit 1000 :step-limit 10000
:parent-selection :lexicase :parent-selection :lexicase
:downsample? true :downsample? true
:ds-function :case-rand :ds-function :case-rand
:downsample-rate 0.1 :downsample-rate 0.5
;:parent-selection :tournament ;:parent-selection :tournament
;:parent-selection :motley-batch-lexicase ;:parent-selection :motley-batch-lexicase
;:max-batch-size [1 2 4 8 16 32 64 128 256] ;:max-batch-size [1 2 4 8 16 32 64 128 256]
;:tournament-size 5 ;:tournament-size 5
;:umad-rate 0.09 ;:umad-rate 0.09
:ah-umad-protect-rate 0.001 ;; ah-umad :ah-umad-protection 10 ;; ah-umad
:ah-umad-vary-rate 0.1 ;; ah-umad :ah-umad-rate 0.1 ;; ah-umad
:ah-umad-tournament-size 1 ;; ah-umad
;:umad-rate [1/2 ;:umad-rate [1/2
; 1/4 1/4 ; 1/4 1/4
; 1/8 1/8 1/8 ; 1/8 1/8 1/8

View File

@ -202,16 +202,24 @@ The function `new-individual` returns a new individual produced by selection and
%) %)
(partition 2 plushy)))) (partition 2 plushy))))
(defn with-mean
"Returns numeric vector v scaled so that the mean value is m"
[m v]
(if (empty? v)
v
(let [initial-mean (/ (reduce + v) (count v))]
(map #(* m (/ % initial-mean)) v))))
(defn ah-rates (defn ah-rates
"Returns the sequence of rates with which each element of plushy should "Returns the sequence of rates with which each element of plushy should
be mutated when using autoconstructive hypervariability." be mutated when using autoconstructive hypervariability."
[plushy protect-rate hypervariable-rate] [plushy protection rate]
(loop [i 0 (loop [i 0
protected true protected true
rates [] rates []
remainder plushy] remainder plushy]
(if (empty? remainder) (if (empty? remainder)
rates (with-mean rate rates)
(if (and (not protected) (if (and (not protected)
(= (first remainder) :protect)) (= (first remainder) :protect))
(recur i (recur i
@ -222,33 +230,33 @@ The function `new-individual` returns a new individual produced by selection and
(if protected (if protected
(not= (first remainder) :vary) (not= (first remainder) :vary)
false) false)
(conj rates (if protected protect-rate hypervariable-rate)) (conj rates (if protected (/ 1 protection) 1))
(rest remainder)))))) (rest remainder))))))
(defn ah-uniform-addition (defn ah-uniform-addition
"Returns plushy with new instructions possibly added before or after each "Returns plushy with new instructions possibly added before or after each
existing instruction. Rates are autoconstructively hypervariable." existing instruction. Rates are autoconstructively hypervariable."
[plushy instructions protect-rate hypervariable-rate] [plushy instructions protection rate]
(apply concat (apply concat
(map #(if (< (rand) %2) (mapv #(if (< (rand) %2)
(shuffle [%1 (utils/random-instruction instructions)]) (shuffle [%1 (utils/random-instruction instructions)])
[%1]) [%1])
plushy plushy
(ah-rates plushy protect-rate hypervariable-rate)))) (ah-rates plushy protection rate))))
(defn ah-uniform-deletion (defn ah-uniform-deletion
"Randomly deletes instructions from plushy at some rate. "Randomly deletes instructions from plushy at some rate.
Rates are autoconstructively hypervariable." Rates are autoconstructively hypervariable."
[plushy protect-rate hypervariable-rate] [plushy protection rate]
(map first (mapv first
(remove (fn [[_ rate]] (remove (fn [[_ rate]]
(< (rand) (< (rand)
(if (zero? rate) (if (zero? rate)
0 0
(/ 1 (+ 1 (/ 1 rate)))))) (/ 1 (+ 1 (/ 1 rate))))))
(map vector (mapv vector
plushy plushy
(ah-rates plushy protect-rate hypervariable-rate))))) (ah-rates plushy protection rate)))))
(defn new-individual (defn new-individual
"Returns a new individual produced by selection and variation of "Returns a new individual produced by selection and variation of
@ -305,17 +313,12 @@ The function `new-individual` returns a new individual produced by selection and
(uniform-deletion rate))) (uniform-deletion rate)))
; ;
:ah-umad ;; autoconstructive hypervariability UMAD :ah-umad ;; autoconstructive hypervariability UMAD
(let [protect-rate (utils/onenum (:ah-umad-protect-rate argmap)) (let [protection (utils/onenum (:ah-umad-protection argmap))
vary-rate (utils/onenum (:ah-umad-vary-rate argmap)) rate (utils/onenum (:ah-umad-rate argmap))
tourn-size (utils/onenum (:ah-umad-tournament-size argmap)) parent-genome (:plushy (selection/select-parent pop argmap))]
parent-genome (:plushy (selection/select-parent pop argmap)) (-> parent-genome
offspring (repeatedly (ah-uniform-addition (:instructions argmap) protection rate)
tourn-size (ah-uniform-deletion protection rate)))
#(-> parent-genome
(ah-uniform-addition (:instructions argmap) protect-rate vary-rate)
(ah-uniform-deletion protect-rate vary-rate)))
hypervariabilities (map #(reduce + (ah-rates % 0 1)) offspring)]
(second (last (sort-by first (map vector hypervariabilities offspring)))))
; ;
:uniform-addition :uniform-addition
(-> (:plushy (selection/select-parent pop argmap)) (-> (:plushy (selection/select-parent pop argmap))