【发布时间】:2018-10-26 16:41:47
【问题描述】:
简介
我正在尝试用 CLIPS 语言实现一个规则——一个人是另一个人的祖先的关系。 约束是这样的规则只能从以下前提推导出来:
(male ?x) ("x 是男性")
(female ?y) ("y 是女性")
(mother-of ?x ?y) ("x is a mother of y")
(father-of ?x ?y) ("x is a Father of 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”(换句话说,构造的规则在逻辑上是正确的)并在这种事实列表的情况下输出预期的结果。
然而,有一个微妙的问题:
此代码仅用于确定第一代和第二代祖先。
换句话说,它仅适用于某人是某人的父亲/母亲或某人的祖父/祖母的情况,但不适用于检查某人是否是曾祖父/曾祖母或某人的曾曾祖父/曾曾祖母等。
上面的代码没有处理这个问题。
如何解决这个问题?
【问题讨论】:
标签: logic relationship clips expert-system ancestry