diff --git a/coq/clients/lsp/mul_bandit.py b/coq/clients/lsp/mul_bandit.py index 02f48315..ef6790eb 100644 --- a/coq/clients/lsp/mul_bandit.py +++ b/coq/clients/lsp/mul_bandit.py @@ -1,22 +1,43 @@ +from bisect import bisect from collections import defaultdict from datetime import timedelta -from math import exp, gamma, inf, log +from math import exp, floor, gamma, inf, log from random import random, uniform -from typing import AbstractSet, MutableSet, Optional +from typing import AbstractSet, MutableSet, Optional, Sequence +from std2.itertools import pairwise -def _kumaraswamy_inv_cdf(y: float, alpha: float, beta: float) -> float: - """ - https://en.wikipedia.org/wiki/Kumaraswamy_distribution - """ - return (1 - (1 - y) ** (1 - beta)) ** (1 / alpha) +def _logit(x: float) -> float: + return log(x / (1 - x)) -def _random_variate_sampling(a: float, b: float) -> float: +def _bins(k: float, n: int) -> Sequence[float]: + return tuple((exp(k * i) - 1) * 1000 for i in range(n)) + (inf,) - u = uniform(0, 1) - return _kumaraswamy_inv_cdf(u, alpha=a, beta=b) + +class _Dist: + def __init__(self) -> None: + self._bins = _bins(k=0.03, n=6) + self._decay = 0.99 + self._cdf = [0.0 for _ in pairwise(self._bins)] + self._sum = 0.0 + + def update(self, x: float) -> None: + binned, inc = False, 0.0 + for i, (lo, hi) in enumerate(pairwise(self._bins)): + binned |= lo <= x < hi + inc += binned + self._cdf[i] = self._cdf[i] * self._decay + binned + self._cdf[-1] = self._cdf[-1] * self._decay + inc + + def inv_cdf(self, p: float) -> float: + assert 0 <= p <= 1 + + i = round((len(self._cdf) - 1) * p) + print(self._cdf[i + 1]) + frac = (p - self._cdf[i]) / (self._cdf[i + 1] - self._cdf[i]) + return self._bins[i] + frac * (self._bins[i + 1] - self._bins[i]) class MultiArmedBandit: @@ -31,3 +52,12 @@ def update( self, clients: AbstractSet[str], client: Optional[str], elapsed: timedelta ) -> None: self._clients |= clients + + +d = _Dist() +d.update(30) +d.update(80) +d.update(161) + +print(d._bins, d._cdf) +print(d.inv_cdf(0.1)) diff --git a/coq/clients/lsp/worker.py b/coq/clients/lsp/worker.py index ec20d740..05c22e9d 100644 --- a/coq/clients/lsp/worker.py +++ b/coq/clients/lsp/worker.py @@ -109,7 +109,7 @@ async def _request(self, context: Context) -> AsyncIterator[LSPcomp]: weight_adjust=self._options.weight_adjust, context=context, chunk=self._max_results, - clients=set(), + clients={"tabby_ml"} ) async for row, peers, elapsed in rows: self._stats.update(peers, client=row.client, elapsed=elapsed) diff --git a/requirements.txt b/requirements.txt index bc8a0c93..43e3596c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,3 @@ -std2@https://github.com/ms-jpq/std2/archive/205d1f52e9b5438ef2b732c77e1144847cafa8d0.tar.gz +std2@https://github.com/ms-jpq/std2/archive/c3ab55a0147b061a24c65cb0abf1e467c92c670c.tar.gz pynvim_pp@https://github.com/ms-jpq/pynvim_pp/archive/34e3a027c595981886d7efd1c91071f3eaa4715d.tar.gz PyYAML