Skip to content

Instantly share code, notes, and snippets.

@minimal
Created July 8, 2009 23:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save minimal/143302 to your computer and use it in GitHub Desktop.
Save minimal/143302 to your computer and use it in GitHub Desktop.
; A Field Guide to
; Genetic Programming
; http://www.lulu.com/items/volume_63/2167000/2167025/2/print/book.pdf
(defn f2-1
"Figure 2.1 max(x+x, x+3*y)"
[x y]
(max (+ x x)
(+ x
(* 3 y))))
(defn gen-rand-expr
"My impl of algorithm 2.1 - Full and grow methods"
[func-set term-set max-depth method]
(if (or (= max-depth 0)
(and (= method :grow)
(< (rand)
(/ (count term-set)
(+ (count term-set) (count func-set))))))
(nth term-set (rand-int (count term-set))) ; choose a terminal
(let [func (nth func-set (rand-int (count func-set)))]
(cons func (for [i [1 2]]
(gen-rand-expr func-set term-set (dec max-depth) method))))))
(gen-rand-expr [+ - /] [1 2 3] 2 :grow)
; (#<core$_PLUS___3332 clojure.core$_PLUS___3332@1f1f38e>
; (#<core$___3358 clojure.core$___3358@1f5eb7f> 3 1) 1)
; (+ (- 3 1) 1)
; 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment