upstream update and new PSB2 problems
This commit is contained in:
parent
ede1abe7c4
commit
8441cfe8d3
64
README.md
64
README.md
@ -4,16 +4,62 @@ Yet another Push-based genetic programming system in Clojure.
|
|||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
To run PushGP from a REPL, load propel.core into your REPL (i.e. `lein repl`),
|
If you have installed [leiningen](https://leiningen.org), which is a tool
|
||||||
and run `-main` with arguments including, first, the problem name, for example:
|
for running Clojure programs, then you can run Propeller on a genetic
|
||||||
`(-main 'simple-regression)` or `(-main 'simple-regression :population-size 100)`.
|
programming problem that is defined within this project from the command
|
||||||
|
line with the command `lein run -m <namespace>`, replacing `<namespace>`
|
||||||
|
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
|
## CLJS Usage
|
||||||
|
|
||||||
|
@ -1,34 +1,10 @@
|
|||||||
(ns propeller.core
|
(ns propeller.core
|
||||||
#?(:clj (:gen-class))
|
#?(: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))))
|
|
||||||
|
|
||||||
(defn -main
|
(defn -main
|
||||||
"Runs propel-gp, giving it a map of arguments."
|
"Not intended to be run; just print a message."
|
||||||
[& args]
|
[& args]
|
||||||
;; Exception for when no args were passed
|
;; Exception for when no args were passed
|
||||||
(when (empty? args)
|
(println "To run a genetic programming problem, provide a the problem's")
|
||||||
(println "You must specify a problem to run.")
|
(println "namespace as specified in the Propeller README file at")
|
||||||
(println "Try, for example:")
|
(println "https://github.com/lspector/propeller/blob/master/README.md"))
|
||||||
(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))))))
|
|
@ -5,7 +5,8 @@
|
|||||||
[propeller.utils :as utils]
|
[propeller.utils :as utils]
|
||||||
[propeller.push.utils.helpers :refer [get-stack-instructions]]
|
[propeller.push.utils.helpers :refer [get-stack-instructions]]
|
||||||
[propeller.push.state :as state]
|
[propeller.push.state :as state]
|
||||||
[propeller.tools.math :as math]))
|
[propeller.tools.math :as math]
|
||||||
|
[propeller.gp :as gp]))
|
||||||
|
|
||||||
; =========== PROBLEM DESCRIPTION ============================
|
; =========== PROBLEM DESCRIPTION ============================
|
||||||
; BASEMENT from PSB2
|
; BASEMENT from PSB2
|
||||||
@ -58,9 +59,23 @@
|
|||||||
:total-error #?(:clj (apply +' errors)
|
:total-error #?(:clj (apply +' errors)
|
||||||
:cljs (apply + errors)))))
|
:cljs (apply + errors)))))
|
||||||
|
|
||||||
(def arglist
|
(defn -main
|
||||||
|
"Runs propel-gp, giving it a map of arguments."
|
||||||
|
[& args]
|
||||||
|
(gp/gp
|
||||||
|
(merge
|
||||||
{:instructions instructions
|
{:instructions instructions
|
||||||
:error-function error-function
|
:error-function error-function
|
||||||
:training-data (:train train-and-test-data)
|
:training-data (:train train-and-test-data)
|
||||||
:testing-data (:test 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)))))
|
||||||
|
|
||||||
|
@ -6,7 +6,8 @@
|
|||||||
[propeller.push.utils.helpers :refer [get-stack-instructions]]
|
[propeller.push.utils.helpers :refer [get-stack-instructions]]
|
||||||
[propeller.push.state :as state]
|
[propeller.push.state :as state]
|
||||||
[clojure.pprint :as pprint]
|
[clojure.pprint :as pprint]
|
||||||
[propeller.tools.math :as math]))
|
[propeller.tools.math :as math]
|
||||||
|
[propeller.gp :as gp]))
|
||||||
|
|
||||||
; =========== PROBLEM DESCRIPTION ===============================
|
; =========== PROBLEM DESCRIPTION ===============================
|
||||||
; BOUNCING BALLS from PSB2
|
; BOUNCING BALLS from PSB2
|
||||||
@ -70,8 +71,22 @@
|
|||||||
:total-error #?(:clj (apply +' errors)
|
:total-error #?(:clj (apply +' errors)
|
||||||
:cljs (apply + errors)))))
|
:cljs (apply + errors)))))
|
||||||
|
|
||||||
(def arglist
|
(defn -main
|
||||||
|
"Runs propel-gp, giving it a map of arguments."
|
||||||
|
[& args]
|
||||||
|
(gp/gp
|
||||||
|
(merge
|
||||||
{:instructions instructions
|
{:instructions instructions
|
||||||
:error-function error-function
|
:error-function error-function
|
||||||
:training-data (:train train-and-test-data)
|
:training-data (:train train-and-test-data)
|
||||||
:testing-data (:test 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)))))
|
||||||
|
@ -6,7 +6,8 @@
|
|||||||
[propeller.push.utils.helpers :refer [get-stack-instructions]]
|
[propeller.push.utils.helpers :refer [get-stack-instructions]]
|
||||||
[propeller.push.state :as state]
|
[propeller.push.state :as state]
|
||||||
[clojure.pprint :as pprint]
|
[clojure.pprint :as pprint]
|
||||||
[propeller.tools.math :as math]))
|
[propeller.tools.math :as math]
|
||||||
|
[propeller.gp :as gp]))
|
||||||
|
|
||||||
; =========== PROBLEM DESCRIPTION ======================
|
; =========== PROBLEM DESCRIPTION ======================
|
||||||
; BOWLING from PSB2
|
; BOWLING from PSB2
|
||||||
@ -58,8 +59,22 @@
|
|||||||
:total-error #?(:clj (apply +' errors)
|
:total-error #?(:clj (apply +' errors)
|
||||||
:cljs (apply + errors)))))
|
:cljs (apply + errors)))))
|
||||||
|
|
||||||
(def arglist
|
(defn -main
|
||||||
|
"Runs propel-gp, giving it a map of arguments."
|
||||||
|
[& args]
|
||||||
|
(gp/gp
|
||||||
|
(merge
|
||||||
{:instructions instructions
|
{:instructions instructions
|
||||||
:error-function error-function
|
:error-function error-function
|
||||||
:training-data (:train train-and-test-data)
|
:training-data (:train train-and-test-data)
|
||||||
:testing-data (:test 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)))))
|
@ -6,7 +6,8 @@
|
|||||||
[propeller.push.utils.helpers :refer [get-stack-instructions]]
|
[propeller.push.utils.helpers :refer [get-stack-instructions]]
|
||||||
[propeller.push.state :as state]
|
[propeller.push.state :as state]
|
||||||
[propeller.tools.math :as math]
|
[propeller.tools.math :as math]
|
||||||
[propeller.tools.metrics :as metrics]))
|
[propeller.tools.metrics :as metrics]
|
||||||
|
[propeller.gp :as gp]))
|
||||||
|
|
||||||
; =========== PROBLEM DESCRIPTION =====================================
|
; =========== PROBLEM DESCRIPTION =====================================
|
||||||
; CAMEL CASE from PSB2
|
; CAMEL CASE from PSB2
|
||||||
@ -94,8 +95,22 @@
|
|||||||
:total-error #?(:clj (apply +' errors)
|
:total-error #?(:clj (apply +' errors)
|
||||||
:cljs (apply + errors)))))
|
:cljs (apply + errors)))))
|
||||||
|
|
||||||
(def arglist
|
(defn -main
|
||||||
|
"Runs propel-gp, giving it a map of arguments."
|
||||||
|
[& args]
|
||||||
|
(gp/gp
|
||||||
|
(merge
|
||||||
{:instructions instructions
|
{:instructions instructions
|
||||||
:error-function error-function
|
:error-function error-function
|
||||||
:training-data (:train train-and-test-data)
|
:training-data (:train train-and-test-data)
|
||||||
:testing-data (:test 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)))))
|
||||||
|
@ -6,7 +6,8 @@
|
|||||||
[propeller.push.utils.helpers :refer [get-stack-instructions]]
|
[propeller.push.utils.helpers :refer [get-stack-instructions]]
|
||||||
[propeller.push.state :as state]
|
[propeller.push.state :as state]
|
||||||
[clojure.pprint :as pprint]
|
[clojure.pprint :as pprint]
|
||||||
[propeller.tools.math :as math]))
|
[propeller.tools.math :as math]
|
||||||
|
[propeller.gp :as gp]))
|
||||||
|
|
||||||
; =========== PROBLEM DESCRIPTION ===============================
|
; =========== PROBLEM DESCRIPTION ===============================
|
||||||
; DICE GAME from PSB2
|
; DICE GAME from PSB2
|
||||||
@ -67,8 +68,22 @@
|
|||||||
:total-error #?(:clj (apply +' errors)
|
:total-error #?(:clj (apply +' errors)
|
||||||
:cljs (apply + errors)))))
|
:cljs (apply + errors)))))
|
||||||
|
|
||||||
(def arglist
|
(defn -main
|
||||||
|
"Runs propel-gp, giving it a map of arguments."
|
||||||
|
[& args]
|
||||||
|
(gp/gp
|
||||||
|
(merge
|
||||||
{:instructions instructions
|
{:instructions instructions
|
||||||
:error-function error-function
|
:error-function error-function
|
||||||
:training-data (:train train-and-test-data)
|
:training-data (:train train-and-test-data)
|
||||||
:testing-data (:test 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)))))
|
||||||
|
@ -6,7 +6,8 @@
|
|||||||
[propeller.push.utils.helpers :refer [get-stack-instructions]]
|
[propeller.push.utils.helpers :refer [get-stack-instructions]]
|
||||||
[propeller.push.state :as state]
|
[propeller.push.state :as state]
|
||||||
[clojure.pprint :as pprint]
|
[clojure.pprint :as pprint]
|
||||||
[propeller.tools.math :as math]))
|
[propeller.tools.math :as math]
|
||||||
|
[propeller.gp :as gp]))
|
||||||
|
|
||||||
; =========== PROBLEM DESCRIPTION =========================
|
; =========== PROBLEM DESCRIPTION =========================
|
||||||
; FUEL COST from PSB2
|
; FUEL COST from PSB2
|
||||||
@ -60,9 +61,23 @@
|
|||||||
:total-error #?(:clj (apply +' errors)
|
:total-error #?(:clj (apply +' errors)
|
||||||
:cljs (apply + errors)))))
|
:cljs (apply + errors)))))
|
||||||
|
|
||||||
(def arglist
|
(defn -main
|
||||||
|
"Runs propel-gp, giving it a map of arguments."
|
||||||
|
[& args]
|
||||||
|
(gp/gp
|
||||||
|
(merge
|
||||||
{:instructions instructions
|
{:instructions instructions
|
||||||
:error-function error-function
|
:error-function error-function
|
||||||
:training-data (:train train-and-test-data)
|
:training-data (:train train-and-test-data)
|
||||||
:testing-data (:test 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)))))
|
||||||
|
|
||||||
|
@ -6,7 +6,8 @@
|
|||||||
[propeller.push.utils.helpers :refer [get-stack-instructions]]
|
[propeller.push.utils.helpers :refer [get-stack-instructions]]
|
||||||
[propeller.push.state :as state]
|
[propeller.push.state :as state]
|
||||||
[propeller.tools.math :as math]
|
[propeller.tools.math :as math]
|
||||||
[propeller.tools.metrics :as metrics]))
|
[propeller.tools.metrics :as metrics]
|
||||||
|
[propeller.gp :as gp]))
|
||||||
|
|
||||||
; =========== PROBLEM DESCRIPTION =============================
|
; =========== PROBLEM DESCRIPTION =============================
|
||||||
; MIDDLE CHARACTER from PSB2
|
; MIDDLE CHARACTER from PSB2
|
||||||
@ -63,8 +64,22 @@
|
|||||||
:total-error #?(:clj (apply +' errors)
|
:total-error #?(:clj (apply +' errors)
|
||||||
:cljs (apply + errors)))))
|
:cljs (apply + errors)))))
|
||||||
|
|
||||||
(def arglist
|
(defn -main
|
||||||
|
"Runs propel-gp, giving it a map of arguments."
|
||||||
|
[& args]
|
||||||
|
(gp/gp
|
||||||
|
(merge
|
||||||
{:instructions instructions
|
{:instructions instructions
|
||||||
:error-function error-function
|
:error-function error-function
|
||||||
:training-data (:train train-and-test-data)
|
:training-data (:train train-and-test-data)
|
||||||
:testing-data (:test 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)))))
|
||||||
|
70
src/propeller/problems/PSB2/solve_boolean.cljc
Normal file
70
src/propeller/problems/PSB2/solve_boolean.cljc
Normal file
@ -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)})
|
||||||
|
|
77
src/propeller/problems/PSB2/spin_words.cljc
Normal file
77
src/propeller/problems/PSB2/spin_words.cljc
Normal file
@ -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)})
|
||||||
|
|
@ -6,7 +6,8 @@
|
|||||||
[propeller.push.utils.helpers :refer [get-stack-instructions]]
|
[propeller.push.utils.helpers :refer [get-stack-instructions]]
|
||||||
[propeller.push.state :as state]
|
[propeller.push.state :as state]
|
||||||
[propeller.tools.math :as math]
|
[propeller.tools.math :as math]
|
||||||
[propeller.tools.metrics :as metrics]))
|
[propeller.tools.metrics :as metrics]
|
||||||
|
[propeller.gp :as gp]))
|
||||||
|
|
||||||
; =========== PROBLEM DESCRIPTION =========================
|
; =========== PROBLEM DESCRIPTION =========================
|
||||||
; SUBSTITUTION CIPHER from PSB2
|
; SUBSTITUTION CIPHER from PSB2
|
||||||
@ -75,8 +76,22 @@
|
|||||||
:total-error #?(:clj (apply +' errors)
|
:total-error #?(:clj (apply +' errors)
|
||||||
:cljs (apply + errors)))))
|
:cljs (apply + errors)))))
|
||||||
|
|
||||||
(def arglist
|
(defn -main
|
||||||
|
"Runs propel-gp, giving it a map of arguments."
|
||||||
|
[& args]
|
||||||
|
(gp/gp
|
||||||
|
(merge
|
||||||
{:instructions instructions
|
{:instructions instructions
|
||||||
:error-function error-function
|
:error-function error-function
|
||||||
:training-data (:train train-and-test-data)
|
:training-data (:train train-and-test-data)
|
||||||
:testing-data (:test 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)))))
|
@ -6,7 +6,8 @@
|
|||||||
[propeller.push.utils.helpers :refer [get-stack-instructions]]
|
[propeller.push.utils.helpers :refer [get-stack-instructions]]
|
||||||
[propeller.push.state :as state]
|
[propeller.push.state :as state]
|
||||||
[propeller.tools.math :as math]
|
[propeller.tools.math :as math]
|
||||||
[propeller.tools.metrics :as metrics]))
|
[propeller.tools.metrics :as metrics]
|
||||||
|
[propeller.gp :as gp]))
|
||||||
|
|
||||||
; =========== PROBLEM DESCRIPTION =============================
|
; =========== PROBLEM DESCRIPTION =============================
|
||||||
; TWITTER from PSB2
|
; TWITTER from PSB2
|
||||||
@ -66,8 +67,22 @@
|
|||||||
:total-error #?(:clj (apply +' errors)
|
:total-error #?(:clj (apply +' errors)
|
||||||
:cljs (apply + errors)))))
|
:cljs (apply + errors)))))
|
||||||
|
|
||||||
(def arglist
|
(defn -main
|
||||||
|
"Runs propel-gp, giving it a map of arguments."
|
||||||
|
[& args]
|
||||||
|
(gp/gp
|
||||||
|
(merge
|
||||||
{:instructions instructions
|
{:instructions instructions
|
||||||
:error-function error-function
|
:error-function error-function
|
||||||
:training-data (:train train-and-test-data)
|
:training-data (:train train-and-test-data)
|
||||||
:testing-data (:test 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)))))
|
@ -2,7 +2,8 @@
|
|||||||
(:require [propeller.genome :as genome]
|
(:require [propeller.genome :as genome]
|
||||||
[propeller.push.interpreter :as interpreter]
|
[propeller.push.interpreter :as interpreter]
|
||||||
[propeller.push.state :as state]
|
[propeller.push.state :as state]
|
||||||
[propeller.tools.math :as math]))
|
[propeller.tools.math :as math]
|
||||||
|
[propeller.gp :as gp]))
|
||||||
|
|
||||||
(defn- target-function
|
(defn- target-function
|
||||||
"Target function: f(x) = x^3 + x + 3"
|
"Target function: f(x) = x^3 + x + 3"
|
||||||
@ -59,8 +60,22 @@
|
|||||||
:total-error #?(:clj (apply +' errors)
|
:total-error #?(:clj (apply +' errors)
|
||||||
:cljs (apply + errors))))))
|
:cljs (apply + errors))))))
|
||||||
|
|
||||||
(def arglist
|
(defn -main
|
||||||
|
"Runs propel-gp, giving it a map of arguments."
|
||||||
|
[& args]
|
||||||
|
(gp/gp
|
||||||
|
(merge
|
||||||
{:instructions instructions
|
{:instructions instructions
|
||||||
:error-function error-function
|
:error-function error-function
|
||||||
:training-data (:train train-and-test-data)
|
:training-data (:train train-and-test-data)
|
||||||
:testing-data (:test 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)))))
|
@ -6,6 +6,7 @@
|
|||||||
[propeller.utils :as utils]
|
[propeller.utils :as utils]
|
||||||
[propeller.push.state :as state]
|
[propeller.push.state :as state]
|
||||||
[propeller.tools.math :as math]
|
[propeller.tools.math :as math]
|
||||||
|
[propeller.gp :as gp]
|
||||||
#?(:cljs [cljs.reader :refer [read-string]])))
|
#?(:cljs [cljs.reader :refer [read-string]])))
|
||||||
|
|
||||||
;; =============================================================================
|
;; =============================================================================
|
||||||
@ -60,11 +61,8 @@
|
|||||||
:test test-set}))
|
:test test-set}))
|
||||||
|
|
||||||
(defn error-function
|
(defn error-function
|
||||||
([argmap individual]
|
[argmap data individual]
|
||||||
(error-function argmap individual :train))
|
|
||||||
([argmap individual subset]
|
|
||||||
(let [program (genome/plushy->push (:plushy individual) argmap)
|
(let [program (genome/plushy->push (:plushy individual) argmap)
|
||||||
data (get train-and-test-data subset)
|
|
||||||
inputs (:inputs data)
|
inputs (:inputs data)
|
||||||
correct-outputs (:outputs data)
|
correct-outputs (:outputs data)
|
||||||
outputs (map (fn [input]
|
outputs (map (fn [input]
|
||||||
@ -90,4 +88,24 @@
|
|||||||
:behaviors parsed-outputs
|
:behaviors parsed-outputs
|
||||||
:errors errors
|
:errors errors
|
||||||
:total-error #?(:clj (apply +' errors)
|
:total-error #?(:clj (apply +' errors)
|
||||||
:cljs (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)))))
|
@ -5,6 +5,7 @@
|
|||||||
[propeller.push.utils.helpers :refer [get-stack-instructions]]
|
[propeller.push.utils.helpers :refer [get-stack-instructions]]
|
||||||
[propeller.utils :as utils]
|
[propeller.utils :as utils]
|
||||||
[propeller.push.state :as state]
|
[propeller.push.state :as state]
|
||||||
|
[propeller.gp :as gp]
|
||||||
#?(:cljs [cljs.reader :refer [read-string]])))
|
#?(:cljs [cljs.reader :refer [read-string]])))
|
||||||
|
|
||||||
;; =============================================================================
|
;; =============================================================================
|
||||||
@ -62,11 +63,8 @@
|
|||||||
:test test-set}))
|
:test test-set}))
|
||||||
|
|
||||||
(defn error-function
|
(defn error-function
|
||||||
([argmap individual]
|
[argmap data individual]
|
||||||
(error-function argmap individual :train))
|
|
||||||
([argmap individual subset]
|
|
||||||
(let [program (genome/plushy->push (:plushy individual) argmap)
|
(let [program (genome/plushy->push (:plushy individual) argmap)
|
||||||
data (get train-and-test-data subset)
|
|
||||||
inputs (:inputs data)
|
inputs (:inputs data)
|
||||||
correct-outputs (:outputs data)
|
correct-outputs (:outputs data)
|
||||||
outputs (map (fn [input]
|
outputs (map (fn [input]
|
||||||
@ -92,4 +90,24 @@
|
|||||||
:behaviors outputs
|
:behaviors outputs
|
||||||
:errors errors
|
:errors errors
|
||||||
:total-error #?(:clj (apply +' errors)
|
:total-error #?(:clj (apply +' errors)
|
||||||
:cljs (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)))))
|
@ -1,7 +1,8 @@
|
|||||||
(ns propeller.problems.string-classification
|
(ns propeller.problems.string-classification
|
||||||
(:require [propeller.genome :as genome]
|
(:require [propeller.genome :as genome]
|
||||||
[propeller.push.interpreter :as interpreter]
|
[propeller.push.interpreter :as interpreter]
|
||||||
[propeller.push.state :as state]))
|
[propeller.push.state :as state]
|
||||||
|
[propeller.gp :as gp]))
|
||||||
|
|
||||||
;; =============================================================================
|
;; =============================================================================
|
||||||
;; String classification
|
;; String classification
|
||||||
@ -79,8 +80,22 @@
|
|||||||
:total-error #?(:clj (apply +' errors)
|
:total-error #?(:clj (apply +' errors)
|
||||||
:cljs (apply + errors)))))
|
:cljs (apply + errors)))))
|
||||||
|
|
||||||
(def arglist
|
(defn -main
|
||||||
|
"Runs propel-gp, giving it a map of arguments."
|
||||||
|
[& args]
|
||||||
|
(gp/gp
|
||||||
|
(merge
|
||||||
{:instructions instructions
|
{:instructions instructions
|
||||||
:error-function error-function
|
:error-function error-function
|
||||||
:training-data (:train train-and-test-data)
|
:training-data (:train train-and-test-data)
|
||||||
:testing-data (:test 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)))))
|
@ -1,7 +1,8 @@
|
|||||||
(ns propeller.problems.valiant
|
(ns propeller.problems.valiant
|
||||||
(:require [propeller.genome :as genome]
|
(:require [propeller.genome :as genome]
|
||||||
[propeller.push.interpreter :as interpreter]
|
[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-vars 100) ;10) ;100) ;1000)
|
||||||
(def num-inputs 50) ;5) ; 50) ;500)
|
(def num-inputs 50) ;5) ; 50) ;500)
|
||||||
@ -58,8 +59,22 @@
|
|||||||
:total-error #?(:clj (apply +' errors)
|
:total-error #?(:clj (apply +' errors)
|
||||||
:cljs (apply + errors)))))
|
:cljs (apply + errors)))))
|
||||||
|
|
||||||
(def arglist
|
(defn -main
|
||||||
|
"Runs propel-gp, giving it a map of arguments."
|
||||||
|
[& args]
|
||||||
|
(gp/gp
|
||||||
|
(merge
|
||||||
{:instructions instructions
|
{:instructions instructions
|
||||||
:error-function error-function
|
:error-function error-function
|
||||||
:training-data (:train train-and-test-data)
|
:training-data (:train train-and-test-data)
|
||||||
:testing-data (:test 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)))))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user