comparison org/worm_learn.clj @ 411:a331d5ff73e0

saving progress for the night. completed self-organizing touch, still working on stream predicates.
author Robert McIntyre <rlm@mit.edu>
date Tue, 18 Mar 2014 23:04:48 -0400
parents e6a7e80f885a
children 54ef2e06c3ef
comparison
equal deleted inserted replaced
410:e6a7e80f885a 411:a331d5ff73e0
142 (subvec v (- c n) c)))) 142 (subvec v (- c n) c))))
143 143
144 (defn touch-average [[coords touch]] 144 (defn touch-average [[coords touch]]
145 (/ (average (map first touch)) (average (map second touch)))) 145 (/ (average (map first touch)) (average (map second touch))))
146 146
147 (def worm-segment-touch-bottom 147 (defn rect-region [[x0 y0] [x1 y1]]
148 [[8 15] [8 16] [8 17] [8 18] [8 19] [8 20] [8 21] [8 22] [9 15] 148 (vec
149 [9 16] [9 17] [9 18] [9 19] [9 20] [9 21] [9 22] [10 15] [10 16] 149 (for [x (range x0 (inc x1))
150 [10 17] [10 18] [10 19] [10 20] [10 21] [10 22] [11 15] [11 16] 150 y (range y0 (inc y1))]
151 [11 17] [11 18] [11 19] [11 20] [11 21] [11 22] [12 15] [12 16] 151 [x y])))
152 [12 17] [12 18] [12 19] [12 20] [12 21] [12 22] [13 15] [13 16] 152
153 [13 17] [13 18] [13 19] [13 20] [13 21] [13 22] [14 15] [14 16] 153 (def worm-segment-touch-bottom (rect-region [8 15] [14 22]))
154 [14 17] [14 18] [14 19] [14 20] [14 21] [14 22]]) 154
155 155 (defn contact
156 156 "Determine how much contact a particular worm segment has with
157 157 other objects. Returns a value between 0 and 1, where 1 is full
158 (defn floor-contact [[coords contact :as touch]] 158 contact and 0 is no contact."
159 (let [raw-average 159 [[coords contact :as touch]]
160 (average 160 (-> (zipmap coords contact)
161 (map 161 (select-keys worm-segment-touch-bottom)
162 first 162 (vals)
163 (vals 163 (#(map first %))
164 (select-keys 164 (average)
165 (zipmap coords contact) 165 (* 10)
166 ))))] 166 (- 1)
167 (Math/abs (- 1. (* 10 raw-average))))) 167 (Math/abs)))
168
169 168
170 (defn wiggling? 169 (defn wiggling?
171 "Is the worm wiggling?" 170 "Is the worm wiggling?"
172 [experiences] 171 [experiences]
173 (vector:last-n experiences 200) 172 (vector:last-n experiences 200)
249 :muscle muscle-data}) 248 :muscle muscle-data})
250 ;;(if (curled? @experiences) (println "Curled")) 249 ;;(if (curled? @experiences) (println "Curled"))
251 ;;(if (straight? @experiences) (println "Straight")) 250 ;;(if (straight? @experiences) (println "Straight"))
252 ;; (println-repl 251 ;; (println-repl
253 ;; (apply format "%.2f %.2f %.2f %.2f %.2f\n" 252 ;; (apply format "%.2f %.2f %.2f %.2f %.2f\n"
254 ;; (map floor-contact touch-data))) 253 ;; (map contact touch-data)))
255 254
256 ) 255 )
257 (muscle-display 256 (muscle-display
258 muscle-data 257 muscle-data
259 (if record (dir! (File. record "muscle")))) 258 (if record (dir! (File. record "muscle"))))