From 7ca3767b0267d9d529596558e0dc094b4cb64a58 Mon Sep 17 00:00:00 2001 From: Ryan Boldi Date: Tue, 7 Nov 2023 22:33:58 -0500 Subject: [PATCH] remove hacky hyperselection code --- src/propeller/gp.cljc | 15 ++++------ src/propeller/hyperselection.cljc | 36 ------------------------ src/propeller/variation.cljc | 8 ++---- test/propeller/push/downsample_test.cljc | 28 ++---------------- test/propeller/utils_test.cljc | 3 +- 5 files changed, 12 insertions(+), 78 deletions(-) delete mode 100644 src/propeller/hyperselection.cljc diff --git a/src/propeller/gp.cljc b/src/propeller/gp.cljc index 9b1d3a5..7c74af6 100644 --- a/src/propeller/gp.cljc +++ b/src/propeller/gp.cljc @@ -5,7 +5,6 @@ [propeller.simplification :as simplification] [propeller.variation :as variation] [propeller.downsample :as downsample] - [propeller.hyperselection :as hyperselection] [propeller.push.instructions.bool] [propeller.push.instructions.character] [propeller.push.instructions.code] @@ -125,17 +124,15 @@ :else (recur (inc generation) (+ evaluations (* population-size (count training-data)) ;every member evaluated on the current sample (if (zero? (mod generation ds-parent-gens)) (* (count parent-reps) (- (count indexed-training-data) (count training-data))) 0) ; the parent-reps not evaluted already on down-sample - (if best-individual-passes-ds (- (count indexed-training-data) (count training-data)) 0)) ; if we checked for generalization or not - (let [reindexed-pop (hyperselection/reindex-pop evaluated-pop argmap)] ; give every individual an index for hyperselection loggin - (hyperselection/log-hyperselection-and-ret - (if (:elitism argmap) - (conj (utils/pmapallv (fn [_] (variation/new-individual reindexed-pop argmap)) + (if best-individual-passes-ds (- (count indexed-training-data) (count training-data)) 0)) ; if we checked for generalization or not + (if (:elitism argmap) + (conj (utils/pmapallv (fn [_] (variation/new-individual evaluated-pop argmap)) (range (dec population-size)) argmap) - (first reindexed-pop)) ;elitism maintains the most-fit individual - (utils/pmapallv (fn [_] (variation/new-individual reindexed-pop argmap)) + (first evaluated-pop)) ;elitism maintains the most-fit individual + (utils/pmapallv (fn [_] (variation/new-individual evaluated-pop argmap)) (range population-size) - argmap)))) + argmap)) (if downsample? (if (zero? (mod generation ds-parent-gens)) (downsample/update-case-distances rep-evaluated-pop indexed-training-data indexed-training-data ids-type (/ solution-error-threshold (count indexed-training-data))) ; update distances every ds-parent-gens generations diff --git a/src/propeller/hyperselection.cljc b/src/propeller/hyperselection.cljc deleted file mode 100644 index 116f761..0000000 --- a/src/propeller/hyperselection.cljc +++ /dev/null @@ -1,36 +0,0 @@ -(ns propeller.hyperselection - (:require [propeller.utils :as utils])) - -(defn sum-list-map-indices - "sums a list of maps that have the :index property's index multiplicity" - [list-of-maps] - (->> list-of-maps - (map #(:index %)) - frequencies)) - -(defn ordered-freqs - "takes a map from indices to frequencies, and returns a sorted list of the frequences is descencing order" - [freqs] - (->> freqs - vals - (sort >))) - -(defn normalize-list-by-popsize [popsize lst] - (map #(double (/ % popsize)) lst)) - -(defn hyperselection-track - "outputs a normalized list of the hyperselection proportion for each parent" - [new-pop] - (->> new-pop - sum-list-map-indices - ordered-freqs - (normalize-list-by-popsize (count new-pop)))) - -(defn log-hyperselection-and-ret [new-pop] - (prn {:hyperselection (hyperselection-track new-pop)}) - new-pop) - -(defn reindex-pop - "assigns each member of the population a unique index before selection to track hyperselection" - [pop argmap] - (utils/pmapallv (fn [indiv index] (assoc indiv :index index)) pop (range (count pop)) argmap)) \ No newline at end of file diff --git a/src/propeller/variation.cljc b/src/propeller/variation.cljc index e4825ab..8af9fd3 100644 --- a/src/propeller/variation.cljc +++ b/src/propeller/variation.cljc @@ -252,10 +252,8 @@ The function `new-individual` returns a new individual produced by selection and (defn new-individual "Returns a new individual produced by selection and variation of individuals in the population." - [pop argmap] - (let [umad-parent (selection/select-parent pop argmap) - parent-ind (:index umad-parent)] ;this is a hack to log hyperselection, only works for umad - {:plushy + [pop argmap] + {:plushy (let [r (rand) op (loop [accum 0.0 ops-probs (vec (:variation argmap))] @@ -347,4 +345,4 @@ The function `new-individual` returns a new individual produced by selection and :else (throw #?(:clj (Exception. (str "No match in new-individual for " op)) :cljs (js/Error - (str "No match in new-individual for " op))))))})) + (str "No match in new-individual for " op))))))}) diff --git a/test/propeller/push/downsample_test.cljc b/test/propeller/push/downsample_test.cljc index 517f421..0e6113f 100644 --- a/test/propeller/push/downsample_test.cljc +++ b/test/propeller/push/downsample_test.cljc @@ -2,8 +2,7 @@ (:require [clojure.test :as t] [propeller.utils :as u] [propeller.simplification :as s] - [propeller.downsample :as ds] - [propeller.hyperselection :as hs])) + [propeller.downsample :as ds])) (t/deftest assign-indices-to-data-test @@ -137,27 +136,4 @@ {:input1 [4] :output1 [14] :index 4 :distances [0 0 0 0 0]}) {:case-delta 0})] (prn {:selected selected}) - (t/is (= 1 (count selected)))))) - - -(t/deftest hyperselection-test - (let [parents1 '({:blah 3 :index 1} {:blah 3 :index 1} - {:blah 3 :index 1} {:blah 3 :index 2}) - parents2 '({:plushy 2 :index 0} {:blah 3 :index 2} - {:blah 3 :index 3} {:index 4}) - emptyparents '({:blah 1} {:blah 1} {:blah 1})] - (t/testing "sum-list-map-indices function works correctly" - (t/is (= {1 3, 2 1} (hs/sum-list-map-indices parents1))) - (t/is (= {0 1, 2 1, 3 1, 4 1} (hs/sum-list-map-indices parents2)))) - (t/testing "ordered-freqs function works correctly" - (t/is (= '(3 1) (hs/ordered-freqs (hs/sum-list-map-indices parents1)))) - (t/is (= '(1 1 1 1) (hs/ordered-freqs (hs/sum-list-map-indices parents2))))) - (t/testing "hyperselection-track works correctly" - (t/is (= '(0.75 0.25) (hs/hyperselection-track parents1))) - (t/is (= '(0.25 0.25 0.25 0.25) (hs/hyperselection-track parents2)))) - (t/testing "reindex-pop works correctly" - (t/is (= '({:blah 3 :index 0} {:blah 3 :index 1} - {:blah 3 :index 2} {:blah 3 :index 3}) (hs/reindex-pop parents1 {}))) - (t/is (= '({:plushy 2 :index 0} {:blah 3 :index 1} - {:blah 3 :index 2} {:index 3}) (hs/reindex-pop parents2 {}))) - (t/is (= '({:blah 1 :index 0} {:blah 1 :index 1} {:blah 1 :index 2}) (hs/reindex-pop emptyparents {})))))) + (t/is (= 1 (count selected)))))) \ No newline at end of file diff --git a/test/propeller/utils_test.cljc b/test/propeller/utils_test.cljc index cc36fec..1461134 100644 --- a/test/propeller/utils_test.cljc +++ b/test/propeller/utils_test.cljc @@ -2,8 +2,7 @@ (:require [clojure.test :as t] [propeller.utils :as u] [propeller.simplification :as s] - [propeller.downsample :as ds] - [propeller.hyperselection :as hs])) + [propeller.downsample :as ds])) (t/deftest first-non-nil-test (t/is (= 1 (u/first-non-nil '(1 2 3))))