From abec0ddf12ef512c923d537f0651c3be68a4f181 Mon Sep 17 00:00:00 2001 From: Shuzo Katayama Date: Tue, 13 Jul 2021 15:59:36 -0400 Subject: [PATCH] twitter --- project.clj | 4 +- src/propeller/core.cljc | 9 +-- .../problems/PSB2/middle_character.cljc | 6 +- src/propeller/problems/PSB2/twitter.cljc | 70 +++++++++++++++++++ 4 files changed, 76 insertions(+), 13 deletions(-) create mode 100644 src/propeller/problems/PSB2/twitter.cljc diff --git a/project.clj b/project.clj index 1e6a442..10c6607 100644 --- a/project.clj +++ b/project.clj @@ -8,6 +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}) - ;:jvm-opts ^:replace []) + :repl-options {:init-ns propeller.core} + :jvm-opts ^:replace []) diff --git a/src/propeller/core.cljc b/src/propeller/core.cljc index 3b906d2..db08439 100755 --- a/src/propeller/core.cljc +++ b/src/propeller/core.cljc @@ -31,15 +31,10 @@ :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." "")} @@ -47,6 +42,4 @@ (map #(if (and (string? %) (not (.contains % "/"))) (read-string %) %) (rest args)))) [:error-function] - identity))) - -;;keyword path as a separate argument lol \ No newline at end of file + identity))) \ No newline at end of file diff --git a/src/propeller/problems/PSB2/middle_character.cljc b/src/propeller/problems/PSB2/middle_character.cljc index 3b3a95c..ee8430e 100644 --- a/src/propeller/problems/PSB2/middle_character.cljc +++ b/src/propeller/problems/PSB2/middle_character.cljc @@ -10,9 +10,9 @@ ; =========== 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. +; 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 ; =============================================================== diff --git a/src/propeller/problems/PSB2/twitter.cljc b/src/propeller/problems/PSB2/twitter.cljc new file mode 100644 index 0000000..f0a17f4 --- /dev/null +++ b/src/propeller/problems/PSB2/twitter.cljc @@ -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 Twitter’s 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 didn’t 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)))))) + +