From 105e8faf4efc7c4e4829f45379532b3d913d4af7 Mon Sep 17 00:00:00 2001 From: Shuzo Katayama Date: Thu, 15 Jul 2021 11:37:43 -0400 Subject: [PATCH] single output PSB2 problems --- src/propeller/problems/PSB2/basement.cljc | 10 +-- .../problems/PSB2/bouncing_balls.cljc | 12 +-- src/propeller/problems/PSB2/bowling.cljc | 11 ++- src/propeller/problems/PSB2/camel_case.cljc | 11 ++- src/propeller/problems/PSB2/dice_game.cljc | 15 ++-- src/propeller/problems/PSB2/fizz_buzz.cljc | 27 ++++-- src/propeller/problems/PSB2/fuel_cost.cljc | 11 ++- src/propeller/problems/PSB2/gcd.cljc | 32 +++++-- src/propeller/problems/PSB2/luhn.cljc | 27 ++++-- .../problems/PSB2/middle_character.cljc | 11 ++- .../problems/PSB2/paired_digits.cljc | 27 ++++-- .../problems/PSB2/shopping_list.cljc | 31 +++++-- src/propeller/problems/PSB2/snow_day.cljc | 31 +++++-- .../problems/PSB2/solve_boolean.cljc | 27 ++++-- src/propeller/problems/PSB2/spin_words.cljc | 55 +++++++++--- .../problems/PSB2/square_digits.cljc | 86 +++++++++++++++++++ .../problems/PSB2/substitution_cipher.cljc | 14 +-- src/propeller/problems/PSB2/twitter.cljc | 10 +-- 18 files changed, 333 insertions(+), 115 deletions(-) create mode 100644 src/propeller/problems/PSB2/square_digits.cljc diff --git a/src/propeller/problems/PSB2/basement.cljc b/src/propeller/problems/PSB2/basement.cljc index 9143c0e..e7598eb 100644 --- a/src/propeller/problems/PSB2/basement.cljc +++ b/src/propeller/problems/PSB2/basement.cljc @@ -68,14 +68,14 @@ :error-function error-function :training-data (:train train-and-test-data) :testing-data (:test train-and-test-data) - :max-generations 500 - :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} (apply hash-map (map #(if (string? %) (read-string %) %) args))))) diff --git a/src/propeller/problems/PSB2/bouncing_balls.cljc b/src/propeller/problems/PSB2/bouncing_balls.cljc index 0cab515..6625385 100644 --- a/src/propeller/problems/PSB2/bouncing_balls.cljc +++ b/src/propeller/problems/PSB2/bouncing_balls.cljc @@ -80,13 +80,13 @@ :error-function error-function :training-data (:train train-and-test-data) :testing-data (:test train-and-test-data) - :max-generations 500 - :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} - (apply hash-map (map #(if (string? %) (read-string %) %) args))))) + (apply hash-map (map #(if (string? %) (read-string %) %) args))))) \ No newline at end of file diff --git a/src/propeller/problems/PSB2/bowling.cljc b/src/propeller/problems/PSB2/bowling.cljc index 6fd63b8..6c80629 100644 --- a/src/propeller/problems/PSB2/bowling.cljc +++ b/src/propeller/problems/PSB2/bowling.cljc @@ -5,7 +5,6 @@ [propeller.utils :as utils] [propeller.push.utils.helpers :refer [get-stack-instructions]] [propeller.push.state :as state] - [clojure.pprint :as pprint] [propeller.tools.math :as math] [propeller.gp :as gp])) @@ -68,13 +67,13 @@ :error-function error-function :training-data (:train train-and-test-data) :testing-data (:test train-and-test-data) - :max-generations 500 - :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} (apply hash-map (map #(if (string? %) (read-string %) %) args))))) \ No newline at end of file diff --git a/src/propeller/problems/PSB2/camel_case.cljc b/src/propeller/problems/PSB2/camel_case.cljc index 3bc2447..08610b1 100644 --- a/src/propeller/problems/PSB2/camel_case.cljc +++ b/src/propeller/problems/PSB2/camel_case.cljc @@ -5,7 +5,6 @@ [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] [propeller.gp :as gp])) @@ -104,13 +103,13 @@ :error-function error-function :training-data (:train train-and-test-data) :testing-data (:test train-and-test-data) - :max-generations 500 - :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} (apply hash-map (map #(if (string? %) (read-string %) %) args))))) diff --git a/src/propeller/problems/PSB2/dice_game.cljc b/src/propeller/problems/PSB2/dice_game.cljc index db669b5..96bed0f 100644 --- a/src/propeller/problems/PSB2/dice_game.cljc +++ b/src/propeller/problems/PSB2/dice_game.cljc @@ -5,7 +5,6 @@ [propeller.utils :as utils] [propeller.push.utils.helpers :refer [get-stack-instructions]] [propeller.push.state :as state] - [clojure.pprint :as pprint] [propeller.tools.math :as math] [propeller.gp :as gp])) @@ -21,12 +20,12 @@ (def train-and-test-data (psb2/fetch-examples "data" "dice-game" 200 2000)) (defn map-vals-input - "Returns all the input values of a map (specific helper method for bouncing-balls)" + "Returns all the input values of a map" [i] (vals (select-keys i [:input1 :input2]))) (defn map-vals-output - "Returns the output values of a map (specific helper method for bouncing-balls)" + "Returns the output values of a map" [i] (get i :output1)) @@ -77,13 +76,13 @@ :error-function error-function :training-data (:train train-and-test-data) :testing-data (:test train-and-test-data) - :max-generations 500 - :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} (apply hash-map (map #(if (string? %) (read-string %) %) args))))) diff --git a/src/propeller/problems/PSB2/fizz_buzz.cljc b/src/propeller/problems/PSB2/fizz_buzz.cljc index 33cbb09..3397851 100644 --- a/src/propeller/problems/PSB2/fizz_buzz.cljc +++ b/src/propeller/problems/PSB2/fizz_buzz.cljc @@ -5,7 +5,8 @@ [propeller.utils :as utils] [propeller.push.utils.helpers :refer [get-stack-instructions]] [propeller.push.state :as state] - [propeller.tools.metrics :as metrics])) + [propeller.tools.metrics :as metrics] + [propeller.gp :as gp])) ; =========== PROBLEM DESCRIPTION ========================= ; FIZZ BUZZ from PSB2 @@ -62,9 +63,23 @@ :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)}) +(defn -main + "Runs propel-gp, giving it a map of arguments." + [& args] + (gp/gp + (merge + {:instructions instructions + :error-function error-function + :training-data (:train train-and-test-data) + :testing-data (:test train-and-test-data) + :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 1.0 :crossover 0.0} + :elitism false} + (apply hash-map (map #(if (string? %) (read-string %) %) args))))) diff --git a/src/propeller/problems/PSB2/fuel_cost.cljc b/src/propeller/problems/PSB2/fuel_cost.cljc index 0f1592a..dbd39af 100644 --- a/src/propeller/problems/PSB2/fuel_cost.cljc +++ b/src/propeller/problems/PSB2/fuel_cost.cljc @@ -5,7 +5,6 @@ [propeller.utils :as utils] [propeller.push.utils.helpers :refer [get-stack-instructions]] [propeller.push.state :as state] - [clojure.pprint :as pprint] [propeller.tools.math :as math] [propeller.gp :as gp])) @@ -70,14 +69,14 @@ :error-function error-function :training-data (:train train-and-test-data) :testing-data (:test train-and-test-data) - :max-generations 500 - :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} (apply hash-map (map #(if (string? %) (read-string %) %) args))))) diff --git a/src/propeller/problems/PSB2/gcd.cljc b/src/propeller/problems/PSB2/gcd.cljc index 5830c20..079cfd3 100644 --- a/src/propeller/problems/PSB2/gcd.cljc +++ b/src/propeller/problems/PSB2/gcd.cljc @@ -5,7 +5,8 @@ [propeller.utils :as utils] [propeller.push.utils.helpers :refer [get-stack-instructions]] [propeller.push.state :as state] - [propeller.tools.math :as math])) + [propeller.tools.math :as math] + [propeller.gp :as gp])) ; =========== PROBLEM DESCRIPTION =============================== ; GCD [GREATEST COMMON DIVISOR] from PSB2 @@ -20,12 +21,12 @@ (defn random-int [] (- (rand-int 201) 100)) (defn map-vals-input - "Returns all the input values of a map (specific helper method for bouncing-balls)" + "Returns all the input values of a map" [i] (vals (select-keys i [:input1 :input2]))) (defn map-vals-output - "Returns the output values of a map (specific helper method for bouncing-balls)" + "Returns the output values of a map" [i] (get i :output1)) @@ -67,9 +68,22 @@ :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)}) - +(defn -main + "Runs propel-gp, giving it a map of arguments." + [& args] + (gp/gp + (merge + {:instructions instructions + :error-function error-function + :training-data (:train train-and-test-data) + :testing-data (:test train-and-test-data) + :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 1.0 :crossover 0.0} + :elitism false} + (apply hash-map (map #(if (string? %) (read-string %) %) args))))) diff --git a/src/propeller/problems/PSB2/luhn.cljc b/src/propeller/problems/PSB2/luhn.cljc index 009cf18..9c2c6fe 100644 --- a/src/propeller/problems/PSB2/luhn.cljc +++ b/src/propeller/problems/PSB2/luhn.cljc @@ -5,7 +5,8 @@ [propeller.utils :as utils] [propeller.push.utils.helpers :refer [get-stack-instructions]] [propeller.push.state :as state] - [propeller.tools.math :as math])) + [propeller.tools.math :as math] + [propeller.gp :as gp])) ; =========== PROBLEM DESCRIPTION ============================ ; LUHN from PSB2 @@ -60,10 +61,24 @@ :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)}) +(defn -main + "Runs propel-gp, giving it a map of arguments." + [& args] + (gp/gp + (merge + {:instructions instructions + :error-function error-function + :training-data (:train train-and-test-data) + :testing-data (:test train-and-test-data) + :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 1.0 :crossover 0.0} + :elitism false} + (apply hash-map (map #(if (string? %) (read-string %) %) args))))) diff --git a/src/propeller/problems/PSB2/middle_character.cljc b/src/propeller/problems/PSB2/middle_character.cljc index 8b7e123..f4d4a31 100644 --- a/src/propeller/problems/PSB2/middle_character.cljc +++ b/src/propeller/problems/PSB2/middle_character.cljc @@ -5,7 +5,6 @@ [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] [propeller.gp :as gp])) @@ -73,13 +72,13 @@ :error-function error-function :training-data (:train train-and-test-data) :testing-data (:test train-and-test-data) - :max-generations 500 - :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} (apply hash-map (map #(if (string? %) (read-string %) %) args))))) diff --git a/src/propeller/problems/PSB2/paired_digits.cljc b/src/propeller/problems/PSB2/paired_digits.cljc index 531ad78..bcccf73 100644 --- a/src/propeller/problems/PSB2/paired_digits.cljc +++ b/src/propeller/problems/PSB2/paired_digits.cljc @@ -5,7 +5,8 @@ [propeller.utils :as utils] [propeller.push.utils.helpers :refer [get-stack-instructions]] [propeller.push.state :as state] - [propeller.tools.math :as math])) + [propeller.tools.math :as math] + [propeller.gp :as gp])) ; =========== PROBLEM DESCRIPTION ============================= ; PAIRED DIGITS from PSB2 @@ -58,9 +59,23 @@ :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)}) +(defn -main + "Runs propel-gp, giving it a map of arguments." + [& args] + (gp/gp + (merge + {:instructions instructions + :error-function error-function + :training-data (:train train-and-test-data) + :testing-data (:test train-and-test-data) + :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 1.0 :crossover 0.0} + :elitism false} + (apply hash-map (map #(if (string? %) (read-string %) %) args))))) diff --git a/src/propeller/problems/PSB2/shopping_list.cljc b/src/propeller/problems/PSB2/shopping_list.cljc index b62cb81..e3120bd 100644 --- a/src/propeller/problems/PSB2/shopping_list.cljc +++ b/src/propeller/problems/PSB2/shopping_list.cljc @@ -5,7 +5,8 @@ [propeller.utils :as utils] [propeller.push.utils.helpers :refer [get-stack-instructions]] [propeller.push.state :as state] - [propeller.tools.math :as math])) + [propeller.tools.math :as math] + [propeller.gp :as gp])) ; =========== PROBLEM DESCRIPTION =============================== ; DICE GAME from PSB2 @@ -21,12 +22,12 @@ (defn random-float [] (- (rand 201) 100)) (defn map-vals-input - "Returns all the input values of a map (specific helper method for bouncing-balls)" + "Returns all the input values of a map" [i] (vals (select-keys i [:input1 :input2]))) (defn map-vals-output - "Returns the output values of a map (specific helper method for bouncing-balls)" + "Returns the output values of a map " [i] (get i :output1)) @@ -68,8 +69,22 @@ :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)}) +(defn -main + "Runs propel-gp, giving it a map of arguments." + [& args] + (gp/gp + (merge + {:instructions instructions + :error-function error-function + :training-data (:train train-and-test-data) + :testing-data (:test train-and-test-data) + :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 1.0 :crossover 0.0} + :elitism false} + (apply hash-map (map #(if (string? %) (read-string %) %) args))))) diff --git a/src/propeller/problems/PSB2/snow_day.cljc b/src/propeller/problems/PSB2/snow_day.cljc index 8bab057..b68c886 100644 --- a/src/propeller/problems/PSB2/snow_day.cljc +++ b/src/propeller/problems/PSB2/snow_day.cljc @@ -5,7 +5,8 @@ [propeller.utils :as utils] [propeller.push.utils.helpers :refer [get-stack-instructions]] [propeller.push.state :as state] - [propeller.tools.math :as math])) + [propeller.tools.math :as math] + [propeller.gp :as gp])) ; =========== PROBLEM DESCRIPTION =============================== ; SNOW DAY from PSB2 @@ -23,12 +24,12 @@ (def train-and-test-data (psb2/fetch-examples "data" "snow-day" 200 2000)) (defn map-vals-input - "Returns all the input values of a map (specific helper method for bouncing-balls)" + "Returns all the input values of a map" [i] (vals (select-keys i [:input1 :input2 :input3 :input4]))) (defn map-vals-output - "Returns the output values of a map (specific helper method for bouncing-balls)" + "Returns the output values of a map" [i] (get i :output1)) @@ -72,8 +73,22 @@ :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)}) +(defn -main + "Runs propel-gp, giving it a map of arguments." + [& args] + (gp/gp + (merge + {:instructions instructions + :error-function error-function + :training-data (:train train-and-test-data) + :testing-data (:test train-and-test-data) + :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 1.0 :crossover 0.0} + :elitism false} + (apply hash-map (map #(if (string? %) (read-string %) %) args))))) diff --git a/src/propeller/problems/PSB2/solve_boolean.cljc b/src/propeller/problems/PSB2/solve_boolean.cljc index f6fd5cc..42c29ea 100644 --- a/src/propeller/problems/PSB2/solve_boolean.cljc +++ b/src/propeller/problems/PSB2/solve_boolean.cljc @@ -5,7 +5,7 @@ [propeller.utils :as utils] [propeller.push.utils.helpers :refer [get-stack-instructions]] [propeller.push.state :as state] - [propeller.tools.metrics :as metrics])) + [propeller.gp :as gp])) ; =========== PROBLEM DESCRIPTION ================================ ; SOLVE BOOLEAN from PSB2 @@ -62,9 +62,22 @@ :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)}) - +(defn -main + "Runs propel-gp, giving it a map of arguments." + [& args] + (gp/gp + (merge + {:instructions instructions + :error-function error-function + :training-data (:train train-and-test-data) + :testing-data (:test train-and-test-data) + :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 1.0 :crossover 0.0} + :elitism false} + (apply hash-map (map #(if (string? %) (read-string %) %) args))))) diff --git a/src/propeller/problems/PSB2/spin_words.cljc b/src/propeller/problems/PSB2/spin_words.cljc index 43b04a3..c67328f 100644 --- a/src/propeller/problems/PSB2/spin_words.cljc +++ b/src/propeller/problems/PSB2/spin_words.cljc @@ -5,7 +5,8 @@ [propeller.utils :as utils] [propeller.push.utils.helpers :refer [get-stack-instructions]] [propeller.push.state :as state] - [propeller.tools.metrics :as metrics])) + [propeller.tools.metrics :as metrics] + [propeller.gp :as gp])) ; =========== PROBLEM DESCRIPTION ============================== ; SPIN WORDS from PSB2 @@ -23,8 +24,29 @@ [] (rand-nth (map char (range 97 122)))) -;; WORK ON THIS TOMORROW; -;; SPIN WORDS STRING ERC; +; random word generator for ERC +; from https://github.com/thelmuth/Clojush/blob/psb2/src/clojush/problems/psb2/spin_words.clj +(defn word-generator + [] + (let [word-len (inc (rand-int (if (< (rand) 0.8) + 8 + 16)))] + (apply str (repeatedly word-len #(random-char))))) + +; String ERC; random word +; from https://github.com/thelmuth/Clojush/blob/psb2/src/clojush/problems/psb2/spin_words.clj +(defn random-input + "Makes a Spin Words input of length len, which is just a string of words, where the + words that are length 5 or greater are reversed" + [len] + (let [words (apply str + (take len ; This looks weird because str isn't lazy, so you + (apply str ; need to take len twice here. + (take len + (interpose " " (repeatedly word-generator))))))] + (if (not= (last words) \space) + words + (apply str (butlast words))))) (def instructions (utils/not-lazy @@ -36,8 +58,7 @@ ;;; close (list 'close) ;;; ERCs (constants) - (list 4 5 \space random-char )))) - + (list 4 5 \space random-char (fn [] (random-input (rand-int 21))))))) (defn error-function [argmap data individual] @@ -69,9 +90,23 @@ :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)}) +(defn -main + "Runs propel-gp, giving it a map of arguments." + [& args] + (gp/gp + (merge + {:instructions instructions + :error-function error-function + :training-data (:train train-and-test-data) + :testing-data (:test train-and-test-data) + :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 1.0 :crossover 0.0} + :elitism false} + (apply hash-map (map #(if (string? %) (read-string %) %) args))))) diff --git a/src/propeller/problems/PSB2/square_digits.cljc b/src/propeller/problems/PSB2/square_digits.cljc new file mode 100644 index 0000000..bb242d4 --- /dev/null +++ b/src/propeller/problems/PSB2/square_digits.cljc @@ -0,0 +1,86 @@ +(ns propeller.problems.PSB2.square-digits + (: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.metrics :as metrics] + [propeller.gp :as gp])) + +; =========== PROBLEM DESCRIPTION ========================= +; SQUARE DIGITS from PSB2 +; Given a positive integer, square each +; digit and concatenate the squares into a returned string. +; +; Source: https://arxiv.org/pdf/2106.06086.pdf +; ============================================================ + +(def train-and-test-data (psb2/fetch-examples "data" "square-digits" 200 2000)) + +(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 data individual] + (let [program (genome/plushy->push (:plushy individual) argmap) + 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))))) + + +(defn -main + "Runs propel-gp, giving it a map of arguments." + [& args] + (gp/gp + (merge + {:instructions instructions + :error-function error-function + :training-data (:train train-and-test-data) + :testing-data (:test train-and-test-data) + :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 1.0 :crossover 0.0} + :elitism false} + (apply hash-map (map #(if (string? %) (read-string %) %) args))))) + + diff --git a/src/propeller/problems/PSB2/substitution_cipher.cljc b/src/propeller/problems/PSB2/substitution_cipher.cljc index 554c203..22ec93c 100644 --- a/src/propeller/problems/PSB2/substitution_cipher.cljc +++ b/src/propeller/problems/PSB2/substitution_cipher.cljc @@ -23,12 +23,12 @@ (def train-and-test-data (psb2/fetch-examples "data" "substitution-cipher" 200 2000)) (defn map-vals-input - "Returns all the input values of a map (specific helper method for substitution-cipher)" + "Returns all the input values of a map" [i] (vals (select-keys i [:input1 :input2 :input3]))) (defn map-vals-output - "Returns the output values of a map (specific helper method for substitution-cipher)" + "Returns the output values of a map" [i] (vals (select-keys i [:output1]))) @@ -85,13 +85,13 @@ :error-function error-function :training-data (:train train-and-test-data) :testing-data (:test train-and-test-data) - :max-generations 500 - :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} (apply hash-map (map #(if (string? %) (read-string %) %) args))))) \ No newline at end of file diff --git a/src/propeller/problems/PSB2/twitter.cljc b/src/propeller/problems/PSB2/twitter.cljc index bdf35f7..7b85fc2 100644 --- a/src/propeller/problems/PSB2/twitter.cljc +++ b/src/propeller/problems/PSB2/twitter.cljc @@ -76,13 +76,13 @@ :error-function error-function :training-data (:train train-and-test-data) :testing-data (:test train-and-test-data) - :max-generations 500 - :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} (apply hash-map (map #(if (string? %) (read-string %) %) args))))) \ No newline at end of file