我正在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