(defmacro ficcle_lt (x y) (if (string-lessp (format nil "~a" x) (format nil "~a" y)) t nil)) (defun ficclefolds (l0 nfolds &key (pred #'<) (key #'identity) ) (let (folds (i nfolds) (l (sort l0 pred :key key))) (dotimes (j nfolds) (push (make-fold :index j) folds)) ; initialize the folds (if (= nfolds 1) (dolist (x l) (counted+ x (fold-test (nth 0 folds))) (counted+ x (fold-train (nth 0 folds)))) (dolist (x l) (decf i) ; go to the next fold (dotimes (j nfolds) (if (= i j) (counted+ x (fold-test (nth j folds))) ; add 1/j to "test" (counted+ x (fold-train (nth j folds))))) ; add rest to "train" (if (zerop i) (setf i nfolds)))) folds)) ; go back to the start (defun JustinRDR (tbl &key (threshold 20) (n 1) (ShowRules t) (ShowABCD t) (ShowABCDGrid nil) (pred 'lt)) (format t "~%RDRule test~%===============~%~A way test on dataset ~A" n (table-name tbl)) (let ((folds (ficclefolds (table-egs tbl) n :pred pred :key #'(lambda (x) (isa x tbl))))) (dolist (fold (reverse folds)) (format t "~%Fold: ~A of ~A~%" (fold-index fold) n) (let ((RDRules (JRDRTrain (table-copy tbl (counted-all (fold-train fold))) (table-majority-class (table-copy tbl (counted-all (fold-train fold)))) threshold))) (if ShowRules (format t "~A~%" (RDRShow RDRules))) (if ShowABCD (format t "~A~%" (abcd-stats (JRDRTest (table-copy tbl (counted-all (fold-test fold))) RDRules) :verbose ShowABCDGrid))))))) (defun randomPred (&rest rest) (evenp (random (length rest)))) (defun Jcheckdata (row ruleset passed) (if (not ruleset) ruleset (progn (if (funcall (RDRule-predicate ruleset) row) (if (not (null (RDRule-true ruleset))) (checkdata row (RDRule-true ruleset) ruleset) (values ruleset ruleset)) (if (not (null (RDRule-false ruleset))) (checkdata row (RDRule-false ruleset) passed) (values ruleset passed)))))) (defun JRDRTestRow (row RDRules) (multiple-value-bind (result passed) (Jcheckdata row RDRules nil) (if (not (null passed)) (RDRule-prediction passed) nil))) (defun JRDRTest (tbl RDRules) (mapcar #'(lambda (x) (let ((test (JRDRTestRow x RDRules))) (cons (eg-class x) test))) (table-rows tbl))) (defun JRDRTrain (tbl major &optional (threshold 20)) (setf (counted-all (table-all tbl)) (mapcar #'(lambda (x) (setf (eg-features x) (RemoveColumnFromList (table-class tbl) (eg-features x))) x) (counted-all (table-all tbl)))) (PatchWorseCase (JRDRTrainer tbl (table-rows tbl) nil major threshold) major)) (defun JRDRTrainer (tbl data ruleset major &optional (threshold 20)) ;(format t "~A~%" data) (let ((count (length data)) (allAvail)) (format t "Majority:~A~%" major) (dotimes (x count) ;(format t "~A~%" (car data)) (multiple-value-bind (MatchRule PassedRule) (Jcheckdata (car data) ruleset nil) ;(format t "MatchRule: ~A ParentRule: ~A~%" MatchRule ParentRule) (if (null MatchRule) ;(progn (let (ls) (progn (dotimes (x (length (counted-all (table-columns tbl)))) (if (/= (table-class tbl) x) (push x ls))) (setf allAvail ls) (setf ruleset (JAddRule (car data) MatchRule ruleset ls major nil)))) ;(format t "Create Ruleset: ~A~%" ruleset)) ;(progn ;(format t "Oracle says: ~A~%" (Oracle MatchRule (car data))) (if (funcall (RDRule-predicate MatchRule) (car data)) (if (Oracle MatchRule (car data)) (pop data) (setf (RDRule-true MatchRule) (JAddRule (car data) (RDRule-true MatchRule) ruleset (RDRule-available MatchRule) major (RDRule-Parent MatchRule)))) (if (not(null PassedRule)) (setf (RDRule-false MatchRule) (JAddRule (car data) (RDRule-false MatchRule) ruleset (RDRule-available PassedRule) major (RDRule-Parent PassedRule))) (setf (RDRule-false MatchRule) (JAddRule (car data) (RDRule-false MatchRule) ruleset allAvail major nil)))))))) ruleset) (defun JAddRule (row matching ruleset available majority parent) ;(format t "Row:~A Parent:~A~%" row parent) (if (= (length available) 0) ;(make-RDRule :predicate `(lambda (datarow) (equal t t)) :prediction majority :predicate-col nil :predicate-val nil) nil; original (progn (let (avail) (if (not(null parent)) (dotimes (x (length (eg-features row))) (if (not(eql (nth x (eg-features parent)) (nth x (eg-features row)))) (push x avail))) (setf avail available)) ;(format t "Avail to pick:~A Available Orginal:~A ~%" avail available) ;debug line (if (null ruleset) (setf matching (JMakeRule row matching ruleset avail)) (if (null matching) (setf matching (JMakeRule row matching ruleset avail)) (if (funcall (RDRule-predicate matching) row) (setf (RDRule-true matching) (JMakeRule row matching ruleset avail)) (setf (RDRule-false matching) (JMakeRule row matching ruleset avail))))) (setf avail (remove (RDRule-predicate-col matching) avail)) (setf (RDRule-available matching) avail))matching))) (defun JMakeRule (row matching ruleset2 available) (let ((decision (multiple-value-list (JMakeDecision row matching ruleset2 available)))) (make-RDRule :predicate (first decision) :prediction (second decision) :predicate-col (third decision) :predicate-val (fourth decision) :parent row))) (defun PatchWorseCase (ruleset majority) (if (not (null (RDRule-false ruleset))) (PatchWorseCase (RDRule-false ruleset) majority) (setf (RDRule-false ruleset) (make-RDRule :predicate #'(lambda (datarow) (not(null datarow))) :prediction majority :predicate-col nil :predicate-val nil)))ruleset) (defmacro JMakePredicate (col value) `(lambda (datarow) (equal (nth ,col (eg-features datarow)) ,value))) (defun JMakeDecision (row matching ruleset3 available) (reset-seed) (let* ((col (nth (my-random-int (length available)) available)) (value (nth col (eg-features row)))) (values (JMakePredicate col value) (eg-class row) col value))) (defun JDist (tbl &key (n 1) (using 'ewd)) (let ((egs (transpose (table-egs tbl))) (newEgs)) (progn (dolist (currentrow egs) (setf newEgs (append newEgs (list (funcall using currentrow n))))) (table-copy tbl (transpose newEgs))))) (defun JDist2 (tbl &key (n 1) (using 'ewd)) (let ((egs (transpose (table-egs tbl))) (newEgs)) (progn (dolist (currentrow egs) (if (numberp (car currentrow)) (setf newEgs (append newEgs (list (funcall using currentrow n)))) (setf newEgs (append newEgs (list currentrow)))))) (table-copy tbl (transpose newEgs))))