;;;====================================================== ;;; Weather Prediction Fuzzy Expert System for London ;;; Final version - no CFs ;;; ;;; These rules were written for CFs so I don't know how well they translate to fuzzy ;;; ;;; ;;; FuzzyCLIPS Version 6.0 Example ;;; ;;; To execute, merely load, reset, and run. ;;;====================================================== ;;; only somebody from USA would predict london rainfall in inches (deftemplate rainfall ;;; how rainy is rainy 0 10 inches ;;; "universe of discourse " ( (dry (0.0 1) ( 0.25 0) ) (light ( 0.0 0 ) ( 0.25 1) (1.0 0 ) ) ( moderate ( 0.25 0 ) ( 1.0 1 ) ( 2.0 0 ) ) ( heavy ( 0.50 0 ) ( 1.5 1) (3 0) ) ( biblical (2.5 0) (8 1) ) ;;; everything up is biblical ) ) ;;; only somebody from USA would predict london rainfall in inches ;;; made to match fuzzy variables for todays rainfall (deftemplate predictrainfall ;;; how rainy is rainy 0 10 inches ;;; "universe of discourse " ( (dry (0.0 1) ( 0.25 0) ) (light ( 0.0 0 ) ( 0.25 1) (1.0 0 ) ) ( moderate ( 0.25 0 ) ( 1.0 1 ) ( 2.0 0 ) ) ( heavy ( 0.50 0 ) ( 1.5 1) (3 0) ) ( biblical (2.5 0) (8 1) ) ;;; everything up is biblical ) ) ;;; only somebody from USA would describe london temperature in F (deftemplate temp ;;; how cold is cold 0 100 degrees ( (cold (25 1) ( 40 0) ) (cool (30 0) ( 50 1) ( 70 0) ) (mild (50 0) (70 1) (90 0) ) (warm (60 0) (75 1) (90 0) ) (hot (70 0) (85 1) ) ;; everything up is hot ) ) (deftemplate clouds ;;; how cloudy is cloudy 0 100 percent ;;; percent of possible sunshine ( ( overcast (10 1) ( 30 0) ) ;;; anything below 10% sunshine is 1.0 overcast ( mostlycloudy (10 0) (30 1) (50 0) ) ( partlycloudy (20 0 ) (40 1) (60 0) ) ( partlysunny (30 0) (50 1) (70 0) ) ( mostlysunny (50 0) (70 1) (90 0) ) ( sunny (60 0) (80 1) ) ;; anything 80% and up is 1.0 sunny ) ) (defrule rainrainrule1b "if it is raining it is likely to continue raining - perhaps with some regression to the mean" (declare (cf 1.0)) ; ?f1 <- (rainfall heavy) => (bind ?rainfall (moment-defuzzify ?f1)) (bind ?rainclipped (get-fs-value (create-fuzzy-value rainfall heavy) ?rainfall )) (printout t "> debug in rainrainrule1b " t " how much rain? " ?rainfall t " membership in heavy : " ?rainclipped t " asserting moderate rainfall " t ) (assert (predictrainfall moderate) ) ) (defrule rainrainrule2b "if it is raining it is likely to continue raining" (declare (cf 1.0)) ; ?f1 <- (rainfall moderate) => (bind ?rainfall (moment-defuzzify ?f1)) (bind ?rainclipped (get-fs-value (create-fuzzy-value rainfall moderate) ?rainfall )) (printout t "> debug in rainrainrule2b " t " how much rain? " ?rainfall t " membership in moderate : " ?rainclipped t " asserting moderate rainfall " t ) (assert (predictrainfall moderate) ) ) (defrule drydryruleD "if it is dry it is likely to continue being dry" (declare (cf 1.0)) ; ?f1 <- (rainfall dry) => (bind ?rainfall (moment-defuzzify ?f1)) (bind ?rainclipped (get-fs-value (create-fuzzy-value rainfall dry) ?rainfall )) (printout t "> debug in drydryruleD " t " how much rain? " ?rainfall t " membership in dry : " ?rainclipped t " asserting dry " t ) (assert (predictrainfall dry) ) ) (defrule rainclearingruleD "if it is raining but not much, maybe it is clearing " (declare (cf 1.0)) ?f1 <- (rainfall light) => (bind ?rainfall (moment-defuzzify ?f1)) (bind ?rainclipped (get-fs-value (create-fuzzy-value rainfall light) ?rainfall )) (printout t "> debug in rainclearingruleD " t " how much rain? " ?rainfall t " membership in light : " ?rainclipped t " asserting dry " t ) (assert (predictrainfall dry) ) ) (defrule raincoldclearingruleD "if it is raining but not much, and it is cold out, maybe it is clearing " (declare (cf 1.0)) ?f1 <- (rainfall light) ?f2 <- (temp cold) => (bind ?rainfall (moment-defuzzify ?f1)) (bind ?temp (moment-defuzzify ?f2)) (bind ?rainclipped (get-fs-value (create-fuzzy-value rainfall light) ?rainfall )) (bind ?tempclipped (get-fs-value (create-fuzzy-value temp cold) ?temp )) (printout t "> debug in raincoldclearingruleD " t " how much rain? " ?rainfall t " membership in light : " ?rainclipped t " how much cold is it? " ?temp t " membership in cold: " ?tempclipped t " asserting dry " t ) (assert (predictrainfall dry) ) ) (defrule dryrainruleB "if it is dry but temperature is warm - maybe we'll get rain" (declare (cf 1.0)) ?f1 <- (rainfall dry) ?f2 <- (temp warm) => (bind ?rainfall (moment-defuzzify ?f1)) (bind ?temp (moment-defuzzify ?f2)) (bind ?rainclipped (get-fs-value (create-fuzzy-value rainfall dry) ?rainfall )) (bind ?tempclipped (get-fs-value (create-fuzzy-value temp warm) ?temp )) (printout t "> debug in dryrainruleB " t " how much rain? " ?rainfall t " membership in dry : " ?rainclipped t " how much cold is it? " ?temp t " membership in warm: " ?tempclipped t " asserting moderate rainfall " t ) (assert (predictrainfall moderate) ) ) (defrule dryovercastrainruleB "if it is dry but temperature is warm and sky is overcast - maybe we'll get rain" (declare (cf 1.0)) ?f1 <- (rainfall dry) ?f2 <- (temp warm) ?f3 <- (clouds overcast) => (bind ?rainfall (moment-defuzzify ?f1)) (bind ?temp (moment-defuzzify ?f2)) (bind ?clouds (moment-defuzzify ?f3)) (bind ?rainclipped (get-fs-value (create-fuzzy-value rainfall dry) ?rainfall )) (bind ?tempclipped (get-fs-value (create-fuzzy-value temp warm) ?temp )) (bind ?cloudsclipped (get-fs-value (create-fuzzy-value clouds overcast) ?temp )) (printout t "> debug in dryovercastrainruleB " t " how much rain? " ?rainfall t " membership in dry : " ?rainclipped t " how much cold is it? " ?temp t " membership in warm: " ?tempclipped t " how cloudy is it? " ?clouds t " membership in overcast: " ?cloudsclipped t " asserting moderate rainfall " t ) (assert (predictrainfall moderate) ) ) ;;; not currently used (defrule combineEvidenceRule " combine evidence reaching the same conclusion" (nevermatch aintthisclever) ?f1 <- (predictrainfall ?answ) ?f2 <- (predictrainfall ?answ) ;;; same answer (test (not (eq ?f1 ?f2))) => ;;(if (not (eq ?f1 f2)) ;;then (printout t "facts " ?f1 " " ?f2 t) (bind ?cf1 (get-cf ?f1)) (bind ?cf2 (get-cf ?f2)) (printout t "one minus " (- 1 ?cf1) t) (printout t "times " (* ?cf2 (- 1 ?cf1)) t) (printout t "revised " (+ ?cf1 (* ?cf2 (- 1 ?cf1))) t) (bind ?newcf (+ ?cf1 (* ?cf2 (- 1 ?cf1)))) (retract ?f1) (retract ?f2) (disable-rule-cf-calculation) ;; turn off rule cf calculations (assert (predictrainfall ?answ) cf ?newcf) (enable-rule-cf-calculation) ;; turn back on again ;;) ;; end if ) ;;; not currently used (defrule answerrule " specify the answer with certainty factor" (nevermatch aintthisclever) ?f <- (predictrainfall ?answ) => (printout t t " The prediction is " ?answ " with certainty factor " (get-cf ?f) t ) ) ; the next rule takes the result of the previous rules and produces a non-fuzzy ; result to identify the amount (between 0 and 1) of rain to predict ; and finally prints out the de-fuzzified result (defrule get-crisp-value-and-print-rslt (declare (salience 1)) ?pr <- (predictrainfall ?) => (bind ?f (moment-defuzzify ?pr)) (plot-fuzzy-value t "+" nil nil ?pr) ;;(bind ?f (maximum-defuzzify ?pr)) (printout t "So far, would Predict Rain of: " ?f " " (get-u-units ?pr) " (go with LAST prediction)" crlf) )