annotate org/self_organizing_touch.clj @ 451:0a4362d1f138

finishing up chapter 3.
author Robert McIntyre <rlm@mit.edu>
date Wed, 26 Mar 2014 20:38:17 -0400
parents 5205535237fb
children f339e3d5cc8c
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@430 65 :experience-watch nil
rlm@410 66 :motor-control
rlm@410 67 (motor-control-program
rlm@410 68 worm-single-segment-muscle-labels
rlm@410 69 (touch-kinesthetics))
rlm@410 70 :end-frame 1200})))
rlm@410 71
rlm@410 72 (def full-contact [(float 0.0) (float 0.1)])
rlm@410 73
rlm@410 74 (defn pure-touch?
rlm@410 75 "This is worm specific code to determine if a large region of touch
rlm@410 76 sensors is either all on or all off."
rlm@410 77 [[coords touch :as touch-data]]
rlm@410 78 (= (set (map first touch)) (set full-contact)))
rlm@410 79
rlm@410 80 (defn remove-similar
rlm@410 81 [coll]
rlm@410 82 (loop [result () coll (sort-by (comp - count) coll)]
rlm@410 83 (if (empty? coll) result
rlm@411 84 (let [[x & xs] coll
rlm@410 85 c (count x)]
rlm@410 86 (if (some
rlm@410 87 (fn [other-set]
rlm@410 88 (let [oc (count other-set)]
rlm@410 89 (< (- (count (union other-set x)) c) (* oc 0.1))))
rlm@410 90 xs)
rlm@410 91 (recur result xs)
rlm@410 92 (recur (cons x result) xs))))))
rlm@410 93
rlm@410 94 (def all-touch-coordinates
rlm@410 95 (concat
rlm@410 96 (rect-region [0 15] [7 22])
rlm@410 97 (rect-region [8 0] [14 29])
rlm@410 98 (rect-region [15 15] [22 22])))
rlm@410 99
rlm@410 100 (defn view-touch-region [coords]
rlm@410 101 (let [touched-region
rlm@410 102 (reduce
rlm@410 103 (fn [m k]
rlm@410 104 (assoc m k [0.0 0.1]))
rlm@410 105 (zipmap all-touch-coordinates (repeat [0.1 0.1])) coords)
rlm@410 106 data
rlm@410 107 [[(vec (keys touched-region)) (vec (vals touched-region))]]
rlm@410 108 touch-display (view-touch)]
rlm@412 109 (dorun (repeatedly 5 #(touch-display data)))))
rlm@410 110
rlm@410 111 (defn learn-touch-regions []
rlm@410 112 (let [experiences (atom [])
rlm@410 113 world (apply-map
rlm@410 114 worm-world
rlm@410 115 (assoc (worm-segment-defaults)
rlm@410 116 :experiences experiences))]
rlm@410 117 (run-world world)
rlm@410 118 (->>
rlm@410 119 @experiences
rlm@410 120 (drop 175)
rlm@410 121 ;; access the single segment's touch data
rlm@410 122 (map (comp first :touch))
rlm@410 123 ;; only deal with "pure" touch data to determine surfaces
rlm@410 124 (filter pure-touch?)
rlm@410 125 ;; associate coordinates with touch values
rlm@410 126 (map (partial apply zipmap))
rlm@410 127 ;; select those regions where contact is being made
rlm@410 128 (map (partial group-by second))
rlm@410 129 (map #(get % full-contact))
rlm@410 130 (map (partial map first))
rlm@410 131 ;; remove redundant/subset regions
rlm@410 132 (map set)
rlm@410 133 remove-similar)))
rlm@410 134
rlm@410 135 (defn learn-and-view-touch-regions []
rlm@410 136 (map view-touch-region
rlm@410 137 (learn-touch-regions)))
rlm@410 138
rlm@410 139