find the average using jess or clips
这是用于查找平均值的剪辑代码……但它不起作用……有人知道如何解决吗?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | (deftemplate fact (slot name) (slot value)) (deftemplate avg (slot result)) (deffacts data (fact (name data-1) (value 3)) (fact (name data-2) (value 1)) (fact (name data-3) (value 2)) (fact (name data-4) (value 2)) (fact (name data-5) (value 4)) (fact (name data-6) (value 3))) (deffact sum (avg (result 0)) (defrule find-avg (fact (name ?name1) (value ?value1)) (avg (modify (result (+ result ?value1)) )) (defrule finding-avg (avg (modify (result (/ result 6))) )) |
在 CLIPS 中,您可以使用事实查询函数来执行此操作:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | CLIPS> (deftemplate fact (slot name) (slot value)) CLIPS> (deftemplate avg (slot result)) CLIPS> (deffacts data (fact (name data-1) (value 3)) (fact (name data-2) (value 1)) (fact (name data-3) (value 2)) (fact (name data-4) (value 2)) (fact (name data-5) (value 4)) (fact (name data-6) (value 3))) CLIPS> (deffunction average (?template ?slot) (bind ?sum 0) (bind ?count 0) (do-for-all-facts ((?f ?template)) TRUE (bind ?sum (+ ?sum (fact-slot-value ?f ?slot))) (bind ?count (+ ?count 1))) (if (= ?count 0) then FALSE else (/ ?sum ?count))) CLIPS> (reset) CLIPS> (assert (avg (result (average fact value)))) <Fact-7> CLIPS> (facts) f-0 (initial-fact) f-1 (fact (name data-1) (value 3)) f-2 (fact (name data-2) (value 1)) f-3 (fact (name data-3) (value 2)) f-4 (fact (name data-4) (value 2)) f-5 (fact (name data-5) (value 4)) f-6 (fact (name data-6) (value 3)) f-7 (avg (result 2.5)) For a total of 8 facts. CLIPS> |
您必须使用
1 2 3 4 5 6 7 8 9 | (defrule find-avg ?avg <- (accumulate (progn (bind ?sum 0)(bind ?count 0)) (progn (bind ?sum (+ ?sum ?value))(++ ?count)) (/ ?sum ?count) (fact (value ?value))) => (printout t"average" ?avg crlf) ) |