【问题标题】:Creating rules that aren't repetitive in CLIPS - Expert System在 CLIPS 中创建不重复的规则 - 专家系统
【发布时间】:2021-02-05 05:40:10
【问题描述】:

我对 CLIPS 非常陌生,正在尝试创建一个用于医学诊断的专家系统。当然,这只是为了练习。我的代码运行至今,但有一些事情我无法开始工作。

对于前 3 个症状(发烧、咳嗽、疲劳),如果任何 2 个回答是,则输出诊断。如果没有,它会继续询问患者不太常见的症状。现在我已经做到了,即使其中一个不常见的症状被回答为是,也有诊断。但是,我想这样做,如果其中至少 3 个回答是肯定的,它会输出诊断。问题是我不知道如何制定一个规则来检查这一点,而不必制作一堆单独的规则来检查每一种是和否组合的可能性,这将是乏味的。

这是我的代码的链接,因为放在这里很长。 https://codeshare.io/arBpq6

【问题讨论】:

    标签: rules clips expert-system


    【解决方案1】:

    定义一些定义模板,以便您可以将问题和答案表示为事实。

    (deftemplate question
       (slot tier (default 1))
       (slot answer)
       (slot query))
    
    (deftemplate answer
       (slot name)
       (slot tier)
       (slot response))
    

    您的系统有三层问题,现在可以表示为事实:

    (deffacts questions
       (question (tier 1)
                 (answer patient-contact)
                 (query "Has the patient been within 6 feet for a total of 15 minutes or more with someone who has confirmed COVID-19? (yes/no) "))
       (question (tier 2)
                 (answer patient-fever)
                 (query "Has the patient been having mild to high fever recently? (yes/no) "))
       (question (tier 2)
                 (answer patient-dry-cough)
                 (query "Has the patient been experiencing a dry cough? (yes/no) "))
       (question (tier 2)
                 (answer patient-fatigue)
                 (query "Has the patient been experiencing unusual levels of fatigue? (yes/no) "))
       (question (tier 3) 
                 (answer patient-senses)
                 (query "Has the patient been experiencing a loss of taste or smell? (yes/no) "))
       (question (tier 3)
                 (answer patient-nasal)
                 (query "Has the patient been experiencing nasal congestion? (yes/no) "))
       (question (tier 3)
                 (answer patient-conjunctivitis)
                 (query "Does the patient have conjunctivitis (red eyes)? (yes/no) "))
       (question (tier 3)
                 (answer patient-sorethroat)
                 (query "Does the patient have a sore throat? (yes/no) "))
       (question (tier 3)
                 (answer patient-headache)
                 (query "Has the patient been having frequent headaches? (yes/no) "))
       (question (tier 3)
                 (answer patient-pain)
                 (query "Has the patient been experiencing joint and muscle pains? (yes/no) "))
       (question (tier 3)
                 (answer patient-rashes)
                 (query "Does the patient have any kind of skin rashes? (yes/no) "))
       (question (tier 3)
                 (answer patient-nausea)
                 (query "Has the patient been experiencing nausea or been vomiting? (yes/no) "))
       (question (tier 3)
                 (answer patient-diarrhea)
                 (query "Has the patient been having diarrhea? (yes/no) "))
       (question (tier 3)
                 (answer patient-dizziness)
                 (query "Has the patient been experiencing dizziness or chills? (yes/no) "))
       (question (tier 3)
                 (answer patient-breath)
                 (query "Has the patient been experiencing shortness of breath? (yes/no) "))
       (question (tier 3)
                 (answer patient-appetite)
                 (query "Has the patient been experiencing a loss of appetite? (yes/no) "))
       (question (tier 3)
                 (answer patient-confusion)
                 (query "Has the patient been experiencing unusual levels of confusion? (yes/no) "))
       (question (tier 3)
                 (answer patient-pressure)
                 (query "Does the patient have persistent pain or pressure in their chest? (yes/no) "))
       (question (tier 3)
                 (answer patient-temperature)
                 (query "Does the patient have a high temperature (above 38 degrees Celsius)? (yes/no) ")))
    

    现在可以为提问创建通用规则。问题一经回答即被删除,并且在回答更高层级的所有问题之前不会提出问题。

    (defrule ask-question
       (not (covid ?))
       ?f <- (question (tier ?t)
                       (answer ?a)
                       (query ?q))
       (not (question (tier ?t2&:(< ?t2 ?t))))
       =>
       (retract ?f)
       (assert (answer (name ?a)
                       (tier ?t)
                       (response (patient-answer-p ?q)))))
    

    您现在可以为每一层添加规则以寻找特定数量的答案:

    (defrule immediate-test ""
       (declare (salience 10))
       (not (covid ?))
       (exists (answer (tier 1)
                       (response yes)))
       =>
       (assert (covid "The patient should be tested immediately as it is highly likely the virus could be transferred to them.")))
          
    (defrule two-common-symptoms ""
       (declare (salience 10))
       (not (covid ?))
       (exists (answer (tier 2)
                       (name ?n1)
                       (response yes))
               (answer (tier 2)
                       (name ?n2&~?n1)
                       (response yes))
               (answer (tier 2)
                       (name ?n3&~?n2&~?n1)
                       (response no)))
       =>
       (assert (covid "The patient shows 2 of the common symptoms, and it is recommended that they get tested.")))
    
    (defrule all-common-symptoms ""
       (declare (salience 10))
       (not (covid ?))
       (exists (answer (tier 2)
                       (name ?n1)
                       (response yes))
               (answer (tier 2)
                       (name ?n2&~?n1)
                       (response yes))
               (answer (tier 2)
                       (name ?n3&~?n2&~?n1)
                       (response yes)))
       =>
       (assert (covid "The patient shows all 3 of the common symptoms, and it is highly recommended that they get tested.")))
    
    (defrule three-uncommon-symptoms ""
       (declare (salience 10))
       (not (covid ?))
       (exists (answer (tier 3)
                       (name ?n1)
                       (response yes))
               (answer (tier 3)
                       (name ?n2&~?n1)
                       (response yes))
               (answer (tier 3)
                       (name ?n3&~?n2&~?n1)
                       (response yes)))
       =>
       (assert (covid "The patient is showing at least 3 of the uncommon symptoms, and therefore it is recommended that they be tested.")))
    

    【讨论】:

    • 谢谢,感谢您为我提供的详细信息。这很有意义,也很容易理解。
    猜你喜欢
    • 2018-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 2011-03-15
    • 1970-01-01
    相关资源
    最近更新 更多