Импортируйте правила в CLIPS и оцените их производительность - PullRequest
0 голосов
/ 07 ноября 2019

Может кто-нибудь объяснить мне, как я могу импортировать созданные в WEKA правила в CLIPS и оценить их эффективность в данных TRS и TES?

Данные, которые я использую

Iнаписал 7 правил из 20 с дерева WEKA. Я включаю также 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))

1 Ответ

0 голосов
/ 07 ноября 2019

Чтобы преобразовать ваши данные, проще всего прочитать данные из файла, когда ваша программа работает, и напрямую утверждать факты. Таким образом, если ваши данные выглядят следующим образом с каждой записью в отдельной строке

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> 
...