;;;====================================================== ;;; Loan Approval Fuzzy Expert System ;;; ;;; ;;; ;;; FuzzyCLIPS Version 6.0 Example ;;; ;;; To execute, merely load, reset, and run. ;;;====================================================== ;;; (deftemplate pay ;;; how much monthly pay is high, low, med 0 10000 dollars ;;; "universe of discourse " ( (low (1200 1) ( 1600 0) ) (medium (1400 0 ) ( 2500 1) (3600 0 ) ) ( high ( 3400 0 ) ( 5000 1 ) ( 6600 0 ) ) ( rich (6500 0) (8000 1) ) ;;; everything up is rich ) ) ;;; (deftemplate savings ;;; how much savings is high, low, med 0 1000000 dollars ;;; "universe of discourse " ( (low (2000 1) ( 10000 0) ) (medium (8000 0 ) ( 30000 1) (52000 0 ) ) ( high ( 45000 0 ) ( 150000 1 ) ( 250000 0 ) ) ( rich (200000 0) (800000 1) ) ;;; everything up is rich ) ) ;;; (deftemplate age ;;; how old is young, middle, old 0 100 years ;;; "universe of discourse " ( (young (18 1) ( 32 0) ) (middle (30 0 ) ( 45 1) (60 0 ) ) ( old ( 50 0 ) ( 65 1 ) ( 80 0 ) ) ( elderly (70 0) (85 1) ) ;;; everything up is elderly ) ) ;;; (deftemplate monthsloan ;;; how long is loan 0 400 months ;;; "universe of discourse " ( (short (3 1) ( 12 0) ) (medium (10 0 ) ( 30 1) (50 0 ) ) ( long ( 48 0 ) ( 150 1 ) ( 250 0 ) ) ( verylong (240 0) (300 1) ) ;;; everything up is verylong ) ) ;;; (deftemplate monthsemployer ;;; how long with employer 0 360 months ;;; "universe of discourse " ( (short (6 1) ( 12 0) ) (medium (10 0 ) ( 36 1) (60 0 ) ) ( long ( 55 0 ) ( 120 1 ) ( 250 0 ) ) ( verylong (240 0) (300 1) ) ;;; everything up is verylong ) ) ;;; (deftemplate approvedamount ;;; how much loan is approved for 0 400000 dollars ;;; "universe of discourse " ( (none (0 1) ( 5 0) ) (small (200 1) ( 500 0) ) (medium (400 0 ) ( 2000 1) (3500 0 ) ) ( large ( 3000 0 ) ( 30000 1 ) ( 50000 0 ) ) ( verylarge (40000 0) (100000 1) ) ;;; everything up is verylong ) ) (defrule highpaylongemployrule1 "if person has high pay and long employment, approve for high amount" (declare (cf 1.0)) ; ?f1 <- (pay high) ?f2 <- (monthsemployer long) => (bind ?pay (moment-defuzzify ?f1)) (bind ?payclipped (get-fs-value (create-fuzzy-value pay high) ?pay )) (bind ?employ (moment-defuzzify ?f2)) (bind ?employclipped (get-fs-value (create-fuzzy-value monthsemployer long) ?employ )) (printout t "> debug in highpaylongemployrule1 how much pay? " ?pay " membership in high : " ?payclipped " how long with employer? " ?employ " membership in long: " ?employclipped t) (assert (approvedamount large) ) ) (defrule longloanshortemployrule2 "if person has short employment and loan is for a long time, approve for nothing" (declare (cf 1.0)) ; ?f1 <- (monthsloan long) ?f2 <- (monthsemployer short) => (bind ?loan (moment-defuzzify ?f1)) (bind ?loanclipped (get-fs-value (create-fuzzy-value monthsloan long) ?loan )) (bind ?employ (moment-defuzzify ?f2)) (bind ?employclipped (get-fs-value (create-fuzzy-value monthsemployer short) ?employ )) (printout t "> debug in longloanshortemployrule2 how long loan? " ?loan " membership in long : " ?loanclipped " how long with employer? " ?employ " membership in short: " ?employclipped t) (assert (approvedamount none) ) ) (defrule shortloanmedemploypayhighrule3 "if person has medium employment and high pay and loan is for a short time, approve for medium" (declare (cf 1.0)) ; ?f1 <- (monthsloan short) ?f2 <- (monthsemployer medium) ?f3 <- (pay high) => (bind ?loan (moment-defuzzify ?f1)) (bind ?loanclipped (get-fs-value (create-fuzzy-value monthsloan short) ?loan )) (bind ?employ (moment-defuzzify ?f2)) (bind ?employclipped (get-fs-value (create-fuzzy-value monthsemployer medium) ?employ )) (bind ?pay (moment-defuzzify ?f3)) (bind ?payclipped (get-fs-value (create-fuzzy-value pay high) ?pay )) (printout t "> debug in shortloanmedemploypayhighrule3 how long loan? " ?loan " membership in short : " ?loanclipped " how long with employer? " ?employ " membership in medium: " ?employclipped " how much pay? " ?pay " membership in high: " ?payclipped t) (assert (approvedamount medium) ) ) (defrule lowpayrule4 "if person has low pay, approve for low amount" (declare (cf 1.0)) ; ?f1 <- (pay low) => (bind ?pay (moment-defuzzify ?f1)) (bind ?payclipped (get-fs-value (create-fuzzy-value pay low) ?pay )) (printout t "> debug in lowpayrule4 how much pay? " ?pay " membership in low : " ?payclipped t) (assert (approvedamount small) ) ) ; 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 loan to approve ; and finally prints out the de-fuzzified result (defrule get-crisp-value-and-print-rslt (declare (salience 1)) ?pr <- (approvedamount ?) => (bind ?f (moment-defuzzify ?pr)) (plot-fuzzy-value t "+" nil nil ?pr) (printout t "So far, would approve loan of: " ?f " " (get-u-units ?pr) " (wait until no more results and use LAST decision)" crlf) )