From 5fd533c24f05d3d4bdc2d48c069f6e4087ef9b55 Mon Sep 17 00:00:00 2001 From: Lee Spector Date: Fri, 3 Nov 2023 12:30:14 -0400 Subject: [PATCH] Correctly handle leadin/trailing :gene --- src/propeller/variation.cljc | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/src/propeller/variation.cljc b/src/propeller/variation.cljc index 6e7fbc7..227b4ec 100644 --- a/src/propeller/variation.cljc +++ b/src/propeller/variation.cljc @@ -279,15 +279,33 @@ The function `new-individual` returns a new individual produced by selection and plushy (ah-rates plushy ah-min ah-max ah-mean))))) +(defn count-genes + "Returns the number of segments between (and before and after) instances of :gene." + [plushy] + (inc (count (filter #(= % :gene) plushy)))) + (defn extract-genes [plushy] - (apply concat - (mapv #(if (some #{:gene} %) - (repeat (dec (count %)) ()) - [%]) - (partition-by #(= % :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))))) #_(extract-genes [:add :x :y :gene :z :gene :gene :gene :w :gene]) +#_(extract-genes [:gene :z :gene :gene :gene :w :gene]) +#_(extract-genes [:gene]) +#_(extract-genes []) (defn autoconstructive-crossover "Crosses over two plushies using autoconstructive crossover, one Push instruction at a time. @@ -333,9 +351,9 @@ The function `new-individual` returns a new individual produced by selection and ; :autoconstructive-crossover (let [plushy1 (:plushy (selection/select-parent pop argmap)) - p1-genes (count (extract-genes plushy1)) + p1-genes (count-genes plushy1) plushy2 (:plushy (selection/select-parent - (filter #(= (count (extract-genes (:plushy %))) + (filter #(= (count-genes (:plushy %)) p1-genes) pop) argmap))]