comparison 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
comparison
equal deleted inserted replaced
404:939bcc5950b2 405:9b4a4da08b78
109 ;; than if I only had vision to work with. Things like scale/position 109 ;; than if I only had vision to work with. Things like scale/position
110 ;; invariance are complete non-issues here. This is the advantage of 110 ;; invariance are complete non-issues here. This is the advantage of
111 ;; body-centered action recognition and what I hope to show with this 111 ;; body-centered action recognition and what I hope to show with this
112 ;; thesis. 112 ;; thesis.
113 113
114 (defn last-nth 114
115 "Create function that will, when called each frame with the senses 115 (defn straight?
116 of a creature, will record those results and return the last n 116 "Is the worm straight?"
117 results." 117 [experiences]
118 [n] 118 (every?
119 (let [last-n '()] 119 (fn [[_ _ bend]]
120 (fn [frame-num {:keys [touch proprioception muscles hearing]}] 120 (< (Math/sin bend) 0.05))
121 (take n (cons [frame-num :stuff] last-n))))) 121 (:proprioception (peek experiences))))
122 122
123 (defn curled?
124 "Is the worm curled up?"
125 [experiences]
126 (every?
127 (fn [[_ _ bend]]
128 (> (Math/sin bend) 0.64))
129 (:proprioception (peek experiences))))
130
131 (defn grand-circle?
132 "Does the worm form a majestic circle (one end touching the other)?"
133 [experiences]
134 (and (curled? experiences)
135 true))
136
137 (defn vector:last-n [v n]
138 (let [c (count v)]
139 (if (< c n) v
140 (subvec v (- c n) c))))
123 141
124 (defn wiggling? 142 (defn wiggling?
125 "Generate a function which, when called each frame with the sensory 143 "Is the worm wiggling?"
126 inputs of a worm, will determine whether the worm is wiggling." 144 [experiences]
127 [{:keys [touch proprioception muscles hearing]}] 145 (vector:last-n
128 (map (fn [f] (f)) proprioception 146
129
130 147
131 )) 148 ))
132 149
133 (defn curling? 150
134 "Is the worm curled up?" 151
135 [] 152
136 )
137 153
138 (defn resting? 154 (defn resting?
139 "Is the worm on the ground in a neutral position?" 155 "Is the worm on the ground in a neutral position?"
140 []) 156 [])
141 157
154 (defn worm-world-defaults [] 170 (defn worm-world-defaults []
155 (let [direct-control (worm-direct-control worm-muscle-labels 40)] 171 (let [direct-control (worm-direct-control worm-muscle-labels 40)]
156 {:view worm-side-view 172 {:view worm-side-view
157 :motor-control (:motor-control direct-control) 173 :motor-control (:motor-control direct-control)
158 :keybindings (:keybindings direct-control) 174 :keybindings (:keybindings direct-control)
159 :record nil})) 175 :record nil
176 :experiences nil}))
160 177
161 (defn dir! [file] 178 (defn dir! [file]
162 (if (not (.exists file)) 179 (if (not (.exists file))
163 (.mkdir file)) 180 (.mkdir file))
164 file) 181 file)
165 182
183 (defn record-experience! [experiences data]
184 (swap! experiences #(conj % data)))
185
166 (defn worm-world 186 (defn worm-world
167 [& {:keys [record motor-control keybindings view] :as settings}] 187 [& {:keys [record motor-control keybindings view experiences] :as settings}]
168 (let [{:keys [record motor-control keybindings view]} 188 (let [{:keys [record motor-control keybindings view]}
169 (merge (worm-world-defaults) settings) 189 (merge (worm-world-defaults) settings)
170 worm (doto (worm-model) (body!)) 190 worm (doto (worm-model) (body!))
171 touch (touch! worm) 191 touch (touch! worm)
172 prop (proprioception! worm) 192 prop (proprioception! worm)
192 world 212 world
193 (dir! (File. record "main-view")))) 213 (dir! (File. record "main-view"))))
194 (speed-up world) 214 (speed-up world)
195 (light-up-everything world)) 215 (light-up-everything world))
196 (fn [world tpf] 216 (fn [world tpf]
197 (let [strong! (motor-control muscles)] 217 (let [muscle-data (motor-control muscles)
198 (println strong!) 218 proprioception-data (prop)
219 touch-data (map #(% (.getRootNode world)) touch)]
220 (when experiences
221 (record-experience!
222 experiences {:touch touch-data
223 :proprioception proprioception-data
224 :muscle muscle-data})
225 (if (curled? @experiences) (println "Curled"))
226 (if (straight? @experiences) (println "Straight"))
227 )
199 (muscle-display 228 (muscle-display
200 strong! 229 muscle-data
201 (if record (dir! (File. record "muscle"))))) 230 (if record (dir! (File. record "muscle"))))
202 (prop-display 231 (prop-display
203 (prop) 232 proprioception-data
204 (if record (dir! (File. record "proprio")))) 233 (if record (dir! (File. record "proprio"))))
205 (touch-display 234 (touch-display
206 (map #(% (.getRootNode world)) touch) 235 touch-data
207 (if record 236 (if record (dir! (File. record "touch")))))))))
208 (File. record "touch")))))))
209
210