简介
我正在尝试以CLIPS语言实施规则-一个人是另一个人的祖先的关系。 约束条件是该规则只能从以下前提中得出:
(男性?x)(“ x是男性”)
(女性?y)(“ y是女性”)
(?x?y的母亲)(“ x是y的母亲”)
(?x?y的父亲)(“ x是y的父亲”)
我的尝试
我编写了以下代码:
(deftemplate father-of
(slot father)
(slot child)
)
(deftemplate mother-of
(slot mother)
(slot child)
)
(deftemplate male
(slot person)
)
(deftemplate female
(slot person)
)
(deffacts family
(father-of (father John) (child Mark))
(father-of (father John) (child Mary))
(mother-of (mother Alice) (child Mark))
(mother-of (mother Alice) (child Mary))
(male (person John))
(male (person Mark))
(female (person Alice))
(female (person Mary))
)
(defrule ancestor
(or
(mother-of (mother ?x) (child ?w))
(father-of (father ?x) (child ?w))
(and
(mother-of (mother ?x) (child ?y))
(or
(mother-of (mother ?y) (child ?w))
(father-of (father ?y) (child ?w))
)
)
(and
(father-of (father ?x) (child ?y))
(or
(mother-of (mother ?y) (child ?w))
(father-of (father ?y) (child ?w))
)
)
)
=>
(printout t ?x " is an ancestor of " ?w crlf)
(assert (ancestor ?x ?w))
)
问题要点
上面的代码编译并返回“ true”(换句话说,构造规则在逻辑上是正确的),并在此类事实列表的情况下输出预期结果。
但是,存在一个细微的问题:
此代码仅用于确定 的第一代和第二代祖先。
换句话说,它仅在某人是某人的父亲/母亲或某人的祖父/祖母的情况下有效,但不适用于检查某人是否为曾祖父/曾祖母。或某人的曾曾祖父/曾曾祖母等。
上面的代码无法解决此问题。
如何克服这个问题?
答案 0 :(得分:1)
CLIPS>
(deftemplate father-of
(slot father)
(slot child))
CLIPS>
(deftemplate mother-of
(slot mother)
(slot child))
CLIPS>
(deffacts family
(father-of (father Bob) (child Frank))
(mother-of (mother Linda) (child Frank))
(father-of (father Frank) (child John))
(mother-of (mother Susan) (child John))
(father-of (father John) (child Mark))
(mother-of (mother Alice) (child Mark)))
CLIPS>
(defrule ancestor
(or (mother-of (mother ?x) (child ?w))
(father-of (father ?x) (child ?w))
(and (ancestor ?x ?y)
(ancestor ?y ?w)))
(not (ancestor ?x ?w))
=>
(printout t ?x " is an ancestor of " ?w crlf)
(assert (ancestor ?x ?w)))
CLIPS> (reset)
CLIPS> (run)
Alice is an ancestor of Mark
John is an ancestor of Mark
Susan is an ancestor of John
Susan is an ancestor of Mark
Frank is an ancestor of John
Frank is an ancestor of Mark
Linda is an ancestor of Frank
Linda is an ancestor of Mark
Linda is an ancestor of John
Bob is an ancestor of Frank
Bob is an ancestor of Mark
Bob is an ancestor of John
CLIPS>