Problem file adds data to argmap, error function takes data to use, so far just simple-regression.

This commit is contained in:
Lee Spector 2021-07-13 21:47:48 -04:00
parent 09375d1643
commit 719b2c6444
2 changed files with 20 additions and 28 deletions

View File

@ -52,16 +52,18 @@
(let [evaluated-pop (sort-by :total-error (let [evaluated-pop (sort-by :total-error
(#?(:clj pmap (#?(:clj pmap
:cljs map) :cljs map)
(partial error-function argmap) population)) (partial error-function argmap (:training-data argmap))
population))
best-individual (first evaluated-pop)] best-individual (first evaluated-pop)]
(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)) (zero? (:total-error best-individual))
(do (prn {:success-generation generation}) (do (prn {:success-generation generation})
(prn {:total-test-error (:total-error (error-function argmap best-individual :test))}) (prn {:total-test-error (:total-error (error-function argmap
(#?(:clj shutdown-agents)) (:testing-data argmap)
) best-individual))})
(#?(:clj shutdown-agents)))
;; ;;
(>= generation max-generations) (>= generation max-generations)
nil nil

View File

@ -4,21 +4,21 @@
[propeller.push.state :as state] [propeller.push.state :as state]
[propeller.tools.math :as math])) [propeller.tools.math :as math]))
;; =============================================================================
;; Problem: f(x) = 7x^2 - 20x + 13
;; =============================================================================
(defn- target-function-hard
"Target function: f(x) = 7x^2 - 20x + 13"
[x]
(+ (* 7 x x) (* -20 x) 13))
(defn- target-function (defn- target-function
"Target function: f(x) = x^3 + x + 3" "Target function: f(x) = x^3 + x + 3"
[x] [x]
(+ (* x x x) x 3)) (+ (* x x x) x 3))
; Set of original propel instructions (def training-data
(let [training-inputs (range -10 11)]
{:inputs training-inputs
:outputs (map target-function training-inputs)}))
(def testing-data
(let [testing-inputs (concat (range -20 -10) (range 11 21))]
{:inputs testing-inputs
:outputs (map target-function testing-inputs)}))
(def instructions (def instructions
(list :in1 (list :in1
:integer_add :integer_add
@ -32,25 +32,13 @@
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 data 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)
data (get (train-and-test-data target-function) subset)
inputs (:inputs data) inputs (:inputs data)
correct-outputs (:outputs data) correct-outputs (:outputs data)
outputs (map (fn [input] outputs (map (fn [input]
@ -75,4 +63,6 @@
(def arglist (def arglist
{:instructions instructions {:instructions instructions
:error-function error-function}) :error-function error-function
:training-data training-data
:testing-data testing-data})