|
| 1 | +from examples.rl.program_evaluator import ProgramEvaluator |
| 2 | +import numpy as np |
| 3 | + |
| 4 | +from typing import List, Optional, Tuple, TypeVar, Generic |
| 5 | + |
| 6 | + |
| 7 | +T = TypeVar("T", covariant=True) |
| 8 | + |
| 9 | + |
| 10 | +class TopkManager(Generic[T]): |
| 11 | + # |
| 12 | + def __init__( |
| 13 | + self, |
| 14 | + evaluator: ProgramEvaluator, |
| 15 | + c: float = 0.7, |
| 16 | + k: int = 2, |
| 17 | + ) -> None: |
| 18 | + self.evaluator = evaluator |
| 19 | + self.candidates: List[T] = [] |
| 20 | + self.k = k |
| 21 | + self.c = c |
| 22 | + |
| 23 | + def num_candidates(self) -> int: |
| 24 | + return len(self.candidates) |
| 25 | + |
| 26 | + def challenge_with( |
| 27 | + self, |
| 28 | + new_candidate: T, |
| 29 | + max_budget: int = 100, |
| 30 | + prior_experience: List[float] = [], |
| 31 | + ) -> Tuple[Optional[T], int]: |
| 32 | + """ |
| 33 | + return: the T ejected and the no of calls to get_return |
| 34 | + """ |
| 35 | + # Add new program |
| 36 | + self.evaluator.add_returns(new_candidate, prior_experience) |
| 37 | + self.candidates.append(new_candidate) |
| 38 | + ejected_candidate, budget_used = self.__run_until_ejection__(max_budget) |
| 39 | + if ejected_candidate: |
| 40 | + self.__eject__(ejected_candidate) |
| 41 | + return ejected_candidate, budget_used |
| 42 | + |
| 43 | + def get_best_stats(self) -> Tuple[T, float, float, float, float]: |
| 44 | + best_arm = np.argmax([self.evaluator.mean_return(p) for p in self.candidates]) |
| 45 | + candidate = self.candidates[best_arm] |
| 46 | + n = self.evaluator.samples(candidate) |
| 47 | + if n == 0: |
| 48 | + return candidate, float("nan"), float("inf"), -float("inf"), float("inf") |
| 49 | + rew = self.evaluator.returns(candidate) |
| 50 | + mean_return = np.mean(rew) |
| 51 | + return ( |
| 52 | + candidate, |
| 53 | + mean_return, |
| 54 | + n, |
| 55 | + min(rew), |
| 56 | + max(rew), |
| 57 | + ) |
| 58 | + |
| 59 | + def run_at_least(self, min_budget: int, min_score: float = -float("inf")) -> int: |
| 60 | + best_arm = np.argmax([self.evaluator.mean_return(p) for p in self.candidates]) |
| 61 | + candidate = self.candidates[best_arm] |
| 62 | + initial: int = self.evaluator.samples(candidate) |
| 63 | + budget_used: int = 0 |
| 64 | + while ( |
| 65 | + initial + budget_used < min_budget |
| 66 | + and self.evaluator.mean_return(candidate) >= min_score |
| 67 | + ): |
| 68 | + budget_used += 1 |
| 69 | + has_no_error = self.evaluator.eval(candidate) |
| 70 | + if not has_no_error: |
| 71 | + break |
| 72 | + return budget_used |
| 73 | + |
| 74 | + def __run_until_ejection__(self, max_budget: int) -> Tuple[Optional[T], int]: |
| 75 | + """ |
| 76 | + return: the T ejected and the cost |
| 77 | + """ |
| 78 | + budget_used: int = 0 |
| 79 | + while self.__get_candidate_to_eject__() is None and budget_used < max_budget: |
| 80 | + index: int = np.argmin([self.evaluator.samples(p) for p in self.candidates]) |
| 81 | + candidate: T = self.candidates[index] |
| 82 | + has_no_error = self.evaluator.eval(candidate) |
| 83 | + if not has_no_error: |
| 84 | + return candidate, budget_used |
| 85 | + budget_used += 1 |
| 86 | + return self.__get_candidate_to_eject__( |
| 87 | + len(self.candidates) >= self.k |
| 88 | + ), budget_used |
| 89 | + |
| 90 | + def __get_candidate_to_eject__(self, force: bool = False) -> Optional[T]: |
| 91 | + if len(self.candidates) == 1: |
| 92 | + return None |
| 93 | + mean_returns = [self.evaluator.mean_return(p) for p in self.candidates] |
| 94 | + worst_arm = np.argmin(mean_returns) |
| 95 | + worst = self.candidates[worst_arm] |
| 96 | + if force: |
| 97 | + return worst |
| 98 | + best_arm = np.argmax(mean_returns) |
| 99 | + best = self.candidates[best_arm] |
| 100 | + |
| 101 | + if mean_returns[best_arm] - self.uncertainty(best) >= mean_returns[ |
| 102 | + worst_arm |
| 103 | + ] + self.uncertainty(worst): |
| 104 | + return worst |
| 105 | + return None |
| 106 | + |
| 107 | + def uncertainty(self, candidate: T) -> float: |
| 108 | + n = self.evaluator.samples(candidate) |
| 109 | + if n == 0: |
| 110 | + return float("inf") |
| 111 | + return self.c * np.sqrt( |
| 112 | + np.log(sum(self.evaluator.samples(p) for p in self.candidates)) / n |
| 113 | + ) |
| 114 | + |
| 115 | + def __eject__(self, candidate: T): |
| 116 | + self.evaluator.delete_data(candidate) |
| 117 | + self.candidates.remove(candidate) |
0 commit comments