Skip to content

Commit

Permalink
fixed defparfun broken because of unexpected behavior from split-with…
Browse files Browse the repository at this point in the history
…, updated id3 benchmark
  • Loading branch information
dpzmick committed Mar 21, 2016
1 parent e07c06d commit d969641
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 17 deletions.
12 changes: 7 additions & 5 deletions src/benchmark/core.clj
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
(ns benchmark.core
(:require [clojure.java.io :as io])
(:require [clojure.string :refer [split starts-with? join]])
(:require [benchmark.search :refer :all])
(:require [benchmark.tree-sum :refer :all])
(:require [benchmark.fib :refer :all]))
(:require [clojure.java.io :as io]
[clojure.string :refer [split starts-with? join]]
[benchmark.search :refer :all]
[benchmark.tree-sum :refer :all]
[benchmark.fib :refer :all]
[benchmark.id3 :refer :all]))


(defn- env-expand [string]
(if (starts-with? string "$")
Expand Down
47 changes: 39 additions & 8 deletions src/benchmark/id3.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
;; example of a datum {:a true :b false :c true}

(ns benchmark.id3
(:require [clojure.pprint :refer :all])
(:require [criterium.core :as cr])
(:require [com.dpzmick.util :refer :all])
(:require [com.dpzmick.parallel-macros.defparfun :refer [defparfun]]))

(defn rand-bool [] (case (rand-int 2)
Expand Down Expand Up @@ -42,15 +43,45 @@
(cons split-attr [(id3 left (rest sattrs) target)
(id3 right (rest sattrs) target)]))))))

(defn make-random-data
(defparfun id3-defparfun [dataset attrs target] (> (count attrs) 50)
(if (or (empty? dataset) (empty? attrs))
[]
(let
[sattrs (sort-by #(entropy dataset %) attrs)]
(if (= (first sattrs) target)
[]
(let [split-attr (first sattrs)
left (filter #(not (has? split-attr %)) dataset)
right (filter #(has? split-attr %) dataset)]
(cons split-attr [(id3-defparfun left (rest sattrs) target)
(id3-defparfun right (rest sattrs) target)]))))))

;; helper functions to generate the dataset
(defn- make-attrs [m] (take m (map #(keyword (str %)) (range))))

(defn- make-random-data
[n attrs]
(let [rand-datum (fn [] (reduce #(assoc %1 %2 (rand-bool)) {} attrs))]
(take n (repeatedly #(rand-datum)))))

(defn make-attrs [m] (take m (map #(keyword (str %)) (range))))
(defn- make-random-input [num-attrs num-elemts]
(let [attrs (make-attrs num-attrs)]
{
:attrs attrs
:data (make-random-data num-elemts attrs)
}))

(defn doid3 [n m]
(let
[attrs (make-attrs m)
data (make-random-data n attrs)]
(id3 data (rest attrs) (first attrs))))
(defn create-input-file [num-attrs num-elemts file-to-create]
(let [d (make-random-input (read-string num-attrs) (read-string num-elemts))]
(spit file-to-create d)))

(defn read-input-file [filename] (read-string (slurp filename)))

(defn id3-serial [filename]
(let [{data :data attrs :attrs} (read-input-file filename)]
(cr/bench (id3 data (rest attrs) (first attrs)))))


(defn id3-parfun [filename]
(let [{data :data attrs :attrs} (read-input-file filename)]
(cr/bench (id3-defparfun data (rest attrs) (first attrs)))))
9 changes: 5 additions & 4 deletions src/com/dpzmick/ast_manip/bb_edit.clj
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,19 @@
(declare recursive-dependency-helper)
(defn recursive-dependency
[vname bindings]
(log "rec-dep on" vname "bindings" bindings)
(if (not (apply distinct? (map first bindings)))
(throw (Exception. multiple-names)))

(let
[[deps not-deps] (split-with #(dependency? vname (second %)) bindings)]
[[deps not-deps] (correct-split-with #(dependency? vname (second %)) bindings)]
(recursive-dependency-helper deps not-deps deps)))

;; worklist holds the new bindings which still need some sort of action
(defn- recursive-dependency-helper [depends not-dep worklist]
; (println "depends" depends)
; (println "not-dep" not-dep)
; (println "worklist" worklist)
(log "depends" depends)
(log "not-dep" not-dep)
(log "worklist" worklist)

(if (empty? worklist)
;; we are done, nothing new was added last time
Expand Down
6 changes: 6 additions & 0 deletions src/com/dpzmick/util.clj
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,9 @@
"true if coll contains elm"
[coll elm]
(some #(= elm %) coll))

(defn correct-split-with [pred coll]
(let
[a (filter pred coll)
b (filter #(not (pred %)) coll)]
[a b]))

0 comments on commit d969641

Please sign in to comment.