Parameterizes solution-error-threshold and mapper with defaults

This commit is contained in:
erp12 2021-10-04 11:51:41 -04:00
parent f1032ba904
commit bdf278af8f

View File

@ -31,21 +31,22 @@
(defn gp (defn gp
"Main GP loop." "Main GP loop."
[{:keys [population-size max-generations error-function instructions [{:keys [population-size max-generations error-function instructions
max-initial-plushy-size] max-initial-plushy-size solution-error-threshold mapper]
: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`.
mapper #?(:clj pmap :cljs map)}
:as argmap}] :as argmap}]
;; ;;
(prn {:starting-args (update (update argmap :error-function str) :instructions str)}) (prn {:starting-args (update (update argmap :error-function str) :instructions str)})
(println) (println)
;; ;;
(loop [generation 0 (loop [generation 0
population (repeatedly population (mapper
population-size (fn [_] {:plushy (genome/make-random-plushy instructions max-initial-plushy-size)})
#(hash-map :plushy (genome/make-random-plushy (range population-size))]
instructions
max-initial-plushy-size)))]
(let [evaluated-pop (sort-by :total-error (let [evaluated-pop (sort-by :total-error
(#?(:clj pmap (mapper
:cljs map)
(partial error-function argmap (:training-data argmap)) (partial error-function argmap (:training-data argmap))
population)) population))
best-individual (first evaluated-pop)] best-individual (first evaluated-pop)]
@ -54,11 +55,10 @@
(report evaluated-pop generation argmap)) (report evaluated-pop generation argmap))
(cond (cond
;; Success on training cases is verified on testing cases ;; Success on training cases is verified on testing cases
(zero? (:total-error best-individual)) (<= (:total-error best-individual) solution-error-threshold)
(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))}))
(#?(:clj shutdown-agents)))
;; ;;
(>= generation max-generations) (>= generation max-generations)
nil nil