From ac3690837d392e5972233cf13b4e37551ac38175 Mon Sep 17 00:00:00 2001 From: Lee Spector Date: Wed, 1 Nov 2023 18:28:37 -0400 Subject: [PATCH] Add autoconstructive-crossover --- src/propeller/problems/boolean/mul3.cljc | 2 +- src/propeller/variation.cljc | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/propeller/problems/boolean/mul3.cljc b/src/propeller/problems/boolean/mul3.cljc index d204d34..db56c0c 100644 --- a/src/propeller/problems/boolean/mul3.cljc +++ b/src/propeller/problems/boolean/mul3.cljc @@ -314,7 +314,7 @@ ;:parent-selection :motley-batch-lexicase ;:max-batch-size [1 2 4 8 16 32 64 128 256] ;:tournament-size 5 - ;:umad-rate 0.09 + :umad-rate 0.09 ;:ah-umad-min 0.01 ;:ah-umad-max 0.5 ;:ah-umad-mean 0.05 diff --git a/src/propeller/variation.cljc b/src/propeller/variation.cljc index 0a142a9..0e65e93 100644 --- a/src/propeller/variation.cljc +++ b/src/propeller/variation.cljc @@ -279,6 +279,22 @@ The function `new-individual` returns a new individual produced by selection and plushy (ah-rates plushy ah-min ah-max ah-mean))))) +(defn autoconstructive-crossover + "Crosses over two individuals using autoconstructive crossover, one Push instruction at a time. + Pads shorter one from the end of the list of instructions." + [plushy-a plushy-b] + (let [a-genes (partition-by #(= % :gene) plushy-a) + b-genes (partition-by #(= % :gene) plushy-b) + shorter (min-key count a-genes b-genes) + longer (if (= shorter a-genes) + b-genes + a-genes) + length-diff (- (count longer) (count shorter)) + shorter-padded (concat shorter (repeat length-diff ()))] + (flatten (mapv #(if (< (rand) 0.5) %1 %2) + shorter-padded + longer)))) + (defn new-individual "Returns a new individual produced by selection and variation of individuals in the population." @@ -306,6 +322,11 @@ The function `new-individual` returns a new individual produced by selection and (tail-aligned-crossover (:plushy (selection/select-parent pop argmap)) (:plushy (selection/select-parent pop argmap))) + ; + :autoconstructive-crossover + (autoconstructive-crossover + (:plushy (selection/select-parent pop argmap)) + (:plushy (selection/select-parent pop argmap))) ; :umad (let [rate (utils/onenum (:umad-rate argmap))]