Skip to main content

task_allocation/route/
rewards.rs

1//! Common shapes of time-dependent rewards, for [`Route::reward`](super::Route::reward).
2//!
3//! Each takes the full reward, the finish time and the shape's parameters, and returns what
4//! the task is worth at that finish time. [`deadline`] and [`linear_decay`] return `None`
5//! when it is too late; wrap [`halving`] in `Some`. None of them grows with later finish
6//! times.
7
8/// The full `reward` up to and including `deadline`, and `None` after it.
9///
10/// ```
11/// use task_allocation::route::rewards::deadline;
12///
13/// assert_eq!(deadline(10, 60, 60), Some(10));
14/// assert_eq!(deadline(10, 61, 60), None);
15/// ```
16#[must_use]
17pub const fn deadline(reward: u64, finish: u64, deadline: u64) -> Option<u64> {
18    if finish <= deadline {
19        Some(reward)
20    } else {
21        None
22    }
23}
24
25/// The full `reward` up to `from`, falling linearly to zero at `until`, and `None` after
26/// `until`. Rounds down. When `until` is not after `from`, this is a [`deadline`] at `from`.
27///
28/// ```
29/// use task_allocation::route::rewards::linear_decay;
30///
31/// assert_eq!(linear_decay(100, 10, 10, 110), Some(100));
32/// assert_eq!(linear_decay(100, 60, 10, 110), Some(50));
33/// assert_eq!(linear_decay(100, 110, 10, 110), Some(0));
34/// assert_eq!(linear_decay(100, 111, 10, 110), None);
35/// ```
36#[must_use]
37pub fn linear_decay(reward: u64, finish: u64, from: u64, until: u64) -> Option<u64> {
38    if finish <= from {
39        return Some(reward);
40    }
41
42    if finish > until {
43        return None;
44    }
45
46    let remaining = u128::from(until - finish);
47    let span = u128::from(until - from);
48    let scaled = u128::from(reward) * remaining / span;
49
50    // The quotient is at most `reward`, so it always fits.
51    Some(u64::try_from(scaled).unwrap_or(reward))
52}
53
54/// The full `reward` at time zero, halving every `half_life`, with linear steps in between,
55/// and never `None`. Rounds down. A `half_life` of zero gives the full reward at time zero
56/// and zero after.
57///
58/// This is the integer counterpart of CBBA's classic time-discounted reward `λ^t · reward`.
59///
60/// ```
61/// use task_allocation::route::rewards::halving;
62///
63/// assert_eq!(halving(100, 0, 10), 100);
64/// assert_eq!(halving(100, 10, 10), 50);
65/// assert_eq!(halving(100, 15, 10), 37);
66/// assert_eq!(halving(100, 20, 10), 25);
67/// ```
68#[must_use]
69pub fn halving(reward: u64, finish: u64, half_life: u64) -> u64 {
70    if half_life == 0 {
71        return if finish == 0 { reward } else { 0 };
72    }
73
74    let halvings = finish / half_life;
75    if halvings >= 64 {
76        return 0;
77    }
78
79    let high = reward >> halvings;
80    let low = high / 2;
81    let into = u128::from(finish % half_life);
82    let drop = (u128::from(high - low) * into).div_ceil(u128::from(half_life));
83
84    // The drop is at most `high - low`, so it always fits.
85    high - u64::try_from(drop).unwrap_or(high - low)
86}