簡體   English   中英

在 CLIPS 中創建不重復的規則 - 專家系統

[英]Creating rules that aren't repetitive in CLIPS - Expert System

我對 CLIPS 非常陌生,並試圖創建一個用於醫學診斷的專家系統。 當然這只是為了練習。 我的代碼運行到目前為止,但是,有一些事情我無法工作。

對於前 3 個症狀(發燒、咳嗽、疲勞),如果任何 2 個回答是,則輸出診斷。 如果沒有,它會繼續詢問患者不太常見的症狀。 現在我已經做到了,即使其中一個不常見的症狀被回答是,也有一個診斷。 但是,我想這樣做,如果其中至少有 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.")))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM