add get-brain-less-than to get a brain of a specific quality

This commit is contained in:
mehbark 2023-01-11 22:12:18 -05:00
parent 0f3e55b472
commit 86f84db19a

View file

@ -1,3 +1,5 @@
;;TODO: actually interesting brains, refactor, better children handling
(defmacro r (defmacro r
[] []
'(use 'nn.core :reload) '(use 'nn.core :reload)
@ -33,7 +35,9 @@
(defn min-key-seq (defn min-key-seq
"The minimum of a seq by key" "The minimum of a seq by key"
[k seq] [k seq]
(apply min-key k seq)) (if (empty? seq)
nil
(apply min-key k seq)))
(defn nn (defn nn
"Create a \"neural network\" from a brain, an activation function, and a variation function (that takes a number)" "Create a \"neural network\" from a brain, an activation function, and a variation function (that takes a number)"
@ -247,6 +251,12 @@
(map (partial score-with-evolver evolver)) (map (partial score-with-evolver evolver))
min-seq)) min-seq))
(defn best-brain
"Get the best brain of an evolver"
[evolver]
(min-key-seq (partial score-with-evolver evolver)
(:brains evolver)))
(defn worst-score (defn worst-score
"Get the best score of an evolver" "Get the best score of an evolver"
[evolver] [evolver]
@ -265,3 +275,13 @@
(map (comp str best-score) (map (comp str best-score)
(take 1000 (evolution (n-multiplier-evolver n))))) (take 1000 (evolution (n-multiplier-evolver n)))))
"]")) "]"))
(defn get-brain-less-than
"Evolve an evolver until it's best brain's score is less than the given value,
then return the best brain"
[n evolver]
(->> evolver
evolution
(take-while #(> (best-score %) n))
last
best-brain))