diff --git a/project.clj b/project.clj index 6aa55f3..1e6a442 100644 --- a/project.clj +++ b/project.clj @@ -9,3 +9,5 @@ [net.clojars.schneau/psb2 "1.1.0"]] :main ^:skip-aot propeller.core :repl-options {:init-ns propeller.core}) + ;:jvm-opts ^:replace []) + diff --git a/src/propeller/core.cljc b/src/propeller/core.cljc index e85cab7..3b906d2 100755 --- a/src/propeller/core.cljc +++ b/src/propeller/core.cljc @@ -31,10 +31,15 @@ :population-size 500 :max-initial-plushy-size 100 :step-limit 200 + ;:max-generations 300 + ;:population-size 1000 + ;:max-initial-plushy-size 250 + ;:step-limit 2000 :parent-selection :lexicase :tournament-size 5 :umad-rate 0.1 :variation {:umad 0.5 :crossover 0.5} + ;:variation {:umad 1.0 :crossover 0.0} :elitism false :PSB2-path "" :PSB2-problem (clojure.string/replace (first args) #"PSB2." "")} diff --git a/src/propeller/gp.cljc b/src/propeller/gp.cljc index 6774a3e..47d8d82 100755 --- a/src/propeller/gp.cljc +++ b/src/propeller/gp.cljc @@ -28,8 +28,6 @@ :average-genome-length (float (/ (reduce + (map count (map :plushy pop))) (count pop))) :average-total-error (float (/ (reduce + (map :total-error pop)) (count pop)))}) (println))) -; (clojure.pprint/pprint - (defn gp "Main GP loop." diff --git a/src/propeller/problems/PSB2/middle_character.cljc b/src/propeller/problems/PSB2/middle_character.cljc new file mode 100644 index 0000000..3b3a95c --- /dev/null +++ b/src/propeller/problems/PSB2/middle_character.cljc @@ -0,0 +1,67 @@ +(ns propeller.problems.PSB2.middle-character + (:require [psb2.core :as psb2] + [propeller.genome :as genome] + [propeller.push.interpreter :as interpreter] + [propeller.utils :as utils] + [propeller.push.utils.helpers :refer [get-stack-instructions]] + [propeller.push.state :as state] + [propeller.tools.math :as math] + [propeller.tools.metrics :as metrics])) + +; =========== PROBLEM DESCRIPTION ============================= +; MIDDLE CHARACTER from PSB2 +;Given a string, return the middle +;character as a string if it is odd length; return the two middle +;characters as a string if it is even length. +; +; Source: https://arxiv.org/pdf/2106.06086.pdf +; =============================================================== + +(defn random-int [] (- (rand-int 201) 100)) + +(def instructions + (utils/not-lazy + (concat + ;;; stack-specific instructions + (get-stack-instructions #{:exec :integer :boolean :char :string :print}) + ;;; input instructions + (list :in1) + ;;; close + (list 'close) + ;;; ERCs (constants) + (list "" 0 1 2 random-int)))) + +(defn error-function + ([argmap individual] + (error-function argmap individual :train)) + ([argmap individual subset] + (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] + (state/peek-stack + (interpreter/interpret-program + program + (assoc state/empty-state :input {:in1 input}) + (:step-limit argmap)) + :string)) + inputs) + parsed-outputs (map (fn [output] + (try (read-string output) + #?(:clj (catch Exception e 1000.0) + :cljs (catch js/Error. e 1000.0)))) + outputs) + errors (map (fn [correct-output output] + (if (= output :no-stack-item) + 10000 + (metrics/levenshtein-distance (str correct-output) (str output)))) + correct-outputs + parsed-outputs)] + (assoc individual + :behaviors parsed-outputs + :errors errors + :total-error #?(:clj (apply +' errors) + :cljs (apply + errors)))))) + + diff --git a/src/propeller/problems/PSB2/substitution_cipher.cljc b/src/propeller/problems/PSB2/substitution_cipher.cljc index 83d96d2..9c21bfb 100644 --- a/src/propeller/problems/PSB2/substitution_cipher.cljc +++ b/src/propeller/problems/PSB2/substitution_cipher.cljc @@ -41,7 +41,6 @@ ;;; ERCs (constants) (list 0 "")))) - (defn error-function ([argmap individual] (error-function argmap individual :train)) @@ -75,5 +74,4 @@ :behaviors parsed-outputs :errors errors :total-error #?(:clj (apply +' errors) - :cljs (apply + errors)))))) - + :cljs (apply + errors)))))) \ No newline at end of file diff --git a/src/propeller/tools/metrics.cljc b/src/propeller/tools/metrics.cljc index bebd992..156265f 100755 --- a/src/propeller/tools/metrics.cljc +++ b/src/propeller/tools/metrics.cljc @@ -75,7 +75,7 @@ ;; we need to initialize the prev-row with the edit distance ;; between the various prefixes of b and the empty string (range (inc (count (str b)))) - (str a)))) + (str a))))) (defn sequence-similarity "Returns a number between 0 and 1, indicating how similar the sequences are