diff --git a/README.md b/README.md index 56511eb..6debf61 100644 --- a/README.md +++ b/README.md @@ -4,16 +4,62 @@ Yet another Push-based genetic programming system in Clojure. ## Usage -To run PushGP from a REPL, load propel.core into your REPL (i.e. `lein repl`), -and run `-main` with arguments including, first, the problem name, for example: -`(-main 'simple-regression)` or `(-main 'simple-regression :population-size 100)`. +If you have installed [leiningen](https://leiningen.org), which is a tool +for running Clojure programs, then you can run Propeller on a genetic +programming problem that is defined within this project from the command +line with the command `lein run -m `, replacing `` +with the actual namespace that you will find at the top of the problem file. + +For example, you can run the simple-regression genetic programming problem with: + +``` +lein run -m propeller.problems.simple-regression +``` + +Additional command-line arguments may +be provided to override the default key/value pairs specified in the +problem file, for example: + + +``` +lein run -m propeller.problems.simple-regression :population-size 100 +``` + +On Unix operating systems, including MacOS, you can use something +like the following to send output both to the terminal +and to a text file (called `outfile` in this example): + +``` +lein run -m propeller.problems.simple-regression | tee outfile +``` + +If you want to provide command line arguments that include +characters that may be interpreted by your command line shell +before they get to Clojure, then enclose those in double +quotes, like in this example that provides a non-default +value for the `:variation` argument, which is a clojure map +containing curly brackets that may confuse your shell: + +``` +lein run -m propeller.problems.simple-regression :variation "{:umad 1.0}" +``` + +To run a genetic programming problem from a REPL, start +your REPL for the project (e.g. with `lein repl` at the +command line when in the project directory, or through your +IDE) and then do something like the following (which in +this case runs the simple-regression problem with +`:population-size` 100): + +``` +(require 'propeller.problems.simple-regression) +(in-ns 'propeller.problems.simple-regression) +(-main :population-size 100 :variation {:umad 1.0}) +``` + +If you want to run the problem with the default parameters, +then you should call `-main` without arguments, as `(-main)`. -To run PushGP on the genetic programming problem p from the -command line, execute `lein run p`. For example `lein run simple-regression`. Additional command-line arguments may -be provided to override the default key/value pairs specified in `-main`, for -example, `lein run simple-regression :population-size 100`. You can use something -like `lein run simple-regression | tee outfile` to send output both to the terminal -and to `outfile`. ## CLJS Usage diff --git a/src/propeller/core.cljc b/src/propeller/core.cljc index f33c73b..0023a41 100755 --- a/src/propeller/core.cljc +++ b/src/propeller/core.cljc @@ -1,34 +1,10 @@ (ns propeller.core - #?(:clj (:gen-class)) - (:require [propeller.gp :as gp] - #?(:cljs [cljs.reader :refer [read-string]]))) - -(defn eval-problem-var - [problem-name var-name] - (eval (symbol (str "propeller.problems." problem-name "/" var-name)))) + #?(:clj (:gen-class))) (defn -main - "Runs propel-gp, giving it a map of arguments." + "Not intended to be run; just print a message." [& args] ;; Exception for when no args were passed - (when (empty? args) - (println "You must specify a problem to run.") - (println "Try, for example:") - (println " lein run software.smallest") - (System/exit 1)) - (require (symbol (str "propeller.problems." (first args)))) - (gp/gp - (merge - {:max-generations 500 - :population-size 500 - :max-initial-plushy-size 100 - :step-limit 200 - :parent-selection :lexicase - :tournament-size 5 - :umad-rate 0.1 - :variation {:umad 0.5 :crossover 0.5} - :elitism false} - (eval-problem-var (first args) "arglist") - (apply hash-map - (map #(if (and (string? %) (not (.contains % "/"))) (read-string %) %) - (rest args)))))) \ No newline at end of file + (println "To run a genetic programming problem, provide a the problem's") + (println "namespace as specified in the Propeller README file at") + (println "https://github.com/lspector/propeller/blob/master/README.md")) \ No newline at end of file diff --git a/src/propeller/problems/PSB2/basement.cljc b/src/propeller/problems/PSB2/basement.cljc index c89d2ab..9143c0e 100644 --- a/src/propeller/problems/PSB2/basement.cljc +++ b/src/propeller/problems/PSB2/basement.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 ============================ ; BASEMENT 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 500 + :population-size 500 + :max-initial-plushy-size 100 + :step-limit 200 + :parent-selection :lexicase + :tournament-size 5 + :umad-rate 0.1 + :variation {:umad 0.5 :crossover 0.5} + :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 1a4f053..0cab515 100644 --- a/src/propeller/problems/PSB2/bouncing_balls.cljc +++ b/src/propeller/problems/PSB2/bouncing_balls.cljc @@ -6,7 +6,8 @@ [propeller.push.utils.helpers :refer [get-stack-instructions]] [propeller.push.state :as state] [clojure.pprint :as pprint] - [propeller.tools.math :as math])) + [propeller.tools.math :as math] + [propeller.gp :as gp])) ; =========== PROBLEM DESCRIPTION =============================== ; BOUNCING BALLS from PSB2 @@ -70,8 +71,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 500 + :population-size 500 + :max-initial-plushy-size 100 + :step-limit 200 + :parent-selection :lexicase + :tournament-size 5 + :umad-rate 0.1 + :variation {:umad 0.5 :crossover 0.5} + :elitism false} + (apply hash-map (map #(if (string? %) (read-string %) %) args))))) diff --git a/src/propeller/problems/PSB2/bowling.cljc b/src/propeller/problems/PSB2/bowling.cljc index f0cdbc9..6fd63b8 100644 --- a/src/propeller/problems/PSB2/bowling.cljc +++ b/src/propeller/problems/PSB2/bowling.cljc @@ -6,7 +6,8 @@ [propeller.push.utils.helpers :refer [get-stack-instructions]] [propeller.push.state :as state] [clojure.pprint :as pprint] - [propeller.tools.math :as math])) + [propeller.tools.math :as math] + [propeller.gp :as gp])) ; =========== PROBLEM DESCRIPTION ====================== ; BOWLING from PSB2 @@ -58,8 +59,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)}) \ No newline at end of file +(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 500 + :population-size 500 + :max-initial-plushy-size 100 + :step-limit 200 + :parent-selection :lexicase + :tournament-size 5 + :umad-rate 0.1 + :variation {:umad 0.5 :crossover 0.5} + :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 ac6a04d..3bc2447 100644 --- a/src/propeller/problems/PSB2/camel_case.cljc +++ b/src/propeller/problems/PSB2/camel_case.cljc @@ -6,7 +6,8 @@ [propeller.push.utils.helpers :refer [get-stack-instructions]] [propeller.push.state :as state] [propeller.tools.math :as math] - [propeller.tools.metrics :as metrics])) + [propeller.tools.metrics :as metrics] + [propeller.gp :as gp])) ; =========== PROBLEM DESCRIPTION ===================================== ; CAMEL CASE from PSB2 @@ -94,8 +95,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 500 + :population-size 500 + :max-initial-plushy-size 100 + :step-limit 200 + :parent-selection :lexicase + :tournament-size 5 + :umad-rate 0.1 + :variation {:umad 0.5 :crossover 0.5} + :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 3de73f9..db669b5 100644 --- a/src/propeller/problems/PSB2/dice_game.cljc +++ b/src/propeller/problems/PSB2/dice_game.cljc @@ -6,7 +6,8 @@ [propeller.push.utils.helpers :refer [get-stack-instructions]] [propeller.push.state :as state] [clojure.pprint :as pprint] - [propeller.tools.math :as math])) + [propeller.tools.math :as math] + [propeller.gp :as gp])) ; =========== PROBLEM DESCRIPTION =============================== ; DICE GAME from PSB2 @@ -67,8 +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 500 + :population-size 500 + :max-initial-plushy-size 100 + :step-limit 200 + :parent-selection :lexicase + :tournament-size 5 + :umad-rate 0.1 + :variation {:umad 0.5 :crossover 0.5} + :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 9769fff..0f1592a 100644 --- a/src/propeller/problems/PSB2/fuel_cost.cljc +++ b/src/propeller/problems/PSB2/fuel_cost.cljc @@ -6,7 +6,8 @@ [propeller.push.utils.helpers :refer [get-stack-instructions]] [propeller.push.state :as state] [clojure.pprint :as pprint] - [propeller.tools.math :as math])) + [propeller.tools.math :as math] + [propeller.gp :as gp])) ; =========== PROBLEM DESCRIPTION ========================= ; FUEL COST from PSB2 @@ -60,9 +61,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 500 + :population-size 500 + :max-initial-plushy-size 100 + :step-limit 200 + :parent-selection :lexicase + :tournament-size 5 + :umad-rate 0.1 + :variation {:umad 0.5 :crossover 0.5} + :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 d023e52..8b7e123 100644 --- a/src/propeller/problems/PSB2/middle_character.cljc +++ b/src/propeller/problems/PSB2/middle_character.cljc @@ -6,7 +6,8 @@ [propeller.push.utils.helpers :refer [get-stack-instructions]] [propeller.push.state :as state] [propeller.tools.math :as math] - [propeller.tools.metrics :as metrics])) + [propeller.tools.metrics :as metrics] + [propeller.gp :as gp])) ; =========== PROBLEM DESCRIPTION ============================= ; MIDDLE CHARACTER from PSB2 @@ -63,8 +64,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 500 + :population-size 500 + :max-initial-plushy-size 100 + :step-limit 200 + :parent-selection :lexicase + :tournament-size 5 + :umad-rate 0.1 + :variation {:umad 0.5 :crossover 0.5} + :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 new file mode 100644 index 0000000..f6fd5cc --- /dev/null +++ b/src/propeller/problems/PSB2/solve_boolean.cljc @@ -0,0 +1,70 @@ +(ns propeller.problems.PSB2.solve-boolean + (: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])) + +; =========== PROBLEM DESCRIPTION ================================ +; SOLVE BOOLEAN from PSB2 +; Given a string representing a Boolean +; expression consisting of T, F, |, and &, evaluate it and return +; the resulting Boolean. +; +; Source: https://arxiv.org/pdf/2106.06086.pdf +; ================================================================== + +(def train-and-test-data (psb2/fetch-examples "data" "solve-boolean" 200 2000)) + +(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 true false \t \f \& \|)))) + +(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)) + :boolean)) + 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 + (if (= correct-output output) + 0 + 1))) + correct-outputs + parsed-outputs)] + (assoc individual + :behaviors parsed-outputs + :errors errors + :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)}) + diff --git a/src/propeller/problems/PSB2/spin_words.cljc b/src/propeller/problems/PSB2/spin_words.cljc new file mode 100644 index 0000000..43b04a3 --- /dev/null +++ b/src/propeller/problems/PSB2/spin_words.cljc @@ -0,0 +1,77 @@ +(ns propeller.problems.PSB2.spin-words + (: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])) + +; =========== PROBLEM DESCRIPTION ============================== +; SPIN WORDS from PSB2 +; Given a string of one or more words +; (separated by spaces), reverse all of the words that are five +; or more letters long and return the resulting string. +; +; Source: https://arxiv.org/pdf/2106.06086.pdf +; ================================================================ + +(def train-and-test-data (psb2/fetch-examples "data" "spin-words" 200 2000)) + +; Visible character ERC +(defn random-char + [] + (rand-nth (map char (range 97 122)))) + +;; WORK ON THIS TOMORROW; +;; SPIN WORDS STRING ERC; + +(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 4 5 \space random-char )))) + + +(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 correct-output output))) + correct-outputs + parsed-outputs)] + (assoc individual + :behaviors parsed-outputs + :errors errors + :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)}) + diff --git a/src/propeller/problems/PSB2/substitution_cipher.cljc b/src/propeller/problems/PSB2/substitution_cipher.cljc index 4798660..554c203 100644 --- a/src/propeller/problems/PSB2/substitution_cipher.cljc +++ b/src/propeller/problems/PSB2/substitution_cipher.cljc @@ -6,7 +6,8 @@ [propeller.push.utils.helpers :refer [get-stack-instructions]] [propeller.push.state :as state] [propeller.tools.math :as math] - [propeller.tools.metrics :as metrics])) + [propeller.tools.metrics :as metrics] + [propeller.gp :as gp])) ; =========== PROBLEM DESCRIPTION ========================= ; SUBSTITUTION CIPHER from PSB2 @@ -75,8 +76,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)}) \ No newline at end of file +(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 500 + :population-size 500 + :max-initial-plushy-size 100 + :step-limit 200 + :parent-selection :lexicase + :tournament-size 5 + :umad-rate 0.1 + :variation {:umad 0.5 :crossover 0.5} + :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 1a386ec..bdf35f7 100644 --- a/src/propeller/problems/PSB2/twitter.cljc +++ b/src/propeller/problems/PSB2/twitter.cljc @@ -6,7 +6,8 @@ [propeller.push.utils.helpers :refer [get-stack-instructions]] [propeller.push.state :as state] [propeller.tools.math :as math] - [propeller.tools.metrics :as metrics])) + [propeller.tools.metrics :as metrics] + [propeller.gp :as gp])) ; =========== PROBLEM DESCRIPTION ============================= ; TWITTER from PSB2 @@ -66,8 +67,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)}) \ No newline at end of file +(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 500 + :population-size 500 + :max-initial-plushy-size 100 + :step-limit 200 + :parent-selection :lexicase + :tournament-size 5 + :umad-rate 0.1 + :variation {:umad 0.5 :crossover 0.5} + :elitism false} + (apply hash-map (map #(if (string? %) (read-string %) %) args))))) \ No newline at end of file diff --git a/src/propeller/problems/simple_regression.cljc b/src/propeller/problems/simple_regression.cljc index 6bcde53..e0ed37d 100755 --- a/src/propeller/problems/simple_regression.cljc +++ b/src/propeller/problems/simple_regression.cljc @@ -2,7 +2,8 @@ (:require [propeller.genome :as genome] [propeller.push.interpreter :as interpreter] [propeller.push.state :as state] - [propeller.tools.math :as math])) + [propeller.tools.math :as math] + [propeller.gp :as gp])) (defn- target-function "Target function: f(x) = x^3 + x + 3" @@ -59,8 +60,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)}) \ No newline at end of file +(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 500 + :population-size 500 + :max-initial-plushy-size 100 + :step-limit 200 + :parent-selection :lexicase + :tournament-size 5 + :umad-rate 0.1 + :variation {:umad 0.5 :crossover 0.5} + :elitism false} + (apply hash-map (map #(if (string? %) (read-string %) %) args))))) \ No newline at end of file diff --git a/src/propeller/problems/software/number_io.cljc b/src/propeller/problems/software/number_io.cljc index afc449d..2acae61 100755 --- a/src/propeller/problems/software/number_io.cljc +++ b/src/propeller/problems/software/number_io.cljc @@ -6,6 +6,7 @@ [propeller.utils :as utils] [propeller.push.state :as state] [propeller.tools.math :as math] + [propeller.gp :as gp] #?(:cljs [cljs.reader :refer [read-string]]))) ;; ============================================================================= @@ -60,34 +61,51 @@ :test test-set})) (defn error-function - ([argmap individual] - (error-function argmap individual :train)) - ([argmap individual subset] - (let [program (genome/plushy->push (:plushy individual) argmap) - data (get train-and-test-data subset) - inputs (:inputs data) - correct-outputs (:outputs data) - outputs (map (fn [input] - (state/peek-stack - (interpreter/interpret-program - program - (assoc state/empty-state :input {:in1 (first input) - :in2 (last input)} - :output '("")) - (:step-limit argmap)) - :output)) - 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] - (min 1000.0 (math/abs (- correct-output output)))) - correct-outputs - parsed-outputs)] - (assoc individual - :behaviors parsed-outputs - :errors errors - :total-error #?(:clj (apply +' errors) - :cljs (apply + errors)))))) + [argmap data individual] + (let [program (genome/plushy->push (:plushy individual) argmap) + inputs (:inputs data) + correct-outputs (:outputs data) + outputs (map (fn [input] + (state/peek-stack + (interpreter/interpret-program + program + (assoc state/empty-state :input {:in1 (first input) + :in2 (last input)} + :output '("")) + (:step-limit argmap)) + :output)) + 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] + (min 1000.0 (math/abs (- correct-output 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 500 + :population-size 500 + :max-initial-plushy-size 100 + :step-limit 200 + :parent-selection :lexicase + :tournament-size 5 + :umad-rate 0.1 + :variation {:umad 0.5 :crossover 0.5} + :elitism false} + (apply hash-map (map #(if (string? %) (read-string %) %) args))))) \ No newline at end of file diff --git a/src/propeller/problems/software/smallest.cljc b/src/propeller/problems/software/smallest.cljc index c48876b..a70ac59 100755 --- a/src/propeller/problems/software/smallest.cljc +++ b/src/propeller/problems/software/smallest.cljc @@ -5,6 +5,7 @@ [propeller.push.utils.helpers :refer [get-stack-instructions]] [propeller.utils :as utils] [propeller.push.state :as state] + [propeller.gp :as gp] #?(:cljs [cljs.reader :refer [read-string]]))) ;; ============================================================================= @@ -62,34 +63,51 @@ :test test-set})) (defn error-function - ([argmap individual] - (error-function argmap individual :train)) - ([argmap individual subset] - (let [program (genome/plushy->push (:plushy individual) argmap) - data (get train-and-test-data subset) - inputs (:inputs data) - correct-outputs (:outputs data) - outputs (map (fn [input] - (state/peek-stack - (interpreter/interpret-program - program - (assoc state/empty-state :input {:in1 (get input 0) - :in2 (get input 1) - :in3 (get input 2) - :in4 (get input 3)} - :output '("")) - (:step-limit argmap)) - :output)) - inputs) - errors (map (fn [correct-output output] - (let [parsed-output (try (read-string output) - #?(:clj (catch Exception e 1000.0) - :cljs (catch js/Error. e 1000.0)))] - (if (= correct-output parsed-output) 0 1))) - correct-outputs - outputs)] - (assoc individual - :behaviors outputs - :errors errors - :total-error #?(:clj (apply +' errors) - :cljs (apply + errors)))))) + [argmap data individual] + (let [program (genome/plushy->push (:plushy individual) argmap) + inputs (:inputs data) + correct-outputs (:outputs data) + outputs (map (fn [input] + (state/peek-stack + (interpreter/interpret-program + program + (assoc state/empty-state :input {:in1 (get input 0) + :in2 (get input 1) + :in3 (get input 2) + :in4 (get input 3)} + :output '("")) + (:step-limit argmap)) + :output)) + inputs) + errors (map (fn [correct-output output] + (let [parsed-output (try (read-string output) + #?(:clj (catch Exception e 1000.0) + :cljs (catch js/Error. e 1000.0)))] + (if (= correct-output parsed-output) 0 1))) + 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 500 + :population-size 500 + :max-initial-plushy-size 100 + :step-limit 200 + :parent-selection :lexicase + :tournament-size 5 + :umad-rate 0.1 + :variation {:umad 0.5 :crossover 0.5} + :elitism false} + (apply hash-map (map #(if (string? %) (read-string %) %) args))))) \ No newline at end of file diff --git a/src/propeller/problems/string_classification.cljc b/src/propeller/problems/string_classification.cljc index aa49c16..a06490d 100755 --- a/src/propeller/problems/string_classification.cljc +++ b/src/propeller/problems/string_classification.cljc @@ -1,7 +1,8 @@ (ns propeller.problems.string-classification (:require [propeller.genome :as genome] [propeller.push.interpreter :as interpreter] - [propeller.push.state :as state])) + [propeller.push.state :as state] + [propeller.gp :as gp])) ;; ============================================================================= ;; String classification @@ -79,8 +80,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)}) \ No newline at end of file +(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 500 + :population-size 500 + :max-initial-plushy-size 100 + :step-limit 200 + :parent-selection :lexicase + :tournament-size 5 + :umad-rate 0.1 + :variation {:umad 0.5 :crossover 0.5} + :elitism false} + (apply hash-map (map #(if (string? %) (read-string %) %) args))))) \ No newline at end of file diff --git a/src/propeller/problems/valiant.cljc b/src/propeller/problems/valiant.cljc index 7cfc407..a98410a 100644 --- a/src/propeller/problems/valiant.cljc +++ b/src/propeller/problems/valiant.cljc @@ -1,7 +1,8 @@ (ns propeller.problems.valiant (:require [propeller.genome :as genome] [propeller.push.interpreter :as interpreter] - [propeller.push.state :as state])) + [propeller.push.state :as state] + [propeller.gp :as gp])) (def num-vars 100) ;10) ;100) ;1000) (def num-inputs 50) ;5) ; 50) ;500) @@ -58,8 +59,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 500 + :population-size 500 + :max-initial-plushy-size 100 + :step-limit 200 + :parent-selection :lexicase + :tournament-size 5 + :umad-rate 0.1 + :variation {:umad 0.5 :crossover 0.5} + :elitism false} + (apply hash-map (map #(if (string? %) (read-string %) %) args)))))