突变问题 - Clojure

发布于 2024-09-02 23:22:00 字数 2100 浏览 3 评论 0原文

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

随机函数的代码:

(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 的链接/书籍吗?)

having trouble changing an element of my function represented as a list.

code for random function:

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

I will provide also a mutation function, which is still not good enough. I need to be able to eval my statement, so the following is still insufficient.

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

(side note: do you have any links/books for learning clojure?)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

可爱暴击 2024-09-09 23:22:00

查看 clojure.walk 命名空间。很可能是这样的函数 clojure.walk/ postwalk-replace 就是您要寻找的。

Check out the clojure.walk namespace. It's likely that a function like clojure.walk/postwalk-replace is what you're looking for.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文