Skip to content

Instantly share code, notes, and snippets.

@nasser
Last active August 29, 2015 14:10
Show Gist options
  • Select an option

  • Save nasser/a11a27e0e28538bc573e to your computer and use it in GitHub Desktop.

Select an option

Save nasser/a11a27e0e28538bc573e to your computer and use it in GitHub Desktop.
(ns dtw.core
(:use arcadia.core)
(:import [UnityEngine Vector3]))
(defn distance [a b]
(Vector3/Distance a b))
(defn dtw [s t]
(let [n (count s)
m (count t)
bottom (apply hash-map
(mapcat (fn [i] [[i 0] Double/PositiveInfinity])
(range 1 n)))
left (apply hash-map
(mapcat (fn [i] [[0 i] Double/PositiveInfinity])
(range 1 m)))
d (merge {[0 0] 0} bottom left)]
((reduce (fn [acc [i j]]
(let [cost (distance (nth s i)
(nth t j))]
(assoc acc [i j] (+ cost
(min (acc [(dec i) j])
(acc [i (dec j)])
(acc [(dec i) (dec j)]))))))
d
(for [i (range 1 n)
j (range 1 m)]
[i j]))
[(dec n) (dec m)])))
(dtw (repeatedly 10 #(UnityEngine.Random/insideUnitSphere))
(repeatedly 10 #(UnityEngine.Random/insideUnitSphere)))
; => 9.79245162010193
(let [s (repeatedly 20 #(UnityEngine.Random/insideUnitSphere))]
(dtw s s))
; => 0.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment