File size: 1,095 Bytes
c31266d f88e8d1 c31266d f88e8d1 c31266d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | import random
from .data import small_final_calculation
from .envs import Pool, make_episode
class SmallCalculationPool(Pool):
fraction: float = .8
def __init__(self, rows: list[dict]):
if not 0 <= self.fraction <= 1:
raise ValueError('Small-calculation sampling fraction must be in [0, 1]')
super().__init__(rows)
small = {row['group'] for row in rows
if small_final_calculation(dict(steps=row['provenance']['equations']))}
self.small_by_hint = {level: [row for row in selected if row['group'] in small]
for level, selected in self.by_hint.items()}
def sample(self, rng: random.Random):
level = rng.randint(1, 16)
if self.small_by_hint[level] and rng.random() < self.fraction:
row = rng.choice(self.small_by_hint[level])
else:
bands = list(self.by_band[level])
band = rng.choices(bands, [self.weights[name] for name in bands])[0]
row = rng.choice(self.by_band[level][band])
return make_episode(row, level)
|