diff org/worm_learn.clj @ 405:9b4a4da08b78

worming on stream predicates.
author Robert McIntyre <rlm@mit.edu>
date Mon, 17 Mar 2014 20:52:16 -0400
parents 939bcc5950b2
children 40b67bb71430
line wrap: on
line diff
     1.1 --- a/org/worm_learn.clj	Mon Mar 17 17:29:59 2014 -0400
     1.2 +++ b/org/worm_learn.clj	Mon Mar 17 20:52:16 2014 -0400
     1.3 @@ -111,29 +111,45 @@
     1.4  ;; body-centered action recognition and what I hope to show with this
     1.5  ;; thesis.
     1.6  
     1.7 -(defn last-nth
     1.8 -  "Create function that will, when called each frame with the senses
     1.9 -   of a creature, will record those results and return the last n
    1.10 -   results."
    1.11 -  [n]
    1.12 -  (let [last-n  '()]
    1.13 -    (fn [frame-num {:keys [touch proprioception muscles hearing]}]
    1.14 -      (take n (cons [frame-num :stuff] last-n)))))
    1.15 -      
    1.16 +
    1.17 +(defn straight?
    1.18 +  "Is the worm straight?"
    1.19 +  [experiences]
    1.20 +  (every?
    1.21 +   (fn [[_ _ bend]]
    1.22 +     (< (Math/sin bend) 0.05))
    1.23 +   (:proprioception (peek experiences))))
    1.24 +
    1.25 +(defn curled?
    1.26 +  "Is the worm curled up?"
    1.27 +  [experiences]
    1.28 +  (every?
    1.29 +   (fn [[_ _ bend]]
    1.30 +     (> (Math/sin bend) 0.64))
    1.31 +   (:proprioception (peek experiences))))
    1.32 +
    1.33 +(defn grand-circle?
    1.34 +  "Does the worm form a majestic circle (one end touching the other)?"
    1.35 +  [experiences]
    1.36 +  (and (curled? experiences)
    1.37 +       true))
    1.38 +
    1.39 +(defn vector:last-n [v n]
    1.40 +  (let [c (count v)]
    1.41 +    (if (< c n) v
    1.42 +        (subvec v (- c n) c))))
    1.43  
    1.44  (defn wiggling?
    1.45 -  "Generate a function which, when called each frame with the sensory
    1.46 -   inputs of a worm, will determine whether the worm is wiggling."
    1.47 -  [{:keys [touch proprioception muscles hearing]}]
    1.48 -  (map (fn [f] (f)) proprioception
    1.49 -   
    1.50 +  "Is the worm wiggling?"
    1.51 +  [experiences]
    1.52 +  (vector:last-n
    1.53 +  
    1.54  
    1.55  ))
    1.56  
    1.57 -(defn curling?
    1.58 -  "Is the worm curled up?"
    1.59 -  []
    1.60 -  )
    1.61 +
    1.62 +
    1.63 +
    1.64  
    1.65  (defn resting?
    1.66    "Is the worm on the ground in a neutral position?"
    1.67 @@ -156,15 +172,19 @@
    1.68      {:view worm-side-view
    1.69       :motor-control (:motor-control direct-control)
    1.70       :keybindings (:keybindings direct-control)
    1.71 -     :record nil}))
    1.72 +     :record nil
    1.73 +     :experiences nil}))
    1.74  
    1.75  (defn dir! [file]
    1.76    (if (not (.exists file))
    1.77      (.mkdir file))
    1.78    file)
    1.79 -  
    1.80 +
    1.81 +(defn record-experience! [experiences data]
    1.82 +  (swap! experiences #(conj % data)))
    1.83 +
    1.84  (defn worm-world
    1.85 -  [& {:keys [record motor-control keybindings view] :as settings}]
    1.86 +  [& {:keys [record motor-control keybindings view experiences] :as settings}]
    1.87    (let [{:keys [record motor-control keybindings view]}
    1.88          (merge (worm-world-defaults) settings)
    1.89          worm (doto (worm-model) (body!))
    1.90 @@ -194,17 +214,23 @@
    1.91           (speed-up world)
    1.92           (light-up-everything world))
    1.93         (fn [world tpf]
    1.94 -         (let [strong! (motor-control muscles)]
    1.95 -           (println strong!)
    1.96 +         (let [muscle-data (motor-control muscles)
    1.97 +               proprioception-data (prop)
    1.98 +               touch-data (map #(% (.getRootNode world)) touch)]
    1.99 +           (when experiences
   1.100 +             (record-experience!
   1.101 +              experiences {:touch touch-data
   1.102 +                           :proprioception proprioception-data
   1.103 +                           :muscle muscle-data})
   1.104 +             (if (curled? @experiences) (println "Curled"))
   1.105 +             (if (straight? @experiences)    (println "Straight"))
   1.106 +             )
   1.107             (muscle-display
   1.108 -            strong!
   1.109 -            (if record (dir! (File. record "muscle")))))
   1.110 -         (prop-display
   1.111 -          (prop)
   1.112 -          (if record (dir! (File. record "proprio"))))
   1.113 -         (touch-display 
   1.114 -          (map #(% (.getRootNode world)) touch)
   1.115 -          (if record
   1.116 -            (File. record "touch")))))))
   1.117 -
   1.118 -   
   1.119 \ No newline at end of file
   1.120 +            muscle-data
   1.121 +            (if record (dir! (File. record "muscle"))))
   1.122 +           (prop-display
   1.123 +            proprioception-data
   1.124 +            (if record (dir! (File. record "proprio"))))
   1.125 +           (touch-display 
   1.126 +            touch-data
   1.127 +            (if record (dir! (File. record "touch")))))))))