From 864c8fb8ee6f486db11c177df2f82877326f5564 Mon Sep 17 00:00:00 2001 From: Lee Spector Date: Sat, 23 Jan 2021 16:14:09 -0500 Subject: [PATCH] Print behavioral diversity and avg total error; add tail-aligned crossover and diploid operators; fix valiant problem --- .DS_Store | Bin 8196 -> 8196 bytes src/.DS_Store | Bin 6148 -> 6148 bytes src/propeller/.DS_Store | Bin 6148 -> 6148 bytes src/propeller/gp.cljc | 4 ++ src/propeller/problems/valiant.cljc | 7 +-- src/propeller/session.cljc | 74 ---------------------------- src/propeller/variation.cljc | 54 ++++++++++++++++++++ 7 files changed, 62 insertions(+), 77 deletions(-) diff --git a/.DS_Store b/.DS_Store index 4c8696c854640d8b1087a87465a54826e6211a0c..b9e3ed4cde58e282d3701268d3e1003f97e862b9 100644 GIT binary patch delta 303 zcmZp1XmQw}CQ#q*!N9=4!l1{H&XCDalAG`1l9ZF51Qg?#Ehh1E%7H_UsPZXzC|8h>gZT}%hRQVLV@&y@& z!O8i#1wcIv46+I!k`-unCPNBCDnnvUI+85Ise3@#I3#Ig%k`(7wtEY)97P4fdh9At z9KaN?nOV??iNnyq%v?vo%wn>waHu@v%lSZinHh>1iWriSt-San`+C*Z$s2_w*r3ew P!jeqP3Y%XEOYr~z)5uT9 diff --git a/src/.DS_Store b/src/.DS_Store index 059041468db135bd0d2cc7fe59bb35aa6d395f07..da17f2d6d1484bfb55b14e6e12bdca890d2d7e85 100644 GIT binary patch delta 79 zcmZoMXffEZk%`07!cs@U(8zf5PNqa=W(L>Eh0J0?x%ntG%B<18MF)%Q2oD7h*eY<~h aKC{H+SY{qJI9HXKnSlYMdh=T5a1j91Qy44& diff --git a/src/propeller/.DS_Store b/src/propeller/.DS_Store index a19382269acf6b4531519f866c878f6dca85fd8f..306b0a39dc35785d3aa367defd71ec8b17637caa 100644 GIT binary patch delta 472 zcmZoMXfc=|#>B!ku~2NHo+6{b#(>?7ixZfc7zHNtFfHb=w6N4sFf=lryqzh(J}2EU zI5|JJfB_8bEPxaTLjgk(Lq0eKLoP#cZoZ34QcivnNG{2uYIES>Lyp+hr4XgA zAOmhE3(%Z|$%4$5^(;VJOBsq8GEiK^$l3-}0hdE|$;;a^mJNHsEjEVAV{+;Xm0Z3SBnH2|UlVLf&gC=Or<0672x mHdd=MZf57;=K#jr=0N7}%#-;=961;n7??ovnB)qu~2NHo+2a5#(>?7j4YFRSQc{_8km{uD41DH-p-Ogc^|9FW>F3y mmWd6kH?wo_a{#q$7UcNOJegm_k%IvU7#SE?Hb;o8VFmz*+Y|@@ diff --git a/src/propeller/gp.cljc b/src/propeller/gp.cljc index 2a46aeb..0eb0426 100755 --- a/src/propeller/gp.cljc +++ b/src/propeller/gp.cljc @@ -25,8 +25,12 @@ (println "Best behaviors:" (:behaviors best)) (println "Genotypic diversity:" (float (/ (count (distinct (map :plushy pop))) (count pop)))) + (println "Behavioral diversity:" + (float (/ (count (distinct (map :behaviors pop))) (count pop)))) (println "Average genome length:" (float (/ (reduce + (map count (map :plushy pop))) (count pop)))) + (println "Average total error:" + (float (/ (reduce + (map :total-error pop)) (count pop)))) (println))) (defn gp diff --git a/src/propeller/problems/valiant.cljc b/src/propeller/problems/valiant.cljc index c51e9a4..f51d8b2 100644 --- a/src/propeller/problems/valiant.cljc +++ b/src/propeller/problems/valiant.cljc @@ -3,8 +3,8 @@ [propeller.push.interpreter :as interpreter] [propeller.push.state :as state])) -(def num-vars 100) ;1000) -(def num-inputs 50) ;500) +(def num-vars 100) ;10) ;100) ;1000) +(def num-inputs 50) ;5) ; 50) ;500) (def num-train 500) ;5000) (def num-test 200) @@ -23,10 +23,11 @@ :outputs (map even-parity? test-inputs)}})) (def instructions - (vec (concat (for [i (range num-inputs)] (keyword (str "in" i))) + (vec (concat (for [i (range num-vars)] (keyword (str "in" i))) (take num-inputs (cycle [:boolean_xor :boolean_or + :boolean_and :boolean_not :exec_if 'close diff --git a/src/propeller/session.cljc b/src/propeller/session.cljc index fbca6ec..c9f9e22 100755 --- a/src/propeller/session.cljc +++ b/src/propeller/session.cljc @@ -5,7 +5,6 @@ [propeller.variation :as variation] [propeller.problems.simple-regression :as regression] [propeller.problems.string-classification :as string-classif] - propeller.problems.valiant [propeller.push.core :as push] [propeller.push.interpreter :as interpreter] [propeller.push.state :as state] @@ -154,76 +153,3 @@ ; } ; :elitism false ; :diploid true}) - - -;(gp/gp {:instructions propeller.problems.valiant/instructions -; :error-function propeller.problems.valiant/error-function -; :max-generations 500 -; :population-size 50 -; :max-initial-plushy-size 1000 -; :step-limit 2000 -; :parent-selection :tournament -; :tournament-size 10 -; :umad-rate 0.01 -; :diploid-flip-rate 0.01 -; :variation {:umad 0.9 -; :diploid-flip 0.1 -; } -; :elitism false -; :diploid true}) - -;(gp/gp {:instructions propeller.problems.valiant/instructions -; :error-function propeller.problems.valiant/error-function -; :max-generations 500 -; :population-size 100 -; :max-initial-plushy-size 1000 -; :step-limit 2000 -; :parent-selection :tournament -; :tournament-size 10 -; :umad-rate 0.01 -; :diploid-flip-rate 0.01 -; :variation {:umad 0.9 -; :diploid-flip 0.1 -; } -; :elitism false -; :diploid true}) - -;;; below is when I switched to just xor - -;(gp/gp {:instructions propeller.problems.valiant/instructions -; :error-function propeller.problems.valiant/error-function -; :max-generations 500 -; :population-size 500 -; :max-initial-plushy-size 50 -; :step-limit 2000 -; :parent-selection :lexicase -; :tournament-size 2 -; :umad-rate 0.01 -; :diploid-flip-rate 0.01 -; :variation {:umad 0.5 -; :crossover 0.25 -; :diploid-flip 0.25 -; } -; :elitism false -; :diploid true}) - -;; separated diploid from other operators - -(gp/gp {:instructions propeller.problems.valiant/instructions - :error-function propeller.problems.valiant/error-function - :max-generations 500 - :population-size 1000 - :max-initial-plushy-size 10 - :step-limit 1000 - :parent-selection :lexicase - :tournament-size 2 - :umad-rate 0.01 - :replacement-rate 0.01 - :diploid-flip-rate 0.01 - :variation {:diploid-umad 0.25 - :diploid-crossover 0.25 - :diploid-flip 0.25 - :uniform-replacement 0.25 - } - :elitism false - :diploid true}) diff --git a/src/propeller/variation.cljc b/src/propeller/variation.cljc index d35c452..19578eb 100755 --- a/src/propeller/variation.cljc +++ b/src/propeller/variation.cljc @@ -16,6 +16,20 @@ shorter-padded longer)))) +(defn tail-aligned-crossover + "Crosses over two individuals using uniform crossover. Pads shorter one on the left." + [plushy-a plushy-b] + (let [shorter (min-key count plushy-a plushy-b) + longer (if (= shorter plushy-a) + plushy-b + plushy-a) + length-diff (- (count longer) (count shorter)) + shorter-padded (concat (repeat length-diff :crossover-padding) shorter)] + (remove #(= % :crossover-padding) + (map #(if (< (rand) 0.5) %1 %2) + shorter-padded + longer)))) + (defn diploid-crossover "Crosses over two individuals using uniform crossover. Pads shorter one." [plushy-a plushy-b] @@ -32,6 +46,22 @@ shorter-padded longer))))) +(defn tail-aligned-diploid-crossover + "Crosses over two individuals using uniform crossover. Pads shorter one on the left." + [plushy-a plushy-b] + (let [plushy-a (partition 2 plushy-a) + plushy-b (partition 2 plushy-b) + shorter (min-key count plushy-a plushy-b) + longer (if (= shorter plushy-a) + plushy-b + plushy-a) + length-diff (- (count longer) (count shorter)) + shorter-padded (concat (repeat length-diff :crossover-padding) shorter)] + (flatten (remove #(= % :crossover-padding) + (map #(if (< (rand) 0.5) %1 %2) + shorter-padded + longer))))) + (defn uniform-addition "Returns plushy with new instructions possibly added before or after each existing instruction." @@ -51,6 +81,16 @@ %) plushy)) +(defn diploid-uniform-silent-replacement + "Returns plushy with new instructions possibly replacing existing + instructions, but only among the silent member of each pair." + [plushy instructions replacement-rate] + (interleave (map first (partition 2 plushy)) + (map #(if (< (rand) replacement-rate) + (utils/random-instruction instructions) + %) + (map second (partition 2 plushy))))) + (defn diploid-uniform-addition "Returns plushy with new instructions possibly added before or after each existing instruction." @@ -105,6 +145,11 @@ (:plushy (selection/select-parent pop argmap)) (:plushy (selection/select-parent pop argmap))) ; + :tail-aligned-crossover + (tail-aligned-crossover + (:plushy (selection/select-parent pop argmap)) + (:plushy (selection/select-parent pop argmap))) + ; :umad (-> (:plushy (selection/select-parent pop argmap)) (uniform-addition (:instructions argmap) (:umad-rate argmap)) @@ -118,6 +163,10 @@ (-> (:plushy (selection/select-parent pop argmap)) (uniform-replacement (:instructions argmap) (:replacement-rate argmap))) ; + :diploid-uniform-silent-replacement + (-> (:plushy (selection/select-parent pop argmap)) + (diploid-uniform-silent-replacement (:instructions argmap) (:replacement-rate argmap))) + ; :uniform-deletion (-> (:plushy (selection/select-parent pop argmap)) (uniform-deletion (:umad-rate argmap))) @@ -127,6 +176,11 @@ (:plushy (selection/select-parent pop argmap)) (:plushy (selection/select-parent pop argmap))) ; + :tail-aligned-diploid-crossover + (tail-aligned-diploid-crossover + (:plushy (selection/select-parent pop argmap)) + (:plushy (selection/select-parent pop argmap))) + ; :diploid-umad (-> (:plushy (selection/select-parent pop argmap)) (diploid-uniform-addition (:instructions argmap) (:umad-rate argmap))