Use exponential decay for PersonRelay association rank scoring

This commit is contained in:
Mike Dilger 2024-07-24 12:58:24 +12:00
parent f0bbb47905
commit 7ebb8c243e
2 changed files with 19 additions and 6 deletions

View File

@ -101,3 +101,9 @@ impl<C: speedy::Context> speedy::Writable<C> for Private {
self.0.write_to(writer)
}
}
pub fn exponential_decay(base: f32, halflife_seconds: u64, elapsed_seconds: u64) -> f32 {
use std::f32::consts::E;
let decay_constant = 2.0_f32.ln() / halflife_seconds as f32;
base * E.powf(-decay_constant * elapsed_seconds as f32)
}

View File

@ -45,12 +45,11 @@ impl PersonRelay2 {
}
}
// Includes a value of 20 if in their relay list.
// Includes a value of 4 (halflife of 14 days) if their events have been seen there recently
// Includes a value of 2 (halflife of 7 days) if a relay hint suggested it
pub fn association_rank(&self, now: Unixtime, write: bool) -> u64 {
let now = now.0 as u64;
let scorefn = |when: u64, fade_period: u64, base: u64| -> u64 {
let dur = now.saturating_sub(when); // seconds since
base * fade_period / fade_period.max(dur)
};
let mut score = 0;
@ -68,12 +67,20 @@ impl PersonRelay2 {
// last_fetched is gossip verified happened-to-work-before
if let Some(when) = self.last_fetched {
score += scorefn(when, 60 * 60 * 24 * 3, 4);
let base = 4.0_f32;
let halflife_seconds = 60 * 60 * 24 * 14;
let elapsed_seconds = now.saturating_sub(when);
let delta = crate::misc::exponential_decay(base, halflife_seconds, elapsed_seconds);
score += delta as u64;
}
// last_suggested is an anybody-signed suggestion
if let Some(when) = self.last_suggested {
score += scorefn(when, 60 * 60 * 24 * 2, 1);
let base = 2.0_f32;
let halflife_seconds = 60 * 60 * 24 * 7;
let elapsed_seconds = now.saturating_sub(when);
let delta = crate::misc::exponential_decay(base, halflife_seconds, elapsed_seconds);
score += delta as u64;
}
score