From 372e45895e6af4723caaaf103070f730601f2425 Mon Sep 17 00:00:00 2001 From: Shuzo Katayama Date: Mon, 19 Jul 2021 16:26:10 -0400 Subject: [PATCH 1/2] replaced :output with :print --- src/propeller/problems/PSB2/coin_sums.cljc | 96 +++++++++++++++++++ .../problems/software/number_io.cljc | 4 +- src/propeller/problems/software/smallest.cljc | 4 +- .../push/instructions/input_output.cljc | 12 +-- src/propeller/push/state.cljc | 3 +- 5 files changed, 108 insertions(+), 11 deletions(-) create mode 100644 src/propeller/problems/PSB2/coin_sums.cljc diff --git a/src/propeller/problems/PSB2/coin_sums.cljc b/src/propeller/problems/PSB2/coin_sums.cljc new file mode 100644 index 0000000..3c5a73c --- /dev/null +++ b/src/propeller/problems/PSB2/coin_sums.cljc @@ -0,0 +1,96 @@ +(ns propeller.problems.PSB2.coin-sums + (: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.gp :as gp] + [propeller.push.utils.macros :refer [def-instruction + generate-instructions]])) + +; =========== PROBLEM DESCRIPTION ============================ +; COIN SUMS from PSB2 +; Given a number of cents, find the fewest +; number of US coins (pennies, nickles, dimes, quarters) needed +; to make that amount, and return the number of each type +; of coin as a separate output +; +; Source: https://arxiv.org/pdf/2106.06086.pdf +; =============================================================== + +(def train-and-test-data (psb2/fetch-examples "data" "coin-sums" 200 2000)) + +; Random integer between -100 and 100 (from smallest) +(defn random-int [] (- (rand-int 201) 100)) + +(def-instruction + :output_pennies + ^{:stacks [:integer]} + (fn [state] + (if (empty? (:integer state)) + state + (let [top-int (state/peek-stack :integer state)] + )))) + ;(stack-assoc top-int :output 0 state)))) + ;(assoc map key val) + +(def instructions + (utils/not-lazy + (concat + ;;; stack-specific instructions + (get-stack-instructions #{:exec :integer :boolean :print}) + ;;; input instructions + (list :in1) + ;;; close + (list 'close) + ;;; ERCs (constants) + (list 0 1 5 10 25)))) + +(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)) + :integer)) + inputs) + errors (map (fn [correct-output output] + (if (= output :no-stack-item) + 1000000 + (min 1000.0 (math/abs (- correct-output output))))) + correct-outputs + outputs)] + (assoc individual + :behaviors 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/software/number_io.cljc b/src/propeller/problems/software/number_io.cljc index 2acae61..fcbef5f 100755 --- a/src/propeller/problems/software/number_io.cljc +++ b/src/propeller/problems/software/number_io.cljc @@ -71,9 +71,9 @@ program (assoc state/empty-state :input {:in1 (first input) :in2 (last input)} - :output '("")) + :print '("")) (:step-limit argmap)) - :output)) + :print)) inputs) parsed-outputs (map (fn [output] (try (read-string output) diff --git a/src/propeller/problems/software/smallest.cljc b/src/propeller/problems/software/smallest.cljc index a70ac59..9ca4dbb 100755 --- a/src/propeller/problems/software/smallest.cljc +++ b/src/propeller/problems/software/smallest.cljc @@ -75,9 +75,9 @@ :in2 (get input 1) :in3 (get input 2) :in4 (get input 3)} - :output '("")) + :print '("")) (:step-limit argmap)) - :output)) + :print)) inputs) errors (map (fn [correct-output output] (let [parsed-output (try (read-string output) diff --git a/src/propeller/push/instructions/input_output.cljc b/src/propeller/push/instructions/input_output.cljc index 9d52d54..b597c7b 100755 --- a/src/propeller/push/instructions/input_output.cljc +++ b/src/propeller/push/instructions/input_output.cljc @@ -31,9 +31,9 @@ :print_newline ^{:stacks [:print]} (fn [state] - (let [current-output (state/peek-stack state :output) - popped-state (state/pop-stack state :output)] - (state/push-to-stack popped-state :output (str current-output \newline))))) + (let [current-output (state/peek-stack state :print) + popped-state (state/pop-stack state :print)] + (state/push-to-stack popped-state :print (str current-output \newline))))) (def _print ^{:stacks [:print] @@ -46,10 +46,10 @@ (char? top-item)) top-item (pr-str top-item)) - current-output (state/peek-stack state :output) - popped-state (state/pop-stack (state/pop-stack state stack) :output)] + current-output (state/peek-stack state :print) + popped-state (state/pop-stack (state/pop-stack state stack) :print)] (state/push-to-stack popped-state - :output + :print (str current-output top-item-str)))))) (generate-instructions diff --git a/src/propeller/push/state.cljc b/src/propeller/push/state.cljc index b49227e..647509c 100755 --- a/src/propeller/push/state.cljc +++ b/src/propeller/push/state.cljc @@ -7,8 +7,9 @@ :exec '() :float '() :input {} + :output {} :integer '() - :output '("") + :print '("") :string '() :vector_boolean '() :vector_float '() From 8fae435046c3fcd6c635eb556b3b9db8be6221c9 Mon Sep 17 00:00:00 2001 From: Shuzo Katayama <62683709+DuckNoodleSoup@users.noreply.github.com> Date: Mon, 19 Jul 2021 16:36:31 -0400 Subject: [PATCH 2/2] Delete coin_sums.cljc --- src/propeller/problems/PSB2/coin_sums.cljc | 96 ---------------------- 1 file changed, 96 deletions(-) delete mode 100644 src/propeller/problems/PSB2/coin_sums.cljc diff --git a/src/propeller/problems/PSB2/coin_sums.cljc b/src/propeller/problems/PSB2/coin_sums.cljc deleted file mode 100644 index 3c5a73c..0000000 --- a/src/propeller/problems/PSB2/coin_sums.cljc +++ /dev/null @@ -1,96 +0,0 @@ -(ns propeller.problems.PSB2.coin-sums - (: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.gp :as gp] - [propeller.push.utils.macros :refer [def-instruction - generate-instructions]])) - -; =========== PROBLEM DESCRIPTION ============================ -; COIN SUMS from PSB2 -; Given a number of cents, find the fewest -; number of US coins (pennies, nickles, dimes, quarters) needed -; to make that amount, and return the number of each type -; of coin as a separate output -; -; Source: https://arxiv.org/pdf/2106.06086.pdf -; =============================================================== - -(def train-and-test-data (psb2/fetch-examples "data" "coin-sums" 200 2000)) - -; Random integer between -100 and 100 (from smallest) -(defn random-int [] (- (rand-int 201) 100)) - -(def-instruction - :output_pennies - ^{:stacks [:integer]} - (fn [state] - (if (empty? (:integer state)) - state - (let [top-int (state/peek-stack :integer state)] - )))) - ;(stack-assoc top-int :output 0 state)))) - ;(assoc map key val) - -(def instructions - (utils/not-lazy - (concat - ;;; stack-specific instructions - (get-stack-instructions #{:exec :integer :boolean :print}) - ;;; input instructions - (list :in1) - ;;; close - (list 'close) - ;;; ERCs (constants) - (list 0 1 5 10 25)))) - -(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)) - :integer)) - inputs) - errors (map (fn [correct-output output] - (if (= output :no-stack-item) - 1000000 - (min 1000.0 (math/abs (- correct-output output))))) - correct-outputs - outputs)] - (assoc individual - :behaviors 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))))) - -