tune testing
This commit is contained in:
parent
021d8724cd
commit
42d51c896a
|
@ -4,16 +4,20 @@ import numpy as np
|
||||||
# import numpy.typing as npt
|
# import numpy.typing as npt
|
||||||
import numpy.random as npr
|
import numpy.random as npr
|
||||||
from scipy.stats import f_oneway
|
from scipy.stats import f_oneway
|
||||||
|
from scipy.stats.stats import F_onewayResult
|
||||||
|
|
||||||
|
|
||||||
class ClocktowerManager:
|
class ClocktowerManager:
|
||||||
__slots__ = ['bounds', 'data', 'rng', 'significance', 'best_guess', 'min_correct_tries']
|
__slots__ = ['bounds', 'data', 'rng', 'significance', 'best_guess', 'min_correct_tries',
|
||||||
|
'anova', 'use_smart_strategy']
|
||||||
bounds: Tuple[int, int]
|
bounds: Tuple[int, int]
|
||||||
data: Dict[int, np.ndarray]
|
data: Dict[int, np.ndarray]
|
||||||
rng: npr.Generator
|
rng: npr.Generator
|
||||||
significance: float
|
significance: float
|
||||||
min_correct_tries: int
|
min_correct_tries: int
|
||||||
best_guess: Optional[int]
|
best_guess: Optional[int]
|
||||||
|
anova: Optional[F_onewayResult]
|
||||||
|
use_smart_strategy: bool
|
||||||
|
|
||||||
def __init__(self, bounds: Tuple[int, int] = (0, 256),
|
def __init__(self, bounds: Tuple[int, int] = (0, 256),
|
||||||
significance: float = 0.01,
|
significance: float = 0.01,
|
||||||
|
@ -27,21 +31,30 @@ class ClocktowerManager:
|
||||||
self.rng = rng
|
self.rng = rng
|
||||||
self.min_correct_tries = min_correct_tries
|
self.min_correct_tries = min_correct_tries
|
||||||
self.best_guess = None
|
self.best_guess = None
|
||||||
|
self.anova = None
|
||||||
|
self.use_smart_strategy = True
|
||||||
|
|
||||||
def next_guess(self) -> Optional[int]:
|
def next_guess(self) -> Optional[int]:
|
||||||
if (self.best_guess is not None and
|
|
||||||
len(self.data[self.best_guess]) >= self.min_correct_tries):
|
|
||||||
return None
|
|
||||||
|
|
||||||
min_count = min([len(x) for x in self.data.values()])
|
min_count = min([len(x) for x in self.data.values()])
|
||||||
if min_count < 3 or self.rng.uniform(0, 1) > 0.5:
|
min_keys = [k for k in self.data.keys() if len(self.data[k]) == min_count]
|
||||||
min_keys = [k for k in self.data.keys() if len(self.data[k]) == min_count]
|
|
||||||
return self.rng.choice(min_keys)
|
if self.use_smart_strategy:
|
||||||
elif self.best_guess is not None:
|
if (self.best_guess is not None and
|
||||||
return self.best_guess
|
len(self.data[self.best_guess]) >= self.min_correct_tries):
|
||||||
|
return None
|
||||||
|
|
||||||
|
if min_count < 3 or self.rng.uniform(0, 1) > 0.8:
|
||||||
|
return self.rng.choice(min_keys)
|
||||||
|
elif self.best_guess is not None:
|
||||||
|
return self.best_guess
|
||||||
|
else:
|
||||||
|
means = {k: v.mean() for k, v in self.data.items()}
|
||||||
|
return max(means.items(), key=lambda x: x[1])[0]
|
||||||
else:
|
else:
|
||||||
means = {k: v.mean() for k, v in self.data.items()}
|
if self.best_guess is not None:
|
||||||
return max(means.items(), key=lambda x: x[1])[0]
|
return None
|
||||||
|
else:
|
||||||
|
return self.rng.choice(min_keys)
|
||||||
|
|
||||||
def update(self, guess: int, value: float) -> None:
|
def update(self, guess: int, value: float) -> None:
|
||||||
if guess not in self.data.keys():
|
if guess not in self.data.keys():
|
||||||
|
@ -53,10 +66,8 @@ class ClocktowerManager:
|
||||||
return
|
return
|
||||||
if max([len(v) for v in inputs]) < 2:
|
if max([len(v) for v in inputs]) < 2:
|
||||||
return
|
return
|
||||||
res = f_oneway(*inputs)
|
self.anova = f_oneway(*inputs)
|
||||||
print("results", res)
|
if self.anova.pvalue <= self.significance:
|
||||||
if res.pvalue <= self.significance:
|
|
||||||
print("significant!")
|
|
||||||
self.best_guess = max(self.data.items(), key=lambda v: v[1].mean())[0]
|
self.best_guess = max(self.data.items(), key=lambda v: v[1].mean())[0]
|
||||||
else:
|
else:
|
||||||
self.best_guess = None
|
self.best_guess = None
|
||||||
|
@ -69,12 +80,15 @@ class ClocktowerManager:
|
||||||
|
|
||||||
def main() -> None:
|
def main() -> None:
|
||||||
gen = npr.default_rng()
|
gen = npr.default_rng()
|
||||||
stdev = 7059
|
# stdev = 7059
|
||||||
u0 = 500000
|
# u0 = 500000
|
||||||
u1 = 506046
|
# u1 = 506046
|
||||||
# stdev = 1000
|
# stdev = 1000
|
||||||
# u0 = 500000
|
# u0 = 500000
|
||||||
# u1 = 506046
|
# u1 = 506046
|
||||||
|
stdev = 7000 * 2
|
||||||
|
u0 = 500000
|
||||||
|
u1 = 506046
|
||||||
correct_guess = 0x42
|
correct_guess = 0x42
|
||||||
|
|
||||||
def sample(guess):
|
def sample(guess):
|
||||||
|
@ -86,13 +100,14 @@ def main() -> None:
|
||||||
num_guesses += 1
|
num_guesses += 1
|
||||||
guess = mgr.next_guess()
|
guess = mgr.next_guess()
|
||||||
if guess is None:
|
if guess is None:
|
||||||
print("answer", hex(mgr.get_best_guess()))
|
|
||||||
break
|
break
|
||||||
print("guessing", hex(guess), "(guess", num_guesses, ")")
|
print("guessing", f"0x{guess:02x}", "(guess", num_guesses, ")", "(state", mgr.anova, ")")
|
||||||
value = sample(guess)
|
value = sample(guess)
|
||||||
mgr.update(guess, value)
|
mgr.update(guess, value)
|
||||||
if len(mgr.data[correct_guess]) > 0:
|
# if len(mgr.data[correct_guess]) > 0:
|
||||||
print("means", mgr.data[correct_guess].mean(),
|
# print("means", mgr.data[correct_guess].mean(),
|
||||||
np.hstack([v for k, v in mgr.data.items() if k != correct_guess]).mean())
|
# np.hstack([v for k, v in mgr.data.items() if k != correct_guess]).mean())
|
||||||
|
|
||||||
|
print("state", mgr.anova)
|
||||||
|
print(f"answer 0x{mgr.get_best_guess():02x}")
|
||||||
print("took", num_guesses, "guesses")
|
print("took", num_guesses, "guesses")
|
||||||
|
|
Loading…
Reference in New Issue