Add train/test data support to simple-regression problem

This commit is contained in:
Lee Spector 2020-11-23 09:51:19 -05:00
parent 509c22b79c
commit 3ba030ceec

View File

@ -32,15 +32,27 @@
0 0
1)) 1))
(defn train-and-test-data
[target-function]
(let [train-inputs (range -10 11)
test-inputs (concat (range -20 -10) (range 11 21))]
{:train {:inputs train-inputs
:outputs (map target-function train-inputs)}
:test {:inputs test-inputs
:outputs (map target-function test-inputs)}}))
(defn error-function (defn error-function
"Finds the behaviors and errors of an individual. The error is the absolute "Finds the behaviors and errors of an individual. The error is the absolute
deviation between the target output value and the program's selected behavior, deviation between the target output value and the program's selected behavior,
or 1000000 if no behavior is produced. The behavior is here defined as the or 1000000 if no behavior is produced. The behavior is here defined as the
final top item on the INTEGER stack." final top item on the INTEGER stack."
[argmap individual] ([argmap individual]
(error-function argmap individual :train))
([argmap individual subset]
(let [program (genome/plushy->push (:plushy individual) argmap) (let [program (genome/plushy->push (:plushy individual) argmap)
inputs (range -10 11) data (get (train-and-test-data target-function) subset)
correct-outputs (map target-function inputs) inputs (:inputs data)
correct-outputs (:outputs data)
outputs (map (fn [input] outputs (map (fn [input]
(state/peek-stack (state/peek-stack
(interpreter/interpret-program (interpreter/interpret-program
@ -59,4 +71,4 @@
:behaviors outputs :behaviors outputs
:errors errors :errors errors
:total-error #?(:clj (apply +' errors) :total-error #?(:clj (apply +' errors)
:cljs (apply + errors))))) :cljs (apply + errors))))))