【问题标题】:how to combine these two rules with CLIPS?如何将这两个规则与 CLIPS 结合起来?
【发布时间】:2013-04-16 01:29:02
【问题描述】:

我在 CLIPS 中有两条规则,如果它们都是真的,我想将它们结合起来……但不知道该怎么做。我有一个名为grant-eligible....的属性。当我这样做时,似乎我的代码进入了无限循环......

所以这是我的规则:

    (defrule complete "rule for app completeness"
  ?f <- (application (transcript-received Yes) (app-complete FALSE)
    (gpa
                ?v_gpa&:(
                    > ?v_gpa 0)))

  =>
  (modify ?f (app-complete TRUE)))


    (defrule denied "rule for admission - DENIED"
  ?f <- (application (app-complete TRUE) (app-decision FALSE)
    (gpa
                ?v_gpa&:(
                    < ?v_gpa 3.0))

    (ssat
                ?v_ssat&:(
                    >= ?v_ssat 0.0))



        )


  =>
  (modify ?f (app-decision DENIED))

  )

(defrule accepted "rule for admission - ACCEPTED"
  ?f <- (application (app-complete TRUE) (app-decision FALSE)
    (gpa
                ?v_gpa&:(
                    >= ?v_gpa 3.5))

    (ssat
                ?v_ssat&:(
                    >= ?v_ssat 1500))


        )

  =>
  (modify ?f (app-decision ACCEPTED))

  )

这是我现在正在尝试实现的那些

(defrule female-finaid "rule for finaid applications for female students"
  ?f <- (application (app-decision ACCEPTED) 
    (gender F) (grade-entry Freshman) (country USA)
    (grant-eligible TRUE)
    (grant ?v_grant)
    )

  =>
  (modify ?f
            (grant (+ ?v_grant 5000))
            (grant-eligible TRUE)
        )
    )

    (defrule great-students-finaid "rule for finaid applications for female students"
  ?f <- (application (app-decision ACCEPTED) 
    (country USA)
    (grant-eligible TRUE)
    (grant ?v_grant)
    (gpa
                ?v_gpa&:(
                    >= ?v_gpa 4.0))
    )

  =>
  (modify ?f
            (grant (+ ?v_grant 4500))
            (grant-eligible FALSE)
        )
    )

如果这两个规则都成立,那么授予的资助应该是 9500,或者可能是 5000,或者可能是 4500...有什么想法吗?

解决方案:(其中ff-grant-eligible 和es-grant-eligible 是我的控制事实...它们代表 ff=female finaid 和 es=excellent student)

    (defrule female-finaid "rule for finaid applications for female students"
  ?f <- (application (app-decision ACCEPTED) (ff-grant-eligible TRUE)
    (gender F) (grade-entry Freshman) (country USA)

    (grant ?v_grant)
    )

  =>
  (modify ?f
            (grant (+ ?v_grant 5000))
            (ff-grant-eligible FALSE)
        )
    )

    (defrule great-students-finaid "rule for finaid applications for female students"
  ?f <- (application (app-decision ACCEPTED) (es-grant-eligible TRUE)
    (country USA)

    (grant ?v_grant)
    (gpa
                ?v_gpa&:(
                    >= ?v_gpa 4.0))
    )

  =>
  (modify ?f
            (grant (+ ?v_grant 4500))
            (es-grant-eligible FALSE)
        )
    )

【问题讨论】:

    标签: expert-system clips knowledge-capture


    【解决方案1】:

    您可以通过多种方式控制程序的执行(例如,控制事实、显着性、模块)。该答案将在应用程序处理的各个阶段使用控制事实(具有显着性)。我还将假设您有一个与每个应用程序关联的唯一 id 插槽。

    考虑以下事实和规则:

    (deffacts application-stages "ordered sequence of stages for an application"
      (stages app-received app-accept-reject app-evaluate-grants
              app-apply-grants app-complete))
    
    (defrule go-to-next-stage "Advances to the next application stage"
      ?stage <- (app-stage ?id ?current-stage)
      (stages $? ?current-stage ?next-stage $?)
      =>
      (retract ?stage)
      (assert (app-stage ?id ?next-stage))
      (printout t "Application " ?id " moved from stage " ?current-stage
                  " to " ?next-stage "." crlf))
    

    application-stages deffact 定义应用程序的阶段顺序,go-to-next-stage 规则推进应用程序阶段。由于该规则的显着性低于默认值(0),因此只有在当前阶段没有其他规则对应的情况下才会执行该规则。因此,如果您的程序中没有其他规则,您将得到以下结果:

    CLIPS> (reset)
    CLIPS> (assert (app-stage app-001 app-received))
    <Fact-2>
    CLIPS> (run)
    Application app-001 moved from stage app-received to app-accept-reject.
    Application app-001 moved from stage app-accept-reject to app-evaluate-grants.
    Application app-001 moved from stage app-evaluate-grants to app-apply-grants.
    Application app-001 moved from stage app-apply-grants to app-complete.
    CLIPS> 
    

    但是,如果您有任何与特定阶段相关的规则,它们将首先被执行。因此,您可以像这样向app-evaluate-grants 阶段添加规则:

    (defrule female-finaid "rule for finaid applications for female students"
      (app-stage ?id app-evaluate-grants)
      (application (app-decision ACCEPTED) (id ?id)
        (gender F) (grade-entry Freshman) (country USA)
      =>
      (assert (grant ?id female 5000)))
    

    您同样可以添加great-student-finaid 规则。然后,app-apply-grants 阶段有一个规则:

    (defrule apply-grant "Adds the amount of a grant to an application"
      (app-stage ?id app-apply-grants)
      ?grant <- (grant ?id ? ?amount)
      ?app <- (application (id ?id) (grant ?v_grant))
      =>
      (retract ?grant)
      (modify (?app (grant (+ ?v_grant ?amount))))
    

    以这种方式建模的好处之一是您不必在应用程序的数据中包含控制事实(例如,grant-eligible)。也就是说,您的控制逻辑与数据模型是分开的。请注意,您可以通过使用 CLIPS 模块(通过defmodule)实现与我在这里所做的相同的效果,这通常是可取的,但它需要更长的答案(而且这个答案已经相当长了)。

    【讨论】:

    • 感谢@bogatron 的彻底回复……我有点不清楚。过去几周一直在学习 CLIPS。所以我不明白这是什么意思(assert (grant ?id female 5000)))...我也没有apply-grants stage。我使用grant-eligible 属性来阻止规则一遍又一遍地触发,但显然这不起作用。
    • (assert (grant ?id female 5000))) 指定将应用于应用程序的授权。我没有意识到您使用grant-eligible 作为控制事实。看看我更新的答案(删除了grant-eligible),看看这是否有意义。
    • 感谢您的回答。我已经编辑了我的原始问题,以提供我已经拥有的应用程序完成和应用程序决策规则。所以考虑到学生被接受(申请完成后)......那么我想看看我是否可以给他们助学金。
    • 我看到了更新,但它不会真正改变我提供的答案。同样,我会避免将grant-eligible 放入应用程序,因为这是一个控制事实,而不是应用程序数据的一部分。我提供的解决方案不需要它,因为 app-stage 事实处理过程的顺序方面。
    • 谢谢! @bogatron,我读了 50 次(字面意思),但我想根据我对 CLIPS 的经验,它并没有深入我的脑海。哈哈。我最终做的是添加2个单独的控制事实,每个规则1个......然后在规则的末尾我将事实设置为FALSE,这样它就不会陷入无限循环。 ...非常感谢您的帮助...我什至不知道我在使用控制事实,所以您的指导很有帮助。
    猜你喜欢
    • 1970-01-01
    • 2017-10-23
    • 2015-08-11
    • 2021-09-28
    • 2021-09-05
    • 2016-08-13
    • 1970-01-01
    • 1970-01-01
    • 2013-05-11
    相关资源
    最近更新 更多