yatp icon indicating copy to clipboard operation
yatp copied to clipboard

calculate appropriate level 0 chance

Open sticnarf opened this issue 4 years ago • 2 comments

Problem

Currently the maximum level 0 chance is 0.98. Sometimes we still fail to achieve our target even with this maximum value. For example, if we have plenty of tasks of all levels to run and the average time spent in handling a level 0 task once is 50us while the average time spent in handling other tasks once is 1500us. We can calculate the final time proportion of level 0 is (50 * 0.98) / (50 * 0.98 + 1500 * 0.02) = 0.62, which is far lower than we expected.

Solution

Firstly, this PR raises the maximum level 0 chance to 0.9999, which should suffice in most cases. And the way we adjust the chance is improved. The new chance is now calculated based on the assumption that we have plenty of tasks in all levels. (It is OK when the assumption is false because then we actually don't need to balance tasks of different levels.

Formula

Say the average time spent in handling level 1 and 2 tasks once is k times of that for level 0 task, the current time proportion of level 0 tasks is m, the target proportion is m0 and the chance of level 0 tasks is p. We want to get an appropriate new chance of level 0 tasks, p'.

So, we have simultaneous equations below:

p/(p+k(1-p)) = m p'/(p'+k(1-p')) = m0

Solving the equations we can get p' = m0(1-m)p/(m0(1-m0)-(m-m0)(p-(1-m0))).

In our implementation, we ensure 0 < p < 1, 0 <= m <= 1, 0 < m0 < 1. The denominator can be proved to be greater than zero so we needn't check it in our code.

sticnarf avatar Apr 15 '20 06:04 sticnarf

the denominator can be proved to be greater than zero

How?

BusyJay avatar Apr 20 '20 08:04 BusyJay

the denominator can be proved to be greater than zero

How?

if image

then image

Because m and m_0p are all > 0, p-(1-m0) > 0. So we have image

And also, we know m <= 1, then,

image

Multiply the denominator in both side, we get image

But both (1-m0) and (1-p) > 0, so we get a contradiction. QED

sticnarf avatar Jun 10 '20 07:06 sticnarf