【问题标题】:CLIPS(recursion) - family relations: how to implement correctly the relation of being an ancestor?CLIPS(recursion) - 亲子关系:如何正确实现祖宗关系?
【发布时间】: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


    【解决方案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> 
    

    【讨论】:

    • 感谢您的帮助,现在我了解了递归在 CLIPS 语言中的工作原理!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多