(ns code.pls.mcs (:use (code.utils utils preprocess)) (:use (incanter core stats))) (defn voter-list [one D distance] (let [nuns (sift one D 'nuns) nlns (sift one D 'nlns) dnun (second (get-nearest one nuns distance)) voters (remove #(= % 'none) (map #(if (< (distance one %) dnun) % 'none) nlns))] voters)) (defn new-voters [onevlist vlist] (remove #(= % 'none) (map #(if (member? % vlist) 'none %) onevlist))) (defn new-prototypes [prototypes vlist] (new-voters prototypes vlist)) (defn get-pstar [D vld] (let [max-voters (first (reverse (sort-by second vld))) pstar (first max-voters) pvlist (conj (last max-voters) pstar) finalD (new-prototypes D pvlist) updated-vlistD1 (loop [uvd finalD result []] (if (empty? uvd) result (recur (rest uvd) (conj result (first (filter #(= (first %) (first uvd)) vld)))))) updated-vlistD2 (loop [uvd updated-vlistD1 result []] (if (empty? uvd) result (recur (rest uvd) (conj result (let [nv (new-voters (last (first uvd)) pvlist) nv1 (vector (first (first uvd)) (nrow nv) nv)] nv1)))))] [pstar finalD updated-vlistD2])) (defn mcs1 [D vld] (loop [v vld cd D pd [] result []] (let [gps (get-pstar cd v)] (if (= (nrow pd) (nrow cd)) [cd (matrix (butlast result))] (recur (last gps) (second gps) cd (conj result (first gps))))))) (defn mcs [_ D distance _] (loop [cd D pd [] result []] (let [m1 (mcs1 cd (map #(vector % (nrow (voter-list % cd distance)) (voter-list % cd distance)) cd))] (if (= (nrow pd) (nrow cd)) (extract-unique (matrix (apply concat result))) (recur (first m1) cd (conj result (second m1)))))))