def get_ordering(self, point, count, problem): if self.kind == "greedy": if np.random.rand() >= self.epsilon * (self.decay ** count) and self.solved: #first sort based on distance to inputted point candidates = sorted(self.solved, key=lambda entry: np.linalg.norm(entry.datapoint - point))[:len(self.solved) // 10 + 1] #extract most similar 10%, resort based on speed fast = sorted(candidates, key=lambda entry: entry.time) order = list(OrderedDict((x.solve_method, True) for x in fast).keys()) #randomly append solvers not found in sort remaining = [x for x in SOLVERS.keys() if x not in order] np.random.shuffle(remaining) order = order + remaining else: order = Random.get_ordering(self, point, count, problem) elif self.kind == "single": if np.random.rand() >= self.epsilon * (self.decay ** count) and self.solved: #first sort based on distance to inputted point candidate = sorted(self.solved, key=lambda entry: np.linalg.norm(entry.datapoint - point))[0] order = list(OrderedDict((x.solve_method, True) for x in [candidate]).keys()) remaining = [x for x in SOLVERS.keys() if x != candidate] np.random.shuffle(remaining) order = order + remaining else: order = Random.get_ordering(self, point, count, problem) else: candidate = sorted(self.solved, key=lambda entry: np.linalg.norm(entry.datapoint - point)) order = list(OrderedDict((x.solve_method, True) for x in candidate).keys()) remaining = [x for x in SOLVERS.keys() if x not in order] np.random.shuffle(remaining) order = order + remaining return list(unique_everseen(order))
def get_ordering(self, point, count, problem): point = np.array(point).reshape(1, -1) if self.fitted: choice = self.clf.predict(point) order = [list(SOLVERS.keys())[int(choice)]] else: order = [] remaining = [x for x in SOLVERS.keys() if x not in order] np.random.shuffle(remaining) order = order + remaining return list(unique_everseen(order))
def get_ordering(self, point, count, problem): if self.kind == "single": t_order = self.dist.get_ordering() order = [[list(SOLVERS.keys())[int(choice)] for choice in t_order][0]] remaining = [x for x in SOLVERS.keys() if x not in order] np.random.shuffle(remaining) order = order + remaining else: t_order = self.dist.get_ordering() order = [list(SOLVERS.keys())[int(choice)] for choice in t_order] return list(unique_everseen(order))
def get_ordering(self, point, count, problem): order = list(SOLVERS.keys()) explore = np.random.binomial(1, self.epsilon) if explore == 1 and sum(self.values) > 0: # return list of solvers sorted by self.values value_order = sorted(list(range(len(SOLVERS))), key=lambda x: self.values[x], reverse=True) order = [ list(SOLVERS.keys())[int(choice)] for choice in value_order ] return order else: # shuffle to explore np.random.shuffle(order) return order
def get_ordering(self, point, count, problem): for i, _ in enumerate(SOLVERS): self.p[i] = (1 - self.gamma) * self.w[i] / sum( self.w) + self.gamma / len(SOLVERS) ordering = np.random.choice(list(SOLVERS.keys()), size=len(SOLVERS), replace=False, p=self.p) return list(unique_everseen(ordering))
def get_ordering(self, point, count, problem): if np.random.rand() >= self.epsilon * (self.decay ** count) and self.solved: candidates = sorted(self.solved, key=lambda entry: np.linalg.norm(entry.datapoint - point))[:self.k] methods = [x.solve_method for x in candidates] ss = list(SOLVERS.keys()) np.random.shuffle(ss) order = sorted(ss, key= lambda x: -1 * methods.count(x)) else: order = Random.get_ordering(self, point, count, problem) return order
def update(self, solved_prob, rewards): X = np.array(solved_prob.datapoint).reshape(1, -1) y = np.array([list(SOLVERS.keys()).index(solved_prob.solve_method)]) if self.fitted: self.clf.partial_fit(X, y) else: self.clf.partial_fit(X, y, classes=np.unique(list(range(len(SOLVERS))))) self.fitted = True #TODO: Implement pruning if is_solved(solved_prob.result): self.solved.append(solved_prob)
def get_ordering(self, point, count, problem): if not self.initialized: self.initialize(len(point)) point = point.reshape((len(point), 1)) beta = np.linalg.inv(self.A_0) @ self.B_0 thetas = [np.linalg.inv(self.As[i]) @ (self.Bs[i] - self.Cs[i] @ beta) for i in range(len(SOLVERS))] sigmas = [point.T @ np.linalg.inv(self.A_0) @ point - 2 * point.T @ \ np.linalg.inv(self.A_0) @ self.Cs[i].T @ np.linalg.inv(self.As[i]) @ point \ + point.T @ np.linalg.inv(self.As[i]) @ point + point.T @ \ np.linalg.inv(self.As[i]) @ self.Cs[i] @ np.linalg.inv(self.A_0) @ self.Cs[i].T @ np.linalg.inv(self.As[i]) @ point\ for i in range(len(SOLVERS))] ps = [thetas[i].T @ point + beta.T @ point + self.alpha * np.sqrt(sigmas[i]) for i in range(len(SOLVERS))] ss = list(range(len(ps))) np.random.shuffle(ss) i_order = sorted(ss, key=lambda x: -1 * ps[x]) order = [list(SOLVERS.keys())[int(choice)] for choice in i_order] return list(unique_everseen(order))
def get_ordering(self, point, count, problem): order = list(SOLVERS.keys()) np.random.shuffle(order) return order
def __init__(self): self.fitted = [False for _ in SOLVERS] self.models = [SGDRegressor() for _ in SOLVERS] self.solvers_to_i = {s: i for i, s in enumerate(list(SOLVERS.keys()))}