Merge pull request #23 from DuckNoodleSoup/storage

Middle Character and Twitter
This commit is contained in:
Lee Spector 2021-07-13 17:36:51 -04:00 committed by GitHub
commit d3b102c509
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 143 additions and 10 deletions

View File

@ -8,4 +8,6 @@
[org.clojure/test.check "1.1.0"]
[net.clojars.schneau/psb2 "1.1.0"]]
:main ^:skip-aot propeller.core
:repl-options {:init-ns propeller.core})
:repl-options {:init-ns propeller.core}
:jvm-opts ^:replace [])

View File

@ -43,5 +43,3 @@
(rest args))))
[:error-function]
identity)))
;;keyword path as a separate argument lol

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))
@ -76,4 +75,3 @@
:errors errors
:total-error #?(:clj (apply +' errors)
:cljs (apply + errors))))))

View File

@ -0,0 +1,70 @@
(ns propeller.problems.PSB2.twitter
(: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 =============================
; TWITTER from PSB2
; Given a string representing a tweet, validate whether the tweet
; meets Twitters original character requirements. If the tweet
; has more than 140 characters, return the string "Too many characters".
; If the tweet is empty, return the string "You didnt type anything".
; Otherwise, return "Your tweet has X characters", where
; the X is the number of characters in the tweet.
;
; 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 140 "Too many characters" "You didn't type anything" "your tweet has " " characters"))))
(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

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