Merge branch 'master' into test-vector-instructions
This commit is contained in:
commit
a985b6a9b5
1
.gitignore
vendored
1
.gitignore
vendored
@ -22,3 +22,4 @@ notes
|
|||||||
# use to hold the data from
|
# use to hold the data from
|
||||||
# https://github.com/thelmuth/program-synthesis-benchmark-datasets
|
# https://github.com/thelmuth/program-synthesis-benchmark-datasets
|
||||||
/data
|
/data
|
||||||
|
**/.DS_Store
|
||||||
|
BIN
src/.DS_Store
vendored
BIN
src/.DS_Store
vendored
Binary file not shown.
BIN
src/propeller/.DS_Store
vendored
BIN
src/propeller/.DS_Store
vendored
Binary file not shown.
@ -25,8 +25,12 @@
|
|||||||
(println "Best behaviors:" (:behaviors best))
|
(println "Best behaviors:" (:behaviors best))
|
||||||
(println "Genotypic diversity:"
|
(println "Genotypic diversity:"
|
||||||
(float (/ (count (distinct (map :plushy pop))) (count pop))))
|
(float (/ (count (distinct (map :plushy pop))) (count pop))))
|
||||||
|
(println "Behavioral diversity:"
|
||||||
|
(float (/ (count (distinct (map :behaviors pop))) (count pop))))
|
||||||
(println "Average genome length:"
|
(println "Average genome length:"
|
||||||
(float (/ (reduce + (map count (map :plushy pop))) (count pop))))
|
(float (/ (reduce + (map count (map :plushy pop))) (count pop))))
|
||||||
|
(println "Average total error:"
|
||||||
|
(float (/ (reduce + (map :total-error pop)) (count pop))))
|
||||||
(println)))
|
(println)))
|
||||||
|
|
||||||
(defn gp
|
(defn gp
|
||||||
|
62
src/propeller/problems/valiant.cljc
Normal file
62
src/propeller/problems/valiant.cljc
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
(ns propeller.problems.valiant
|
||||||
|
(:require [propeller.genome :as genome]
|
||||||
|
[propeller.push.interpreter :as interpreter]
|
||||||
|
[propeller.push.state :as state]))
|
||||||
|
|
||||||
|
(def num-vars 100) ;10) ;100) ;1000)
|
||||||
|
(def num-inputs 50) ;5) ; 50) ;500)
|
||||||
|
(def num-train 500) ;5000)
|
||||||
|
(def num-test 200)
|
||||||
|
|
||||||
|
(def train-and-test-data
|
||||||
|
(let [input-indices (take num-inputs (shuffle (range num-vars)))
|
||||||
|
rand-vars (fn [] (vec (repeatedly num-vars #(< (rand) 0.5))))
|
||||||
|
even-parity? (fn [vars]
|
||||||
|
(even? (count (filter #(= % true)
|
||||||
|
(map #(nth vars %)
|
||||||
|
input-indices)))))
|
||||||
|
train-inputs (repeatedly num-train rand-vars)
|
||||||
|
test-inputs (repeatedly num-test rand-vars)]
|
||||||
|
{:train {:inputs train-inputs
|
||||||
|
:outputs (map even-parity? train-inputs)}
|
||||||
|
:test {:inputs test-inputs
|
||||||
|
:outputs (map even-parity? test-inputs)}}))
|
||||||
|
|
||||||
|
(def instructions
|
||||||
|
(vec (concat (for [i (range num-vars)] (keyword (str "in" i)))
|
||||||
|
(take num-inputs
|
||||||
|
(cycle [:boolean_xor
|
||||||
|
:boolean_or
|
||||||
|
:boolean_and
|
||||||
|
:boolean_not
|
||||||
|
:exec_if
|
||||||
|
'close
|
||||||
|
])))))
|
||||||
|
|
||||||
|
(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 (zipmap (for [i (range (count input))]
|
||||||
|
(keyword (str "in" i)))
|
||||||
|
input))
|
||||||
|
(:step-limit argmap))
|
||||||
|
:boolean))
|
||||||
|
inputs)
|
||||||
|
errors (map #(if (= %1 %2) 0 1)
|
||||||
|
correct-outputs
|
||||||
|
outputs)]
|
||||||
|
(assoc individual
|
||||||
|
:behaviors outputs
|
||||||
|
:errors errors
|
||||||
|
:total-error #?(:clj (apply +' errors)
|
||||||
|
:cljs (apply + errors))))))
|
@ -10,145 +10,146 @@
|
|||||||
[propeller.push.state :as state]
|
[propeller.push.state :as state]
|
||||||
[propeller.push.utils.helpers :refer [get-stack-instructions]]))
|
[propeller.push.utils.helpers :refer [get-stack-instructions]]))
|
||||||
|
|
||||||
#_(interpreter/interpret-program
|
;#_(interpreter/interpret-program
|
||||||
'(1 2 :integer_add) state/empty-state 1000)
|
; '(1 2 :integer_add) state/empty-state 1000)
|
||||||
|
;
|
||||||
#_(interpreter/interpret-program
|
;#_(interpreter/interpret-program
|
||||||
'(3 3 :integer_eq :exec_if (1 "yes") (2 "no"))
|
; '(3 3 :integer_eq :exec_if (1 "yes") (2 "no"))
|
||||||
state/empty-state
|
; state/empty-state
|
||||||
1000)
|
; 1000)
|
||||||
|
;
|
||||||
#_(interpreter/interpret-program
|
;#_(interpreter/interpret-program
|
||||||
'(:in1 :string_reverse 1 :string_take "?" :string_eq :exec_if
|
; '(:in1 :string_reverse 1 :string_take "?" :string_eq :exec_if
|
||||||
(:in1 " I am asking." :string_concat)
|
; (:in1 " I am asking." :string_concat)
|
||||||
(:in1 " I am saying." :string_concat))
|
; (:in1 " I am saying." :string_concat))
|
||||||
(assoc state/empty-state :input {:in1 "Can you hear me?"})
|
; (assoc state/empty-state :input {:in1 "Can you hear me?"})
|
||||||
1000)
|
; 1000)
|
||||||
|
;
|
||||||
#_(interpreter/interpret-program
|
;#_(interpreter/interpret-program
|
||||||
'(:in1 :string_reverse 1 :string_take "?" :string_eq :exec_if
|
; '(:in1 :string_reverse 1 :string_take "?" :string_eq :exec_if
|
||||||
(:in1 " I am asking." :string_concat)
|
; (:in1 " I am asking." :string_concat)
|
||||||
(:in1 " I am saying." :string_concat))
|
; (:in1 " I am saying." :string_concat))
|
||||||
(assoc state/empty-state :input {:in1 "I can hear you."})
|
; (assoc state/empty-state :input {:in1 "I can hear you."})
|
||||||
1000)
|
; 1000)
|
||||||
|
;
|
||||||
#_(genome/plushy->push
|
;#_(genome/plushy->push
|
||||||
(genome/make-random-plushy (get-stack-instructions #{:float :integer :exec :boolean}) 20))
|
; (genome/make-random-plushy (get-stack-instructions #{:float :integer :exec :boolean}) 20))
|
||||||
|
;
|
||||||
#_(gp/gp {:instructions propeller.problems.software.number-io/instructions
|
;#_(gp/gp {:instructions propeller.problems.software.number-io/instructions
|
||||||
:error-function propeller.problems.software.number-io/error-function
|
; :error-function propeller.problems.software.number-io/error-function
|
||||||
:max-generations 500
|
; :max-generations 500
|
||||||
:population-size 500
|
; :population-size 500
|
||||||
:max-initial-plushy-size 100
|
; :max-initial-plushy-size 100
|
||||||
:step-limit 200
|
; :step-limit 200
|
||||||
:parent-selection :lexicase
|
; :parent-selection :lexicase
|
||||||
:tournament-size 5
|
; :tournament-size 5
|
||||||
:umad-rate 0.1
|
; :umad-rate 0.1
|
||||||
:variation {:umad 0.5 :crossover 0.5}
|
; :variation {:umad 0.5 :crossover 0.5}
|
||||||
:elitism false})
|
; :elitism false})
|
||||||
|
;
|
||||||
#_(gp/gp {:instructions propeller.problems.simple-regression/instructions
|
;#_(gp/gp {:instructions propeller.problems.simple-regression/instructions
|
||||||
:error-function propeller.problems.simple-regression/error-function
|
; :error-function propeller.problems.simple-regression/error-function
|
||||||
:max-generations 500
|
; :max-generations 500
|
||||||
:population-size 500
|
; :population-size 500
|
||||||
:max-initial-plushy-size 100
|
; :max-initial-plushy-size 100
|
||||||
:step-limit 200
|
; :step-limit 200
|
||||||
:parent-selection :tournament
|
; :parent-selection :tournament
|
||||||
:tournament-size 5
|
; :tournament-size 5
|
||||||
:umad-rate 0.01
|
; :umad-rate 0.01
|
||||||
:variation {:umad 1.0
|
; :variation {:umad 1.0
|
||||||
:crossover 0.0}
|
; :crossover 0.0}
|
||||||
:elitism false})
|
; :elitism false})
|
||||||
|
;
|
||||||
#_(gp/gp {:instructions propeller.problems.simple-regression/instructions
|
;#_(gp/gp {:instructions propeller.problems.simple-regression/instructions
|
||||||
:error-function propeller.problems.simple-regression/error-function
|
; :error-function propeller.problems.simple-regression/error-function
|
||||||
:max-generations 500
|
; :max-generations 500
|
||||||
:population-size 500
|
; :population-size 500
|
||||||
:max-initial-plushy-size 100
|
; :max-initial-plushy-size 100
|
||||||
:step-limit 200
|
; :step-limit 200
|
||||||
:parent-selection :tournament
|
; :parent-selection :tournament
|
||||||
:tournament-size 5
|
; :tournament-size 5
|
||||||
:umad-rate 0.1
|
; :umad-rate 0.1
|
||||||
:variation {:umad 1.0
|
; :variation {:umad 1.0
|
||||||
:crossover 0.0}
|
; :crossover 0.0}
|
||||||
:elitism false})
|
; :elitism false})
|
||||||
|
;
|
||||||
|
;
|
||||||
#_(gp/gp {:instructions propeller.problems.simple-regression/instructions
|
;#_(gp/gp {:instructions propeller.problems.simple-regression/instructions
|
||||||
:error-function propeller.problems.simple-regression/error-function
|
; :error-function propeller.problems.simple-regression/error-function
|
||||||
:max-generations 500
|
; :max-generations 500
|
||||||
:population-size 500
|
; :population-size 500
|
||||||
:max-initial-plushy-size 100
|
; :max-initial-plushy-size 100
|
||||||
:step-limit 200
|
; :step-limit 200
|
||||||
:parent-selection :lexicase
|
; :parent-selection :lexicase
|
||||||
:tournament-size 5
|
; :tournament-size 5
|
||||||
:umad-rate 0.1
|
; :umad-rate 0.1
|
||||||
:variation {:umad 1.0
|
; :variation {:umad 1.0
|
||||||
:crossover 0.0}
|
; :crossover 0.0}
|
||||||
:elitism false})
|
; :elitism false})
|
||||||
|
;
|
||||||
#_(gp/gp {:instructions propeller.problems.simple-regression/instructions
|
;#_(gp/gp {:instructions propeller.problems.simple-regression/instructions
|
||||||
:error-function propeller.problems.simple-regression/error-function
|
; :error-function propeller.problems.simple-regression/error-function
|
||||||
:max-generations 500
|
; :max-generations 500
|
||||||
:population-size 500
|
; :population-size 500
|
||||||
:max-initial-plushy-size 100
|
; :max-initial-plushy-size 100
|
||||||
:step-limit 200
|
; :step-limit 200
|
||||||
:parent-selection :lexicase
|
; :parent-selection :lexicase
|
||||||
:tournament-size 5
|
; :tournament-size 5
|
||||||
:umad-rate 0.1
|
; :umad-rate 0.1
|
||||||
:diploid-flip-rate 0.1
|
; :diploid-flip-rate 0.1
|
||||||
:variation {:umad 0.8
|
; :variation {:umad 0.8
|
||||||
:diploid-flip 0.2}
|
; :diploid-flip 0.2}
|
||||||
:elitism false
|
; :elitism false
|
||||||
:diploid true})
|
; :diploid true})
|
||||||
|
;
|
||||||
|
;
|
||||||
#_(gp/gp {:instructions propeller.problems.software.smallest/instructions
|
;#_(gp/gp {:instructions propeller.problems.software.smallest/instructions
|
||||||
:error-function propeller.problems.software.smallest/error-function
|
; :error-function propeller.problems.software.smallest/error-function
|
||||||
:max-generations 500
|
; :max-generations 500
|
||||||
:population-size 500
|
; :population-size 500
|
||||||
:max-initial-plushy-size 100
|
; :max-initial-plushy-size 100
|
||||||
:step-limit 200
|
; :step-limit 200
|
||||||
:parent-selection :lexicase
|
; :parent-selection :lexicase
|
||||||
:tournament-size 5
|
; :tournament-size 5
|
||||||
:umad-rate 0.1
|
; :umad-rate 0.1
|
||||||
:diploid-flip-rate 0.1
|
; :diploid-flip-rate 0.1
|
||||||
:variation {;:umad 0.8
|
; :variation {;:umad 0.8
|
||||||
;:diploid-flip 0.2
|
; ;:diploid-flip 0.2
|
||||||
:umad 1
|
; :umad 1
|
||||||
}
|
; }
|
||||||
:elitism false
|
; :elitism false
|
||||||
:diploid false})
|
; :diploid false})
|
||||||
|
;
|
||||||
#_(gp/gp {:instructions propeller.problems.software.smallest/instructions
|
;#_(gp/gp {:instructions propeller.problems.software.smallest/instructions
|
||||||
:error-function propeller.problems.software.smallest/error-function
|
; :error-function propeller.problems.software.smallest/error-function
|
||||||
:max-generations 500
|
; :max-generations 500
|
||||||
:population-size 500
|
; :population-size 500
|
||||||
:max-initial-plushy-size 200 ;100
|
; :max-initial-plushy-size 200 ;100
|
||||||
:step-limit 200
|
; :step-limit 200
|
||||||
:parent-selection :lexicase
|
; :parent-selection :lexicase
|
||||||
:tournament-size 5
|
; :tournament-size 5
|
||||||
:umad-rate 0.1
|
; :umad-rate 0.1
|
||||||
:diploid-flip-rate 0.1
|
; :diploid-flip-rate 0.1
|
||||||
:variation {:umad 0.8
|
; :variation {:umad 0.8
|
||||||
:diploid-flip 0.2
|
; :diploid-flip 0.2
|
||||||
;:umad 1
|
; ;:umad 1
|
||||||
}
|
; }
|
||||||
:elitism false
|
; :elitism false
|
||||||
:diploid true})
|
; :diploid true})
|
||||||
|
;
|
||||||
#_(gp/gp {:instructions propeller.problems.string-classification/instructions
|
;
|
||||||
:error-function propeller.problems.string-classification/error-function
|
;(gp/gp {:instructions propeller.problems.string-classification/instructions
|
||||||
:max-generations 500
|
; :error-function propeller.problems.string-classification/error-function
|
||||||
:population-size 500
|
; :max-generations 500
|
||||||
:max-initial-plushy-size 100
|
; :population-size 500
|
||||||
:step-limit 200
|
; :max-initial-plushy-size 100
|
||||||
:parent-selection :lexicase
|
; :step-limit 200
|
||||||
:tournament-size 5
|
; :parent-selection :lexicase
|
||||||
:umad-rate 0.1
|
; :tournament-size 5
|
||||||
:diploid-flip-rate 0.1
|
; :umad-rate 0.1
|
||||||
:variation {:umad 0.8
|
; :diploid-flip-rate 0.1
|
||||||
:diploid-flip 0.2
|
; :variation {:umad 0.8
|
||||||
}
|
; :diploid-flip 0.2
|
||||||
:elitism false
|
; }
|
||||||
:diploid true})
|
; :elitism false
|
||||||
|
; :diploid true})
|
||||||
|
@ -16,6 +16,20 @@
|
|||||||
shorter-padded
|
shorter-padded
|
||||||
longer))))
|
longer))))
|
||||||
|
|
||||||
|
(defn tail-aligned-crossover
|
||||||
|
"Crosses over two individuals using uniform crossover. Pads shorter one on the left."
|
||||||
|
[plushy-a plushy-b]
|
||||||
|
(let [shorter (min-key count plushy-a plushy-b)
|
||||||
|
longer (if (= shorter plushy-a)
|
||||||
|
plushy-b
|
||||||
|
plushy-a)
|
||||||
|
length-diff (- (count longer) (count shorter))
|
||||||
|
shorter-padded (concat (repeat length-diff :crossover-padding) shorter)]
|
||||||
|
(remove #(= % :crossover-padding)
|
||||||
|
(map #(if (< (rand) 0.5) %1 %2)
|
||||||
|
shorter-padded
|
||||||
|
longer))))
|
||||||
|
|
||||||
(defn diploid-crossover
|
(defn diploid-crossover
|
||||||
"Crosses over two individuals using uniform crossover. Pads shorter one."
|
"Crosses over two individuals using uniform crossover. Pads shorter one."
|
||||||
[plushy-a plushy-b]
|
[plushy-a plushy-b]
|
||||||
@ -32,6 +46,22 @@
|
|||||||
shorter-padded
|
shorter-padded
|
||||||
longer)))))
|
longer)))))
|
||||||
|
|
||||||
|
(defn tail-aligned-diploid-crossover
|
||||||
|
"Crosses over two individuals using uniform crossover. Pads shorter one on the left."
|
||||||
|
[plushy-a plushy-b]
|
||||||
|
(let [plushy-a (partition 2 plushy-a)
|
||||||
|
plushy-b (partition 2 plushy-b)
|
||||||
|
shorter (min-key count plushy-a plushy-b)
|
||||||
|
longer (if (= shorter plushy-a)
|
||||||
|
plushy-b
|
||||||
|
plushy-a)
|
||||||
|
length-diff (- (count longer) (count shorter))
|
||||||
|
shorter-padded (concat (repeat length-diff :crossover-padding) shorter)]
|
||||||
|
(flatten (remove #(= % :crossover-padding)
|
||||||
|
(map #(if (< (rand) 0.5) %1 %2)
|
||||||
|
shorter-padded
|
||||||
|
longer)))))
|
||||||
|
|
||||||
(defn uniform-addition
|
(defn uniform-addition
|
||||||
"Returns plushy with new instructions possibly added before or after each
|
"Returns plushy with new instructions possibly added before or after each
|
||||||
existing instruction."
|
existing instruction."
|
||||||
@ -51,6 +81,16 @@
|
|||||||
%)
|
%)
|
||||||
plushy))
|
plushy))
|
||||||
|
|
||||||
|
(defn diploid-uniform-silent-replacement
|
||||||
|
"Returns plushy with new instructions possibly replacing existing
|
||||||
|
instructions, but only among the silent member of each pair."
|
||||||
|
[plushy instructions replacement-rate]
|
||||||
|
(interleave (map first (partition 2 plushy))
|
||||||
|
(map #(if (< (rand) replacement-rate)
|
||||||
|
(utils/random-instruction instructions)
|
||||||
|
%)
|
||||||
|
(map second (partition 2 plushy)))))
|
||||||
|
|
||||||
(defn diploid-uniform-addition
|
(defn diploid-uniform-addition
|
||||||
"Returns plushy with new instructions possibly added before or after each
|
"Returns plushy with new instructions possibly added before or after each
|
||||||
existing instruction."
|
existing instruction."
|
||||||
@ -105,6 +145,11 @@
|
|||||||
(:plushy (selection/select-parent pop argmap))
|
(:plushy (selection/select-parent pop argmap))
|
||||||
(:plushy (selection/select-parent pop argmap)))
|
(:plushy (selection/select-parent pop argmap)))
|
||||||
;
|
;
|
||||||
|
:tail-aligned-crossover
|
||||||
|
(tail-aligned-crossover
|
||||||
|
(:plushy (selection/select-parent pop argmap))
|
||||||
|
(:plushy (selection/select-parent pop argmap)))
|
||||||
|
;
|
||||||
:umad
|
:umad
|
||||||
(-> (:plushy (selection/select-parent pop argmap))
|
(-> (:plushy (selection/select-parent pop argmap))
|
||||||
(uniform-addition (:instructions argmap) (:umad-rate argmap))
|
(uniform-addition (:instructions argmap) (:umad-rate argmap))
|
||||||
@ -118,6 +163,10 @@
|
|||||||
(-> (:plushy (selection/select-parent pop argmap))
|
(-> (:plushy (selection/select-parent pop argmap))
|
||||||
(uniform-replacement (:instructions argmap) (:replacement-rate argmap)))
|
(uniform-replacement (:instructions argmap) (:replacement-rate argmap)))
|
||||||
;
|
;
|
||||||
|
:diploid-uniform-silent-replacement
|
||||||
|
(-> (:plushy (selection/select-parent pop argmap))
|
||||||
|
(diploid-uniform-silent-replacement (:instructions argmap) (:replacement-rate argmap)))
|
||||||
|
;
|
||||||
:uniform-deletion
|
:uniform-deletion
|
||||||
(-> (:plushy (selection/select-parent pop argmap))
|
(-> (:plushy (selection/select-parent pop argmap))
|
||||||
(uniform-deletion (:umad-rate argmap)))
|
(uniform-deletion (:umad-rate argmap)))
|
||||||
@ -127,6 +176,11 @@
|
|||||||
(:plushy (selection/select-parent pop argmap))
|
(:plushy (selection/select-parent pop argmap))
|
||||||
(:plushy (selection/select-parent pop argmap)))
|
(:plushy (selection/select-parent pop argmap)))
|
||||||
;
|
;
|
||||||
|
:tail-aligned-diploid-crossover
|
||||||
|
(tail-aligned-diploid-crossover
|
||||||
|
(:plushy (selection/select-parent pop argmap))
|
||||||
|
(:plushy (selection/select-parent pop argmap)))
|
||||||
|
;
|
||||||
:diploid-umad
|
:diploid-umad
|
||||||
(-> (:plushy (selection/select-parent pop argmap))
|
(-> (:plushy (selection/select-parent pop argmap))
|
||||||
(diploid-uniform-addition (:instructions argmap) (:umad-rate argmap))
|
(diploid-uniform-addition (:instructions argmap) (:umad-rate argmap))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user