(ns forensics.mesov2 (:use (forensics utils))) ;---------------------------------------------- ;---------------------------------------------- ; MESO functions ;---------------------------------------------- ;---------------------------------------------- (declare find-nearest-ui FindNearest selectpivots subpartitions get-label create-subpartitions for-me) ;---------------------------------- ; Sensitivity sphere creation (ssc) ; Initialize ; distance& = 0 ; mu (initial center) = x (pattern choosen randomly) ; spheres = empty ; f = activation function ; fgrow = a growth function ;---------------------------------- (def *spheres* []) (def *distance&* 0.0) (defn FindNearestSub [one sub-lst] (let [d (map #(EuclideanDistance [one (first (first %))]) sub-lst)] (second (first (sort-by first (map #(vector %1 %2) d sub-lst)))))) (defn find-nearest-ui [one spheres] (let [d (map #(EuclideanDistance [one (first %)]) spheres)] (second (first (sort-by first (map #(vector %1 %2) d spheres)))))) (defn ssc [data1 q1 f1 fgrow1 c1 v1] (let [mu (first data1)] ;(nth data1 (Math/round (rand (- (count data1) 1))))] (def *spheres* [[mu mu]]) (loop [data data1 q q1 f f1 fgrow fgrow1 c c1 v v1] (let [one (first data) nearest-sphere (find-nearest-ui one *spheres*) mean-nearest-sphere (first nearest-sphere) d (EuclideanDistance [one mean-nearest-sphere]) r (/ (count *spheres*) (count data))] (if (<= d *distance&*) (let [new-sphere (conj (apply vector (rest nearest-sphere)) one) mean-new-sphere (cons (apply vector (mean1 new-sphere)) new-sphere)] (and (def *spheres* (apply vector (remove #(= nearest-sphere %) *spheres*))) (def *spheres* (conj *spheres* (apply vector mean-new-sphere))))) (and (if (= *distance&* 0) (def *distance&* (* c d)) (def *distance&* (+ *distance&* (fgrow [d *distance&* f c r v])))) (def *spheres* (conj *spheres* [one one]))))) (if (= (count *spheres*) q) (def *spheres* (vector (rest data) (apply vector (map #(vector %) *spheres*)))) (recur (rest data) q f fgrow c v)))) (let [result *spheres*] (def *spheres* []) result)) (defn ssc1 [data1 q1 f1 fgrow1 c1 v1] (let [all-info (ssc data1 q1 f1 fgrow1 c1 v1)] (def *sphere-parts* (second all-info)) (loop [data (first all-info) q q1 f f1 fgrow fgrow1 c c1 v v1] (let [one (first data) nearest-sub (FindNearestSub one *sphere-parts*) nearest-sphere (find-nearest-ui one nearest-sub) mean-nearest-sphere (first nearest-sphere) d (EuclideanDistance [one mean-nearest-sphere]) r (/ (apply + (map #(count %) *sphere-parts*)) (count data))] (if (<= d *distance&*) (let [new-sphere (conj (apply vector (rest nearest-sphere)) one) mean-new-sphere (cons (apply vector (mean1 new-sphere)) new-sphere) remove-old-sphere (apply vector (remove #(= nearest-sphere %) nearest-sub)) new-sub (conj remove-old-sphere mean-new-sphere) remove-old-sub (apply vector (remove #(= nearest-sub %) *sphere-parts*)) new-sphere-parts (conj remove-old-sub new-sub)] (def *sphere-parts* new-sphere-parts)) (and (def *distance&* (+ *distance&* (fgrow [d *distance&* f c r v]))) (def *sphere-parts* (conj *sphere-parts* (conj nearest-sub [one one]))) (def *sphere-parts* (apply vector (remove #(= nearest-sub %) *sphere-parts*)))))) (if (= (count data) 1) *sphere-parts* (recur (rest data) q f fgrow c v))) (let [result *sphere-parts*] (def *distance&* 0.0) result))) ;---------------------------------- ; Activation functions (f) - 7 candidates (6 done) ; v = compression rate - fraction of patterns removed during compression. ; c = a configuration parameter in the range [0. 1.0] ; r = spheres / patterns ;---------------------------------- (defn f1 [c r] (+ (/ 1 2) (/ (Math/tanh (- (/ (* 3 r) c) 3)) 2))) (defn f2 [c r] (/ r c)) (defn f3 [c r] (Math/pow (f2 c r) 3)) (defn f4 [c r] (Math/log10 (/ (* 9 r) c))) (defn f5 [c r] (- 1 (f3 c r))) (defn f7 [c _] c) ;----------------------- ; f6 for compression ;----------------------- (defn f6 [v c r] (+ (/ 1 2) (/ (Math/tanh (- (/ (* 3 r) (max v c)) 3)) 2))) ;----------------------------------- ; Grow functions & ; d = distance between the new pattern and the nearest sensitivity sphere ; distance& = current distance ; f = activation function ; grow& = f without compression ; grow&-c = f with compression ;----------------------------------- (defn grow& [[d distance& f c r _]] (/ (* (- d distance&) (/ distance& d) (f c r)) (+ 1 (Math/log (Math/pow (+ (- d distance&) 1) 2))))) (defn grow&2 [[d distance& f c r _]] (/ (* (- d distance&) (f c r)) (+ 1 (Math/log (Math/pow (+ (- d distance&) 1) 2))))) (defn grow&1 [[d distance& f c r _]] (/ (* (- d distance&) (/ 1 d) (f c r)) (+ 1 (Math/log (Math/pow (+ (- d distance&) 1) 2))))) (defn grow&-c [[d distance& f c r v]] (/ (* (- d distance&) (/ distance& d) (f c r v)) (+ 1 (Math/log (Math/pow (+ (- d distance&) 1) 2))))) ;------------------------------------- ; The tree structure ; q = number of children per tree node ; p = a partition pivot sphere ; parent = parent node for a set of children ; root = root node of the tree ; part = partition associated with the parent node ; (initially holds all spheres) ; Okay - clojure and this algorithm is not working ; with me. So I'm going to build a tree for each ; test pattern! ; let the result be the majority class of a sphere - to break ties 'first' is choosen ;------------------------------------- (declare ssc-test-one) (def *labels* []) (defn ssc-test-all [test-lst q part] (doseq [one test-lst] (def *labels* (conj *labels* (ssc-test-one one q (FindNearestSub one part))))) (let [result *labels*] (def *labels* []) result)) (defn ssc-test-one1 [test-pattern q part] (if (or (= (count part) 1) (< (count part) q)) (if (= (count part) 1) (get-label (first part) test-pattern) (get-label (find-nearest-ui test-pattern part) test-pattern)) (ssc-test-one test-pattern q (rest (for-me q part test-pattern))))) (defn ssc-test-one [test-pattern q part] (get-label (find-nearest-ui test-pattern part) test-pattern)) (defn for-me [q part test-pattern] (let [subpartitions (create-subpartitions q part) nearest-sub (FindNearestSub test-pattern subpartitions)] nearest-sub)) (defn get-label [sphere test-pattern] (if (= (count sphere) 1) (last (first sphere)) (let [d (map #(EuclideanDistance [test-pattern %]) sphere)] (second (first (reverse (compress (sort (last (Transpose (map second (take (if (< (count sphere) 4) (count sphere) (int (Math/sqrt (count sphere)))) (sort-by first (map #(vector %1 %2) d sphere)))))))))))))) (defn create-subpartitions [q part] (let [pivots (selectpivots q part) nearest (map #(list %1 %2) part (map #(find-nearest-ui (first %) pivots) part)) subpartitions (subpartitions pivots nearest)] subpartitions)) (def *subpartitions* []) (defn subpartitions [pivots nearest] (doseq [one pivots] (def *subpartitions* (conj *subpartitions* (cons one (remove #(= nil %) (map #(if (= one (second %)) (first %)) nearest)))))) (let [result *subpartitions*] (def *subpartitions* []) result)) (defn selectpivots [q part] (take q part))