问题描述
我对CLIPS十分陌生,并试图创建一个用于医学诊断的专家系统。当然,这只是为了练习。我的代码运行至今,但是,有些事情我无法工作。
对于前三个症状(发烧,咳嗽,疲劳),如果回答两个,则输出诊断。如果不是这样,它将继续向患者询问不太常见的症状。现在,我做到了,这样,即使即使是一种不常见的症状都得到了回答,也可以进行诊断。但是,我想这样做,以便如果至少回答3个,则输出诊断信息。问题是我不知道如何制定规则来对此进行检查,而不必制定一堆单独的规则来检查是否存在的每种可能性以及没有组合的繁琐工作。
这里是指向我的代码的链接,因为放置在这里很长。 https://codeshare.io/arBpq6
解决方法
定义一些预设,以便您可以将问题和答案表示为事实。
(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.")))