Report on :best-gene-count and :average-gene-count in bmx; move gene utilities to utils

This commit is contained in:
Lee Spector 2023-11-20 21:09:06 -05:00
parent 5ce452b6ff
commit c61934e50b
3 changed files with 48 additions and 42 deletions

View File

@ -21,6 +21,7 @@
[evaluations pop generation argmap training-data] [evaluations pop generation argmap training-data]
(let [best (first pop)] (let [best (first pop)]
(utils/pretty-map-println (utils/pretty-map-println
(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)
@ -34,7 +35,12 @@
: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 (:bmx (:variation argmap)) 0) 0) ; using bmx
{:best-gene-count (utils/count-genes (:plushy best))
:average-gene-count (float (/ (reduce + (map utils/count-genes (map :plushy pop)))
(count pop)))}
{})))))
(defn cleanup (defn cleanup
[] []

View File

@ -162,3 +162,29 @@
(doseq [[k v] (rest mp-seq)] (doseq [[k v] (rest mp-seq)]
(println (str " " (pr-str k v))))) (println (str " " (pr-str k v)))))
(println "}")) (println "}"))
(defn count-genes
"A utility for best match crossover (bmx). Returns the number of segments
between (and before and after) instances of :gene."
[plushy]
(inc (count (filter #(= % :gene) plushy))))
(defn extract-genes
"A utility for best match crossover (bmx). Returns the segments of the plushy
before/between/after instances of :gene."
[plushy]
(loop [genes []
current-gene []
remainder plushy]
(cond (empty? remainder)
(conj genes current-gene)
;
(= (first remainder) :gene)
(recur (conj genes current-gene)
[]
(rest remainder))
;
:else
(recur genes
(conj current-gene (first remainder))
(rest remainder)))))

View File

@ -213,37 +213,11 @@ The function `new-individual` returns a new individual produced by selection and
plushy plushy
(ah-rates plushy ah-min ah-max ah-mean))))) (ah-rates plushy ah-min ah-max ah-mean)))))
(defn count-genes
"A utility for best match crossover (bmx). Returns the number of segments
between (and before and after) instances of :gene."
[plushy]
(inc (count (filter #(= % :gene) plushy))))
(defn extract-genes
"A utility for best match crossover (bmx). Returns the segments of the plushy
before/between/after instances of :gene."
[plushy]
(loop [genes []
current-gene []
remainder plushy]
(cond (empty? remainder)
(conj genes current-gene)
;
(= (first remainder) :gene)
(recur (conj genes current-gene)
[]
(rest remainder))
;
:else
(recur genes
(conj current-gene (first remainder))
(rest remainder)))))
(defn bmx (defn bmx
"Crosses over two plushies using best match crossover (bmx)." "Crosses over two plushies using best match crossover (bmx)."
[plushy-a plushy-b rate] [plushy-a plushy-b rate]
(let [a-genes (extract-genes plushy-a) (let [a-genes (utils/extract-genes plushy-a)
b-genes (extract-genes plushy-b)] b-genes (utils/extract-genes plushy-b)]
(flatten (interpose :gene (flatten (interpose :gene
(mapv (fn [g] (mapv (fn [g]
(if (< (rand) rate) (if (< (rand) rate)