指数退避

Exponential backoff

假设我有等式 T = sum(A**n) for n 从 1 到 M。

现在假设我知道 M 和 T,但想要 A。我将如何解决 A?

我想在出现错误时进行指数退避,但我不希望退避的总时间大于T,也不希望最大重试次数超过M。所以我' d 需要找到 A.

n 从 1 到 M 的 sum(A**n) 的封闭形式解是 (A^(M+1) - 1) / (A - 1) - 1。要查看此作品,让 M = 3 和 A = 2。然后 2^1 + 2^2 + 2^3 = 14,和 (2^4 - 1) / (2 - 1) - 1 = 15 / 1 - 1 = 14.

所以,我们有封闭形式的表达式 T = (A ^ (M + 1) - 1) / (A - 1) - 1。这是一个超越方程,没有封闭形式的解。然而,因为 RHS 在 A 中单调递增(A 的较大值总是给出较大的表达式值),那么我们可以做相当于二进制搜索的操作来找到任意精度的答案:

L = 0
H = MAX(T, 2)
A = (L + H) / 2
while |(A ^ (M + 1) - 1) / (A - 1) - 1 - T| > precision
    if (A ^ (M + 1) - 1) / (A - 1) - 1 > T then
        H = A
    else then
        L = A
    end if
    A = (L + H) / 2
loop

示例:T = 14,M = 3,epsilon = 0.25

L = 0
H = MAX(15, 2) = 14
A = L + H / 2 = 7

|(A ^ (M + 1) - 1) / (A - 1) - 1 - T|
 = 385 > 0.25
H = A = 7
A = (L + H) / 2 = 3.5

|(A ^ (M + 1) - 1) / (A - 1) - 1 - T|
 = 44.625 > 0.25
H = A = 3.5
A = (L + H) / 2 = 1.75

|(A ^ (M + 1) - 1) / (A - 1) - 1 - T|
 = 3.828125 > 0.25
L = A = 1.75
A = (L + H) / 2 = 2.625

|(A ^ (M + 1) - 1) / (A - 1) - 1 - T|
 = 13.603515625 > 0.25
H = A = 2.625
A = (L + H) / 2 = 2.1875

|(A ^ (M + 1) - 1) / (A - 1) - 1 - T|
 = 3.440185546875 > 0.25
H = A = 2.1875
A = (L + H) / 2 = 1.96875

|(A ^ (M + 1) - 1) / (A - 1) - 1 - T|
 = 0.524444580078125 > 0.25
L = A = 1.96875
A = (L + H) / 2 = 2.078125

|(A ^ (M + 1) - 1) / (A - 1) - 1 - T|
 = 1.371326446533203125 > 0.25
H = A = 2.078125
A = (L + H) / 2 = 2.0234375

|(A ^ (M + 1) - 1) / (A - 1) - 1 - T|
 = 0.402295589447021484375 > 0.25
H = A = 2.0234375
A = (L + H) / 2 = 1.99609375

|(A ^ (M + 1) - 1) / (A - 1) - 1 - T|
 = 0.066299498081207275390625 < 0.25

Solution: 1.99609375