view 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
line wrap: on
line source
1 (ns org.aurellem.worm-learn
2 "General worm creation framework."
3 {:author "Robert McIntyre"}
4 (:use (cortex world util import body sense
5 hearing touch vision proprioception movement
6 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-program
30 "Create a function which will execute the motor script"
31 [muscle-labels
32 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 (dorun
39 (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 effectors
44 (map #(@current-forces % 0) muscle-labels)))))))
46 (defn worm-direct-control
47 "Create keybindings and a muscle control program that will enable
48 the user to control the worm via the keyboard."
49 [muscle-labels activation-strength]
50 (let [strengths (mapv (fn [_] (atom 0)) muscle-labels)
51 activator
52 (fn [n]
53 (fn [world pressed?]
54 (let [strength (if pressed? activation-strength 0)]
55 (swap! (nth strengths n) (constantly strength)))))
56 activators
57 (map activator (range (count muscle-labels)))
58 worm-keys
59 ["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-control
66 (fn [effectors]
67 (doall
68 (map (fn [strength effector]
69 (effector (deref strength)))
70 strengths effectors)))
71 :keybindings
72 ;; 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 radically
76 ;; different patterns -- a sinusoidal wiggling motion, and a curling
77 ;; motions that causes the worm to form a circle.
79 (def curl-script
80 [[370 :d-up 40]
81 [600 :d-up 0]])
83 (def period 18)
85 (def worm-muscle-labels
86 [:base-up :base-down
87 :a-down :a-up
88 :b-up :b-down
89 :c-down :c-up
90 :d-up :d-down])
92 (defn gen-wiggle [[flexor extensor :as muscle-pair] time-base]
93 (let [period period
94 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-script
101 (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 pick
106 ;; out the defining features of the different actions available to the
107 ;; worm. For this project, I am going to explicitely define functions
108 ;; that recognize curling and wiggling respectively. These functions
109 ;; are defined using all the information available from an embodied
110 ;; simulation of the action. Note how much easier they are to define
111 ;; than if I only had vision to work with. Things like scale/position
112 ;; invariance are complete non-issues here. This is the advantage of
113 ;; body-centered action recognition and what I hope to show with this
114 ;; 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) v
142 (subvec v (- c n) c))))
144 (defn touch-average [[coords touch]]
145 (/ (average (map first touch)) (average (map second touch))))
147 (defn rect-region [[x0 y0] [x1 y1]]
148 (vec
149 (for [x (range x0 (inc x1))
150 y (range y0 (inc y1))]
151 [x y])))
153 (def worm-segment-touch-bottom (rect-region [8 15] [14 22]))
155 (defn contact
156 "Determine how much contact a particular worm segment has with
157 other objects. Returns a value between 0 and 1, where 1 is full
158 contact and 0 is no contact."
159 [[coords contact :as touch]]
160 (-> (zipmap coords contact)
161 (select-keys worm-segment-touch-bottom)
162 (vals)
163 (#(map first %))
164 (average)
165 (* 10)
166 (- 1)
167 (Math/abs)))
169 (defn wiggling?
170 "Is the worm wiggling?"
171 [experiences]
172 (vector:last-n experiences 200)
174 )
176 (def standard-world-view
177 [(Vector3f. 4.207176, -3.7366982, 3.0816958)
178 (Quaternion. 0.11118768, 0.87678415, 0.24434438, -0.3989771)])
180 (def worm-side-view
181 [(Vector3f. 4.207176, -3.7366982, 3.0816958)
182 (Quaternion. -0.11555642, 0.88188726, -0.2854942, -0.3569518)])
184 (def degenerate-worm-view
185 [(Vector3f. -0.0708936, -8.570261, 2.6487997)
186 (Quaternion. -2.318909E-4, 0.9985348, 0.053941682, 0.004291452)])
188 (defn worm-world-defaults []
189 (let [direct-control (worm-direct-control worm-muscle-labels 40)]
190 {:view worm-side-view
191 :motor-control (:motor-control direct-control)
192 :keybindings (:keybindings direct-control)
193 :record nil
194 :experiences nil
195 :worm-model worm-model
196 :end-frame nil}))
198 (defn dir! [file]
199 (if-not (.exists file)
200 (.mkdir file))
201 file)
203 (defn record-experience! [experiences data]
204 (swap! experiences #(conj % data)))
206 (defn worm-world
207 [& {:keys [record motor-control keybindings view experiences
208 worm-model end-frame] :as settings}]
209 (let [{:keys [record motor-control keybindings view experiences
210 worm-model end-frame]}
211 (merge (worm-world-defaults) settings)
212 worm (doto (worm-model) (body!))
213 touch (touch! worm)
214 prop (proprioception! worm)
215 muscles (movement! worm)
217 touch-display (view-touch)
218 prop-display (view-proprioception)
219 muscle-display (view-movement)
221 floor (box 10 1 10 :position (Vector3f. 0 -10 0)
222 :color ColorRGBA/Gray :mass 0)
223 timer (IsoTimer. 60)]
225 (world
226 (nodify [worm floor])
227 (merge standard-debug-controls keybindings)
228 (fn [world]
229 (position-camera world view)
230 (.setTimer world timer)
231 (display-dilated-time world timer)
232 (if record
233 (Capture/captureVideo
234 world
235 (dir! (File. record "main-view"))))
236 (speed-up world)
237 (light-up-everything world))
238 (fn [world tpf]
239 (if (and end-frame (> (.getTime timer) end-frame))
240 (.stop world))
241 (let [muscle-data (motor-control muscles)
242 proprioception-data (prop)
243 touch-data (map #(% (.getRootNode world)) touch)]
244 (when experiences
245 (record-experience!
246 experiences {:touch touch-data
247 :proprioception proprioception-data
248 :muscle muscle-data})
249 ;;(if (curled? @experiences) (println "Curled"))
250 ;;(if (straight? @experiences) (println "Straight"))
251 ;; (println-repl
252 ;; (apply format "%.2f %.2f %.2f %.2f %.2f\n"
253 ;; (map contact touch-data)))
255 )
256 (muscle-display
257 muscle-data
258 (if record (dir! (File. record "muscle"))))
259 (prop-display
260 proprioception-data
261 (if record (dir! (File. record "proprio"))))
262 (touch-display
263 touch-data
264 (if record (dir! (File. record "touch")))))))))