Use pmapallv for evaluation in GP, after debugging and adding shutdown-agents

This commit is contained in:
Lee Spector 2023-08-30 12:36:47 -04:00
parent 4faf2300ee
commit a2192ca37d
2 changed files with 24 additions and 17 deletions

View File

@ -13,7 +13,8 @@
[propeller.push.instructions.polymorphic] [propeller.push.instructions.polymorphic]
[propeller.push.instructions.string] [propeller.push.instructions.string]
[propeller.push.instructions.vector] [propeller.push.instructions.vector]
[propeller.selection :as selection])) [propeller.selection :as selection]
[propeller.utils :as utils]))
(defn report (defn report
"Reports information for each generation." "Reports information for each generation."
@ -34,24 +35,23 @@
(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 solution-error-threshold mapper] max-initial-plushy-size solution-error-threshold]
:or {solution-error-threshold 0.0 :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 (mapper population (utils/pmapallv
(fn [_] {:plushy (genome/make-random-plushy instructions max-initial-plushy-size)}) (fn [_] {:plushy (genome/make-random-plushy instructions max-initial-plushy-size)})
(range population-size))] ;creates population of random plushys (range population-size)
argmap)] ;creates population of random plushys
(let [evaluated-pop (sort-by :total-error (let [evaluated-pop (sort-by :total-error
(mapper (utils/pmapallv
(partial error-function argmap (:training-data argmap)) (partial error-function argmap (:training-data argmap))
population)) ;population sorted by :total-error population ;population sorted by :total-error
argmap))
best-individual (first evaluated-pop) best-individual (first evaluated-pop)
argmap (if (= (:parent-selection argmap) :epsilon-lexicase) argmap (if (= (:parent-selection argmap) :epsilon-lexicase)
(assoc argmap :epsilons (selection/epsilon-list evaluated-pop)) (assoc argmap :epsilons (selection/epsilon-list evaluated-pop))
@ -66,11 +66,17 @@
(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))})
(when (:simplification? argmap) (when (:simplification? argmap)
(let [simplified-plushy (simplification/auto-simplify-plushy (:plushy best-individual) error-function argmap)] (let [simplified-plushy (simplification/auto-simplify-plushy
(prn {:total-test-error-simplified (:total-error (error-function argmap (:testing-data argmap) (hash-map :plushy simplified-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)))})))
#?(:clj (shutdown-agents)))
;; ;;
(>= generation max-generations) (>= generation max-generations)
nil #?(:clj (shutdown-agents))
;; ;;
:else (recur (inc generation) :else (recur (inc generation)
(if (:elitism argmap) (if (:elitism argmap)

View File

@ -1,6 +1,7 @@
(ns propeller.utils (ns propeller.utils
"Useful functions." "Useful functions."
(:require [clojure.zip :as zip])) (:require [clojure.zip :as zip]
[clojure.repl :as repl]))
(defn first-non-nil (defn first-non-nil
"Returns the first non-nil values from the collection, or returns `nil` if "Returns the first non-nil values from the collection, or returns `nil` if
@ -97,7 +98,7 @@ multicore processor utilization, and 4) takes only one coll so far."
(doall (map f coll)) (doall (map f coll))
(let [agents (map #(agent % :error-handler (let [agents (map #(agent % :error-handler
(fn [agnt except] (fn [agnt except]
(clojure.repl/pst except 1000) (repl/pst except 1000)
(System/exit 0))) (System/exit 0)))
coll)] coll)]
(dorun (map #(send % f) agents)) (dorun (map #(send % f) agents))