Skip to content

Commit

Permalink
Move opens into instructions/parentheses; implement :auto-close to se…
Browse files Browse the repository at this point in the history
…t probability of adding a close from opens produced by instructions
  • Loading branch information
lspector committed Jan 1, 2024
1 parent 5eb34a8 commit a10addc
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/propeller/genome.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"The genetic material in Propeller. A `plushy` is a list of Push instructions that represent a Push program.
They hold the genetic material for an `individual`. In the initial population, we create random plushys."
{:doc/format :markdown}
(:require [propeller.push.instructions :as instructions]
(:require [propeller.push.instructions.parentheses :as parentheses]
[propeller.utils :as utils]))

(defn make-random-plushy
Expand All @@ -21,7 +21,7 @@ They hold the genetic material for an `individual`. In the initial population, w
[plushy argmap]
(let [opener? #(and (vector? %) (= (first %) 'open))] ;; [open <n>] marks opens
(loop [push () ;; iteratively build the Push program from the plushy
plushy (mapcat #(let [n (get instructions/opens %)]
plushy (mapcat #(let [n (get parentheses/opens %)]
(if (and n
(> n 0))
[% ['open n]]
Expand Down
33 changes: 33 additions & 0 deletions src/propeller/push/instructions/parentheses.cljc
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
(ns propeller.push.instructions.parentheses)

;; Number of blocks opened by instructions (default = 0)
(def opens
"Number of blocks opened by instructions. The default is 0."
{:exec_dup 1
:exec_dup_times 1
:exec_dup_items 0 ; explicitly set to 0 to make it clear that this is intended
:exec_eq 0 ; explicitly set to 0 to make it clear that this is intended
:exec_pop 1
:exec_rot 3
:exec_shove 1
:exec_swap 2
:exec_yank 0 ; explicitly set to 0 to make it clear that this is intended
:exec_yank_dup 0 ; explicitly set to 0 to make it clear that this is intended
:exec_deep_dup 0 ; explicitly set to 0 to make it clear that this is intended
:exec_print 1
:exec_if 2
:exec_when 1
:exec_while 1
:exec_do_while 1
:exec_do_range 1
:exec_do_count 1
:exec_do_times 1
:exec_k 2
:exec_s 3
:exec_y 1
:string_iterate 1
:vector_boolean_iterate 1
:vector_string_iterate 1
:vector_integer_iterate 1
:vector_float_iterate 1
})
23 changes: 17 additions & 6 deletions src/propeller/utils.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
(:require [clojure.zip :as zip]
[clojure.repl :as repl]
[propeller.tools.metrics :as metrics]
[propeller.tools.math :as math]))
[propeller.tools.math :as math]
[propeller.push.instructions.parentheses :as parentheses]))

(defn filter-by-index
"filters a collection by a list of indices"
Expand Down Expand Up @@ -50,11 +51,21 @@
"Returns a random instruction from a supplied pool of instructions, evaluating
ERC-producing functions to a constant literal."
[instructions argmap]
(let [instruction (rand-nth instructions)]
(if (fn? instruction)
(instruction)
instruction)))

(if (:auto-close argmap)
(let [instructions (remove #(= % 'close) instructions)
p (/ (apply + (filter identity
(map #(get parentheses/opens %) instructions)))
(count instructions))]
(if (< (rand) p)
'close
(let [instruction (rand-nth instructions)]
(if (fn? instruction)
(instruction)
instruction))))
(let [instruction (rand-nth instructions)]
(if (fn? instruction)
(instruction)
instruction))))

(defn count-points
"Returns the number of points in tree, where each atom and each pair of parentheses
Expand Down

0 comments on commit a10addc

Please sign in to comment.