Replace :bmx-gap-probability with :bmx-gene-length-limit

This commit is contained in:
Lee Spector 2023-12-26 15:32:06 -05:00
parent c3a748e5cc
commit 77b160e454
5 changed files with 38 additions and 14 deletions

View File

@ -7,16 +7,14 @@ They hold the genetic material for an `individual`. In the initial population, w
(defn make-random-plushy (defn make-random-plushy
"Creates and returns a new plushy made of random instructions." "Creates and returns a new plushy made of random instructions."
[{:keys [instructions max-initial-plushy-size bmx? bmx-gap-probability]}] [{:keys [instructions max-initial-plushy-size bmx? bmx-gene-length-limit]}]
(if bmx? (let [plushy (repeatedly (rand-int max-initial-plushy-size)
(repeatedly #(utils/random-instruction instructions))]
(rand-int max-initial-plushy-size) (if bmx?
#(if (< (rand) bmx-gap-probability) (-> plushy
:gap (utils/fill-empty-genes instructions)
(utils/random-instruction instructions))) (utils/enforce-gene-length-limit bmx-gene-length-limit))
(repeatedly plushy)))
(rand-int max-initial-plushy-size)
#(utils/random-instruction instructions))))
(defn plushy->push-internal (defn plushy->push-internal
[plushy argmap] [plushy argmap]

View File

@ -313,7 +313,7 @@
:single-thread-mode false :single-thread-mode false
:bmx? true :bmx? true
:bmx-exchange-rate 0.5 :bmx-exchange-rate 0.5
:bmx-gap-probability 0.1 :bmx-gene-length-limit 10
:bmx-gap-change-probability 0.01 :bmx-gap-change-probability 0.01
:bmx-complementary? true} :bmx-complementary? true}
(apply hash-map (map #(if (string? %) (read-string %) %) args))))) (apply hash-map (map #(if (string? %) (read-string %) %) args)))))

View File

@ -57,7 +57,9 @@
#_(genome/plushy->push #_(genome/plushy->push
(genome/make-random-plushy (genome/make-random-plushy
{:instructions (instructions/get-stack-instructions #{:float :integer :exec :boolean}) {:instructions (instructions/get-stack-instructions #{:float :integer :exec :boolean})
:bmx-gap-probability 20})) :max-initial-plushy-size 100
:bmx? true
:bmx-gene-length-limit 10}))
;; One way of running a genetic programming problem defined in the project ;; One way of running a genetic programming problem defined in the project
;; is to require the problem's namespace and then call `gp/gp` using the ;; is to require the problem's namespace and then call `gp/gp` using the

View File

@ -209,3 +209,25 @@
(random-instruction instructions) (random-instruction instructions)
gene)) gene))
(extract-genes plushy))))) (extract-genes plushy)))))
(defn break-up
"A utility function for bmx-related genetic operators. Returns the provided
:gap-free plushy with gaps randomly inserted to ensure that no gene is longer
than the provided limit."
[gene limit]
(if (> (count gene) limit)
(let [i (inc (rand-int (dec (count gene))))]
(concat (break-up (take i gene) limit)
[:gap]
(break-up (drop i gene) limit)))
gene))
(defn enforce-gene-length-limit
"A utility function for bmx-related genetic operators. Returns the provided
plushy with any over-length genes broken into non-empty pieces, recursively
until all genes obey the limit."
[plushy limit]
(flatten (interpose :gap
(mapv (fn [gene]
(break-up gene limit))
(extract-genes plushy)))))

View File

@ -244,7 +244,8 @@ The function `new-individual` returns a new individual produced by selection and
(-> (bmx plushy1 plushy2 bmx-exchange-rate max-distance argmap) (-> (bmx plushy1 plushy2 bmx-exchange-rate max-distance argmap)
(uniform-gap-addition gap-change-prob) (uniform-gap-addition gap-change-prob)
(uniform-gap-deletion gap-change-prob) (uniform-gap-deletion gap-change-prob)
(utils/fill-empty-genes (:instructions argmap)))) (utils/fill-empty-genes (:instructions argmap))
(utils/enforce-gene-length-limit (:bmx-gene-length-limit argmap))))
; ;
:umad ;; uniform mutation by addition and deletion, see uniform-deletion for the :umad ;; uniform mutation by addition and deletion, see uniform-deletion for the
;; adjustment that makes this size neutral on average ;; adjustment that makes this size neutral on average
@ -276,7 +277,8 @@ The function `new-individual` returns a new individual produced by selection and
(uniform-gap-deletion gap-change-prob) (uniform-gap-deletion gap-change-prob)
(uniform-addition (:instructions argmap) umad-rate) (uniform-addition (:instructions argmap) umad-rate)
(uniform-deletion umad-rate) (uniform-deletion umad-rate)
(utils/fill-empty-genes (:instructions argmap)))) (utils/fill-empty-genes (:instructions argmap))
(utils/enforce-gene-length-limit (:bmx-gene-length-limit argmap))))
; ;
:rumad ;; responsive UMAD, uses a deletion rate computed from the actual :rumad ;; responsive UMAD, uses a deletion rate computed from the actual
;; number of additions made ;; number of additions made