我正在Clojure中構建一個系統,實時消耗事件並根據最近收到多少類似消息對其執行操作。我想使用基於牛頓冷卻的近因分數來實現這一點。使用牛頓冷卻clojure中的新近性映射
換句話說,當一個事件到達時,我希望能夠給它一個1.0之間的分數(以前從未發生過,或者牛頓方程中的「環境溫度」)和10.0(熱的熱的熱量,發生過幾次在過去的一分鐘)。
我對這個數據結構是什麼樣子有個模糊的想法 - 每個「事件類型」都是一個映射關鍵字,每個映射值都應該包含一些前一個事件的時間戳集,也許是當前「熱「的事件類型,但我不能完全弄清楚如何開始實施超越。具體來說,我很難弄清楚如何從牛頓的非常通用的實際方程中去,並將其應用於這個特定的場景。
有沒有人有任何指針?有人可能會建議一個更簡單的「新近程度分數算法」來讓我開始,這可以用牛頓冷卻道路取代嗎?
編輯:這是一些clojure代碼!它將這些事件稱爲字母,但顯然可以重新用於採取任何其他類型的對象。
(ns heater.core
(:require [clojure.contrib.generic.math-functions :as math]))
(def letter-recency-map (ref {}))
(def MIN-TEMP 1.0)
(def MAX-TEMP 10.0)
;; Cooling time is 15 seconds
(def COOLING-TIME 15000)
;; Events required to reach max heat
(def EVENTS-TO-HEAT 5.0)
(defn temp-since [t since now]
(+
MIN-TEMP
(*
(math/exp (/
(- (- now since))
COOLING-TIME))
(- t MIN-TEMP))))
(defn temp-post-event [temp-pre-event]
(+ temp-pre-event
(/
(- MAX-TEMP temp-pre-event)
EVENTS-TO-HEAT)))
(defn get-letter-heat [letter]
(dosync
(let [heat-record (get (ensure letter-recency-map) letter)]
(if (= heat-record nil)
(do
(alter letter-recency-map conj {letter {:time (System/currentTimeMillis) :heat 1.0}})
MIN-TEMP)
(let [now (System/currentTimeMillis)
new-temp-cooled (temp-since (:heat heat-record) (:time heat-record) now)
new-temp-event (temp-post-event new-temp-cooled)]
(alter letter-recency-map conj {letter {:time now :heat new-temp-event}})
new-temp-event)))))
+1是一個很好的問題。我會很樂意看到你得到的答案。 –
+1。並添加了'算法'標籤。 – 4e6