首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Clojure中指定大小的随机AST生成

使用Clojure中指定大小的随机AST生成
EN

Stack Overflow用户
提问于 2018-09-01 08:29:27
回答 2查看 218关注 0票数 2

我想要生成一个随机的抽象语法树

代码语言:javascript
复制
(def terminal-set #{'x 'R})
(def function-arity {'+ 2, '- 2, '* 2, '% 2})
(def function-set (into #{} (keys function-arity)))
(def terminal-vec (into [] terminal-set))
(def function-vec (into [] function-set))

;; protected division
(defn % [^Number x ^Number y]
  (if (zero? y)
    0
    (/ x y)))

具有指定大小的

代码语言:javascript
复制
(defn treesize [tree] (count (flatten tree)))

继Sean,2013年“元启发式的要点”一书中的算法,Lulu,第二版,可在https://cs.gmu.edu/~sean/book/metaheuristics/上找到

我们随机扩展无叶节点树的地平线,直到非叶节点的数目,加上剩余的点,大于或等于所需的大小。然后,我们用叶节点填充剩余的插槽:

例如

代码语言:javascript
复制
(+ (* x (+ x x)) x)

是七号的。

书中的算法使用了指针/参考Q,在那里非常方便。在我的例子中,我必须使用某种递归来构造树。问题是,我不能在使用递归的所有算法之间保持树的状态size,这会导致更大的树:

代码语言:javascript
复制
(defn ptc2-tree
  "Generate a random tree up to its `max-size`.
  Note: `max-size` is the number of nodes, not the same as its depth."
  [max-size]
  (if (> 2 max-size)
    (rand-nth terminal-vec)
    (let [fun   (rand-nth function-vec)
          arity (function-arity fun)]
      (cons fun (repeatedly arity #(ptc2-tree (- max-size arity 1)))))))

我也尝试使用atom来表示大小,但是仍然无法得到我想要的树大小,它要么太小,要么太大,这取决于实现。

除此之外,我还必须以某种方式将插入新节点/树的位置随机化。

我怎么写这个算法?

编辑:--对正确解决方案的最后触摸:

代码语言:javascript
复制
(defn sequentiate [v] 
  (map #(if (seqable? %) (sequentiate %) %) (seq v)))
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-09-01 11:14:14

下面或多或少是本文中PTC2算法的逐字翻译.它不是完全惯用的blocks代码;您可能希望将其拆分为函数/小块,这是您认为合理的。

代码语言:javascript
复制
(defn ptc2 [target-size]
  (if (= 1 target-size)
    (rand-nth terminal-vec)
    (let [f (rand-nth function-vec)
          arity (function-arity f)]
      ;; Generate a tree like [`+ nil nil] and iterate upon it
      (loop [ast (into [f] (repeat arity nil))
             ;; q will be something like ([1] [2]), being a list of paths to the
             ;; nil elements in the AST
             q (for [i (range arity)] [(inc i)])
             c 1]
        (if (< (+ c (count q)) target-size)
          ;; Replace one of the nils in the tree with a new node
          (let [a (rand-nth q)
                f (rand-nth function-vec)
                arity (function-arity f)]
            (recur (assoc-in ast a (into [f] (repeat arity nil)))
                   (into (remove #{a} q)
                         (for [i (range arity)] (conj a (inc i))))
                   (inc c)))
          ;; In the end, fill all remaining slots with terminals
          (reduce (fn [t path] (assoc-in t path (rand-nth terminal-vec)))
                  ast q))))))

您可以使用Clojure的loop构造(或者reduce来保持迭代的状态--在这个算法中,状态包括:

  • ast,它是一个嵌套向量,表示正在构建的公式,其中尚未完成的节点被标记为nil
  • q,它对应于伪代码中的Q,是指向ast中未完成节点的路径列表,
  • c,它是树中非叶节点的计数。

结果,您得到的结果如下:

代码语言:javascript
复制
(ptc2 10) ;; => [* [- R [% R [% x x]]] [- x R]]

我们使用向量(而不是列表)来创建AST,因为它允许我们使用assoc-in逐步构建树;如果需要,您可能希望自己将其转换为嵌套列表。

票数 2
EN

Stack Overflow用户

发布于 2018-09-01 21:36:47

巧合的是,我一直在用图佩洛森林图书馆编写AST操作代码。你可以参见这里的示例代码这里有一段2017年Clojure/Conj的视频

下面展示了我将如何解决这个问题。我试着使名字尽可能的不言自明,所以应该很容易理解算法是如何进行的。

基本要素:

代码语言:javascript
复制
(def op->arity {:add 2
                :sub 2
                :mul 2
                :div 2
                :pow 2})
(def op-set (set (keys op->arity)))
(defn choose-rand-op [] (rand-elem op-set))

(def arg-set #{:x :y})
(defn choose-rand-arg [] (rand-elem arg-set))

(defn num-hids [] (count (all-hids)))

帮助者职能:

代码语言:javascript
复制
(s/defn hid->empty-kids :- s/Int
  [hid :- HID]
  (let [op             (hid->attr hid :op)
        arity          (grab op op->arity)
        kid-slots-used (count (hid->kids hid))
        result         (- arity kid-slots-used)]
    (verify (= 2 arity))
    (verify (not (neg? result)))
    result))

(s/defn node-has-empty-slot? :- s/Bool
  [hid :- HID]
  (pos? (hid->empty-kids hid)))

(s/defn total-empty-kids :- s/Int
  []
  (reduce +
    (mapv hid->empty-kids (all-hids))))

(s/defn add-op-node :- HID
  [op :- s/Keyword]
  (add-node {:tag :op :op op} )) ; add node w no kids

(s/defn add-leaf-node :- tsk/KeyMap
  [parent-hid :- HID
   arg :- s/Keyword]
  (kids-append parent-hid [(add-leaf {:tag :arg :arg arg})]))

(s/defn need-more-op? :- s/Bool
  [tgt-size :- s/Int]
  (let [num-op            (num-hids)
        total-size-so-far (+ num-op (total-empty-kids))
        result            (< total-size-so-far tgt-size)]
    result))

主要算法:

代码语言:javascript
复制
(s/defn build-rand-ast :- tsk/Vec ; bush result
  [ast-size]
  (verify (<= 3 ast-size)) ; 1 op & 2 args minimum;  #todo refine this
  (with-debug-hid
    (with-forest (new-forest)
      (let [root-hid (add-op-node (choose-rand-op))] ; root of AST
        ; Fill in random op nodes into the tree
        (while (need-more-op? ast-size)
          (let [node-hid (rand-elem (all-hids))]
            (when (node-has-empty-slot? node-hid)
              (kids-append node-hid
                [(add-op-node (choose-rand-op))]))))
        ; Fill in random arg nodes in empty leaf slots
        (doseq [node-hid (all-hids)]
          (while (node-has-empty-slot? node-hid)
            (add-leaf-node node-hid (choose-rand-arg))))
        (hid->bush root-hid)))))

(defn bush->form [it]
  (let [head (xfirst it)
        tag  (grab :tag head)]
    (if (= :op tag)
      (list (kw->sym (grab :op head))
        (bush->form (xsecond it))
        (bush->form (xthird it)))
      (kw->sym (grab :arg head)))))

(dotest
  (let [tgt-size 13]
    (dotimes [i 5]
      (let [ast     (build-rand-ast tgt-size)
            res-str (pretty-str ast)]
        (nl)
        (println res-str)
        (println (pretty-str (bush->form ast))) ))))

它打印结果的两种等级的“灌木”格式,以及语言形式以及。以下是两个典型的结果:

代码语言:javascript
复制
[{:tag :op, :op :mul}
 [{:tag :op, :op :div}
  [{:tag :op, :op :pow}
   [{:tag :op, :op :sub}
    [{:tag :arg, :arg :y, :value nil}]
    [{:tag :arg, :arg :x, :value nil}]]
   [{:tag :op, :op :div}
    [{:tag :arg, :arg :y, :value nil}]
    [{:tag :arg, :arg :y, :value nil}]]]
  [{:tag :arg, :arg :y, :value nil}]]
 [{:tag :op, :op :pow}
  [{:tag :arg, :arg :x, :value nil}]
  [{:tag :arg, :arg :y, :value nil}]]]

(mul (div (pow (sub y x) (div y y)) y) (pow x y))


[{:tag :op, :op :div}
 [{:tag :op, :op :mul}
  [{:tag :op, :op :pow}
   [{:tag :arg, :arg :x, :value nil}]
   [{:tag :arg, :arg :y, :value nil}]]
  [{:tag :op, :op :add}
   [{:tag :op, :op :div}
    [{:tag :arg, :arg :x, :value nil}]
    [{:tag :arg, :arg :y, :value nil}]]
   [{:tag :arg, :arg :x, :value nil}]]]
 [{:tag :op, :op :mul}
  [{:tag :arg, :arg :x, :value nil}]
  [{:tag :arg, :arg :y, :value nil}]]]

(div (mul (pow x y) (add (div x y) x)) (mul x y))

为了简单起见,我使用了三个字母的操作码,而不是数学符号,但是它们可以很容易地被Clojure函数符号名代替,以便输入到eval

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52126382

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档