""" Example of using the scikit-optimize backend and gaussian_process algorithm with BBopt. To run this example, just run: > bbopt ./skopt_example.py """ # BBopt setup: from bbopt import BlackBoxOptimizer bb = BlackBoxOptimizer(file=__file__) if __name__ == "__main__": bb.run(alg="gaussian_process") # Let's use some parameters! x0 = bb.randrange("x0", 1, 11, guess=5) x1 = bb.uniform("x1", 0, 1) x2 = bb.choice("x2", [-10, -1, 0, 1, 10]) # And let's set our goal! y = x0 + x1*x2 bb.minimize(y) # Finally, we'll print out the value we used for debugging purposes. if __name__ == "__main__": print(repr(y))
if __name__ == "__main__": bb.run_backend("mixture", [ ("random", 1), ("tree_structured_parzen_estimator", 1), ("annealing", 1), ("gaussian_process", 1), ("random_forest", 1), ("extra_trees", 1), ("gradient_boosted_regression_trees", 1), ]) # If we're not serving, store which algorithm the # mixture backend has selected. from bbopt.backends.mixture import MixtureBackend if isinstance(bb.backend, MixtureBackend): bb.remember({ "alg": bb.backend.selected_alg, }) # Set up a parameter from a choice and a random sample. xs = bb.sample("xs", range(10), 5, guess=[3, 4, 5, 6, 7]) y = bb.choice("y", [1, 10, 100], guess=10) # Set the goal to be the absolute difference of sum(xs) and y. loss = abs(sum(xs) - y) bb.minimize(loss) # Finally, we'll print out the value we used for debugging purposes. if __name__ == "__main__": print(repr(loss))
# BBopt setup: from bbopt import BlackBoxOptimizer bb = BlackBoxOptimizer(file=__file__) if __name__ == "__main__": bb.run_backend( "mixture", distribution=[ ("gaussian_process", float("inf")), ("tree_structured_parzen_estimator", 1), ], remove_erroring_algs=True, ) # Set some parameters that skopt supports. x0 = bb.randint("x0", 1, 10, guess=5) x1 = bb.choice("x1", [-10, -1, 0, 1, 10]) # Set a parameter that only hyperopt supports. x2 = bb.normalvariate("x2", mu=0, sigma=1) if not bb.is_serving: assert bb.backend.selected_alg == "tree_structured_parzen_estimator", bb.backend.selected_alg # Set the goal. y = x0 + x1 * x2 bb.minimize(y) # Print out the value we used for debugging purposes. if __name__ == "__main__": print(repr(y))
""" Example of using the default "any_fast" algorithm. To run this example, just run: > bbopt ./any_fast_example.py """ # BBopt setup: from bbopt import BlackBoxOptimizer bb = BlackBoxOptimizer(file=__file__) if __name__ == "__main__": bb.run() # alg="any_fast" should be the default # We set u ~ dist(0, 1) * sin(dist(0, 1)) where dist is uniform or normal. from math import sin dist = bb.choice("dist", ["uniform", "normal"]) if dist == "normal": u = bb.normalvariate("x0_n", 0, 1) * sin(bb.normalvariate("x1_n", 0, 1)) else: u = bb.random("x0_u") * sin(bb.random("x1_u")) # If we used hyperopt-only parameters, we shouldn't have skopt. if hasattr(bb.backend, "selected_backend"): bb.remember({"backend": bb.backend.selected_backend}) if dist == "normal": assert bb.backend.selected_backend != "scikit-optimize", bb.backend.selected_backend else: bb.remember({"backend": bb.backend.backend_name}) # Set u as the thing to minimize. bb.minimize(u)