changed path to map

This commit is contained in:
Shuzo Katayama 2021-07-03 14:40:26 -04:00
parent 829b41e253
commit 6bd8a60c41
7 changed files with 123 additions and 1575 deletions

View File

@ -1 +1 @@
/Volumes/Samsung_T5/EvolutionaryComputing/PSB2 /Volumes/Samsung_T5/EvolutionaryComputing/PSB2/

File diff suppressed because one or more lines are too long

View File

@ -21,29 +21,38 @@
(System/exit 1)) (System/exit 1))
;; Creates problems ;; Creates problems
;; * default params changed to PSB2 defaults *
(require (symbol (str "propeller.problems." (first args)))) (require (symbol (str "propeller.problems." (first args))))
(when (string/includes? (first args) "PSB2") ;(when (string/includes? (first args) "PSB2")
(spit "PSB2_path.txt" (second args)) ; (spit "PSB2_path.txt" (second args))
(println (str "Set path to PSB2 as " (second args)))) ; (println (str "Set path to PSB2 as " (second args))))
(gp/gp (gp/gp
(update-in (update-in
(merge (merge
{:instructions (eval-problem-var (first args) "instructions") {:instructions (eval-problem-var (first args) "instructions")
:error-function (eval-problem-var (first args) "error-function") :error-function (eval-problem-var (first args) "error-function")
:max-generations 500 :max-generations 300
:population-size 500 :population-size 1000
:max-initial-plushy-size 100 :max-initial-plushy-size 250
:step-limit 200 :step-limit 2000
: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 1.0 :crossover 0.0}
:elitism false} :elitism false
:PSB2-path "/Volumes/Samsung_T5/EvolutionaryComputing/PSB2/"}
(apply hash-map (apply hash-map
(if (string/includes? (first args) "PSB2") (map #(if (string? %) (read-string %) %)
(map #(if (string? %) (read-string %) %) (rest args))))
(rest (remove #(= % (second args)) args))) ;(apply hash-map
(map #(if (string? %) (read-string %) %) ; (if (string/includes? (first args) "PSB2")
(rest args))))) ; ;; Removes PSB2 path as an arg so that it isn't included in parameter changes
; (map #(if (string? %) (read-string %) %)
; (rest (remove #(= % (second args)) args)))
; ;; Regular parameter changes
; (map #(if (string? %) (read-string %) %)
; (rest args)))))
[:error-function] [:error-function]
identity))) identity)))
;;keyword path as a separate argument lol

View File

@ -1,33 +1,47 @@
(ns propeller.gp (ns propeller.gp
(:require [clojure.string] (:require [clojure.string]
[clojure.pprint] [clojure.pprint]
[propeller.genome :as genome] [propeller.genome :as genome]
[propeller.variation :as variation] [propeller.variation :as variation]
[propeller.push.instructions.bool] [propeller.push.instructions.bool]
[propeller.push.instructions.character] [propeller.push.instructions.character]
[propeller.push.instructions.code] [propeller.push.instructions.code]
[propeller.push.instructions.input-output] [propeller.push.instructions.input-output]
[propeller.push.instructions.numeric] [propeller.push.instructions.numeric]
[propeller.push.instructions.polymorphic] [propeller.push.instructions.polymorphic]
[propeller.push.instructions.string] [propeller.push.instructions.string]
[propeller.push.instructions.vector])) [propeller.push.instructions.vector]))
(defn report (defn report
"Reports information each generation." "Reports information each generation."
[pop generation argmap] [pop generation argmap]
(let [best (first pop)] (let [best (first pop)]
(clojure.pprint/pprint {:generation generation (println {:generation generation
:best-plushy (:plushy best) :best-plushy (:plushy best)
:best-program (genome/plushy->push (:plushy best) argmap) :best-program (genome/plushy->push (:plushy best) argmap)
:best-total-error (:total-error best) :best-total-error (:total-error best)
:best-errors (:errors best) :best-errors (:errors best)
:best-behaviors (:behaviors best) :best-behaviors (:behaviors best)
:genotypic-diversity (float (/ (count (distinct (map :plushy pop))) (count pop))) :genotypic-diversity (float (/ (count (distinct (map :plushy pop))) (count pop)))
:behavioral-diversity (float (/ (count (distinct (map :behaviors pop))) (count pop))) :behavioral-diversity (float (/ (count (distinct (map :behaviors pop))) (count pop)))
:average-genome-length (float (/ (reduce + (map count (map :plushy pop))) (count pop))) :average-genome-length (float (/ (reduce + (map count (map :plushy pop))) (count pop)))
:average-total-error (float (/ (reduce + (map :total-error pop)) (count pop)))}) :average-total-error (float (/ (reduce + (map :total-error pop)) (count pop)))})
(println))) (println)))
;(let [best (first pop)]
; (clojure.pprint/pprint {:generation generation
; :best-plushy (:plushy best)
; :best-program (genome/plushy->push (:plushy best) argmap)
; :best-total-error (:total-error best)
; :best-errors (:errors best)
; :best-behaviors (:behaviors best)
; :genotypic-diversity (float (/ (count (distinct (map :plushy pop))) (count pop)))
; :behavioral-diversity (float (/ (count (distinct (map :behaviors pop))) (count pop)))
; :average-genome-length (float (/ (reduce + (map count (map :plushy pop))) (count pop)))
; :average-total-error (float (/ (reduce + (map :total-error pop)) (count pop)))})
; (println)))
(defn gp (defn gp
"Main GP loop." "Main GP loop."
[{:keys [population-size max-generations error-function instructions [{:keys [population-size max-generations error-function instructions
@ -37,6 +51,7 @@
(println {:starting-args argmap}) (println {:starting-args argmap})
(println) (println)
;; ;;
(print "got here 1")
(loop [generation 0 (loop [generation 0
population (repeatedly population (repeatedly
population-size population-size

View File

@ -8,13 +8,12 @@
[clojure.pprint :as pprint] [clojure.pprint :as pprint]
[propeller.tools.math :as math])) [propeller.tools.math :as math]))
;; Get path from text file
(def PSB2-path (slurp "PSB2_path.txt"))
; Random integer between -100 and 100 (from smallest) ; Random integer between -100 and 100 (from smallest)
(defn random-int [] (- (rand-int 201) 100)) (defn random-int [] (- (rand-int 201) 100))
(def train-and-test-data (psb2/fetch-examples PSB2-path "fuel-cost" 200 2000)) (defn train-and-test-data
[PSB2-path]
(psb2/fetch-examples PSB2-path "fuel-cost" 200 2000))
(def instructions (def instructions
(utils/not-lazy (utils/not-lazy
@ -35,7 +34,7 @@
(error-function argmap individual :train)) (error-function argmap individual :train))
([argmap individual subset] ([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) data (get (train-and-test-data (get argmap :PSB2-path)) subset)
inputs (map (fn [i] (get i :input1)) data) inputs (map (fn [i] (get i :input1)) data)
correct-outputs (map (fn [i] (get i :output1)) data) correct-outputs (map (fn [i] (get i :output1)) data)
outputs (map (fn [input] outputs (map (fn [input]

View File

@ -11,6 +11,16 @@
;; Get path from text file ;; Get path from text file
(def PSB2-path (slurp "PSB2_path.txt")) (def PSB2-path (slurp "PSB2_path.txt"))
(defn map-vals-input
"Returns all the input values of a map (specific helper method for substitution-cipher)"
[i]
(vals (select-keys i [:input1 :input2 :input3])))
(defn map-vals-output
"Returns the output values of a map (specific helper method for substitution-cipher)"
[i]
(vals (select-keys i [:output1])))
(def train-and-test-data (psb2/fetch-examples PSB2-path "substitution-cipher" 200 2000)) (def train-and-test-data (psb2/fetch-examples PSB2-path "substitution-cipher" 200 2000))
(def instructions (def instructions
@ -19,29 +29,28 @@
;;; stack-specific instructions ;;; stack-specific instructions
(get-stack-instructions #{:exec :integer :boolean :char :string :print}) (get-stack-instructions #{:exec :integer :boolean :char :string :print})
;;; input instructions ;;; input instructions
(list :in1) (list :in1 :in2 :in3)
;;; close ;;; close
(list 'close) (list 'close)
;;; ERCs (constants) ;;; ERCs (constants)
(list 0 "")))) (list 0 ""))))
; Error function takes from integer stack, calculates error based on absolute value of
; difference between output and correct output.
(defn error-function (defn error-function
([argmap individual] ([argmap individual]
(error-function argmap individual :train)) (error-function argmap individual :train))
([argmap individual subset] ([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) data (get train-and-test-data subset)
inputs (map (fn [i] (get i :input1)) data) inputs (map (fn [i] (map-vals-input i)) data)
correct-outputs (map (fn [i] (get i :output1)) data) correct-outputs (map (fn [i] (map-vals-output i)) data)
outputs (map (fn [input] outputs (map (fn [input]
(state/peek-stack (state/peek-stack
(interpreter/interpret-program (interpreter/interpret-program
program program
(assoc state/empty-state :input {:in1 (get input 0) (assoc state/empty-state :input {:in1 (nth input 0)
:in2 (get input 1) :in2 (nth input 1)
:in3 (get input 2)}) :in3 (nth input 2)})
(:step-limit argmap)) (:step-limit argmap))
:string)) :string))
inputs) inputs)

View File

@ -23,6 +23,37 @@
[seq1 seq2] [seq1 seq2]
(apply + (map #(if (= %1 %2) 0 1) seq1 seq2))) (apply + (map #(if (= %1 %2) 0 1) seq1 seq2)))
;; helper method for levenshtein-distance
(defn compute-next-row
"computes the next row using the prev-row current-element and the other seq"
[prev-row current-element other-seq pred]
(reduce
(fn [row [diagonal above other-element]]
(let [update-val (if (pred other-element current-element)
;; if the elements are deemed equivalent according to the predicate
;; pred, then no change has taken place to the string, so we are
;; going to set it the same value as diagonal (which is the previous edit-distance)
diagonal
;; in the case where the elements are not considered equivalent, then we are going
;; to figure out if its a substitution (then there is a change of 1 from the previous
;; edit distance) thus the value is diagonal + 1 or if its a deletion, then the value
;; is present in the columns, but not in the rows, the edit distance is the edit-distance
;; of last of row + 1 (since we will be using vectors, peek is more efficient)
;; or it could be a case of insertion, then the value is above+1, and we chose
;; the minimum of the three
(inc (min diagonal above (peek row))))]
(conj row update-val)))
;; we need to initialize the reduce function with the value of a row, since we are
;; constructing this row from the previous one, the row is a vector of 1 element which
;; consists of 1 + the first element in the previous row (edit distance between the prefix so far
;; and an empty string)
[(inc (first prev-row))]
;; for the reduction to go over, we need to provide it with three values, the diagonal
;; which is the same as prev-row because it starts from 0, the above, which is the next element
;; from the list and finally the element from the other sequence itself.
(map vector prev-row (next prev-row) other-seq)))
(defn levenshtein-distance (defn levenshtein-distance
"Levenshtein Distance - http://en.wikipedia.org/wiki/Levenshtein_distance "Levenshtein Distance - http://en.wikipedia.org/wiki/Levenshtein_distance
In Information Theory and Computer Science, the Levenshtein distance is a In Information Theory and Computer Science, the Levenshtein distance is a
@ -31,8 +62,8 @@
little mutability as possible. Still maintains the O(nm) guarantee." little mutability as possible. Still maintains the O(nm) guarantee."
[a b & {p :predicate :or {p =}}] [a b & {p :predicate :or {p =}}]
(cond (cond
(empty? a) (count b) (empty? (str a)) (count (str b)) ;; sometimes stack pushes numbers, force
(empty? b) (count a) (empty? (str b)) (count (str a)) ;; a and b to be strings
:else (peek :else (peek
(reduce (reduce
;; we use a simple reduction to convert the previous row into the ;; we use a simple reduction to convert the previous row into the
@ -40,11 +71,11 @@
;; element, the previous-row computed so far, and the predicate ;; element, the previous-row computed so far, and the predicate
;; to compare for equality ;; to compare for equality
(fn [prev-row current-element] (fn [prev-row current-element]
(compute-next-row prev-row current-element b p)) (compute-next-row prev-row current-element (str b) p))
;; we need to initialize the prev-row with the edit distance ;; we need to initialize the prev-row with the edit distance
;; between the various prefixes of b and the empty string ;; between the various prefixes of b and the empty string
(range (inc (count b))) (range (inc (count (str b))))
a)))) (str a)))))
(defn sequence-similarity (defn sequence-similarity
"Returns a number between 0 and 1, indicating how similar the sequences are "Returns a number between 0 and 1, indicating how similar the sequences are