def search(self): """Search for an optimal solution.""" self.reset() if not self.ready(): raise Exception("Optimizer not ready.") best = GbSolution(None, float('-Inf')) top_ranked = None while not self.done(): self.iters += 1 candidates = self.sample(self.sample_size, top_ranked, best) top_ranked, winner = self.get_top_ranked(candidates) self.estimate(top_ranked, best) if best.cost < winner.cost: best = winner if self.callback_func is not None: self.callback_func( best, self.cost_func.evals / float(self.max_evals)) if self.cost_func.retest_last: best.cost = self.cost_func(np.array([best.params]), is_last=True)[0] if self.callback_func is not None: self.callback_func(best, 1.0) return best
def get_top_ranked(self, candidates): costs = self.cost_func(candidates) maxindx = np.argmax(costs) winner, loser = GbSolution(candidates[maxindx], costs[maxindx]), GbSolution( candidates[not maxindx], costs[not maxindx]) return winner, winner
def test_calculate_means_vars_no_best(self): means = np.array([2.0]) vars = np.array([9.0]) acc_means = np.array([1.5]) acc_vars = np.array([4.0]) cand_size = 5 learning_rate = 0.6 best = GbSolution(None, float("-Inf")) new_means, new_vars = \ Tilda._estimate_gaussian(means, vars, acc_means, acc_vars, best, cand_size, learning_rate) self.assertAlmostEqual(new_means[0], 1.16, 2) self.assertAlmostEqual(new_vars[0], 4.2681, 2)
def build_solution(self, params, cost): return GbSolution(params, cost)
def build_solution(self, params, cost): return GbSolution(params, cost, self.distr.roots, self.distr.children)