Hello everyone!

I am having trouble with genetic programming. I am trying to construct
a random population of trees representing functions. The following is
how I do my random trees:

(defn makerandomtree-10
[pc maxdepth maxwidth fpx ppx]
(if-let [output
  (if (and (< (rand) fpx) (> maxdepth 0))
    (let [head (nth operations (rand-int (count operations)))
          children (doall (loop[function (list)
                        width maxwidth]
                      (if (pos? width)
                        (recur (concat function
                          (list (makerandomtree-10 pc (dec maxdepth)
                            (+ 2 (rand-int (- maxwidth 1))) fpx ppx)))
                          (dec width))
                        function)))]
        (concat (list head) children))
    (if (and (< (rand) ppx) (>= pc 0))
        (nth parameters (rand-int (count parameters)))
        (rand-int 100)))]
output
))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

And it works. The thing that doesn't work is mutation. I have already
done 10 versions of this, and all doesn't work good enough. The
following is the thing that works the best, but not really good
enough.

(defn mutate-5
"chooses a node changes that"
[function pc maxwidth pchange]
(if (< (rand) pchange)
    (let [output (makerandomtree-10 pc 3 maxwidth 0.5 0.6)]
        (if (seq? output) output (list output)))
    ;mutate the children of root
    ;declare an empty accumulator list, with root as its head
        (let [head (list (first function))
              children (loop [acc(list)
                              walker (next function)] (println "----------") 
(println
walker) (println "-----ACC-----") (println acc)
                        (if (not walker)
                            acc
                            (if (or (seq? (first function)) (contains? (set 
operations)
(first function)))
                                  (recur (concat acc (mutate-5 walker pc 
maxwidth pchange))
                                    (next walker))
                                  (if (< (rand) pchange)
                                    (if (some (set parameters) walker)
                                        (recur (concat acc (list (nth 
parameters (rand-int (count
parameters)))))
                                                (if (seq? walker) (next walker) 
nil))
                                        (recur (concat acc (list (rand-int 
100)))
                                                (if (seq? walker) (next walker) 
nil)))
                                    (recur acc (if (seq? walker) (next walker) 
nil))))
                         ))]
                (concat head (list children)))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


I am sorry that it is somewhat obfuscated. As you can see, I am a
newbie to clojure, and to functional programming in general.

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to