Implemented max-min, which takes the maximum minimum distance into account instead of average distances

This commit is contained in:
Ryan Boldi 2022-03-29 15:32:34 -04:00
parent bff8f0673c
commit 217f195047
4 changed files with 35 additions and 9 deletions

View File

@ -20,7 +20,7 @@
[training-data {:keys [downsample-rate]}] [training-data {:keys [downsample-rate]}]
(take (int (* downsample-rate (count training-data))) (shuffle training-data))) (take (int (* downsample-rate (count training-data))) (shuffle training-data)))
(defn select-downsample-tournament (defn select-downsample-avg
"uses case-tournament selection to select a downsample that is biased to being spread out" "uses case-tournament selection to select a downsample that is biased to being spread out"
[training-data {:keys [downsample-rate case-t-size]}] [training-data {:keys [downsample-rate case-t-size]}]
(let [shuffled-cases (shuffle training-data) (let [shuffled-cases (shuffle training-data)
@ -42,6 +42,27 @@
(shuffle (concat (utils/drop-nth selected-case-index tournament) (shuffle (concat (utils/drop-nth selected-case-index tournament)
rest-of-cases)))))))) rest-of-cases))))))))
(defn select-downsample-maxmin
"uses tournament selection to select a downsample that has it's cases maximally far away"
[training-data {:keys [downsample-rate case-t-size]}]
(let [shuffled-cases (shuffle training-data)
goal-size (int (* downsample-rate (count training-data)))]
(loop [new-downsample (conj [] (first shuffled-cases))
cases-to-pick-from (rest shuffled-cases)]
(if (>= (count new-downsample) goal-size)
new-downsample
(let [tournament (take case-t-size cases-to-pick-from)
rest-of-cases (drop case-t-size cases-to-pick-from)
min-case-distances (metrics/min-of-colls
(map (fn [distance-list]
(utils/filter-by-index distance-list (map #(:index %) tournament)))
(map #(:distances %) new-downsample)))
selected-case-index (metrics/argmax min-case-distances)]
(prn {:min-case-distances min-case-distances :selected-case-index selected-case-index})
(recur (conj new-downsample (nth tournament selected-case-index))
(shuffle (concat (utils/drop-nth selected-case-index tournament)
rest-of-cases))))))))
(defn select-downsample-metalex (defn select-downsample-metalex
"uses meta-lexicase selection to select a downsample that is biased to being spread out" "uses meta-lexicase selection to select a downsample that is biased to being spread out"
[training-data {:keys [downsample-rate]}]) [training-data {:keys [downsample-rate]}])

View File

@ -52,7 +52,8 @@
(prn {:data (some #(when (zero? (:index %)) %) indexed-training-data)}) (prn {:data (some #(when (zero? (:index %)) %) indexed-training-data)})
(let [training-data (if (= (:parent-selection argmap) :ds-lexicase) (let [training-data (if (= (:parent-selection argmap) :ds-lexicase)
(case (:ds-function argmap) (case (:ds-function argmap)
:case-tournament (downsample/select-downsample-tournament indexed-training-data 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)) (downsample/select-downsample-random indexed-training-data argmap))
indexed-training-data) ;defaults to random indexed-training-data) ;defaults to random
full-evaluated-pop (sort-by :total-error full-evaluated-pop (sort-by :total-error
@ -65,13 +66,8 @@
population)) population))
best-individual (first ds-evaluated-pop) best-individual (first ds-evaluated-pop)
best-individual-passes-ds (and (= (:parent-selection argmap) :ds-lexicase) (<= (:total-error best-individual) solution-error-threshold)) best-individual-passes-ds (and (= (:parent-selection argmap) :ds-lexicase) (<= (:total-error best-individual) solution-error-threshold))
tot-evaluated-pop (when best-individual-passes-ds ;evaluate the whole pop on all training data
(sort-by :total-error
(mapper
(partial error-function argmap (:training-data argmap))
population)))
;;best individual on all training-cases ;;best individual on all training-cases
tot-best-individual (if best-individual-passes-ds (first tot-evaluated-pop) best-individual)] tot-best-individual (if best-individual-passes-ds (first full-evaluated-pop) best-individual)]
(prn (first training-data)) (prn (first training-data))
(if (:custom-report argmap) (if (:custom-report argmap)
((:custom-report argmap) ds-evaluated-pop generation argmap) ((:custom-report argmap) ds-evaluated-pop generation argmap)

View File

@ -18,9 +18,13 @@
(defn mean-of-colls (defn mean-of-colls
"returns the mean of multiple colls" "returns the mean of multiple colls"
[coll] [coll]
;(prn {:func :mean-of-colls :coll coll})
(map mean (math/transpose coll))) (map mean (math/transpose coll)))
(defn min-of-colls
"returns the smallest value of multiple colls"
[coll]
(map #(apply min %) (math/transpose coll)))
(defn median (defn median
"Returns the median of a collection." "Returns the median of a collection."
[coll] [coll]

View File

@ -14,6 +14,11 @@
(t/is (= (m/mean-of-colls '((1 2 3) (4 3 2 1))) '(2.5 2.5 2.5))) (t/is (= (m/mean-of-colls '((1 2 3) (4 3 2 1))) '(2.5 2.5 2.5)))
(t/is (= (m/mean-of-colls '((1))) '(1.0)))) (t/is (= (m/mean-of-colls '((1))) '(1.0))))
(t/deftest min-of-colls-test
(t/is (= (m/min-of-colls '((1 2 3 4) (4 3 2 1))) '(1 2 2 1)))
(t/is (= (m/min-of-colls '((1 2 3) (4 3 2 1))) '(1 2 2)))
(t/is (= (m/min-of-colls '((1))) '(1))))
(t/deftest mean-test (t/deftest mean-test
(t/is (= (m/mean '(1 2 3 4)) 2.5)) (t/is (= (m/mean '(1 2 3 4)) 2.5))
(t/is (= (m/mean '()) 0))) (t/is (= (m/mean '()) 0)))