Merge pull request #1 from skwak22/master

Replace :refer :all with :require
This commit is contained in:
Maria-Cristiana Gîrjău 2020-06-25 12:12:48 -04:00 committed by GitHub
commit 64d43756b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 66 additions and 66 deletions

View File

@ -1,14 +1,14 @@
(ns propeller.core (ns propeller.core
(:gen-class) (:gen-class)
(:require [propeller.gp :refer :all] (:require [propeller.gp :as gp]
[propeller.push.core :refer :all] [propeller.push.core :as push]
(propeller.problems [simple-regression :refer :all] (propeller.problems [simple-regression :refer [regression-error-function]]
[string-classification :refer :all]))) [string-classification :refer [string-classification-error-function]])))
(defn -main (defn -main
"Runs propel-gp, giving it a map of arguments." "Runs propel-gp, giving it a map of arguments."
[& args] [& args]
(gp (update-in (merge {:instructions default-instructions (gp/gp (update-in (merge {:instructions push/default-instructions
:error-function regression-error-function :error-function regression-error-function
:max-generations 500 :max-generations 500
:population-size 500 :population-size 500

View File

@ -1,12 +1,12 @@
(ns propeller.genome (ns propeller.genome
(:require [propeller.push.core :refer :all])) (:require [propeller.push.core :as push]))
(defn plushy->push (defn plushy->push
"Returns the Push program expressed by the given plushy representation." "Returns the Push program expressed by the given plushy representation."
[plushy] [plushy]
(let [opener? #(and (vector? %) (= (first %) 'open))] ;; [open <n>] marks opens (let [opener? #(and (vector? %) (= (first %) 'open))] ;; [open <n>] marks opens
(loop [push () ;; iteratively build the Push program from the plushy (loop [push () ;; iteratively build the Push program from the plushy
plushy (mapcat #(if-let [n (get opens %)] [% ['open n]] [%]) plushy)] plushy (mapcat #(if-let [n (get push/opens %)] [% ['open n]] [%]) plushy)]
(if (empty? plushy) ;; maybe we're done? (if (empty? plushy) ;; maybe we're done?
(if (some opener? push) ;; done with plushy, but unclosed open (if (some opener? push) ;; done with plushy, but unclosed open
(recur push '(close)) ;; recur with one more close (recur push '(close)) ;; recur with one more close

View File

@ -1,7 +1,7 @@
(ns propeller.gp (ns propeller.gp
(:require [propeller.push.core :refer [instruction-table]] (:require [propeller.push.core :refer [instruction-table]]
(propeller [genome :refer :all] (propeller [genome :as genome]
[variation :refer :all]))) [variation :as variation])))
(defn report (defn report
"Reports information each generation." "Reports information each generation."
@ -11,7 +11,7 @@
(println " Report for Generation" generation) (println " Report for Generation" generation)
(println "-------------------------------------------------------") (println "-------------------------------------------------------")
(print "Best plushy: ") (prn (:plushy best)) (print "Best plushy: ") (prn (:plushy best))
(print "Best program: ") (prn (plushy->push (:plushy best))) (print "Best program: ") (prn (genome/plushy->push (:plushy best)))
(println "Best total error:" (:total-error best)) (println "Best total error:" (:total-error best))
(println "Best errors:" (:errors best)) (println "Best errors:" (:errors best))
(println "Best behaviors:" (:behaviors best)) (println "Best behaviors:" (:behaviors best))
@ -39,7 +39,7 @@
population (repeatedly population (repeatedly
population-size population-size
#(hash-map :plushy #(hash-map :plushy
(make-random-plushy instructions (genome/make-random-plushy instructions
max-initial-plushy-size)))] max-initial-plushy-size)))]
(let [evaluated-pop (sort-by :total-error (let [evaluated-pop (sort-by :total-error
(map (partial error-function argmap) (map (partial error-function argmap)
@ -51,7 +51,7 @@
:else (recur (inc generation) :else (recur (inc generation)
(if (:elitism argmap) (if (:elitism argmap)
(conj (repeatedly (dec population-size) (conj (repeatedly (dec population-size)
#(new-individual evaluated-pop argmap)) #(variation/new-individual evaluated-pop argmap))
(first evaluated-pop)) (first evaluated-pop))
(repeatedly population-size (repeatedly population-size
#(new-individual evaluated-pop argmap)))))))) #(variation/new-individual evaluated-pop argmap))))))))

View File

@ -1,7 +1,7 @@
(ns propeller.problems.string-classification (ns propeller.problems.string-classification
(:require [propeller.genome :refer :all] (:require [propeller.genome :as genome]
(propeller.push [state :refer :all] (propeller.push [state :as state]
[interpreter :refer :all]))) [interpreter :as interpreter])))
;; ============================================================================= ;; =============================================================================
;; String classification ;; String classification
@ -13,14 +13,14 @@
behavior is produced. The behavior is here defined as the final top item on behavior is produced. The behavior is here defined as the final top item on
the BOOLEAN stack." the BOOLEAN stack."
[argmap individual] [argmap individual]
(let [program (plushy->push (:plushy individual)) (let [program (genome/plushy->push (:plushy individual))
inputs ["GCG" "GACAG" "AGAAG" "CCCA" "GATTACA" "TAGG" "GACT"] inputs ["GCG" "GACAG" "AGAAG" "CCCA" "GATTACA" "TAGG" "GACT"]
correct-outputs [false false false false true true true] correct-outputs [false false false false true true true]
outputs (map (fn [input] outputs (map (fn [input]
(peek-stack (state/peek-stack
(interpret-program (interpreter/interpret-program
program program
(assoc empty-state :input {:in1 input}) (assoc state/empty-state :input {:in1 input})
(:step-limit argmap)) (:step-limit argmap))
:boolean)) :boolean))
inputs) inputs)

View File

@ -1,11 +1,11 @@
(ns propeller.push.interpreter (ns propeller.push.interpreter
(:require [propeller.push.core :refer [instruction-table]]) (:require [propeller.push.core :refer [instruction-table]]
(:require [propeller.push.state :refer :all])) [propeller.push.state :as state]))
(defn interpret-one-step (defn interpret-one-step
"Takes a Push state and executes the next instruction on the exec stack." "Takes a Push state and executes the next instruction on the exec stack."
[state] [state]
(let [popped-state (pop-stack state :exec) (let [popped-state (state/pop-stack state :exec)
first-instruction-raw (first (:exec state)) first-instruction-raw (first (:exec state))
first-instruction (if (keyword? first-instruction-raw) first-instruction (if (keyword? first-instruction-raw)
(first-instruction-raw @instruction-table) (first-instruction-raw @instruction-table)
@ -15,16 +15,16 @@
(first-instruction popped-state) (first-instruction popped-state)
; ;
(integer? first-instruction) (integer? first-instruction)
(push-to-stack popped-state :integer first-instruction) (state/push-to-stack popped-state :integer first-instruction)
; ;
(string? first-instruction) (string? first-instruction)
(push-to-stack popped-state :string first-instruction) (state/push-to-stack popped-state :string first-instruction)
; ;
(seq? first-instruction) (seq? first-instruction)
(update popped-state :exec #(concat %2 %1) first-instruction) (update popped-state :exec #(concat %2 %1) first-instruction)
; ;
(or (= first-instruction true) (= first-instruction false)) (or (= first-instruction true) (= first-instruction false))
(push-to-stack popped-state :boolean first-instruction) (state/push-to-stack popped-state :boolean first-instruction)
; ;
:else :else
(throw (Exception. (str "Unrecognized Push instruction in program: " (throw (Exception. (str "Unrecognized Push instruction in program: "

View File

@ -1,6 +1,6 @@
(ns propeller.push.utils (ns propeller.push.utils
(:require [propeller.push.core :refer [def-instruction]] (:require [propeller.push.core :refer [def-instruction]]
[propeller.push.state :refer :all])) [propeller.push.state :as state]))
;; A utility function for making Push instructions. Takes a state, a function ;; A utility function for making Push instructions. Takes a state, a function
;; to apply to the args, the stacks to take the args from, and the stack to ;; to apply to the args, the stacks to take the args from, and the stack to
@ -8,12 +8,12 @@
;; given stacks), and pushes the result onto the return-stack ;; given stacks), and pushes the result onto the return-stack
(defn make-instruction (defn make-instruction
[state function arg-stacks return-stack] [state function arg-stacks return-stack]
(let [popped-args (get-args-from-stacks state arg-stacks)] (let [popped-args (state/get-args-from-stacks state arg-stacks)]
(if (= popped-args :not-enough-args) (if (= popped-args :not-enough-args)
state state
(let [result (apply function (:args popped-args)) (let [result (apply function (:args popped-args))
new-state (:state popped-args)] new-state (:state popped-args)]
(push-to-stack new-state return-stack result))))) (state/push-to-stack new-state return-stack result)))))
;; Given a sequence of stacks, e.g. [:float :integer], and a sequence of suffix ;; Given a sequence of stacks, e.g. [:float :integer], and a sequence of suffix
;; function strings, e.g. [_+, _*, _=], automates the generation of all possible ;; function strings, e.g. [_+, _*, _=], automates the generation of all possible
@ -28,7 +28,7 @@
;; Pretty-prints a Push state, for logging or debugging purposes ;; Pretty-prints a Push state, for logging or debugging purposes
(defn print-state (defn print-state
[state] [state]
(doseq [stack stacks] (doseq [stack state/stacks]
(printf "%-15s = " stack) (printf "%-15s = " stack)
(prn (if (get state stack) (get state stack) '())) (prn (if (get state stack) (get state stack) '()))
(flush))) (flush)))

View File

@ -1,41 +1,41 @@
(ns propeller.session (ns propeller.session
(:require (propeller [gp :refer :all] (:require (propeller [gp :as gp]
[variation :refer :all] [variation :as variation]
[selection :refer :all] [selection :as selection]
[genome :refer :all]) [genome :as genome])
(propeller.push [interpreter :refer :all] (propeller.push [interpreter :as interpreter]
[core :refer :all] [core :as push]
[state :refer :all]) [state :as state])
(propeller.problems [simple-regression :refer :all] (propeller.problems [simple-regression :refer [regression-error-function]]
[string-classification :refer :all]))) [string-classification :refer [string-classification-error-function]])))
#_(interpret-program '(1 2 integer_+) empty-push-state 1000) #_(interpreter/interpret-program '(1 2 integer_+) empty-push-state 1000)
#_(interpret-program '(3 5 integer_= exec_if (1 "yes") (2 "no")) #_(interpreter/interpret-program '(3 5 integer_= exec_if (1 "yes") (2 "no"))
empty-push-state empty-push-state
1000) 1000)
#_(interpret-program '(in1 string_reverse 1 string_take "?" string_= exec_if #_(interpreter/interpret-program '(in1 string_reverse 1 string_take "?" string_= 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 empty-push-state :input {:in1 "Can you hear me?"}) (assoc empty-push-state :input {:in1 "Can you hear me?"})
1000) 1000)
#_(interpret-program '(in1 string_reverse 1 string_take "?" string_= exec_if #_(interpreter/interpret-program '(in1 string_reverse 1 string_take "?" string_= 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 empty-push-state :input {:in1 "I can hear you."}) (assoc empty-push-state :input {:in1 "I can hear you."})
1000) 1000)
#_(plushy->push (make-random-plushy default-instructions 20)) #_(genome/plushy->push (genome/make-random-plushy push/default-instructions 20))
#_(interpret-program (plushy->push (make-random-plushy default-instructions 20)) #_(interpreter/interpret-program (genome/plushy->push (genome/make-random-plushy push/default-instructions 20))
(assoc empty-push-state :input {:in1 "I can hear you."}) (assoc empty-push-state :input {:in1 "I can hear you."})
1000) 1000)
;; Target function: f(x) = x^3 + x + 3 ;; Target function: f(x) = x^3 + x + 3
#_(gp {:instructions default-instructions #_(gp/gp {:instructions push/default-instructions
:error-function regression-error-function :error-function regression-error-function
:max-generations 50 :max-generations 50
:population-size 200 :population-size 200
@ -44,7 +44,7 @@
:parent-selection :tournament :parent-selection :tournament
:tournament-size 5}) :tournament-size 5})
#_(gp {:instructions default-instructions #_(gp/gp {:instructions push/default-instructions
:error-function string-classification-error-function :error-function string-classification-error-function
:max-generations 50 :max-generations 50
:population-size 200 :population-size 200

View File

@ -1,5 +1,5 @@
(ns propeller.variation (ns propeller.variation
(:require [propeller.selection :refer :all])) (:require [propeller.selection :as selection]))
(defn crossover (defn crossover
"Crosses over two individuals using uniform crossover. Pads shorter one." "Crosses over two individuals using uniform crossover. Pads shorter one."
@ -40,12 +40,12 @@
(let [prob (rand)] (let [prob (rand)]
(cond (cond
(< prob (:crossover (:variation argmap))) (< prob (:crossover (:variation argmap)))
(crossover (:plushy (select-parent pop argmap)) (crossover (:plushy (selection/select-parent pop argmap))
(:plushy (select-parent pop argmap))) (:plushy (selection/select-parent pop argmap)))
(< prob (+ (:crossover (:variation argmap)) (< prob (+ (:crossover (:variation argmap))
(:umad (:variation argmap)) 2)) (:umad (:variation argmap)) 2))
(uniform-deletion (uniform-addition (:plushy (select-parent pop argmap)) (uniform-deletion (uniform-addition (:plushy (selection/select-parent pop argmap))
(:instructions argmap) (:instructions argmap)
(:umad-rate argmap)) (:umad-rate argmap))
(:umad-rate argmap)) (:umad-rate argmap))
:else (:plushy (select-parent pop argmap))))}) :else (:plushy (selection/select-parent pop argmap))))})