diff --git a/src/propeller/gp.cljc b/src/propeller/gp.cljc index 2a3eaff..1ad3f71 100755 --- a/src/propeller/gp.cljc +++ b/src/propeller/gp.cljc @@ -10,8 +10,7 @@ [propeller.push.instructions.numeric] [propeller.push.instructions.polymorphic] [propeller.push.instructions.string] - [propeller.push.instructions.vector] - [psb2.core :as psb2])) + [propeller.push.instructions.vector])) (defn report "Reports information each generation." @@ -32,46 +31,40 @@ (defn gp "Main GP loop." [{:keys [population-size max-generations error-function instructions - max-initial-plushy-size PSB2-path PSB2-problem] + max-initial-plushy-size] :as argmap}] ;; (prn {:starting-args (update (update argmap :error-function str) :instructions str)}) (println) ;; - (let [PSB2-data (if (= PSB2-path "") - #{} - (psb2/fetch-examples PSB2-path PSB2-problem 200 2000)) - argmap (assoc argmap :train-and-test-data PSB2-data)] - - (loop [generation 0 - population (repeatedly - population-size - #(hash-map :plushy (genome/make-random-plushy - instructions - max-initial-plushy-size)))] - (let [evaluated-pop (sort-by :total-error - (#?(:clj pmap - :cljs map) - (partial error-function argmap (:training-data argmap)) - population)) - best-individual (first evaluated-pop)] - (report evaluated-pop generation argmap) - (cond - ;; Success on training cases is verified on testing cases - (zero? (:total-error best-individual)) - (do (prn {:success-generation generation}) - (prn {:total-test-error (:total-error (error-function argmap - (:testing-data argmap) - best-individual))}) - (#?(:clj shutdown-agents))) - ;; - (>= generation max-generations) - nil - ;; - :else (recur (inc generation) - (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))))))))) + (loop [generation 0 + population (repeatedly + population-size + #(hash-map :plushy (genome/make-random-plushy + instructions + max-initial-plushy-size)))] + (let [evaluated-pop (sort-by :total-error + (#?(:clj pmap + :cljs map) + (partial error-function argmap (:training-data argmap)) + population)) + best-individual (first evaluated-pop)] + (report evaluated-pop generation argmap) + (cond + ;; Success on training cases is verified on testing cases + (zero? (:total-error best-individual)) + (do (prn {:success-generation generation}) + (prn {:total-test-error + (:total-error (error-function argmap (:testing-data argmap) best-individual))}) + (#?(:clj shutdown-agents))) + ;; + (>= generation max-generations) + nil + ;; + :else (recur (inc generation) + (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)))))))) diff --git a/src/propeller/problems/PSB2/basement.cljc b/src/propeller/problems/PSB2/basement.cljc index 643189b..54e8523 100644 --- a/src/propeller/problems/PSB2/basement.cljc +++ b/src/propeller/problems/PSB2/basement.cljc @@ -6,7 +6,8 @@ [propeller.push.utils.helpers :refer [get-stack-instructions]] [propeller.push.state :as state] [clojure.pprint :as pprint] - [propeller.tools.math :as math])) + [propeller.tools.math :as math] + [psb2.core :as psb2])) ; =========== PROBLEM DESCRIPTION ============================ ; BASEMENT from PSB2 @@ -17,6 +18,7 @@ ; 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) (defn random-int [] (- (rand-int 201) 100)) @@ -34,11 +36,8 @@ (list random-int -1 0 1 [])))) (defn error-function - ([argmap individual] - (error-function argmap individual :train)) - ([argmap individual subset] + ([argmap data individual] (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) correct-outputs (map (fn [i] (get i :output1)) data) outputs (map (fn [input] @@ -61,5 +60,9 @@ :total-error #?(:clj (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)})