Separate plushy pre-processing from plushy to push translation

This commit is contained in:
Lee Spector 2023-11-08 19:09:43 -05:00
parent 25e8efe255
commit 3ccd5542ea

View File

@ -12,20 +12,9 @@ They hold the genetic material for an `individual`. In the initial population, w
(rand-int max-initial-plushy-size) (rand-int max-initial-plushy-size)
#(utils/random-instruction instructions))) #(utils/random-instruction instructions)))
(defn plushy->push (defn plushy->push-internal
"Returns the Push program expressed by the given plushy representation. [plushy argmap]
(let [opener? #(and (vector? %) (= (first %) 'open))] ;; [open <n>] marks opens
The function takes in a plushy representation as input and converts it into a Push program by iteratively processing
the plushy elements and adding instructions to the push program.
It also handles the case where there are open instructions that need to be closed before the end of the program.
"
([plushy] (plushy->push plushy {}))
([plushy argmap]
(let [plushy (if (or (> (or (:ah-umad (:variation argmap)) 0) 0) ;; must strip :vary and :protect
(> (or (:autoconstructive-crossover (:variation argmap)) 0) 0)) ;; must strip :gene
(filter (complement #{:vary :protect :gene}) plushy)
plushy)
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 #(let [n (get instructions/opens %)] plushy (mapcat #(let [n (get instructions/opens %)]
(if (and n (if (and n
@ -50,4 +39,17 @@ They hold the genetic material for an `individual`. In the initial population, w
(concat pre-open [post-open ['open (dec num-open)]]))) (concat pre-open [post-open ['open (dec num-open)]])))
(rest plushy)) (rest plushy))
(recur push (rest plushy))) ;; unmatched close, ignore (recur push (rest plushy))) ;; unmatched close, ignore
(recur (concat push [i]) (rest plushy))))))))) ;; anything else (recur (concat push [i]) (rest plushy)))))))) ;; anything else
(defn plushy->push
"Returns the Push program expressed by the given plushy representation."
;; use an empty argmap if none provided
([plushy]
(plushy->push plushy {}))
;; call plushy->push-internal with possibly-preprocessed plushy
([plushy argmap]
(plushy->push-internal (if (or (> (or (:ah-umad (:variation argmap)) 0) 0) ;; must strip :vary and :protect
(> (or (:autoconstructive-crossover (:variation argmap)) 0) 0)) ;; must strip :gene
(filter (complement #{:vary :protect :gene}) plushy)
plushy)
argmap)))