added middle character

This commit is contained in:
Shuzo Katayama 2021-07-13 15:40:18 -04:00
parent a7deb03611
commit 2cead8b88d
6 changed files with 76 additions and 6 deletions

View File

@ -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 [])

View File

@ -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." "")}

View File

@ -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."

View File

@ -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))))))

View File

@ -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))))))

View File

@ -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