annotate org/worm_learn.clj @ 526:01934317b25b

changes from conference.
author Robert McIntyre <rlm@mit.edu>
date Mon, 21 Apr 2014 02:11:29 -0400
parents ced955c3c84f
children 0b891e0dd809
rev   line source
rlm@394 1 (ns org.aurellem.worm-learn
rlm@394 2 "General worm creation framework."
rlm@394 3 {:author "Robert McIntyre"}
rlm@394 4 (:use (cortex world util import body sense
rlm@408 5 hearing touch vision proprioception movement
rlm@408 6 test))
rlm@394 7 (:import (com.jme3.math ColorRGBA Vector3f))
rlm@394 8 (:import java.io.File)
rlm@394 9 (:import com.jme3.audio.AudioNode)
rlm@397 10 (:import com.aurellem.capture.RatchetTimer)
rlm@397 11 (:import (com.aurellem.capture Capture IsoTimer))
rlm@397 12 (:import (com.jme3.math Vector3f ColorRGBA)))
rlm@406 13
rlm@413 14 (import org.apache.commons.math3.transform.TransformType)
rlm@413 15 (import org.apache.commons.math3.transform.FastFourierTransformer)
rlm@413 16 (import org.apache.commons.math3.transform.DftNormalization)
rlm@413 17
rlm@406 18 (use 'clojure.pprint)
rlm@408 19 (use 'clojure.set)
rlm@394 20 (dorun (cortex.import/mega-import-jme3))
rlm@394 21 (rlm.rlm-commands/help)
rlm@394 22
rlm@400 23 (load-bullet)
rlm@394 24
rlm@399 25 (def hand "Models/test-creature/hand.blend")
rlm@394 26
rlm@399 27 (defn worm-model []
rlm@399 28 (load-blender-model "Models/worm/worm.blend"))
rlm@394 29
rlm@449 30 (defn worm []
rlm@449 31 (let [model (load-blender-model "Models/worm/worm.blend")]
rlm@449 32 {:body (doto model (body!))
rlm@449 33 :touch (touch! model)
rlm@449 34 :proprioception (proprioception! model)
rlm@449 35 :muscles (movement! model)}))
rlm@449 36
rlm@451 37 (defn worm* []
rlm@451 38 (let [model (load-blender-model "Models/worm/worm-of-the-imagination.blend")]
rlm@451 39 {:body (doto model (body!))
rlm@451 40 :touch (touch! model)
rlm@451 41 :proprioception (proprioception! model)
rlm@451 42 :muscles (movement! model)}))
rlm@451 43
rlm@451 44
rlm@400 45 (def output-base (File. "/home/r/proj/cortex/render/worm-learn/curl"))
rlm@394 46
rlm@397 47
rlm@399 48 (defn motor-control-program
rlm@399 49 "Create a function which will execute the motor script"
rlm@406 50 [muscle-labels
rlm@399 51 script]
rlm@399 52 (let [current-frame (atom -1)
rlm@399 53 keyed-script (group-by first script)
rlm@399 54 current-forces (atom {}) ]
rlm@399 55 (fn [effectors]
rlm@399 56 (let [indexed-effectors (vec effectors)]
rlm@399 57 (dorun
rlm@399 58 (for [[_ part force] (keyed-script (swap! current-frame inc))]
rlm@399 59 (swap! current-forces (fn [m] (assoc m part force)))))
rlm@399 60 (doall (map (fn [effector power]
rlm@399 61 (effector (int power)))
rlm@399 62 effectors
rlm@406 63 (map #(@current-forces % 0) muscle-labels)))))))
rlm@397 64
rlm@404 65 (defn worm-direct-control
rlm@404 66 "Create keybindings and a muscle control program that will enable
rlm@404 67 the user to control the worm via the keyboard."
rlm@404 68 [muscle-labels activation-strength]
rlm@404 69 (let [strengths (mapv (fn [_] (atom 0)) muscle-labels)
rlm@404 70 activator
rlm@404 71 (fn [n]
rlm@404 72 (fn [world pressed?]
rlm@404 73 (let [strength (if pressed? activation-strength 0)]
rlm@404 74 (swap! (nth strengths n) (constantly strength)))))
rlm@404 75 activators
rlm@404 76 (map activator (range (count muscle-labels)))
rlm@404 77 worm-keys
rlm@404 78 ["key-f" "key-r"
rlm@404 79 "key-g" "key-t"
rlm@413 80 "key-h" "key-y"
rlm@404 81 "key-j" "key-u"
rlm@413 82 "key-k" "key-i"
rlm@413 83 "key-l" "key-o"]]
rlm@404 84 {:motor-control
rlm@404 85 (fn [effectors]
rlm@404 86 (doall
rlm@404 87 (map (fn [strength effector]
rlm@404 88 (effector (deref strength)))
rlm@404 89 strengths effectors)))
rlm@404 90 :keybindings
rlm@404 91 ;; assume muscles are listed in pairs and map them to keys.
rlm@404 92 (zipmap worm-keys activators)}))
rlm@400 93
rlm@400 94 ;; These are scripts that direct the worm to move in two radically
rlm@400 95 ;; different patterns -- a sinusoidal wiggling motion, and a curling
rlm@400 96 ;; motions that causes the worm to form a circle.
rlm@400 97
rlm@400 98 (def curl-script
rlm@415 99 [[150 :d-flex 40]
rlm@415 100 [250 :d-flex 0]])
rlm@400 101
rlm@400 102 (def period 18)
rlm@400 103
rlm@404 104 (def worm-muscle-labels
rlm@414 105 [:base-ex :base-flex
rlm@414 106 :a-ex :a-flex
rlm@414 107 :b-ex :b-flex
rlm@414 108 :c-ex :c-flex
rlm@414 109 :d-ex :d-flex])
rlm@399 110
rlm@399 111 (defn gen-wiggle [[flexor extensor :as muscle-pair] time-base]
rlm@399 112 (let [period period
rlm@399 113 power 45]
rlm@399 114 [[time-base flexor power]
rlm@399 115 [(+ time-base period) flexor 0]
rlm@399 116 [(+ time-base period 1) extensor power]
rlm@399 117 [(+ time-base (+ (* 2 period) 2)) extensor 0]]))
rlm@399 118
rlm@399 119 (def wiggle-script
rlm@414 120 (mapcat gen-wiggle (repeat 4000 [:a-ex :a-flex])
rlm@406 121 (range 100 1000000 (+ 3 (* period 2)))))
rlm@399 122
rlm@399 123
rlm@415 124 (defn shift-script [shift script]
rlm@415 125 (map (fn [[time label power]] [(+ time shift) label power])
rlm@415 126 script))
rlm@415 127
rlm@415 128 (def do-all-the-things
rlm@415 129 (concat
rlm@415 130 curl-script
rlm@415 131 [[300 :d-ex 40]
rlm@415 132 [320 :d-ex 0]]
rlm@415 133 (shift-script 280 (take 16 wiggle-script))))
rlm@415 134
rlm@400 135 ;; Normally, we'd use unsupervised/supervised machine learning to pick
rlm@400 136 ;; out the defining features of the different actions available to the
rlm@400 137 ;; worm. For this project, I am going to explicitely define functions
rlm@400 138 ;; that recognize curling and wiggling respectively. These functions
rlm@400 139 ;; are defined using all the information available from an embodied
rlm@400 140 ;; simulation of the action. Note how much easier they are to define
rlm@400 141 ;; than if I only had vision to work with. Things like scale/position
rlm@400 142 ;; invariance are complete non-issues here. This is the advantage of
rlm@400 143 ;; body-centered action recognition and what I hope to show with this
rlm@400 144 ;; thesis.
rlm@400 145
rlm@405 146
rlm@415 147 ;; curled? relies on proprioception, resting? relies on touch,
rlm@415 148 ;; wiggling? relies on a fourier analysis of muscle contraction, and
rlm@415 149 ;; grand-circle? relies on touch and reuses curled? as a gaurd.
rlm@405 150
rlm@405 151 (defn curled?
rlm@405 152 "Is the worm curled up?"
rlm@405 153 [experiences]
rlm@405 154 (every?
rlm@405 155 (fn [[_ _ bend]]
rlm@405 156 (> (Math/sin bend) 0.64))
rlm@405 157 (:proprioception (peek experiences))))
rlm@405 158
rlm@411 159 (defn rect-region [[x0 y0] [x1 y1]]
rlm@411 160 (vec
rlm@411 161 (for [x (range x0 (inc x1))
rlm@411 162 y (range y0 (inc y1))]
rlm@411 163 [x y])))
rlm@407 164
rlm@415 165 (def worm-segment-bottom (rect-region [8 15] [14 22]))
rlm@407 166
rlm@411 167 (defn contact
rlm@411 168 "Determine how much contact a particular worm segment has with
rlm@411 169 other objects. Returns a value between 0 and 1, where 1 is full
rlm@411 170 contact and 0 is no contact."
rlm@415 171 [touch-region [coords contact :as touch]]
rlm@411 172 (-> (zipmap coords contact)
rlm@415 173 (select-keys touch-region)
rlm@411 174 (vals)
rlm@411 175 (#(map first %))
rlm@411 176 (average)
rlm@411 177 (* 10)
rlm@411 178 (- 1)
rlm@411 179 (Math/abs)))
rlm@406 180
rlm@415 181 (defn resting?
rlm@443 182 "Is the worm resting on the ground?"
rlm@415 183 [experiences]
rlm@415 184 (every?
rlm@415 185 (fn [touch-data]
rlm@415 186 (< 0.9 (contact worm-segment-bottom touch-data)))
rlm@415 187 (:touch (peek experiences))))
rlm@415 188
rlm@415 189 (defn vector:last-n [v n]
rlm@415 190 (let [c (count v)]
rlm@415 191 (if (< c n) v
rlm@415 192 (subvec v (- c n) c))))
rlm@415 193
rlm@413 194 (defn fft [nums]
rlm@414 195 (map
rlm@414 196 #(.getReal %)
rlm@414 197 (.transform
rlm@414 198 (FastFourierTransformer. DftNormalization/STANDARD)
rlm@414 199 (double-array nums) TransformType/FORWARD)))
rlm@413 200
rlm@413 201 (def indexed (partial map-indexed vector))
rlm@413 202
rlm@414 203 (defn max-indexed [s]
rlm@414 204 (first (sort-by (comp - second) (indexed s))))
rlm@414 205
rlm@400 206 (defn wiggling?
rlm@405 207 "Is the worm wiggling?"
rlm@405 208 [experiences]
rlm@451 209 (let [analysis-interval 96]
rlm@414 210 (when (> (count experiences) analysis-interval)
rlm@414 211 (let [a-flex 3
rlm@414 212 a-ex 2
rlm@414 213 muscle-activity
rlm@414 214 (map :muscle (vector:last-n experiences analysis-interval))
rlm@414 215 base-activity
rlm@451 216 (map #(- (% a-flex) (% a-ex)) muscle-activity)
rlm@451 217 accept?
rlm@451 218 (fn [activity]
rlm@451 219 (->> activity (fft) (take 20) (map #(Math/abs %))
rlm@451 220 (max-indexed) (first) (<= 2)))]
rlm@451 221 (or (accept? (take 64 base-activity))
rlm@451 222 (accept? (take 64 (drop 20 base-activity))))))))
rlm@451 223
rlm@451 224
rlm@414 225
rlm@415 226 (def worm-segment-bottom-tip (rect-region [15 15] [22 22]))
rlm@414 227
rlm@415 228 (def worm-segment-top-tip (rect-region [0 15] [7 22]))
rlm@414 229
rlm@415 230 (defn grand-circle?
rlm@415 231 "Does the worm form a majestic circle (one end touching the other)?"
rlm@415 232 [experiences]
rlm@420 233 (and (curled? experiences)
rlm@415 234 (let [worm-touch (:touch (peek experiences))
rlm@415 235 tail-touch (worm-touch 0)
rlm@415 236 head-touch (worm-touch 4)]
rlm@451 237 (and (< 0.1 (contact worm-segment-bottom-tip tail-touch))
rlm@451 238 (< 0.1 (contact worm-segment-top-tip head-touch))))))
rlm@400 239
rlm@418 240
rlm@449 241 (declare phi-space phi-scan debug-experience)
rlm@418 242
rlm@418 243
rlm@418 244
rlm@400 245 (def standard-world-view
rlm@400 246 [(Vector3f. 4.207176, -3.7366982, 3.0816958)
rlm@400 247 (Quaternion. 0.11118768, 0.87678415, 0.24434438, -0.3989771)])
rlm@400 248
rlm@400 249 (def worm-side-view
rlm@400 250 [(Vector3f. 4.207176, -3.7366982, 3.0816958)
rlm@400 251 (Quaternion. -0.11555642, 0.88188726, -0.2854942, -0.3569518)])
rlm@400 252
rlm@400 253 (def degenerate-worm-view
rlm@400 254 [(Vector3f. -0.0708936, -8.570261, 2.6487997)
rlm@400 255 (Quaternion. -2.318909E-4, 0.9985348, 0.053941682, 0.004291452)])
rlm@399 256
rlm@404 257 (defn worm-world-defaults []
rlm@404 258 (let [direct-control (worm-direct-control worm-muscle-labels 40)]
rlm@430 259 (merge direct-control
rlm@430 260 {:view worm-side-view
rlm@430 261 :record nil
rlm@430 262 :experiences (atom [])
rlm@430 263 :experience-watch debug-experience
rlm@451 264 :worm worm
rlm@430 265 :end-frame nil})))
rlm@407 266
rlm@404 267 (defn dir! [file]
rlm@410 268 (if-not (.exists file)
rlm@404 269 (.mkdir file))
rlm@404 270 file)
rlm@405 271
rlm@405 272 (defn record-experience! [experiences data]
rlm@405 273 (swap! experiences #(conj % data)))
rlm@405 274
rlm@444 275 (defn enable-shadows [world]
rlm@444 276 (let [bsr (doto
rlm@444 277 (BasicShadowRenderer. (asset-manager) 512)
rlm@444 278 (.setDirection (.normalizeLocal (Vector3f. 1 -1 -1))))]
rlm@444 279 (.addProcessor (.getViewPort world) bsr)))
rlm@443 280
rlm@444 281 (defn enable-good-shadows [world]
rlm@444 282 (let [pssm
rlm@444 283 (doto (PssmShadowRenderer. (asset-manager) 1024 3)
rlm@444 284 (.setDirection (.normalizeLocal (Vector3f. -1 -3 -1)))
rlm@444 285 (.setLambda (float 0.55))
rlm@444 286 (.setShadowIntensity (float 0.6))
rlm@444 287 (.setCompareMode PssmShadowRenderer$CompareMode/Software)
rlm@444 288 (.setFilterMode PssmShadowRenderer$FilterMode/Bilinear))]
rlm@444 289 (.addProcessor (.getViewPort world) pssm)))
rlm@444 290
rlm@449 291 (defn debug-experience
rlm@449 292 [experiences text]
rlm@449 293 (cond
rlm@449 294 (grand-circle? experiences) (.setText text "Grand Circle")
rlm@449 295 (curled? experiences) (.setText text "Curled")
rlm@449 296 (wiggling? experiences) (.setText text "Wiggling")
rlm@451 297 (resting? experiences) (.setText text "Resting")
rlm@451 298 :else (.setText text "Unknown")))
rlm@443 299
rlm@445 300
rlm@399 301 (defn worm-world
rlm@451 302 [& {:keys [record motor-control keybindings view experiences
rlm@451 303 worm end-frame experience-watch] :as settings}]
rlm@407 304 (let [{:keys [record motor-control keybindings view experiences
rlm@451 305 worm end-frame experience-watch]}
rlm@404 306 (merge (worm-world-defaults) settings)
rlm@449 307
rlm@404 308 touch-display (view-touch)
rlm@404 309 prop-display (view-proprioception)
rlm@404 310 muscle-display (view-movement)
rlm@449 311 {:keys [proprioception touch muscles body]} (worm)
rlm@404 312
rlm@444 313 floor
rlm@444 314 (box 5 1 5 :position (Vector3f. 0 -10 0)
rlm@444 315 :mass 0
rlm@444 316 :texture "Textures/aurellem.png"
rlm@444 317 :material "Common/MatDefs/Misc/Unshaded.j3md")
rlm@445 318 timer (IsoTimer. 60)
rlm@445 319
rlm@445 320 font (.loadFont (asset-manager) "Interface/Fonts/Console.fnt")
rlm@445 321 worm-action (doto (BitmapText. font false)
rlm@445 322 (.setSize 35)
rlm@445 323 (.setColor (ColorRGBA/Black)))]
rlm@399 324
rlm@404 325 (world
rlm@449 326 (nodify [body floor])
rlm@404 327 (merge standard-debug-controls keybindings)
rlm@404 328 (fn [world]
rlm@445 329 (.setLocalTranslation
rlm@445 330 worm-action 20 470 0)
rlm@445 331 (.attachChild (.getGuiNode world) worm-action)
rlm@445 332
rlm@444 333 (enable-good-shadows world)
rlm@449 334 (.setShadowMode body RenderQueue$ShadowMode/CastAndReceive)
rlm@444 335 (.setShadowMode floor RenderQueue$ShadowMode/Receive)
rlm@444 336
rlm@444 337 (.setBackgroundColor (.getViewPort world) (ColorRGBA/White))
rlm@443 338 (.setDisplayStatView world false)
rlm@443 339 (.setDisplayFps world false)
rlm@404 340 (position-camera world view)
rlm@407 341 (.setTimer world timer)
rlm@449 342 ;;(display-dilated-time world timer)
rlm@430 343 (when record
rlm@445 344 (dir! record)
rlm@404 345 (Capture/captureVideo
rlm@404 346 world
rlm@404 347 (dir! (File. record "main-view"))))
rlm@451 348 (speed-up world 0.5)
rlm@444 349 ;;(light-up-everything world)
rlm@444 350 )
rlm@404 351 (fn [world tpf]
rlm@410 352 (if (and end-frame (> (.getTime timer) end-frame))
rlm@407 353 (.stop world))
rlm@414 354 (let [muscle-data (vec (motor-control muscles))
rlm@449 355 proprioception-data (proprioception)
rlm@415 356 touch-data (mapv #(% (.getRootNode world)) touch)]
rlm@405 357 (when experiences
rlm@405 358 (record-experience!
rlm@405 359 experiences {:touch touch-data
rlm@405 360 :proprioception proprioception-data
rlm@418 361 :muscle muscle-data}))
rlm@418 362 (when experience-watch
rlm@445 363 (experience-watch @experiences worm-action))
rlm@404 364 (muscle-display
rlm@405 365 muscle-data
rlm@430 366 (when record (dir! (File. record "muscle"))))
rlm@405 367 (prop-display
rlm@405 368 proprioception-data
rlm@430 369 (when record (dir! (File. record "proprio"))))
rlm@405 370 (touch-display
rlm@405 371 touch-data
rlm@430 372 (when record (dir! (File. record "touch")))))))))
rlm@407 373
rlm@407 374
rlm@407 375
rlm@416 376 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
rlm@416 377 ;;;;;;;; Phi-Space ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
rlm@416 378 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
rlm@416 379
rlm@416 380 (defn generate-phi-space []
rlm@416 381 (let [experiences (atom [])]
rlm@416 382 (run-world
rlm@416 383 (apply-map
rlm@416 384 worm-world
rlm@416 385 (merge
rlm@416 386 (worm-world-defaults)
rlm@416 387 {:end-frame 700
rlm@416 388 :motor-control
rlm@416 389 (motor-control-program worm-muscle-labels do-all-the-things)
rlm@416 390 :experiences experiences})))
rlm@416 391 @experiences))
rlm@416 392
rlm@416 393 (defn bin [digits]
rlm@416 394 (fn [angles]
rlm@416 395 (->> angles
rlm@416 396 (flatten)
rlm@416 397 (map (juxt #(Math/sin %) #(Math/cos %)))
rlm@416 398 (flatten)
rlm@416 399 (mapv #(Math/round (* % (Math/pow 10 (dec digits))))))))
rlm@416 400
rlm@418 401 ;; k-nearest neighbors with spatial binning. Only returns a result if
rlm@418 402 ;; the propriceptive data is within 10% of a previously recorded
rlm@418 403 ;; result in all dimensions.
rlm@416 404 (defn gen-phi-scan [phi-space]
rlm@416 405 (let [bin-keys (map bin [3 2 1])
rlm@416 406 bin-maps
rlm@417 407 (map (fn [bin-key]
rlm@417 408 (group-by
rlm@417 409 (comp bin-key :proprioception phi-space)
rlm@417 410 (range (count phi-space)))) bin-keys)
rlm@416 411 lookups (map (fn [bin-key bin-map]
rlm@416 412 (fn [proprio] (bin-map (bin-key proprio))))
rlm@416 413 bin-keys bin-maps)]
rlm@416 414 (fn lookup [proprio-data]
rlm@419 415 (set (some #(% proprio-data) lookups)))))
rlm@419 416
rlm@419 417
rlm@419 418 (defn longest-thread
rlm@419 419 "Find the longest thread from phi-index-sets. The index sets should
rlm@419 420 be ordered from most recent to least recent."
rlm@419 421 [phi-index-sets]
rlm@419 422 (loop [result '()
rlm@419 423 [thread-bases & remaining :as phi-index-sets] phi-index-sets]
rlm@419 424 (if (empty? phi-index-sets)
rlm@420 425 (vec result)
rlm@419 426 (let [threads
rlm@419 427 (for [thread-base thread-bases]
rlm@419 428 (loop [thread (list thread-base)
rlm@419 429 remaining remaining]
rlm@419 430 (let [next-index (dec (first thread))]
rlm@419 431 (cond (empty? remaining) thread
rlm@419 432 (contains? (first remaining) next-index)
rlm@419 433 (recur
rlm@419 434 (cons next-index thread) (rest remaining))
rlm@419 435 :else thread))))
rlm@419 436 longest-thread
rlm@419 437 (reduce (fn [thread-a thread-b]
rlm@419 438 (if (> (count thread-a) (count thread-b))
rlm@419 439 thread-a thread-b))
rlm@419 440 '(nil)
rlm@419 441 threads)]
rlm@419 442 (recur (concat longest-thread result)
rlm@419 443 (drop (count longest-thread) phi-index-sets))))))
rlm@419 444
rlm@416 445
rlm@416 446 (defn init []
rlm@416 447 (def phi-space (generate-phi-space))
rlm@416 448 (def phi-scan (gen-phi-scan phi-space))
rlm@416 449 )
rlm@418 450
rlm@420 451
rlm@420 452 (defn infer-nils
rlm@420 453 "Replace nils with the next available non-nil element in the
rlm@420 454 sequence, or barring that, 0."
rlm@420 455 [s]
rlm@430 456 (loop [i (dec (count s))
rlm@430 457 v (transient s)]
rlm@430 458 (if (zero? i) (persistent! v)
rlm@430 459 (if-let [cur (v i)]
rlm@430 460 (if (get v (dec i) 0)
rlm@430 461 (recur (dec i) v)
rlm@430 462 (recur (dec i) (assoc! v (dec i) cur)))
rlm@430 463 (recur i (assoc! v i 0))))))
rlm@420 464
rlm@420 465 ;; tests
rlm@420 466
rlm@420 467 ;;(infer-nils [1 nil 1 1]) [1 1 1 1]
rlm@420 468 ;;(infer-nils [1 1 1 nil]) [1 1 1 0]
rlm@420 469 ;;(infer-nils [nil 2 1 1]) [2 2 1 1]
rlm@420 470
rlm@420 471
rlm@451 472 (defn empathy-demonstration []
rlm@420 473 (let [proprio (atom ())]
rlm@420 474 (fn
rlm@451 475 [experiences text]
rlm@420 476 (let [phi-indices (phi-scan (:proprioception (peek experiences)))]
rlm@420 477 (swap! proprio (partial cons phi-indices))
rlm@420 478 (let [exp-thread (longest-thread (take 300 @proprio))
rlm@451 479 empathy (mapv phi-space (infer-nils exp-thread))]
rlm@420 480 (println-repl (vector:last-n exp-thread 22))
rlm@420 481 (cond
rlm@451 482 (grand-circle? empathy) (.setText text "Grand Circle")
rlm@451 483 (curled? empathy) (.setText text "Curled")
rlm@451 484 (wiggling? empathy) (.setText text "Wiggling")
rlm@451 485 (resting? empathy) (.setText text "Resting")
rlm@451 486 :else (.setText text "Unknown")))))))
rlm@420 487
rlm@420 488 (defn init-interactive []
rlm@420 489 (def phi-space
rlm@420 490 (let [experiences (atom [])]
rlm@420 491 (run-world
rlm@420 492 (apply-map
rlm@420 493 worm-world
rlm@420 494 (merge
rlm@420 495 (worm-world-defaults)
rlm@420 496 {:experiences experiences})))
rlm@420 497 @experiences))
rlm@420 498 (def phi-scan (gen-phi-scan phi-space)))
rlm@420 499
rlm@451 500 (defn empathy-experiment-1 [record]
rlm@451 501 (.start (worm-world :experience-watch (empathy-demonstration)
rlm@451 502 :record record :worm worm*)))
rlm@451 503
rlm@451 504
rlm@451 505 (def worm-action-label
rlm@451 506 (juxt grand-circle? curled? wiggling?))
rlm@451 507
rlm@451 508 (defn compare-empathy-with-baseline [accuracy]
rlm@451 509 (let [proprio (atom ())]
rlm@451 510 (fn
rlm@451 511 [experiences text]
rlm@451 512 (let [phi-indices (phi-scan (:proprioception (peek experiences)))]
rlm@451 513 (swap! proprio (partial cons phi-indices))
rlm@451 514 (let [exp-thread (longest-thread (take 300 @proprio))
rlm@451 515 empathy (mapv phi-space (infer-nils exp-thread))
rlm@451 516 experience-matches-empathy
rlm@451 517 (= (worm-action-label experiences)
rlm@451 518 (worm-action-label empathy))]
rlm@451 519 (cond
rlm@451 520 (grand-circle? empathy) (.setText text "Grand Circle")
rlm@451 521 (curled? empathy) (.setText text "Curled")
rlm@451 522 (wiggling? empathy) (.setText text "Wiggling")
rlm@451 523 (resting? empathy) (.setText text "Resting")
rlm@451 524 :else (.setText text "Unknown"))
rlm@451 525
rlm@451 526 (println-repl experience-matches-empathy)
rlm@451 527 (swap! accuracy #(conj % experience-matches-empathy)))))))
rlm@526 528
rlm@526 529 (defn empathy-experiment-2 []
rlm@526 530 (.start (worm-world :experience-watch (compare-empathy-with-baseline
rlm@526 531 (atom []))
rlm@526 532 :record false :worm worm*)))
rlm@526 533
rlm@451 534 (defn accuracy [v]
rlm@451 535 (float (/ (count (filter true? v)) (count v))))
rlm@451 536
rlm@451 537 (defn test-empathy-accuracy []
rlm@451 538 (let [res (atom [])]
rlm@451 539 (run-world
rlm@451 540 (worm-world :experience-watch
rlm@451 541 (compare-empathy-with-baseline res)
rlm@451 542 :worm worm*))
rlm@451 543 (accuracy @res)))
rlm@451 544
rlm@451 545
rlm@451 546
rlm@516 547 (defn dylan-collect-bolts [longest-threads index-sets]
rlm@516 548 (fn
rlm@516 549 [experiences text]
rlm@516 550 (let [phi-indices (phi-scan (:proprioception (peek experiences)))
rlm@516 551 long-thread (longest-thread (vector:last-n @index-sets 300))]
rlm@516 552 (swap! index-sets #(conj % phi-indices))
rlm@516 553 (swap! longest-threads #(conj % long-thread)))))
rlm@451 554
rlm@451 555
rlm@451 556