Clojure core.logic中的树搜索
一段时间以来,我一直对模型化问题感到困惑,我不得不承认,我不知道如何在Clojure core.logic中的树搜索,clojure,tree,clojure-core.logic,tree-search,Clojure,Tree,Clojure Core.logic,Tree Search,一段时间以来,我一直对模型化问题感到困惑,我不得不承认,我不知道如何在core.logic中“正确”解决它 很容易说明:给定一棵树(非循环单向图)和其中的一个顶点,如何使用core.logic定义一个目标,允许lvar成为给定顶点的任何可到达顶点 我从尽可能简单的事情开始: (defrel vertex x) (defrel child) (def facts (pldb/db [vertex 'a] [vertex 'b] [child 'a 'b] [vertex
core.logic
中“正确”解决它
很容易说明:给定一棵树(非循环单向图)和其中的一个顶点,如何使用core.logic
定义一个目标,允许lvar成为给定顶点的任何可到达顶点
我从尽可能简单的事情开始:
(defrel vertex x)
(defrel child)
(def facts
(pldb/db
[vertex 'a]
[vertex 'b] [child 'a 'b]
[vertex 'c] [child 'b 'c]
[vertex 'd] [child 'c 'd]))
考虑到这种配置,我的目标是定义一个允许lvar获取['a'b'c'd]中的值的目标。使用“1跳”可以直接获得可到达的顶点:
您可以为2个跃点添加变量,以此类推,但是。。。它能被推广吗?我想定义一个lvar列表,比如
(let [vars (repeatedly lvar)]
(map #(child (nth vars %) (nth vars (inc %)))
(-> vars count dec range))
(all
...))
但是几次尝试之后,我必须承认我不确定这是正确的方法
(pldb/db-rel vertex x)
(pldb/db-rel child parent child)
(def facts
(pldb/db
[vertex 'a]
[vertex 'b] [child 'a 'b]
[vertex 'c] [child 'b 'c]
[vertex 'd] [child 'c 'd]))
递归目标:
(defn reachable° [from to]
(l/fresh [v]
(l/conde
[(child from to)]
[(child from v) (reachable° v to)])))
测试
=> (pldb/with-db facts
(vec (l/run* [to] (reachable° 'a to))))
[b c d]
递归目标:
(defn reachable° [from to]
(l/fresh [v]
(l/conde
[(child from to)]
[(child from v) (reachable° v to)])))
测试
=> (pldb/with-db facts
(vec (l/run* [to] (reachable° 'a to))))
[b c d]
谢谢你的帮助
事实上我得到了
(defn order-relationo
"Abstract the general pattern of a order relation in a logic, relational way."
[relation x y]
(conde
[(relation x y)]
[(fresh [z]
(relation x z)
(order-relationo relation z y))]))
(def kino
"A goal where the two inputs x and y share kinship: x is an ancestor of y and
y a descandant of x."
(partial order-relationo child))
我问这个问题是因为我很久没有练习逻辑编程了,但现在我觉得它又来了^^
无论如何,非常感谢你的回答,这给了我一个额外的观点。谢谢你的帮助
事实上我得到了
(defn order-relationo
"Abstract the general pattern of a order relation in a logic, relational way."
[relation x y]
(conde
[(relation x y)]
[(fresh [z]
(relation x z)
(order-relationo relation z y))]))
(def kino
"A goal where the two inputs x and y share kinship: x is an ancestor of y and
y a descandant of x."
(partial order-relationo child))
我问这个问题是因为我很久没有练习逻辑编程了,但现在我觉得它又来了^^
无论如何,非常感谢你的回答,这给了我一个额外的观点