Mercurial > cortex
view org/worm_learn.clj @ 408:3b4012b42611
completed demonstration showing automatic partitioning of touch space based on experience.
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Tue, 18 Mar 2014 21:28:04 -0400 |
parents | bd6d03596ea8 |
children | e6a7e80f885a |
line wrap: on
line source
1 (ns org.aurellem.worm-learn2 "General worm creation framework."3 {:author "Robert McIntyre"}4 (:use (cortex world util import body sense5 hearing touch vision proprioception movement6 test))7 (:import (com.jme3.math ColorRGBA Vector3f))8 (:import java.io.File)9 (:import com.jme3.audio.AudioNode)10 (:import com.aurellem.capture.RatchetTimer)11 (:import (com.aurellem.capture Capture IsoTimer))12 (:import (com.jme3.math Vector3f ColorRGBA)))14 (use 'clojure.pprint)15 (use 'clojure.set)16 (dorun (cortex.import/mega-import-jme3))17 (rlm.rlm-commands/help)19 (load-bullet)21 (def hand "Models/test-creature/hand.blend")23 (defn worm-model []24 (load-blender-model "Models/worm/worm.blend"))26 (def output-base (File. "/home/r/proj/cortex/render/worm-learn/curl"))29 (defn motor-control-program30 "Create a function which will execute the motor script"31 [muscle-labels32 script]33 (let [current-frame (atom -1)34 keyed-script (group-by first script)35 current-forces (atom {}) ]36 (fn [effectors]37 (let [indexed-effectors (vec effectors)]38 (dorun39 (for [[_ part force] (keyed-script (swap! current-frame inc))]40 (swap! current-forces (fn [m] (assoc m part force)))))41 (doall (map (fn [effector power]42 (effector (int power)))43 effectors44 (map #(@current-forces % 0) muscle-labels)))))))46 (defn worm-direct-control47 "Create keybindings and a muscle control program that will enable48 the user to control the worm via the keyboard."49 [muscle-labels activation-strength]50 (let [strengths (mapv (fn [_] (atom 0)) muscle-labels)51 activator52 (fn [n]53 (fn [world pressed?]54 (let [strength (if pressed? activation-strength 0)]55 (swap! (nth strengths n) (constantly strength)))))56 activators57 (map activator (range (count muscle-labels)))58 worm-keys59 ["key-f" "key-r"60 "key-g" "key-t"61 "key-y" "key-h"62 "key-j" "key-u"63 "key-i" "key-k"64 "key-o" "key-l"]]65 {:motor-control66 (fn [effectors]67 (doall68 (map (fn [strength effector]69 (effector (deref strength)))70 strengths effectors)))71 :keybindings72 ;; assume muscles are listed in pairs and map them to keys.73 (zipmap worm-keys activators)}))75 ;; These are scripts that direct the worm to move in two radically76 ;; different patterns -- a sinusoidal wiggling motion, and a curling77 ;; motions that causes the worm to form a circle.79 (def curl-script80 [[370 :d-up 40]81 [600 :d-up 0]])83 (def period 18)85 (def worm-muscle-labels86 [:base-up :base-down87 :a-down :a-up88 :b-up :b-down89 :c-down :c-up90 :d-up :d-down])92 (defn gen-wiggle [[flexor extensor :as muscle-pair] time-base]93 (let [period period94 power 45]95 [[time-base flexor power]96 [(+ time-base period) flexor 0]97 [(+ time-base period 1) extensor power]98 [(+ time-base (+ (* 2 period) 2)) extensor 0]]))100 (def wiggle-script101 (mapcat gen-wiggle (repeat 4000 [:a-down :a-up])102 (range 100 1000000 (+ 3 (* period 2)))))105 ;; Normally, we'd use unsupervised/supervised machine learning to pick106 ;; out the defining features of the different actions available to the107 ;; worm. For this project, I am going to explicitely define functions108 ;; that recognize curling and wiggling respectively. These functions109 ;; are defined using all the information available from an embodied110 ;; simulation of the action. Note how much easier they are to define111 ;; than if I only had vision to work with. Things like scale/position112 ;; invariance are complete non-issues here. This is the advantage of113 ;; body-centered action recognition and what I hope to show with this114 ;; thesis.117 (defn straight?118 "Is the worm straight?"119 [experiences]120 (every?121 (fn [[_ _ bend]]122 (< (Math/sin bend) 0.05))123 (:proprioception (peek experiences))))125 (defn curled?126 "Is the worm curled up?"127 [experiences]128 (every?129 (fn [[_ _ bend]]130 (> (Math/sin bend) 0.64))131 (:proprioception (peek experiences))))133 (defn grand-circle?134 "Does the worm form a majestic circle (one end touching the other)?"135 [experiences]136 (and (curled? experiences)137 true)) ;; TODO: add code here.139 (defn vector:last-n [v n]140 (let [c (count v)]141 (if (< c n) v142 (subvec v (- c n) c))))144 (defn touch-average [[coords touch]]145 (/ (average (map first touch)) (average (map second touch))))147 (def worm-segment-touch-bottom148 [[8 15] [8 16] [8 17] [8 18] [8 19] [8 20] [8 21] [8 22] [9 15]149 [9 16] [9 17] [9 18] [9 19] [9 20] [9 21] [9 22] [10 15] [10 16]150 [10 17] [10 18] [10 19] [10 20] [10 21] [10 22] [11 15] [11 16]151 [11 17] [11 18] [11 19] [11 20] [11 21] [11 22] [12 15] [12 16]152 [12 17] [12 18] [12 19] [12 20] [12 21] [12 22] [13 15] [13 16]153 [13 17] [13 18] [13 19] [13 20] [13 21] [13 22] [14 15] [14 16]154 [14 17] [14 18] [14 19] [14 20] [14 21] [14 22]])158 (defn floor-contact [[coords contact :as touch]]159 (let [raw-average160 (average161 (map162 first163 (vals164 (select-keys165 (zipmap coords contact)166 ))))]167 (Math/abs (- 1. (* 10 raw-average)))))170 (defn wiggling?171 "Is the worm wiggling?"172 [experiences]173 (vector:last-n experiences 200)175 )177 (def standard-world-view178 [(Vector3f. 4.207176, -3.7366982, 3.0816958)179 (Quaternion. 0.11118768, 0.87678415, 0.24434438, -0.3989771)])181 (def worm-side-view182 [(Vector3f. 4.207176, -3.7366982, 3.0816958)183 (Quaternion. -0.11555642, 0.88188726, -0.2854942, -0.3569518)])185 (def degenerate-worm-view186 [(Vector3f. -0.0708936, -8.570261, 2.6487997)187 (Quaternion. -2.318909E-4, 0.9985348, 0.053941682, 0.004291452)])189 (defn worm-world-defaults []190 (let [direct-control (worm-direct-control worm-muscle-labels 40)]191 {:view worm-side-view192 :motor-control (:motor-control direct-control)193 :keybindings (:keybindings direct-control)194 :record nil195 :experiences nil196 :worm-model worm-model197 :end-frame nil}))200 (defn dir! [file]201 (if (not (.exists file))202 (.mkdir file))203 file)205 (defn record-experience! [experiences data]206 (swap! experiences #(conj % data)))208 (defn worm-world209 [& {:keys [record motor-control keybindings view experiences210 worm-model end-frame] :as settings}]211 (let [{:keys [record motor-control keybindings view experiences212 worm-model end-frame]}213 (merge (worm-world-defaults) settings)214 worm (doto (worm-model) (body!))215 touch (touch! worm)216 prop (proprioception! worm)217 muscles (movement! worm)219 touch-display (view-touch)220 prop-display (view-proprioception)221 muscle-display (view-movement)223 floor (box 10 1 10 :position (Vector3f. 0 -10 0)224 :color ColorRGBA/Gray :mass 0)225 timer (IsoTimer. 60)]227 (world228 (nodify [worm floor])229 (merge standard-debug-controls keybindings)230 (fn [world]231 (position-camera world view)232 (.setTimer world timer)233 (display-dilated-time world timer)234 (if record235 (Capture/captureVideo236 world237 (dir! (File. record "main-view"))))238 (speed-up world)239 (light-up-everything world))240 (fn [world tpf]241 (if (> (.getTime timer) end-frame)242 (.stop world))243 (let [muscle-data (motor-control muscles)244 proprioception-data (prop)245 touch-data (map #(% (.getRootNode world)) touch)]246 (when experiences247 (record-experience!248 experiences {:touch touch-data249 :proprioception proprioception-data250 :muscle muscle-data})251 ;;(if (curled? @experiences) (println "Curled"))252 ;;(if (straight? @experiences) (println "Straight"))253 ;; (println-repl254 ;; (apply format "%.2f %.2f %.2f %.2f %.2f\n"255 ;; (map floor-contact touch-data)))257 )258 (muscle-display259 muscle-data260 (if record (dir! (File. record "muscle"))))261 (prop-display262 proprioception-data263 (if record (dir! (File. record "proprio"))))264 (touch-display265 touch-data266 (if record (dir! (File. record "touch")))))))))269 ;; A demonstration of self organiging touch maps through experience.271 (def single-worm-segment-view272 [(Vector3f. 2.0681207, -6.1406755, 1.6106138)273 (Quaternion. -0.15558705, 0.843615, -0.3428654, -0.38281822)])275 (def worm-single-segment-muscle-labels276 [:lift-1 :lift-2 :roll-1 :roll-2])278 (defn touch-kinesthetics []279 [[170 :lift-1 40]280 [190 :lift-1 20]281 [206 :lift-1 0]283 [400 :lift-2 40]284 [410 :lift-2 0]286 [570 :lift-2 40]287 [590 :lift-2 20]288 [606 :lift-2 0]290 [800 :lift-1 30]291 [809 :lift-1 0]293 [900 :roll-2 40]294 [905 :roll-2 20]295 [910 :roll-2 0]297 [1000 :roll-2 40]298 [1005 :roll-2 20]299 [1010 :roll-2 0]301 [1100 :roll-2 40]302 [1105 :roll-2 20]303 [1110 :roll-2 0]304 ])306 (defn single-worm-segment []307 (load-blender-model "Models/worm/worm-single-segment.blend"))309 (defn worm-segment-defaults []310 (let [direct-control (worm-direct-control worm-muscle-labels 40)]311 (merge (worm-world-defaults)312 {:worm-model single-worm-segment313 :view single-worm-segment-view314 :motor-control315 (motor-control-program316 worm-single-segment-muscle-labels317 (touch-kinesthetics))318 :end-frame 1200})))320 (def full-contact [(float 0.0) (float 0.1)])322 (defn pure-touch?323 "This is worm specific code to determine if a large region of touch324 sensors is either all on or all off."325 [[coords touch :as touch-data]]326 (= (set (map first touch)) (set full-contact)))328 (defn remove-similar329 [coll]330 (loop [result () coll (sort-by (comp - count) coll)]331 (if (empty? coll) result332 (let [x (first coll)333 xs (rest coll)334 c (count x)]335 (if (some336 (fn [other-set]337 (let [oc (count other-set)]338 (< (- (count (union other-set x)) c) (* oc 0.1))))339 xs)340 (recur result xs)341 (recur (cons x result) xs))))))344 (defn rect-region [[x0 y0] [x1 y1]]345 (vec346 (for [x (range x0 (inc x1))347 y (range y0 (inc y1))]348 [x y])))350 (def all-touch-coordinates351 (concat352 (rect-region [0 15] [7 22])353 (rect-region [8 0] [14 29])354 (rect-region [15 15] [22 22])))356 (defn view-touch-region [coords]357 (let [touched-region358 (reduce359 (fn [m k]360 (assoc m k [0.0 0.1]))361 (zipmap all-touch-coordinates (repeat [0.1 0.1])) coords)362 data363 [[(vec (keys touched-region)) (vec (vals touched-region))]]364 touch-display (view-touch)]365 (touch-display data)366 (touch-display data)))368 (defn learn-touch-regions []369 (let [experiences (atom [])370 world (apply-map371 worm-world372 (assoc (worm-segment-defaults)373 :experiences experiences))]374 (run-world world)375 (->>376 @experiences377 (drop 175)378 ;; access the single segment's touch data379 (map (comp first :touch))380 ;; only deal with "pure" touch data to determine surfaces381 (filter pure-touch?)382 ;; associate coordinates with touch values383 (map (partial apply zipmap))384 ;; select those regions where contact is being made385 (map (partial group-by second))386 (map #(get % full-contact))387 (map (partial map first))388 ;; remove redundant/subset regions389 (map set)390 remove-similar)))392 (defn learn-and-view-touch-regions []393 (map view-touch-region394 (learn-touch-regions)))