【问题标题】:Import rules to CLIPS and evaluate its performance将规则导入 CLIPS 并评估其性能
【发布时间】:2021-03-27 18:27:00
【问题描述】:

谁能解释我如何在 CLIPS 中导入 WEKA 创建的规则并评估其在 TRS 和 TES 数据中的效率?

数据我use

我从 WEKA 树中写出了 20 条规则中的 7 条。我还包括玻璃数据表中的 3 个实例

小测试代码

(deftemplate glass
 (slot n(type FLOAT))
 (slot m(type FLOAT))
 (slot a(type FLOAT))
 (slot b(type FLOAT))
 (slot r(type FLOAT))
 (slot s(type FLOAT))
 (slot k(type FLOAT))
 (slot c(type FLOAT)))


(deftemplate Type
 (slot type))


(deffacts instances1
 (glass (n 13.00)   
        (m 2.28)         
        (a 1.00)
        (b 0.00)))

(deffacts instances2
 (glass (n 13.70)   
        (m 1.80)         
        (a 1.40)
        (b 0.00)))

(deffacts instances3
 (glass (n 13.70)   
        (m 1.90)         
        (a 1.40)
        (b 0.00)))

(defrule R1
 (glass (b ?b))
 (test (<= ?b 0.27))
 (glass (m ?m))
 (test (<= ?m 2.41))
 (glass (n ?n))
 (test (<= ?n 13.78))
 (glass (a ?a))
 (test (<= ?a 1.38))   
 =>
 (assert (Type (type buildwindnonfloat1)))
 (printout t "buildwindnonfloat1 detected" crlf))

(defrule R2
 (glass (b ?b))
 (test (<= ?b 0.27))
 (glass (m ?m))
 (test (<= ?m 2.41))
 (glass (n ?n))
 (test (<= ?n 13.78))
 (glass (a ?a))
 (test (> ?a 1.38))
 (glass (m ?m))
 (test (<= ?m 1.88))  
 =>
 (assert (Type (type containers2)))
 (printout t "containers2 detected" crlf))

(defrule R3
 (glass (b ?b))
 (test (<= ?b 0.27))
 (glass (m ?m))
 (test (<= ?m 2.41))
 (glass (n ?n))
 (test (<= ?n 13.78))
 (glass (a ?a))
 (test (> ?a 1.38))
 (glass (m ?m))
 (test (> ?m 1.88))  
 =>
 (assert (Type (type buildwindnonfloat3)))
 (printout t "buildwindnonfloat3 detected" crlf))

(defrule R4
 (glass (b ?b))
 (test (<= ?b 0.27))
 (glass (m ?m))
 (test (<= ?m 2.41))
 (glass (n ?n))
 (test (> ?n 13.78))   
 =>
 (assert (Type (type tableware4)))
 (printout t "tableware detected" crlf))

(defrule R5
 (glass (b ?b))
 (test (<= ?b 0.27))
 (glass (m ?m))
 (test (> ?m 2.41))
 (glass (a ?a))
 (test (<= ?a 1.4))
 (glass (m ?m))
 (test (<= ?m 3.34))
 (glass (a ?a))
 (test (<= ?a 1.25))  
 =>
 (assert (Type (type buildwindnonfloat5)))
 (printout t "buildwindnonfloat5 detected" crlf))

(defrule R6
 (glass (b ?b))
 (test (<= ?b 0.27))
 (glass (m ?m))
 (test (> ?m 2.41))
 (glass (a ?a))
 (test (<= ?a 1.4))
 (glass (m ?m))
 (test (<= ?m 3.34))
 (glass (a ?a))
 (test (> ?a 1.25))  
 =>
 (assert (Type (type buildwindfloat6)))
 (printout t "buildwindfloat6 detected" crlf))

(defrule R7
 (glass (b ?b))
 (test (<= ?b 0.27))
 (glass (m ?m))
 (test (> ?m 2.41))
 (glass (a ?a))
 (test (<= ?a 1.4))
 (glass (m ?m))
 (test (> ?m 3.34))
 (glass (m ?m))
 (test (<= ?m 3.82))
 (glass (r ?r))
 (test (<= ?r 1.51707))
 (glass (r ?r))
 (test (<= ?r 51596))  
 =>
 (assert (Type (type buildwindfloat7)))
 (printout t "buildwindfloat7 detected" crlf))

【问题讨论】:

  • 恐怕您正在尝试训练专家系统。你应该继续你写的规则。

标签: clips


【解决方案1】:

要转换数据,最简单的方法是在程序运行时从文件中读取数据并直接断言事实。因此,如果您的数据如下所示,每个条目都在自己的行中

1.5159,13.24,3.34,1.47,73.1,0.39,8.22,0,0,'build wind non-float' 
1.5167,13.24,3.57,1.38,72.7,0.56,8.44,0,0.1,'vehic wind float'

然后您可以通过将每一行作为单个字符串读取,将逗号替换为空格,然后将字符串拆分为多个值来读取数据。然后,您可以使用单独的规则将文件中的值映射到 deftemplate 事实中的相应插槽。

存储每个玻璃事实的预期结果,然后您可以将该值与您的规则建议的值进行比较。

         CLIPS (6.31 6/12/19)
CLIPS> 
(deftemplate glass
  (slot n (type FLOAT))
  (slot m (type FLOAT))
  (slot a (type FLOAT))
  (slot b (type FLOAT))
  (slot r (type FLOAT))
  (slot s (type FLOAT))
  (slot k (type FLOAT))
  (slot c (type FLOAT))
  (slot f (type FLOAT))
  (slot type))
CLIPS> 
(deftemplate input
   (multislot data))
CLIPS>    
(deffunction str-rpl (?str ?find ?replace)
   (if (eq ?find "")
      then
      (return ?str))
   (bind ?rs "")
   (bind ?fl (str-length ?find))
   (bind ?i (str-index ?find ?str))
   (while (neq ?i FALSE)
      (bind ?rs (str-cat ?rs (sub-string 1 (- ?i 1) ?str) ?replace))
      (bind ?str (sub-string (+ ?i ?fl) (str-length ?str) ?str))
      (bind ?i (str-index ?find ?str)))
   (bind ?rs (str-cat ?rs ?str))
   ?rs)
CLIPS>  
(defrule get-data
   =>
   (printout t "Input File? ")
   (bind ?file (readline))
   (if (not (open ?file data))
      then
      (printout t "Unable to open file" crlf)
      (return))
   (bind ?line (readline data))
   (while (neq ?line EOF)
      (bind ?line (str-rpl ?line "," " "))
      (bind ?line (str-rpl ?line "'" "\""))
      (assert (input (data (explode$ ?line))))
      (bind ?line (readline data)))
   (close data))
CLIPS> 
(defrule convert-data
   ?i <- (input (data ?r ?n ?m ?a ?s ?k ?c ?b ?f ?type))
   =>
   (retract ?i)
   (assert (glass (r ?r) (n ?n) (m ?m) (a ?a) (s ?s) (k ?k) (c ?c) (b ?b) (f ?f) (type ?type))))
CLIPS> 
(defrule R1
   (glass (b ?b) 
          (m ?m)
          (n ?n)
          (a ?a)
          (type ?type))
 (test (<= ?b 0.27))
 (test (<= ?m 2.41))
 (test (<= ?n 13.78))
 (test (<= ?a 1.38))   
 =>
 (printout t "buildwindnonfloat1 detected type = " ?type crlf))
CLIPS> 
(defrule R2
 (glass (b ?b)
        (m ?m)
        (n ?n)
        (a ?a)
        (type ?type))
 (test (<= ?b 0.27))
 (test (<= ?m 2.41))
 (test (<= ?n 13.78))
 (test (> ?a 1.38))
 (test (<= ?m 1.88))  
 =>
 (printout t "containers2 detected type = " ?type crlf))
CLIPS> 
(defrule R3
 (glass (b ?b)
        (m ?m)
        (n ?n)
        (a ?a)
        (type ?type))
 (test (<= ?b 0.27))
 (test (<= ?m 2.41))
 (test (<= ?n 13.78))
 (test (> ?a 1.38))
 (test (> ?m 1.88))  
 =>
 (printout t "buildwindnonfloat3 detected type = " ?type crlf))
CLIPS> 
(defrule R4
  (glass (b ?b)
        (m ?m)
        (n ?n)
        (type ?type))
 (test (<= ?b 0.27))
 (test (<= ?m 2.41))
 (test (> ?n 13.78))   
 =>
 (printout t "tableware detected type = " ?type crlf))
CLIPS> 
(defrule R5
  (glass (b ?b)
        (m ?m)
        (a ?a)
        (type ?type))
 (test (<= ?b 0.27))
 (test (> ?m 2.41))
 (test (<= ?a 1.4))
 (test (<= ?m 3.34))
 (test (<= ?a 1.25))  
 =>
 (printout t "buildwindnonfloat5 detected type = " ?type crlf))
CLIPS> 
(defrule R6
 (glass (b ?b)
        (m ?m)
        (a ?a)
        (type ?type))
 (test (<= ?b 0.27))
 (test (> ?m 2.41))
 (test (<= ?a 1.4))
 (test (<= ?m 3.34))
 (test (> ?a 1.25))  
 =>
 (printout t "buildwindfloat6 detected type = " ?type crlf))
CLIPS> 
(defrule R7
 (glass (b ?b)
        (m ?m)
        (a ?a)
        (r ?r)
        (type ?type))
 (test (<= ?b 0.27))
 (test (> ?m 2.41))
 (test (<= ?a 1.4))
 (test (> ?m 3.34))
 (test (<= ?m 3.82))
 (test (<= ?r 1.51707))
 (test (<= ?r 51596))  
 =>
 (printout t "buildwindfloat7 detected type = " ?type crlf))
CLIPS> (reset)
CLIPS> (run)
Input File? weka.txt
buildwindfloat7 detected type = vehic wind float
CLIPS> 

【讨论】:

  • 但是如何从包含在txt文件中的数据创建deffact,格式如下:1.5159,13.24,3.34,1.47,73.1,0.39,8.22,0,0,'build wind non- float' 1.5167,13.24,3.57,1.38,72.7,0.56,8.44,0,0.1,'车载风浮'
  • 我得到了这个结果CLIPS&gt; (run) Input File? a.txt [ROUTER1] Logical name data was not recognized by any routers [PRCCODE4] Execution halted during the actions of defrule get-data.
  • 两个最可能的原因是文件与 CLIPS 可执行文件不在同一目录中(或您当前的路径),或者您输入的文件名与实际文件不匹配名字。
  • 我真的很感谢您花时间来澄清一些问题。你是对的,我在不同的文件中有相同的名称 txt,我无法让它运行。但是现在我如何将 CLIPS 系统实现的分类与数据集已有的典型分类进行比较呢?实际上,有多少事实被归入它们真正属于的类别?
  • 我可以看到您的数据中的列与您的 deftemplate 中的槽值之间存在某种关系,但除此之外,我不知道您的规则是如何得出的或他们试图做什么.
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-09-07
  • 2017-04-05
  • 1970-01-01
  • 2020-09-16
  • 2021-02-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多