initial som gui code (can draw a moving map of neurons w/2D weights)
[neural-net.git] / src / neural_net / som_gui.clj
blob826bf600283e6bf3536617ff01045416c5f13d55
1 (ns neural-net.som-gui.clj
2   (:use neural-net.som)
3   (:use neural-net.core)
4   (:import (java.awt Color Graphics Dimension)
5            (java.awt.image BufferedImage)
6            (javax.swing JPanel JFrame)))
8 ;; evolved from example gui code in Rich Hickey's Ants demo
9 ;; (see http://clojure.googlegroups.com/web/ants.clj)
11 (def scale 5)
12 (def dim 100)
14 (defn render-net [#^Graphics o #^neural-net.core.Graph g]
15   (let [pos (comp (partial + (* scale (/ dim 2))) (partial * scale))
16         x (comp pos first)   y (comp pos second)]
17     (dorun                              ; draw neurons
18      (for [w (map :weights (vals (get g :vertices)))]
19        (doto o (.setColor (. Color black))
20              (.drawOval (x w) (y w) 5 5)
21              (.fillOval (x w) (y w) 5 5))))
22     (dorun                              ; draw edges
23      (for [[a b] (get g :edges)]
24        (let [a ((get (get g :vertices) a) :weights)
25              b ((get (get g :vertices) b) :weights )]
26          (doto o (.setColor (. Color black))
27                (.drawLine (x a) (y a) (x b) (y b))))))))
29 (defn render [g]
30   (let [img (new BufferedImage (* scale dim) (* scale dim)
31                  (. BufferedImage TYPE_INT_ARGB))
32         bg (. img (getGraphics))]
33     (doto bg
34       (.setColor (. Color white))
35       (.fillRect 0 0 (. img (getWidth)) (. img (getHeight))))
36     (render-net bg (@net :map))
37     (. g (drawImage img 0 0 nil))
38     (. bg (dispose))))
40 (def panel (doto (proxy [JPanel] []
41                    (paint [g] (render g)))
42              (.setPreferredSize (new Dimension
43                                      (* scale dim)
44                                      (* scale dim)))))
46 (def frame (doto (new JFrame) (.add panel) .pack .show))
48 (comment ) ; interactively re-paint the frame
49 (. panel (repaint))
51 (def animator (agent nil))
53 (def animation-sleep-ms 500)
55 (def running true)
57 (defn animation [x]
58   (when running (send-off *agent* #'animation))
59   (. panel (repaint))
60   (. Thread (sleep animation-sleep-ms))
61   nil)
64 (let [som {:phi self-organizing-map:run
65            :learn self-organizing-map:learn
66            :train self-organizing-map:train
67            :eta 1
68            :update (fn [this dist n x]
69                      (map (fn [x] (* (/ x (inc dist)) (this :eta)))
70                           (map -
71                                x (((get (this :map) :vertices) n) :weights))))
72            :map (neural-net.core.Graph.
73                  {:a {:weights [ 2  0]}
74                   :b {:weights [ 0  2]}
75                   :c {:weights [-2  0]}
76                   :d {:weights [ 0 -2]}}
77                  [[:a :b] [:b :c] [:c :d]])}
78       xs [[16 16]
79           [16 12]
80           [12 16]
81           [12 12]]]
82   (loop [som som xs xs]
83     (println som)
84     (dosync (ref-set net som))
85     (. panel (repaint))
86     (. Thread (sleep 1000))
87     (when (not (empty? xs))
88       (recur (second (train som (first xs) nil nil)) (rest xs)))))