Make arglist in problem file work for first PSB2 problem (basement)

This commit is contained in:
Lee Spector 2021-07-13 22:21:20 -04:00
parent d3e2adb575
commit a571524dab
2 changed files with 42 additions and 46 deletions

View File

@ -10,8 +10,7 @@
[propeller.push.instructions.numeric] [propeller.push.instructions.numeric]
[propeller.push.instructions.polymorphic] [propeller.push.instructions.polymorphic]
[propeller.push.instructions.string] [propeller.push.instructions.string]
[propeller.push.instructions.vector] [propeller.push.instructions.vector]))
[psb2.core :as psb2]))
(defn report (defn report
"Reports information each generation." "Reports information each generation."
@ -32,46 +31,40 @@
(defn gp (defn gp
"Main GP loop." "Main GP loop."
[{:keys [population-size max-generations error-function instructions [{:keys [population-size max-generations error-function instructions
max-initial-plushy-size PSB2-path PSB2-problem] max-initial-plushy-size]
:as argmap}] :as argmap}]
;; ;;
(prn {:starting-args (update (update argmap :error-function str) :instructions str)}) (prn {:starting-args (update (update argmap :error-function str) :instructions str)})
(println) (println)
;; ;;
(let [PSB2-data (if (= PSB2-path "") (loop [generation 0
#{} population (repeatedly
(psb2/fetch-examples PSB2-path PSB2-problem 200 2000)) population-size
argmap (assoc argmap :train-and-test-data PSB2-data)] #(hash-map :plushy (genome/make-random-plushy
instructions
(loop [generation 0 max-initial-plushy-size)))]
population (repeatedly (let [evaluated-pop (sort-by :total-error
population-size (#?(:clj pmap
#(hash-map :plushy (genome/make-random-plushy :cljs map)
instructions (partial error-function argmap (:training-data argmap))
max-initial-plushy-size)))] population))
(let [evaluated-pop (sort-by :total-error best-individual (first evaluated-pop)]
(#?(:clj pmap (report evaluated-pop generation argmap)
:cljs map) (cond
(partial error-function argmap (:training-data argmap)) ;; Success on training cases is verified on testing cases
population)) (zero? (:total-error best-individual))
best-individual (first evaluated-pop)] (do (prn {:success-generation generation})
(report evaluated-pop generation argmap) (prn {:total-test-error
(cond (:total-error (error-function argmap (:testing-data argmap) best-individual))})
;; Success on training cases is verified on testing cases (#?(:clj shutdown-agents)))
(zero? (:total-error best-individual)) ;;
(do (prn {:success-generation generation}) (>= generation max-generations)
(prn {:total-test-error (:total-error (error-function argmap nil
(:testing-data argmap) ;;
best-individual))}) :else (recur (inc generation)
(#?(:clj shutdown-agents))) (if (:elitism argmap)
;; (conj (repeatedly (dec population-size)
(>= generation max-generations) #(variation/new-individual evaluated-pop argmap))
nil (first evaluated-pop))
;; (repeatedly population-size
:else (recur (inc generation) #(variation/new-individual evaluated-pop argmap))))))))
(if (:elitism argmap)
(conj (repeatedly (dec population-size)
#(variation/new-individual evaluated-pop argmap))
(first evaluated-pop))
(repeatedly population-size
#(variation/new-individual evaluated-pop argmap)))))))))

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 ============================
; BASEMENT from PSB2 ; BASEMENT from PSB2
@ -17,6 +18,7 @@
; 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" "basement" 200 2000))
; Random integer between -100 and 100 (from smallest) ; Random integer between -100 and 100 (from smallest)
(defn random-int [] (- (rand-int 201) 100)) (defn random-int [] (- (rand-int 201) 100))
@ -34,11 +36,8 @@
(list random-int -1 0 1 [])))) (list random-int -1 0 1 []))))
(defn error-function (defn error-function
([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 (get argmap :train-and-test-data) subset)
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]
@ -61,5 +60,9 @@
:total-error #?(:clj (apply +' errors) :total-error #?(:clj (apply +' errors)
:cljs (apply + errors)))))) :cljs (apply + errors))))))
(def arglist
{:instructions instructions
:error-function error-function
:training-data (:train train-and-test-data)
:testing-data (:test train-and-test-data)})