-
-
Save mmower/10ca3d5acaf7a8033916 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; Random Boolean Networks code with a little help from the good citizens | |
;; of #clojure (thank you!) | |
(defn or-oper [a b] (or a b)) | |
(defn and-oper [a b] (and a b)) | |
(defn xor-oper [a b] (or (and a (not b)) (and (not a) b))) | |
(defn random-oper | |
[] | |
(let [operators [or-oper and-oper xor-oper] | |
num-opers (count operators)] | |
(nth operators (rand-int num-opers)))) | |
(defn random-state | |
[] | |
(nth [true false] (rand-int 2))) | |
(defn potential-connections | |
[n size exc] | |
(let [pool (shuffle (filter #(not= exc %1) (range size)))] | |
(take n pool))) | |
(defn make-cell-func [size] | |
(fn [n] | |
{:idx n | |
:state (random-state) | |
:connections (potential-connections 2 size n) | |
:oper (random-oper)})) | |
(defn make-network [dim] | |
(let [size (* dim dim) | |
make-cell (make-cell-func size)] | |
(map make-cell (range size)))) | |
(defn evolve-cell | |
[cell network] | |
(let [connections (map #(nth network %1) (:connections cell)) | |
operands (map :state connections) | |
new-state (apply (:oper cell) operands)] | |
(assoc cell :state new-state))) | |
(defn evolve-network | |
[network] | |
(map #(evolve-cell %1 network) network)) | |
(def first-gen-net (make-network 8)) | |
(def second-gen-net (evolve-network first-gen-net)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment