removed simplification complexity from gp.cljc and added it to simplification.cljc.

also updated tests to reflect this
This commit is contained in:
Ryan Boldi 2022-03-03 22:56:10 -05:00
parent 793ff2880d
commit a7b625d942
3 changed files with 11 additions and 11 deletions

View File

@ -60,8 +60,8 @@
(do (prn {:success-generation generation}) (do (prn {:success-generation generation})
(prn {:total-test-error (prn {:total-test-error
(:total-error (error-function argmap (:testing-data argmap) best-individual))}) (:total-error (error-function argmap (:testing-data argmap) best-individual))})
(if (:simplification? argmap) (when (:simplification? argmap)
(let [simplified-plushy (simplification/auto-simplify-plushy argmap (:plushy best-individual) (:simplification-steps argmap) error-function (:training-data argmap) (:simplification-k argmap) (:simplification-verbose? argmap))] (let [simplified-plushy (simplification/auto-simplify-plushy (:plushy best-individual) error-function argmap)]
(prn {:total-test-error-simplified (:total-error (error-function argmap (:testing-data argmap) (hash-map :plushy simplified-plushy)))})))) (prn {:total-test-error-simplified (:total-error (error-function argmap (:testing-data argmap) (hash-map :plushy simplified-plushy)))}))))
;; ;;
(>= generation max-generations) (>= generation max-generations)

View File

@ -13,7 +13,7 @@
"deletes the values at given set of indices" "deletes the values at given set of indices"
[indices plushy] [indices plushy]
(let [sorted-indices (sort > indices)] (let [sorted-indices (sort > indices)]
(keep-indexed #(if (not (some #{%1} sorted-indices)) %2) plushy))) (keep-indexed #(when (not (some #{%1} sorted-indices)) %2) plushy)))
(defn delete-k-random (defn delete-k-random
[k plushy] [k plushy]
@ -21,13 +21,13 @@
(defn auto-simplify-plushy (defn auto-simplify-plushy
"naive auto-simplification" "naive auto-simplification"
[argmap plushy steps error-function training-data k verbose?] [plushy error-function {:keys [simplification-steps training-data simplification-k simplification-verbose?] :as argmap}]
(if verbose? (prn {:start-plushy-length (count plushy) :k k})) (when simplification-verbose? (prn {:start-plushy-length (count plushy) :k simplification-k}))
(let [initial-errors (:errors (error-function argmap training-data {:plushy plushy}))] (let [initial-errors (:errors (error-function argmap training-data {:plushy plushy}))]
(loop [step 0 curr-plushy plushy] (loop [step 0 curr-plushy plushy]
(if (< steps step) (if (< simplification-steps step)
(do (if verbose? (prn {:final-plushy-length (count curr-plushy) :final-plushy curr-plushy})) curr-plushy) (do (when simplification-verbose? (prn {:final-plushy-length (count curr-plushy) :final-plushy curr-plushy})) curr-plushy)
(let [new-plushy (delete-k-random (rand-int k) curr-plushy) (let [new-plushy (delete-k-random (rand-int simplification-k) curr-plushy)
new-plushy-errors (:errors (error-function argmap training-data {:plushy new-plushy})) new-plushy-errors (:errors (error-function argmap training-data {:plushy new-plushy}))
new-equal? (= new-plushy-errors initial-errors)] new-equal? (= new-plushy-errors initial-errors)]
(recur (inc step) (recur (inc step)

View File

@ -71,8 +71,8 @@
(t/testing "auto-simplify-plushy" (t/testing "auto-simplify-plushy"
(t/testing "should handle having an empty plushy" (t/testing "should handle having an empty plushy"
(t/is (= (s/auto-simplify-plushy {} '() 100 (fn [argmap data plushy] 0) {} 3 false) '()))) (t/is (= (s/auto-simplify-plushy '() (fn [argmap data plushy] 0) {:simplification-steps 100 :simplification-k 4 :simplification-verbose? false}) '())))
(let [plushy '(:exec_dup 1 :integer_add close :in1 :integer_add 0 :in1 :in1 :integer_mult :integer_add)] (let [plushy '(:exec_dup 1 :integer_add close :in1 :integer_add 0 :in1 :in1 :integer_mult :integer_add)]
(t/testing "should decrease size of plushy that always has perfect scores" (t/testing "should decrease size of plushy that always has perfect scores"
(t/is (< (count (s/auto-simplify-plushy {} plushy 5 (fn [argmap data plushy] 0) {} 3 false)) (count plushy))) (t/is (< (count (s/auto-simplify-plushy plushy (fn [argmap data plushy] 0) {:simplification-steps 100 :simplification-k 4 :simplification-verbose? false})) (count plushy)))
(t/is (< (count (s/auto-simplify-plushy {} plushy 1 (fn [argmap data plushy] 0) {} 10 false)) (count plushy)))))) (t/is (< (count (s/auto-simplify-plushy plushy (fn [argmap data plushy] 0) {:simplification-steps 100 :simplification-k 10 :simplification-verbose? false})) (count plushy))))))