annotate org/self_organizing_touch.clj @ 429:b5d0f0adf19f

improvements by Dylan!
author Robert McIntyre <rlm@mit.edu>
date Fri, 21 Mar 2014 20:56:56 -0400
parents cc9957241076
children 5205535237fb
rev   line source
rlm@410 1 (ns org.aurellem.self-organizing-touch
rlm@410 2 "Using free play to automatically organize touch perception into regions."
rlm@410 3 {:author "Robert McIntyre"}
rlm@410 4 (:use (cortex world util import body sense
rlm@410 5 hearing touch vision proprioception movement
rlm@410 6 test))
rlm@410 7 (:use [clojure set pprint])
rlm@410 8 (:import (com.jme3.math ColorRGBA Vector3f))
rlm@410 9 (:import java.io.File)
rlm@410 10 (:import com.jme3.audio.AudioNode)
rlm@410 11 (:import com.aurellem.capture.RatchetTimer)
rlm@410 12 (:import (com.aurellem.capture Capture IsoTimer))
rlm@410 13 (:import (com.jme3.math Vector3f ColorRGBA)))
rlm@410 14
rlm@410 15 (use 'org.aurellem.worm-learn)
rlm@410 16 (dorun (cortex.import/mega-import-jme3))
rlm@410 17
rlm@410 18 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
rlm@410 19 ;; A demonstration of self organiging touch maps through experience. ;
rlm@410 20 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
rlm@410 21
rlm@410 22 (def single-worm-segment-view
rlm@410 23 [(Vector3f. 2.0681207, -6.1406755, 1.6106138)
rlm@410 24 (Quaternion. -0.15558705, 0.843615, -0.3428654, -0.38281822)])
rlm@410 25
rlm@410 26 (def worm-single-segment-muscle-labels
rlm@410 27 [:lift-1 :lift-2 :roll-1 :roll-2])
rlm@410 28
rlm@410 29 (defn touch-kinesthetics []
rlm@410 30 [[170 :lift-1 40]
rlm@410 31 [190 :lift-1 20]
rlm@410 32 [206 :lift-1 0]
rlm@410 33
rlm@410 34 [400 :lift-2 40]
rlm@410 35 [410 :lift-2 0]
rlm@410 36
rlm@410 37 [570 :lift-2 40]
rlm@410 38 [590 :lift-2 20]
rlm@410 39 [606 :lift-2 0]
rlm@410 40
rlm@410 41 [800 :lift-1 30]
rlm@410 42 [809 :lift-1 0]
rlm@410 43
rlm@410 44 [900 :roll-2 40]
rlm@410 45 [905 :roll-2 20]
rlm@410 46 [910 :roll-2 0]
rlm@410 47
rlm@410 48 [1000 :roll-2 40]
rlm@410 49 [1005 :roll-2 20]
rlm@410 50 [1010 :roll-2 0]
rlm@410 51
rlm@410 52 [1100 :roll-2 40]
rlm@410 53 [1105 :roll-2 20]
rlm@410 54 [1110 :roll-2 0]
rlm@410 55 ])
rlm@410 56
rlm@410 57 (defn single-worm-segment []
rlm@410 58 (load-blender-model "Models/worm/worm-single-segment.blend"))
rlm@410 59
rlm@410 60 (defn worm-segment-defaults []
rlm@410 61 (let [direct-control (worm-direct-control worm-muscle-labels 40)]
rlm@410 62 (merge (worm-world-defaults)
rlm@410 63 {:worm-model single-worm-segment
rlm@410 64 :view single-worm-segment-view
rlm@410 65 :motor-control
rlm@410 66 (motor-control-program
rlm@410 67 worm-single-segment-muscle-labels
rlm@410 68 (touch-kinesthetics))
rlm@410 69 :end-frame 1200})))
rlm@410 70
rlm@410 71 (def full-contact [(float 0.0) (float 0.1)])
rlm@410 72
rlm@410 73 (defn pure-touch?
rlm@410 74 "This is worm specific code to determine if a large region of touch
rlm@410 75 sensors is either all on or all off."
rlm@410 76 [[coords touch :as touch-data]]
rlm@410 77 (= (set (map first touch)) (set full-contact)))
rlm@410 78
rlm@410 79 (defn remove-similar
rlm@410 80 [coll]
rlm@410 81 (loop [result () coll (sort-by (comp - count) coll)]
rlm@410 82 (if (empty? coll) result
rlm@411 83 (let [[x & xs] coll
rlm@410 84 c (count x)]
rlm@410 85 (if (some
rlm@410 86 (fn [other-set]
rlm@410 87 (let [oc (count other-set)]
rlm@410 88 (< (- (count (union other-set x)) c) (* oc 0.1))))
rlm@410 89 xs)
rlm@410 90 (recur result xs)
rlm@410 91 (recur (cons x result) xs))))))
rlm@410 92
rlm@410 93 (def all-touch-coordinates
rlm@410 94 (concat
rlm@410 95 (rect-region [0 15] [7 22])
rlm@410 96 (rect-region [8 0] [14 29])
rlm@410 97 (rect-region [15 15] [22 22])))
rlm@410 98
rlm@410 99 (defn view-touch-region [coords]
rlm@410 100 (let [touched-region
rlm@410 101 (reduce
rlm@410 102 (fn [m k]
rlm@410 103 (assoc m k [0.0 0.1]))
rlm@410 104 (zipmap all-touch-coordinates (repeat [0.1 0.1])) coords)
rlm@410 105 data
rlm@410 106 [[(vec (keys touched-region)) (vec (vals touched-region))]]
rlm@410 107 touch-display (view-touch)]
rlm@412 108 (dorun (repeatedly 5 #(touch-display data)))))
rlm@410 109
rlm@410 110 (defn learn-touch-regions []
rlm@410 111 (let [experiences (atom [])
rlm@410 112 world (apply-map
rlm@410 113 worm-world
rlm@410 114 (assoc (worm-segment-defaults)
rlm@410 115 :experiences experiences))]
rlm@410 116 (run-world world)
rlm@410 117 (->>
rlm@410 118 @experiences
rlm@410 119 (drop 175)
rlm@410 120 ;; access the single segment's touch data
rlm@410 121 (map (comp first :touch))
rlm@410 122 ;; only deal with "pure" touch data to determine surfaces
rlm@410 123 (filter pure-touch?)
rlm@410 124 ;; associate coordinates with touch values
rlm@410 125 (map (partial apply zipmap))
rlm@410 126 ;; select those regions where contact is being made
rlm@410 127 (map (partial group-by second))
rlm@410 128 (map #(get % full-contact))
rlm@410 129 (map (partial map first))
rlm@410 130 ;; remove redundant/subset regions
rlm@410 131 (map set)
rlm@410 132 remove-similar)))
rlm@410 133
rlm@410 134 (defn learn-and-view-touch-regions []
rlm@410 135 (map view-touch-region
rlm@410 136 (learn-touch-regions)))
rlm@410 137
rlm@410 138