From 667b04312bbf4b40f9a05ed7641028c775f890dd Mon Sep 17 00:00:00 2001 From: Ryan Boldi Date: Sat, 14 May 2022 19:49:51 -0400 Subject: [PATCH] added ability to evaluate individuals every k generations --- src/propeller/gp.cljc | 11 ++++++++--- src/propeller/problems/PSB2/fizz_buzz.cljc | 3 +++ src/propeller/problems/PSB2/fuel_cost.cljc | 3 +++ src/propeller/problems/PSB2/gcd.cljc | 5 ++++- 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/propeller/gp.cljc b/src/propeller/gp.cljc index 9b1f1e9..9668a6f 100644 --- a/src/propeller/gp.cljc +++ b/src/propeller/gp.cljc @@ -33,7 +33,7 @@ (defn gp "Main GP loop." [{:keys [population-size max-generations error-function instructions - max-initial-plushy-size solution-error-threshold mapper ds-parent-rate] + max-initial-plushy-size solution-error-threshold mapper ds-parent-rate ds-parent-gens] :or {solution-error-threshold 0.0 ;; The `mapper` will perform a `map`-like operation to apply a function to every individual ;; in the population. The default is `map` but other options include `mapv`, or `pmap`. @@ -48,13 +48,16 @@ (fn [_] {:plushy (genome/make-random-plushy instructions max-initial-plushy-size)}) (range population-size)) indexed-training-data (downsample/assign-indices-to-data (downsample/initialize-case-distances argmap))] + (prn {:ind-training-data (first indexed-training-data)}) (let [training-data (if (= (:parent-selection argmap) :ds-lexicase) (case (:ds-function argmap) :case-avg (downsample/select-downsample-avg indexed-training-data argmap) :case-maxmin (downsample/select-downsample-maxmin indexed-training-data argmap) (downsample/select-downsample-random indexed-training-data argmap)) indexed-training-data) ;defaults to random - parent-reps (take (* ds-parent-rate (count population)) (shuffle population)) + parent-reps (if (zero? (mod generation ds-parent-gens)) ;every ds-parent-gens generations + (take (* ds-parent-rate (count population)) (shuffle population)) + '()) ;else just empty list rep-evaluated-pop (sort-by :total-error (mapper (partial error-function argmap indexed-training-data) @@ -97,5 +100,7 @@ (repeatedly population-size #(variation/new-individual ds-evaluated-pop argmap))) (if (= (:parent-selection argmap) :ds-lexicase) - (downsample/update-case-distances rep-evaluated-pop indexed-training-data indexed-training-data) ; update distances every generation + (if (zero? (mod generation ds-parent-gens)) + (downsample/update-case-distances rep-evaluated-pop indexed-training-data indexed-training-data) ; update distances every ds-parent-gens generations + indexed-training-data) indexed-training-data)))))) \ No newline at end of file diff --git a/src/propeller/problems/PSB2/fizz_buzz.cljc b/src/propeller/problems/PSB2/fizz_buzz.cljc index ee1f536..d59fdda 100644 --- a/src/propeller/problems/PSB2/fizz_buzz.cljc +++ b/src/propeller/problems/PSB2/fizz_buzz.cljc @@ -68,6 +68,9 @@ :error-function error-function :training-data (:train train-and-test-data) :testing-data (:test train-and-test-data) + :case-t-size (count (:train train-and-test-data)) + :case-parent-rate 0 + :case-parent-gens 1 :max-generations 300 :population-size 1000 :max-initial-plushy-size 250 diff --git a/src/propeller/problems/PSB2/fuel_cost.cljc b/src/propeller/problems/PSB2/fuel_cost.cljc index 9d45455..1953356 100644 --- a/src/propeller/problems/PSB2/fuel_cost.cljc +++ b/src/propeller/problems/PSB2/fuel_cost.cljc @@ -70,6 +70,9 @@ :error-function error-function :training-data (:train train-and-test-data) :testing-data (:test train-and-test-data) + :case-t-size (count (:train train-and-test-data)) + :case-parent-rate 0 + :case-parent-gens 1 :max-generations 300 :population-size 1000 :max-initial-plushy-size 250 diff --git a/src/propeller/problems/PSB2/gcd.cljc b/src/propeller/problems/PSB2/gcd.cljc index f835853..209900e 100644 --- a/src/propeller/problems/PSB2/gcd.cljc +++ b/src/propeller/problems/PSB2/gcd.cljc @@ -78,6 +78,9 @@ :error-function error-function :training-data (:train train-and-test-data) :testing-data (:test train-and-test-data) + :case-t-size (count (:train train-and-test-data)) + :case-parent-rate 0 + :case-parent-gens 1 :max-generations 300 :population-size 1000 :max-initial-plushy-size 250 @@ -88,4 +91,4 @@ :variation {:umad 1.0 :crossover 0.0} :elitism false} (apply hash-map (map #(if (string? %) (read-string %) %) args)))) - (#?(:clj shutdown-agents))) + (#?(:clj shutdown-agents))) \ No newline at end of file