comparison org/worm_learn.clj @ 414:634795361af8

working on wiggling? stream predicate.
author Robert McIntyre <rlm@mit.edu>
date Wed, 19 Mar 2014 15:59:46 -0400
parents 54ef2e06c3ef
children af7945c27474
comparison
equal deleted inserted replaced
413:54ef2e06c3ef 414:634795361af8
79 ;; These are scripts that direct the worm to move in two radically 79 ;; These are scripts that direct the worm to move in two radically
80 ;; different patterns -- a sinusoidal wiggling motion, and a curling 80 ;; different patterns -- a sinusoidal wiggling motion, and a curling
81 ;; motions that causes the worm to form a circle. 81 ;; motions that causes the worm to form a circle.
82 82
83 (def curl-script 83 (def curl-script
84 [[370 :d-up 40] 84 [[370 :d-flex 40]
85 [600 :d-up 0]]) 85 [600 :d-flex 0]])
86 86
87 (def period 18) 87 (def period 18)
88 88
89 (def worm-muscle-labels 89 (def worm-muscle-labels
90 [:base-down :base-up 90 [:base-ex :base-flex
91 :a-down :a-up 91 :a-ex :a-flex
92 :b-down :b-up 92 :b-ex :b-flex
93 :c-down :c-up 93 :c-ex :c-flex
94 :d-down :d-up]) 94 :d-ex :d-flex])
95 95
96 (defn gen-wiggle [[flexor extensor :as muscle-pair] time-base] 96 (defn gen-wiggle [[flexor extensor :as muscle-pair] time-base]
97 (let [period period 97 (let [period period
98 power 45] 98 power 45]
99 [[time-base flexor power] 99 [[time-base flexor power]
100 [(+ time-base period) flexor 0] 100 [(+ time-base period) flexor 0]
101 [(+ time-base period 1) extensor power] 101 [(+ time-base period 1) extensor power]
102 [(+ time-base (+ (* 2 period) 2)) extensor 0]])) 102 [(+ time-base (+ (* 2 period) 2)) extensor 0]]))
103 103
104 (def wiggle-script 104 (def wiggle-script
105 (mapcat gen-wiggle (repeat 4000 [:a-down :a-up]) 105 (mapcat gen-wiggle (repeat 4000 [:a-ex :a-flex])
106 (range 100 1000000 (+ 3 (* period 2))))) 106 (range 100 1000000 (+ 3 (* period 2)))))
107 107
108 108
109 ;; Normally, we'd use unsupervised/supervised machine learning to pick 109 ;; Normally, we'd use unsupervised/supervised machine learning to pick
110 ;; out the defining features of the different actions available to the 110 ;; out the defining features of the different actions available to the
169 (* 10) 169 (* 10)
170 (- 1) 170 (- 1)
171 (Math/abs))) 171 (Math/abs)))
172 172
173 (defn fft [nums] 173 (defn fft [nums]
174 (.transform 174 (map
175 (FastFourierTransformer. DftNormalization/UNITARY) 175 #(.getReal %)
176 (double-array nums) TransformType/FORWARD)) 176 (.transform
177 (FastFourierTransformer. DftNormalization/STANDARD)
178 (double-array nums) TransformType/FORWARD)))
177 179
178 (def indexed (partial map-indexed vector)) 180 (def indexed (partial map-indexed vector))
181
182 (defn max-indexed [s]
183 (first (sort-by (comp - second) (indexed s))))
184
179 185
180 (defn wiggling? 186 (defn wiggling?
181 "Is the worm wiggling?" 187 "Is the worm wiggling?"
182 [experiences] 188 [experiences]
183 (map (comp first :muscle) (vector:last-n experiences 200)) 189 (let [analysis-interval 0x40]
184 190 (when (> (count experiences) analysis-interval)
185 ) 191 (let [a-flex 3
192 a-ex 2
193 muscle-activity
194 (map :muscle (vector:last-n experiences analysis-interval))
195 base-activity
196 (map #(- (% a-flex) (% a-ex)) muscle-activity)]
197 (= 2
198 (first
199 (max-indexed (map #(Math/abs %) (take 20 (fft base-activity))))))))))
200
201 ;; (println-repl
202 ;; (apply format "%d %.2f"
203 ;; (first (sort-by
204 ;; (comp - second)
205 ;; (indexed (take 20 ))))))))))
206
207 ;; (println-repl
208 ;; (apply
209 ;; format
210 ;; (apply str (repeat analysis-interval "%5.1f"))
211 ;; (fft base-activity)))
212
213 ;; ;;(println-repl (last base-activity))
214 ;; )))
215
216
186 217
187 (def standard-world-view 218 (def standard-world-view
188 [(Vector3f. 4.207176, -3.7366982, 3.0816958) 219 [(Vector3f. 4.207176, -3.7366982, 3.0816958)
189 (Quaternion. 0.11118768, 0.87678415, 0.24434438, -0.3989771)]) 220 (Quaternion. 0.11118768, 0.87678415, 0.24434438, -0.3989771)])
190 221
247 (speed-up world) 278 (speed-up world)
248 (light-up-everything world)) 279 (light-up-everything world))
249 (fn [world tpf] 280 (fn [world tpf]
250 (if (and end-frame (> (.getTime timer) end-frame)) 281 (if (and end-frame (> (.getTime timer) end-frame))
251 (.stop world)) 282 (.stop world))
252 (let [muscle-data (motor-control muscles) 283 (let [muscle-data (vec (motor-control muscles))
253 proprioception-data (prop) 284 proprioception-data (prop)
254 touch-data (map #(% (.getRootNode world)) touch)] 285 touch-data (map #(% (.getRootNode world)) touch)]
255 (when experiences 286 (when experiences
256 (record-experience! 287 (record-experience!
257 experiences {:touch touch-data 288 experiences {:touch touch-data
260 ;;(if (curled? @experiences) (println "Curled")) 291 ;;(if (curled? @experiences) (println "Curled"))
261 ;;(if (straight? @experiences) (println "Straight")) 292 ;;(if (straight? @experiences) (println "Straight"))
262 ;; (println-repl 293 ;; (println-repl
263 ;; (apply format "%.2f %.2f %.2f %.2f %.2f\n" 294 ;; (apply format "%.2f %.2f %.2f %.2f %.2f\n"
264 ;; (map contact touch-data))) 295 ;; (map contact touch-data)))
265 296 (wiggling? @experiences)
266 ) 297 )
267 (muscle-display 298 (muscle-display
268 muscle-data 299 muscle-data
269 (if record (dir! (File. record "muscle")))) 300 (if record (dir! (File. record "muscle"))))
270 (prop-display 301 (prop-display