Update bouncing-balls for new problem specification scheme

This commit is contained in:
Lee Spector 2021-07-13 22:31:59 -04:00
parent a571524dab
commit 5aaf914316
2 changed files with 61 additions and 55 deletions

View File

@ -36,33 +36,33 @@
(list random-int -1 0 1 [])))) (list random-int -1 0 1 []))))
(defn error-function (defn error-function
([argmap data individual] [argmap data individual]
(let [program (genome/plushy->push (:plushy individual) argmap) (let [program (genome/plushy->push (:plushy individual) argmap)
inputs (map (fn [i] (get i :input1)) data) inputs (map (fn [i] (get i :input1)) data)
correct-outputs (map (fn [i] (get i :output1)) data) correct-outputs (map (fn [i] (get i :output1)) data)
outputs (map (fn [input] outputs (map (fn [input]
(state/peek-stack (state/peek-stack
(interpreter/interpret-program (interpreter/interpret-program
program program
(assoc state/empty-state :input {:in1 input}) (assoc state/empty-state :input {:in1 input})
(:step-limit argmap)) (:step-limit argmap))
:integer)) :integer))
inputs) inputs)
errors (map (fn [correct-output output] errors (map (fn [correct-output output]
(if (= output :no-stack-item) (if (= output :no-stack-item)
1000000 1000000
(min 1000.0 (math/abs (- correct-output output))))) (min 1000.0 (math/abs (- correct-output output)))))
correct-outputs correct-outputs
outputs)] outputs)]
(assoc individual (assoc individual
:behaviors outputs :behaviors outputs
:errors errors :errors errors
:total-error #?(:clj (apply +' errors) :total-error #?(:clj (apply +' errors)
:cljs (apply + errors)))))) :cljs (apply + errors)))))
(def arglist (def arglist
{:instructions instructions {:instructions instructions
:error-function error-function :error-function error-function
:training-data (:train train-and-test-data) :training-data (:train train-and-test-data)
:testing-data (:test train-and-test-data)}) :testing-data (:test train-and-test-data)})

View File

@ -6,7 +6,8 @@
[propeller.push.utils.helpers :refer [get-stack-instructions]] [propeller.push.utils.helpers :refer [get-stack-instructions]]
[propeller.push.state :as state] [propeller.push.state :as state]
[clojure.pprint :as pprint] [clojure.pprint :as pprint]
[propeller.tools.math :as math])) [propeller.tools.math :as math]
[psb2.core :as psb2]))
; =========== PROBLEM DESCRIPTION =============================== ; =========== PROBLEM DESCRIPTION ===============================
; BOUNCING BALLS from PSB2 ; BOUNCING BALLS from PSB2
@ -19,6 +20,8 @@
; Source: https://arxiv.org/pdf/2106.06086.pdf ; Source: https://arxiv.org/pdf/2106.06086.pdf
; ================================================================== ; ==================================================================
(def train-and-test-data (psb2/fetch-examples "data" "bouncing-balls" 200 2000))
(defn map-vals-input (defn map-vals-input
"Returns all the input values of a map (specific helper method for bouncing-balls)" "Returns all the input values of a map (specific helper method for bouncing-balls)"
[i] [i]
@ -42,31 +45,34 @@
(list 0.0 1.0 2.0)))) (list 0.0 1.0 2.0))))
(defn error-function (defn error-function
([argmap individual] [argmap data individual]
(error-function argmap individual :train)) (let [program (genome/plushy->push (:plushy individual) argmap)
([argmap individual subset] inputs (map (fn [i] (map-vals-input i)) data)
(let [program (genome/plushy->push (:plushy individual) argmap) correct-outputs (map (fn [i] (map-vals-output i)) data)
data (get (get argmap :train-and-test-data) subset) outputs (map (fn [input]
inputs (map (fn [i] (map-vals-input i)) data) (state/peek-stack
correct-outputs (map (fn [i] (map-vals-output i)) data) (interpreter/interpret-program
outputs (map (fn [input] program
(state/peek-stack (assoc state/empty-state :input {:in1 (nth input 0)
(interpreter/interpret-program :in2 (nth input 1)
program :in3 (nth input 2)})
(assoc state/empty-state :input {:in1 (nth input 0) (:step-limit argmap))
:in2 (nth input 1) :float))
:in3 (nth input 2)}) inputs)
(:step-limit argmap)) errors (map (fn [correct-output output]
:float)) (if (= output :no-stack-item)
inputs) 1000000.0
errors (map (fn [correct-output output] (min 1000.0 (math/abs (- correct-output output)))))
(if (= output :no-stack-item) correct-outputs
1000000.0 outputs)]
(min 1000.0 (math/abs (- correct-output output))))) (assoc individual
correct-outputs :behaviors outputs
outputs)] :errors errors
(assoc individual :total-error #?(:clj (apply +' errors)
:behaviors outputs :cljs (apply + errors)))))
:errors errors
:total-error #?(:clj (apply +' errors) (def arglist
:cljs (apply + errors)))))) {:instructions instructions
:error-function error-function
:training-data (:train train-and-test-data)
:testing-data (:test train-and-test-data)})