def brute_force_maximum(MESH, GP, kind='ucb', kappa=1.0, xi=1.0): uf = UtilityFunction(kind=kind, kappa=kappa, xi=xi) mesh_vals = uf.utility(MESH, GP, 2) max_val = mesh_vals.max() max_arg_val = MESH[np.argmax(mesh_vals)] return max_val, max_arg_val
def test_utility_fucntion(): util = UtilityFunction(kind="ucb", kappa=1.0, xi=1.0) assert util.kind == "ucb" util = UtilityFunction(kind="ei", kappa=1.0, xi=1.0) assert util.kind == "ei" util = UtilityFunction(kind="poi", kappa=1.0, xi=1.0) assert util.kind == "poi" with pytest.raises(NotImplementedError): util = UtilityFunction(kind="other", kappa=1.0, xi=1.0)
def main(db_uri, tablename): click.echo("Connecting to database...") database = SQLDatabase(db_uri, tablename) click.echo("Checking existing number of evaluations...") if len(database) < 2: # Check if there are enough existing datapoints for point in initial_points: target = black_box_function(**point) database.append({**point, "target": target}) click.echo("Creating optimizer and utility function...") optimizer = BayesianOptimization(f=None, pbounds=pbounds, random_state=1) utility_function = UtilityFunction(kind="ucb", kappa=3, xi=1) click.echo("Registering evaluations with optimizer...") for evaluation in database: optimizer.register(dissoc(evaluation, "target"), evaluation["target"]) while True: try: point = optimizer.suggest(utility_function) click.echo(f"Evaluating black-box function for {point}") target = black_box_function(**point) click.echo(f"Got {target}") click.echo("Registering with database...") database.append({**point, "target": target}) click.echo("Registering with optimizer...") optimizer.register(point, target) except KeyboardInterrupt: click.echo("Exiting...") break
class BayesianOptimizationHandler(RequestHandler): """Basic functionality for NLP handlers.""" _bo = BayesianOptimization(f=black_box_function, pbounds={ "x": (-4, 4), "y": (-3, 3) }) _uf = UtilityFunction(kind="ucb", kappa=3, xi=1) def post(self): """Deal with incoming requests.""" body = tornado.escape.json_decode(self.request.body) try: self._bo.register( x=body["params"], target=body["target"], ) print("BO has registered: {} points.".format(len(self._bo.space)), end="\n\n") except KeyError: pass finally: suggested_params = self._bo.suggest(self._uf) self.write(json.dumps(suggested_params))
def maximize(self, LHS_path = None, init_points=5, is_LHS=False, n_iter=25, acq='ucb', kappa=2.576, xi=0.0, **gp_params): """Mazimize your function""" self._prime_subscriptions() self.dispatch(Events.OPTMIZATION_START) if LHS_path == None: if is_LHS: self._prime_queue_LHS(init_points) else: self._prime_queue(init_points) else: X, fit = self.load_LHS(LHS_path) for x, eva in zip(X, fit): self.register(x, 1-eva) self.set_gp_params(**gp_params) util = UtilityFunction(kind=acq, kappa=kappa, xi=xi) iteration = 0 while not self._queue.empty or iteration < n_iter: try: x_probe = next(self._queue) except StopIteration: x_probe = self.suggest(util) iteration += 1 self.probe(x_probe, lazy=False) self.dispatch(Events.OPTMIZATION_END)
class BayesianOptimizationHandler(RequestHandler): """Basic functionality for NLP handlers.""" _bo = BayesianOptimization( f=philly_job, # pbounds={"x": (-4, 4), "y": (-3, 3)} pbounds=pbounds) _uf = UtilityFunction(kind="ucb", kappa=3, xi=1) def post(self): """Deal with incoming requests.""" body = tornado.escape.json_decode(self.request.body) suggest = body.pop("suggest") try: self._bo.register( params=body["params"], target=body["target"], ) status = f"BO registered: {body}.\n" status += f"BO has registered: {len(self._bo.space)} points.\n" print(status) except KeyError: pass finally: suggested_params = {} if suggest: suggested_params = self._bo.suggest(self._uf) self.write(json.dumps(suggested_params))
def test_acq_with_poi(): util = UtilityFunction(kind="poi", kappa=1.0, xi=1e-4) episilon = 1e-2 y_max = 2.0 max_arg = acq_max( util.utility, GP, y_max, bounds=np.array([[0, 1], [0, 1]]), random_state=ensure_rng(0), n_iter=200, ) _, brute_max_arg = brute_force_maximum(MESH, GP, kind='poi', kappa=1.0, xi=1e-4) assert all(abs(brute_max_arg - max_arg) < episilon)
def test_acq_with_ucb(): util = UtilityFunction(kind="ucb", kappa=1.0, xi=1.0) episilon = 1e-2 y_max = 2.0 max_arg = acq_max(util.utility, GP, y_max, bounds=np.array([[0, 1], [0, 1]]), btypes=[float, float], random_state=ensure_rng(0), n_iter=20) _, brute_max_arg = brute_force_maximum(MESH, GP, kind='ucb', kappa=1.0, xi=1.0) assert all(abs(brute_max_arg - max_arg) < episilon)
print("Connecting to database...") database = SQLDatabase("sqlite:///test.db", "my_function", arg_types, return_type) print("Checking existing number of evaluations...") if len(database) < 2: # Check if there are enough existing datapoints initial_targets = [] for point in initial_points: target = black_box_function(**point) database.add((point, target)) print("Creating optimizer and utility function...") optimizer = BayesianOptimization(f=None, pbounds=pbounds, random_state=1) utility_funcs = [ UtilityFunction(kind="ucb", kappa=3, xi=1), ..., ] print("Registering evaluations with optimizer...") for (point, target) in database: optimizer.register(point, target) while True: try: points = [] for uf in utility_funcs: points.append(optimizer.suggest(uf)) create_slurm_job(points) run_job()