Update dice-game for new problem specification scheme

This commit is contained in:
Lee Spector 2021-07-13 22:48:54 -04:00
parent b3b1363942
commit e51bdf2fab
2 changed files with 60 additions and 55 deletions

View File

@ -65,34 +65,34 @@
(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))
:string)) :string))
inputs) inputs)
parsed-outputs (map (fn [output] parsed-outputs (map (fn [output]
(try (read-string output) (try (read-string output)
#?(:clj (catch Exception e 1000.0) #?(:clj (catch Exception e 1000.0)
:cljs (catch js/Error. e 1000.0)))) :cljs (catch js/Error. e 1000.0))))
outputs) outputs)
errors (map (fn [correct-output output] errors (map (fn [correct-output output]
(if (= output :no-stack-item) (if (= output :no-stack-item)
10000 10000
(metrics/levenshtein-distance correct-output output))) (metrics/levenshtein-distance correct-output output)))
correct-outputs correct-outputs
parsed-outputs)] parsed-outputs)]
(assoc individual (assoc individual
:behaviors parsed-outputs :behaviors parsed-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

View File

@ -17,6 +17,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" "dice-game" 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]
@ -40,30 +42,33 @@
(list 0.0 1.0)))) (list 0.0 1.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 (:step-limit argmap))
(assoc state/empty-state :input {:in1 (nth input 0) :float))
:in2 (nth input 1)}) 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)})