Mercurial > cortex
view org/worm_learn.clj @ 406:40b67bb71430
save progress.
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Tue, 18 Mar 2014 18:34:10 -0400 |
parents | 9b4a4da08b78 |
children | bd6d03596ea8 |
line wrap: on
line source
1 (ns org.aurellem.worm-learn2 "General worm creation framework."3 {:author "Robert McIntyre"}4 (:use (cortex world util import body sense5 hearing touch vision proprioception movement))6 (:import (com.jme3.math ColorRGBA Vector3f))7 (:import java.io.File)8 (:import com.jme3.audio.AudioNode)9 (:import com.aurellem.capture.RatchetTimer)10 (:import (com.aurellem.capture Capture IsoTimer))11 (:import (com.jme3.math Vector3f ColorRGBA)))13 (use 'clojure.pprint)15 (dorun (cortex.import/mega-import-jme3))16 (rlm.rlm-commands/help)18 (load-bullet)20 (def hand "Models/test-creature/hand.blend")22 (defn worm-model []23 (load-blender-model "Models/worm/worm.blend"))25 (def output-base (File. "/home/r/proj/cortex/render/worm-learn/curl"))28 (defn motor-control-program29 "Create a function which will execute the motor script"30 [muscle-labels31 script]32 (let [current-frame (atom -1)33 keyed-script (group-by first script)34 current-forces (atom {}) ]35 (fn [effectors]36 (let [indexed-effectors (vec effectors)]37 (dorun38 (for [[_ part force] (keyed-script (swap! current-frame inc))]39 (swap! current-forces (fn [m] (assoc m part force)))))40 (doall (map (fn [effector power]41 (effector (int power)))42 effectors43 (map #(@current-forces % 0) muscle-labels)))))))45 (defn worm-direct-control46 "Create keybindings and a muscle control program that will enable47 the user to control the worm via the keyboard."48 [muscle-labels activation-strength]49 (let [strengths (mapv (fn [_] (atom 0)) muscle-labels)50 activator51 (fn [n]52 (fn [world pressed?]53 (let [strength (if pressed? activation-strength 0)]54 (swap! (nth strengths n) (constantly strength)))))55 activators56 (map activator (range (count muscle-labels)))57 worm-keys58 ["key-f" "key-r"59 "key-g" "key-t"60 "key-y" "key-h"61 "key-j" "key-u"62 "key-i" "key-k"63 "key-o" "key-l"]]64 {:motor-control65 (fn [effectors]66 (doall67 (map (fn [strength effector]68 (effector (deref strength)))69 strengths effectors)))70 :keybindings71 ;; assume muscles are listed in pairs and map them to keys.72 (zipmap worm-keys activators)}))74 ;; These are scripts that direct the worm to move in two radically75 ;; different patterns -- a sinusoidal wiggling motion, and a curling76 ;; motions that causes the worm to form a circle.78 (def curl-script79 [[370 :d-up 40]80 [600 :d-up 0]])82 (def period 18)84 (def worm-muscle-labels85 [:base-up :base-down86 :a-down :a-up87 :b-up :b-down88 :c-down :c-up89 :d-up :d-down])91 (defn gen-wiggle [[flexor extensor :as muscle-pair] time-base]92 (let [period period93 power 45]94 [[time-base flexor power]95 [(+ time-base period) flexor 0]96 [(+ time-base period 1) extensor power]97 [(+ time-base (+ (* 2 period) 2)) extensor 0]]))99 (def wiggle-script100 (mapcat gen-wiggle (repeat 4000 [:a-down :a-up])101 (range 100 1000000 (+ 3 (* period 2)))))104 ;; Normally, we'd use unsupervised/supervised machine learning to pick105 ;; out the defining features of the different actions available to the106 ;; worm. For this project, I am going to explicitely define functions107 ;; that recognize curling and wiggling respectively. These functions108 ;; are defined using all the information available from an embodied109 ;; simulation of the action. Note how much easier they are to define110 ;; than if I only had vision to work with. Things like scale/position111 ;; invariance are complete non-issues here. This is the advantage of112 ;; body-centered action recognition and what I hope to show with this113 ;; thesis.116 (defn straight?117 "Is the worm straight?"118 [experiences]119 (every?120 (fn [[_ _ bend]]121 (< (Math/sin bend) 0.05))122 (:proprioception (peek experiences))))124 (defn curled?125 "Is the worm curled up?"126 [experiences]127 (every?128 (fn [[_ _ bend]]129 (> (Math/sin bend) 0.64))130 (:proprioception (peek experiences))))132 (defn grand-circle?133 "Does the worm form a majestic circle (one end touching the other)?"134 [experiences]135 (and (curled? experiences)136 true)) ;; TODO: add code here.138 (defn vector:last-n [v n]139 (let [c (count v)]140 (if (< c n) v141 (subvec v (- c n) c))))143 (defn touch-average [[coords touch]]144 (/ (average (map first touch)) (average (map second touch))))146 (defn floor-contact [[coords contact :as touch]]147 (let [raw-average148 (average149 (map150 first151 (vals152 (select-keys153 (zipmap coords contact)154 [[8 15] [8 16] [8 17] [8 18] [8 19] [8 20] [8 21] [8 22] [9 15]155 [9 16] [9 17] [9 18] [9 19] [9 20] [9 21] [9 22] [10 15] [10 16]156 [10 17] [10 18] [10 19] [10 20] [10 21] [10 22] [11 15] [11 16]157 [11 17] [11 18] [11 19] [11 20] [11 21] [11 22] [12 15] [12 16]158 [12 17] [12 18] [12 19] [12 20] [12 21] [12 22] [13 15] [13 16]159 [13 17] [13 18] [13 19] [13 20] [13 21] [13 22] [14 15] [14 16]160 [14 17] [14 18] [14 19] [14 20] [14 21] [14 22]]))))]161 (Math/abs (- 1. (* 10 raw-average)))))164 (defn wiggling?165 "Is the worm wiggling?"166 [experiences]167 (vector:last-n experiences 200)169 )171 (def standard-world-view172 [(Vector3f. 4.207176, -3.7366982, 3.0816958)173 (Quaternion. 0.11118768, 0.87678415, 0.24434438, -0.3989771)])175 (def worm-side-view176 [(Vector3f. 4.207176, -3.7366982, 3.0816958)177 (Quaternion. -0.11555642, 0.88188726, -0.2854942, -0.3569518)])179 (def degenerate-worm-view180 [(Vector3f. -0.0708936, -8.570261, 2.6487997)181 (Quaternion. -2.318909E-4, 0.9985348, 0.053941682, 0.004291452)])183 (defn worm-world-defaults []184 (let [direct-control (worm-direct-control worm-muscle-labels 40)]185 {:view worm-side-view186 :motor-control (:motor-control direct-control)187 :keybindings (:keybindings direct-control)188 :record nil189 :experiences nil}))191 (defn dir! [file]192 (if (not (.exists file))193 (.mkdir file))194 file)196 (defn record-experience! [experiences data]197 (swap! experiences #(conj % data)))199 (defn worm-world200 [& {:keys [record motor-control keybindings view experiences] :as settings}]201 (let [{:keys [record motor-control keybindings view]}202 (merge (worm-world-defaults) settings)203 worm (doto (worm-model) (body!))204 touch (touch! worm)205 prop (proprioception! worm)206 muscles (movement! worm)208 touch-display (view-touch)209 prop-display (view-proprioception)210 muscle-display (view-movement)212 floor (box 10 1 10 :position (Vector3f. 0 -10 0)213 :color ColorRGBA/Gray :mass 0)]215 (world216 (nodify [worm floor])217 (merge standard-debug-controls keybindings)218 (fn [world]219 (position-camera world view)220 (let [timer (IsoTimer. 60)]221 (.setTimer world timer)222 (display-dilated-time world timer))223 (if record224 (Capture/captureVideo225 world226 (dir! (File. record "main-view"))))227 (speed-up world)228 (light-up-everything world))229 (fn [world tpf]230 (let [muscle-data (motor-control muscles)231 proprioception-data (prop)232 touch-data (map #(% (.getRootNode world)) touch)]233 (when experiences234 (record-experience!235 experiences {:touch touch-data236 :proprioception proprioception-data237 :muscle muscle-data})238 (if (curled? @experiences) (println "Curled"))239 ;;(if (straight? @experiences) (println "Straight"))240 (println-repl241 (apply format "%.2f %.2f %.2f %.2f %.2f\n"242 (map floor-contact touch-data)))244 )245 (muscle-display246 muscle-data247 (if record (dir! (File. record "muscle"))))248 (prop-display249 proprioception-data250 (if record (dir! (File. record "proprio"))))251 (touch-display252 touch-data253 (if record (dir! (File. record "touch")))))))))