【问题标题】:Mutation Problem - Clojure突变问题 - Clojure
【发布时间】:2010-05-23 01:46:07
【问题描述】:

在更改表示为列表的函数元素时遇到问题。

随机函数代码:

(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
))

我还会提供一个突变功能,但还不够好。我需要能够评估我的陈述,所以以下内容仍然不够。

(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)))))

(旁注:你有任何学习 clojure 的链接/书籍吗?)

【问题讨论】:

标签: list clojure functional-programming genetic


【解决方案1】:

查看clojure.walk 命名空间。您可能正在寻找像 clojure.walk/postwalk-replace 这样的函数。

【讨论】:

    猜你喜欢
    • 2019-05-29
    • 1970-01-01
    • 2011-03-16
    • 1970-01-01
    • 1970-01-01
    • 2020-10-09
    • 2020-12-02
    • 2017-06-27
    • 2016-12-22
    相关资源
    最近更新 更多