def benchmark_function( function, seed, n_eval=20, n_initial_points=5, model_class=None, model_kwargs=None, ): lower = np.array([-10]) upper = np.array([10]) rng1 = np.random.RandomState(seed) rng2 = np.random.RandomState(seed) cov_amp = 2 n_dims = lower.shape[0] initial_ls = np.ones([n_dims]) exp_kernel = george.kernels.Matern52Kernel(initial_ls, ndim=n_dims) kernel = cov_amp * exp_kernel prior = DefaultPrior(len(kernel) + 1) if model_class is None: model = GaussianProcess( kernel, prior=prior, rng=rng1, normalize_output=True, normalize_input=True, lower=lower, upper=upper, noise=1e-3, ) else: model = model_class(rng=rng1, **model_kwargs) acq = LogEI(model) max_func = SciPyOptimizer(acq, lower, upper, n_restarts=50, rng=rng2) bo = BayesianOptimization( objective_func=function, lower=np.array([-10]), upper=np.array([10]), acquisition_func=acq, model=model, initial_points=n_initial_points, initial_design=init_latin_hypercube_sampling, rng=rng2, maximize_func=max_func ) bo.run(n_eval) rval = np.minimum.accumulate(bo.y) return rval
def Gp(self, seed): """ Bayesian optimization with Gaussian process(mcmc) """ lower = np.zeros((self.x.shape[2])) upper = np.ones((self.x.shape[2])) inc = np.ones((self.T, 50)) for t in range(self.T): rng = np.random.RandomState(seed) # this per task per random run cov_amp = 2 n_dims = self.x.shape[2] initial_ls = np.ones([n_dims]) exp_kernel = george.kernels.Matern52Kernel(initial_ls, ndim=n_dims) kernel = cov_amp * exp_kernel prior = DefaultPrior(len(kernel) + 1) n_hypers = 3 * len(kernel) if n_hypers % 2 == 1: n_hypers += 1 model = GaussianProcess(kernel, prior=prior, rng=rng, normalize_output=False, normalize_input=False, noise=1e-6) acq = EI(model) # for the initial design, initialize the class with x-task f = Objective_function(self.x[t], self.y[t], self.metadata[t]) indes = InDesign(self.x[t]) randdes = Rand_Design(self.x[t]) max_func = RandomSampling(acq, lower, upper, randdes, n_samples=100, rng=rng) bo = BayesianOptimization( f.call, lower=lower, upper=upper, acquisition_func=acq, model=model, initial_design=randdes.initial_design_random, initial_points=3, rng=rng, maximize_func=max_func) bo.run(num_iterations=50) inc[t] = bo.incumbents_values return inc
def suggest_configuration(self): if self.X is None and self.y is None: new_x = init_random_uniform(self.lower, self.upper, n_points=1, rng=self.rng)[0, :] elif self.X.shape[0] == 1: # We need at least 2 data points to train a GP new_x = init_random_uniform(self.lower, self.upper, n_points=1, rng=self.rng)[0, :] else: cov_amp = 1 n_dims = self.lower.shape[0] initial_ls = np.ones([n_dims]) exp_kernel = george.kernels.Matern52Kernel(initial_ls, ndim=n_dims) kernel = cov_amp * exp_kernel prior = DefaultPrior(len(kernel) + 1) model = GaussianProcessMCMC(kernel, prior=prior, n_hypers=self.n_hypers, chain_length=self.chain_length, burnin_steps=self.burnin, normalize_input=False, normalize_output=True, rng=self.rng, lower=self.lower, upper=self.upper) a = LogEI(model) acquisition_func = MarginalizationGPMCMC(a) max_func = Direct(acquisition_func, self.lower, self.upper, verbose=False) model.train(self.X, self.y) acquisition_func.update(model) new_x = max_func.maximize() next_config = Configuration(self.config_space, vector=new_x) # Transform to sacred configuration result = configspace_config_to_sacred(next_config) return result
def bo(self, fun, seed, model_net, indes, randdes): """ Bayesian Optimization for ABLR Parameters: fun: Function_dataset object mapping of the data model: Net Object seed : int indes : Initial_design Object """ # BO for the network lower = np.zeros((self.x.shape[2])) upper = np.ones((self.x.shape[2])) rng = np.random.RandomState(seed) cov_amp = 2 n_dims = self.x.shape[2] initial_ls = np.ones([n_dims]) exp_kernel = george.kernels.Matern52Kernel(initial_ls, ndim=n_dims) kernel = cov_amp * exp_kernel prior = DefaultPrior(len(kernel) + 1) n_hypers = 3 * len(kernel) if n_hypers % 2 == 1: n_hypers += 1 model = model_net acq = EI(model) f = fun #max_func = RandomSampling(acq,lower,upper,indes,n_samples=300,rng=rng) max_func = RandomSampling(acq, lower, upper, randdes, n_samples=300, rng=rng) bo = BayesianOptimization(f.call, lower=lower, upper=upper, acquisition_func=acq, model=model, initial_design=randdes.initial_design_random, initial_points=3, rng=rng, maximize_func=max_func) bo.run(num_iterations=50) return bo.incumbents_values
def __init__(self, objective_func, X_lower, X_upper, maximizer="direct", acquisition="LogEI", par=None, n_func_evals=4000, n_iters=500): self.objective_func = objective_func self.X_lower = X_lower self.X_upper = X_upper assert self.X_upper.shape[0] == self.X_lower.shape[0] self.task = Task(self.X_lower, self.X_upper, self.objective_func) cov_amp = 2 initial_ls = np.ones([self.task.n_dims]) exp_kernel = george.kernels.Matern32Kernel(initial_ls, ndim=self.task.n_dims) kernel = cov_amp * exp_kernel #kernel = GPy.kern.Matern52(input_dim=task.n_dims) prior = DefaultPrior(len(kernel) + 1) n_hypers = 3 * len(kernel) if n_hypers % 2 == 1: n_hypers += 1 #self.model = GaussianProcessMCMC(kernel, prior=prior, n_hypers=n_hypers, chain_length=500, burnin_steps=100) self.model = GaussianProcess(kernel, prior=prior, dim=self.X_lower.shape[0], noise=1e-3) #self.model = GPyModel(kernel) #MAP ESTMIATE if acquisition == "EI": if par is not None: self.a = EI(self.model, X_upper=self.task.X_upper, X_lower=self.task.X_lower, par=par) else: self.a = EI(self.model, X_upper=self.task.X_upper, X_lower=self.task.X_lower) elif acquisition == "LogEI": if par is not None: self.a = LogEI(self.model, X_upper=self.task.X_upper, X_lower=self.task.X_lower, par=par) else: self.a = LogEI(self.model, X_upper=self.task.X_upper, X_lower=self.task.X_lower) elif acquisition == "PI": self.a = PI(self.model, X_upper=self.task.X_upper, X_lower=self.task.X_lower) elif acquisition == "UCB": if par is not None: self.a = LCB(self.model, X_upper=self.task.X_upper, X_lower=self.task.X_lower, par=par) else: self.a = LCB(self.model, X_upper=self.task.X_upper, X_lower=self.task.X_lower) elif acquisition == "UCB_GP": if par is not None: self.a = LCB_GP(self.model, X_upper=self.task.X_upper, X_lower=self.task.X_lower, par=par) else: self.a = LCB_GP(self.model, X_upper=self.task.X_upper, X_lower=self.task.X_lower) elif acquisition == "InformationGain": self.a = InformationGain(self.model, X_upper=self.task.X_upper, X_lower=self.task.X_lower) elif acquisition == "InformationGainMC": self.a = InformationGainMC( self.model, X_upper=self.task.X_upper, X_lower=self.task.X_lower, ) else: logger.error("ERROR: %s is not a" "valid acquisition function!" % (acquisition)) return None #self.acquisition_func = IntegratedAcquisition(self.model, self.a, self.task.X_lower, self.task.X_upper) self.acquisition_func = self.a if maximizer == "cmaes": self.max_fkt = cmaes.CMAES(self.acquisition_func, self.task.X_lower, self.task.X_upper) elif maximizer == "direct": self.max_fkt = direct.Direct( self.acquisition_func, self.task.X_lower, self.task.X_upper, n_func_evals=n_func_evals, n_iters=n_iters) #default is n_func_evals=400, n_iters=200 elif maximizer == "stochastic_local_search": self.max_fkt = stochastic_local_search.StochasticLocalSearch( self.acquisition_func, self.task.X_lower, self.task.X_upper) elif maximizer == "grid_search": self.max_fkt = grid_search.GridSearch(self.acquisition_func, self.task.X_lower, self.task.X_upper) else: logger.error("ERROR: %s is not a valid function" "to maximize the acquisition function!" % (acquisition)) return None self.bo = BayesianOptimization(acquisition_func=self.acquisition_func, model=self.model, maximize_func=self.max_fkt, task=self.task)
def bayesian_optimization(objective_function, lower, upper, num_iterations=30, maximizer="random", acquisition_func="log_ei", model_type="gp_mcmc", n_init=3, rng=None, output_path=None): """ General interface for Bayesian optimization for global black box optimization problems. Parameters ---------- objective_function: function The objective function that is minimized. This function gets a numpy array (D,) as input and returns the function value (scalar) lower: np.ndarray (D,) The lower bound of the search space upper: np.ndarray (D,) The upper bound of the search space num_iterations: int The number of iterations (initial design + BO) maximizer: {"direct", "cmaes", "random", "scipy"} The optimizer for the acquisition function. NOTE: "cmaes" only works in D > 1 dimensions acquisition_func: {"ei", "log_ei", "lcb", "pi"} The acquisition function model_type: {"gp", "gp_mcmc", "rf"} The model for the objective function. n_init: int Number of points for the initial design. Make sure that it is <= num_iterations. output_path: string Specifies the path where the intermediate output after each iteration will be saved. If None no output will be saved to disk. rng: numpy.random.RandomState Random number generator Returns ------- dict with all results """ assert upper.shape[0] == lower.shape[0], "Dimension miss match" assert np.all(lower < upper), "Lower bound >= upper bound" assert n_init <= num_iterations, "Number of initial design point has to be <= than the number of iterations" if rng is None: rng = np.random.RandomState(np.random.randint(0, 10000)) cov_amp = 2 n_dims = lower.shape[0] initial_ls = np.ones([n_dims]) exp_kernel = george.kernels.Matern52Kernel(initial_ls, ndim=n_dims) kernel = cov_amp * exp_kernel prior = DefaultPrior(len(kernel) + 1) n_hypers = 3 * len(kernel) if n_hypers % 2 == 1: n_hypers += 1 if model_type == "gp": model = GaussianProcess(kernel, prior=prior, rng=rng, normalize_output=False, normalize_input=True, lower=lower, upper=upper) elif model_type == "gp_mcmc": model = GaussianProcessMCMC(kernel, prior=prior, n_hypers=n_hypers, chain_length=200, burnin_steps=100, normalize_input=True, normalize_output=True, rng=rng, lower=lower, upper=upper) elif model_type == "rf": model = RandomForest(rng=rng) else: raise ValueError("'{}' is not a valid model".format(model_type)) if acquisition_func == "ei": a = EI(model) elif acquisition_func == "log_ei": a = LogEI(model) elif acquisition_func == "pi": a = PI(model) elif acquisition_func == "lcb": a = LCB(model) else: raise ValueError("'{}' is not a valid acquisition function".format( acquisition_func)) if model_type == "gp_mcmc": acquisition_func = MarginalizationGPMCMC(a) else: acquisition_func = a if maximizer == "cmaes": max_func = CMAES(acquisition_func, lower, upper, verbose=False, rng=rng) elif maximizer == "direct": max_func = Direct(acquisition_func, lower, upper, verbose=True) elif maximizer == "random": max_func = RandomSampling(acquisition_func, lower, upper, rng=rng) elif maximizer == "scipy": max_func = SciPyOptimizer(acquisition_func, lower, upper, rng=rng) else: raise ValueError("'{}' is not a valid function to maximize the " "acquisition function".format(maximizer)) bo = BayesianOptimization(objective_function, lower, upper, acquisition_func, model, max_func, initial_points=n_init, rng=rng, output_path=output_path) x_best, f_min = bo.run(num_iterations) results = dict() results["x_opt"] = x_best results["f_opt"] = f_min results["incumbents"] = [inc for inc in bo.incumbents] results["incumbent_values"] = [val for val in bo.incumbents_values] results["runtime"] = bo.runtime results["overhead"] = bo.time_overhead results["X"] = [x.tolist() for x in bo.X] results["y"] = [y for y in bo.y] return results
def bayesian_optimization(objective_function, lower, upper, num_iterations=30, X_init=None, Y_init=None, maximizer="random", acquisition_func="log_ei", model_type="gp_mcmc", n_init=3, rng=None, output_path=None, kernel=None, sampling_method="origin", distance="cosine", replacement=True, pool=None, best=None): """ General interface for Bayesian optimization for global black box optimization problems. Parameters ---------- objective_function: function The objective function that is minimized. This function gets a numpy array (D,) as input and returns the function value (scalar) lower: np.ndarray (D,) The lower bound of the search space upper: np.ndarray (D,) The upper bound of the search space num_iterations: int The number of iterations (initial design + BO) X_init: np.ndarray(N,D) Initial points to warmstart BO Y_init: np.ndarray(N,1) Function values of the already initial points maximizer: {"random", "scipy", "differential_evolution"} The optimizer for the acquisition function. acquisition_func: {"ei", "log_ei", "lcb", "pi"} The acquisition function model_type: {"gp", "gp_mcmc", "rf", "bohamiann", "dngo"} The model for the objective function. n_init: int Number of points for the initial design. Make sure that it is <= num_iterations. output_path: string Specifies the path where the intermediate output after each iteration will be saved. If None no output will be saved to disk. rng: numpy.random.RandomState Random number generator kernel: george.kernels.ConstantKernel {"constant", "polynomial", "linear", "dotproduct", "exp", "expsquared", "matern32", "matern52", "rationalquadratic", "cosine", "expsine2", "heuristic"} Specify the kernel for Gaussian process. sampling_method: {"origin", "approx", "exact"} Specify the method to choose next sample to update model. approx: choose the sample in the candidate pool that is closest (measured by distance arg) to the one returned from maximizing acquisition function. exact: evaluate all samples in the candidate pool on acquisition function and choose the one with maximum output. distance: {"cosine", "euclidean"} The distance measurement for approximation sampling. replacement: boolean Whether to sample from pool with replacement. pool: np.ndarray(N,D) Candidate pool containing possible x best: float Stop training when the best point is sampled. Returns ------- dict with all results """ assert upper.shape[0] == lower.shape[0], "Dimension miss match" assert np.all(lower < upper), "Lower bound >= upper bound" assert n_init <= num_iterations, "Number of initial design point has to be <= than the number of iterations" if rng is None: rng = np.random.RandomState(np.random.randint(0, 10000)) cov_amp = 2 #n_dims = lower.shape[0] #initial_ls = np.ones([n_dims]) # if kernel == "constant": # exp_kernel = george.kernels.ConstantKernel(1, ndim=n_dims) # elif kernel == "polynomial": # exp_kernel = george.kernels.PolynomialKernel(log_sigma2=1, order=3, ndim=n_dims) # elif kernel == "linear": # exp_kernel = george.kernels.LinearKernel(log_gamma2=1, order=3, ndim=n_dims) # elif kernel == "dotproduct": # exp_kernel = george.kernels.DotProductKernel(ndim=n_dims) # elif kernel == "exp": # exp_kernel = george.kernels.ExpKernel(initial_ls, ndim=n_dims) # elif kernel == "expsquared": # exp_kernel = george.kernels.ExpSquaredKernel(initial_ls, ndim=n_dims) # elif kernel == "matern32": # exp_kernel = george.kernels.Matern32Kernel(initial_ls, ndim=n_dims) # elif kernel == "matern52": # exp_kernel = george.kernels.Matern52Kernel(initial_ls, ndim=n_dims) # elif kernel == "rationalquadratic": # exp_kernel = george.kernels.RationalQuadraticKernel(log_alpha=1, metric=initial_ls, ndim=n_dims) # elif kernel == "cosine": # exp_kernel = george.kernels.CosineKernel(4, ndim=n_dims) # elif kernel == "expsine2": # exp_kernel = george.kerngels.ExpSine2Kernel(1, 2, ndim=n_dims) # elif kernel == "heuristic": # exp_kernel = george.kernels.PythonKernel(heuristic_kernel_function, ndim=n_dims) # else: # raise ValueError("'{}' is not a valid kernel".format(kernel)) kernel = cov_amp * kernel prior = DefaultPrior(len(kernel) + 1) n_hypers = 3 * len(kernel) if n_hypers % 2 == 1: n_hypers += 1 if model_type == "gp": model = GaussianProcess(kernel, prior=prior, rng=rng, normalize_output=False, normalize_input=True, lower=lower, upper=upper) elif model_type == "gp_mcmc": model = GaussianProcessMCMC(kernel, prior=prior, n_hypers=n_hypers, chain_length=200, burnin_steps=100, normalize_input=True, normalize_output=False, rng=rng, lower=lower, upper=upper) elif model_type == "rf": model = RandomForest(rng=rng) elif model_type == "bohamiann": model = WrapperBohamiann() elif model_type == "dngo": model = DNGO() else: raise ValueError("'{}' is not a valid model".format(model_type)) if acquisition_func == "ei": a = EI(model) elif acquisition_func == "log_ei": a = LogEI(model) elif acquisition_func == "pi": a = PI(model) elif acquisition_func == "lcb": a = LCB(model) else: raise ValueError("'{}' is not a valid acquisition function".format( acquisition_func)) if model_type == "gp_mcmc": acquisition_func = MarginalizationGPMCMC(a) else: acquisition_func = a if maximizer == "random": max_func = RandomSampling(acquisition_func, lower, upper, rng=rng) elif maximizer == "scipy": max_func = SciPyOptimizer(acquisition_func, lower, upper, rng=rng) elif maximizer == "differential_evolution": max_func = DifferentialEvolution(acquisition_func, lower, upper, rng=rng) else: raise ValueError("'{}' is not a valid function to maximize the " "acquisition function".format(maximizer)) if sampling_method == "exact": max_func = ExactSampling(acquisition_func, lower, upper, pool, replacement, rng=rng) init_design = init_exact_random elif sampling_method == "approx": max_func = ApproxSampling(acquisition_func, lower, upper, pool, replacement, distance, rng=rng) init_design = init_exact_random else: init_design = init_latin_hypercube_sampling bo = BayesianOptimization(objective_function, lower, upper, acquisition_func, model, max_func, pool, best, sampling_method, distance, replacement, initial_points=n_init, rng=rng, initial_design=init_design, output_path=output_path) x_best, f_min = bo.run(num_iterations, X=X_init, y=Y_init) results = dict() results["x_opt"] = x_best results["f_opt"] = f_min results["incumbents"] = [inc for inc in bo.incumbents] results["incumbent_values"] = [val for val in bo.incumbents_values] results["runtime"] = bo.runtime results["overhead"] = bo.time_overhead results["X"] = [x.tolist() for x in bo.X] results["y"] = [y for y in bo.y] return results
def entropy_search(objective_function, lower, upper, num_iterations=30, maximizer="random", model="gp_mcmc", n_init=3, output_path=None, rng=None): """ Entropy search for global black box optimization problems. This is a reimplemenation of the entropy search algorithm by Henning and Schuler[1]. [1] Entropy search for information-efficient global optimization. P. Hennig and C. Schuler. JMLR, (1), 2012. Parameters ---------- objective_function: function The objective function that is minimized. This function gets a numpy array (D,) as input and returns the function value (scalar) lower: np.ndarray (D,) The lower bound of the search space upper: np.ndarray (D,) The upper bound of the search space num_iterations: int The number of iterations (initial design + BO) maximizer: {"random", "scipy", "differential_evolution"} Defines how the acquisition function is maximized. model: {"gp", "gp_mcmc"} The model for the objective function. n_init: int Number of points for the initial design. Make sure that it is <= num_iterations. output_path: string Specifies the path where the intermediate output after each iteration will be saved. If None no output will be saved to disk. rng: numpy.random.RandomState Random number generator Returns ------- dict with all results """ assert upper.shape[0] == lower.shape[0], "Dimension miss match" assert np.all(lower < upper), "Lower bound >= upper bound" assert n_init <= num_iterations, "Number of initial design point has to be <= than the number of iterations" if rng is None: rng = np.random.RandomState(np.random.randint(0, 10000)) cov_amp = 2 n_dims = lower.shape[0] initial_ls = np.ones([n_dims]) exp_kernel = george.kernels.Matern52Kernel(initial_ls, ndim=n_dims) kernel = cov_amp * exp_kernel prior = DefaultPrior(len(kernel) + 1) n_hypers = 3 * len(kernel) if n_hypers % 2 == 1: n_hypers += 1 if model == "gp": gp = GaussianProcess(kernel, prior=prior, rng=rng, normalize_output=False, normalize_input=True, lower=lower, upper=upper) elif model == "gp_mcmc": gp = GaussianProcessMCMC(kernel, prior=prior, n_hypers=n_hypers, chain_length=200, burnin_steps=100, normalize_input=True, normalize_output=False, rng=rng, lower=lower, upper=upper) else: print("ERROR: %s is not a valid model!" % model) return a = InformationGain(gp, lower=lower, upper=upper, sampling_acquisition=EI) if model == "gp": acquisition_func = a elif model == "gp_mcmc": acquisition_func = MarginalizationGPMCMC(a) if maximizer == "random": max_func = RandomSampling(acquisition_func, lower, upper, rng=rng) elif maximizer == "scipy": max_func = SciPyOptimizer(acquisition_func, lower, upper, rng=rng) elif maximizer == "differential_evolution": max_func = DifferentialEvolution(acquisition_func, lower, upper, rng=rng) else: print( "ERROR: %s is not a valid function to maximize the acquisition function!" % maximizer) return bo = BayesianOptimization(objective_function, lower, upper, acquisition_func, gp, max_func, initial_design=init_latin_hypercube_sampling, initial_points=n_init, rng=rng, output_path=output_path) x_best, f_min = bo.run(num_iterations) results = dict() results["x_opt"] = x_best results["f_opt"] = f_min results["incumbents"] = [inc for inc in bo.incumbents] results["incumbent_values"] = [val for val in bo.incumbents_values] results["runtime"] = bo.runtime results["overhead"] = bo.time_overhead results["X"] = [x.tolist() for x in bo.X] results["y"] = [y for y in bo.y] return results
def fmin(objective_func, X_lower, X_upper, num_iterations=30, maximizer="direct", acquisition="LogEI", initX=None, initY=None): assert X_upper.shape[0] == X_lower.shape[0] class Task(BaseTask): def __init__(self, X_lower, X_upper, objective_fkt): super(Task, self).__init__(X_lower, X_upper) self.objective_function = objective_fkt task = Task(X_lower, X_upper, objective_func) cov_amp = 2 initial_ls = np.ones([task.n_dims]) exp_kernel = george.kernels.Matern52Kernel(initial_ls, ndim=task.n_dims) kernel = cov_amp * exp_kernel prior = DefaultPrior(len(kernel) + 1) n_hypers = 3 * len(kernel) if n_hypers % 2 == 1: n_hypers += 1 model = GaussianProcessMCMC(kernel, prior=prior, n_hypers=n_hypers, chain_length=200, burnin_steps=100) if acquisition == "EI": a = EI(model, X_upper=task.X_upper, X_lower=task.X_lower) elif acquisition == "LogEI": a = LogEI(model, X_upper=task.X_upper, X_lower=task.X_lower) elif acquisition == "PI": a = PI(model, X_upper=task.X_upper, X_lower=task.X_lower) elif acquisition == "UCB": a = LCB(model, X_upper=task.X_upper, X_lower=task.X_lower) elif acquisition == "InformationGain": a = InformationGain(model, X_upper=task.X_upper, X_lower=task.X_lower) elif acquisition == "InformationGainMC": a = InformationGainMC( model, X_upper=task.X_upper, X_lower=task.X_lower, ) else: logger.error("ERROR: %s is not a" "valid acquisition function!" % (acquisition)) return None acquisition_func = IntegratedAcquisition(model, a, task.X_lower, task.X_upper) if maximizer == "cmaes": max_fkt = cmaes.CMAES(acquisition_func, task.X_lower, task.X_upper) elif maximizer == "direct": max_fkt = direct.Direct(acquisition_func, task.X_lower, task.X_upper) elif maximizer == "stochastic_local_search": max_fkt = stochastic_local_search.StochasticLocalSearch( acquisition_func, task.X_lower, task.X_upper) elif maximizer == "grid_search": max_fkt = grid_search.GridSearch(acquisition_func, task.X_lower, task.X_upper) else: logger.error("ERROR: %s is not a valid function" "to maximize the acquisition function!" % (acquisition)) return None bo = BayesianOptimization(acquisition_func=acquisition_func, model=model, maximize_func=max_fkt, task=task) best_x, f_min = bo.run(num_iterations, X=initX, Y=initY) return task.retransform(best_x), f_min, model, acquisition_func, max_fkt
def fmin(objective_fkt, X_lower, X_upper, num_iterations=30, maximizer="direct", acquisition_fkt="EI"): assert X_upper.shape[0] == X_lower.shape[0] class Task(BaseTask): def __init__(self, X_lower, X_upper, objective_fkt): super(Task, self).__init__(X_lower, X_upper) self.objective_function = objective_fkt task = Task(X_lower, X_upper, objective_fkt) noise = 1.0 cov_amp = 2 initial_ls = np.ones([task.n_dims]) exp_kernel = george.kernels.Matern52Kernel(initial_ls, ndim=task.n_dims) noise_kernel = george.kernels.WhiteKernel(noise, ndim=task.n_dims) kernel = cov_amp * (exp_kernel + noise_kernel) prior = DefaultPrior(len(kernel)) model = GaussianProcessMCMC(kernel, prior=prior, n_hypers=20, chain_length=100, burnin_steps=50) if acquisition_fkt == "EI": a = EI(model, X_upper=task.X_upper, X_lower=task.X_lower) elif acquisition_fkt == "PI": a = PI(model, X_upper=task.X_upper, X_lower=task.X_lower) elif acquisition_fkt == "UCB": a = LCB(model, X_upper=task.X_upper, X_lower=task.X_lower) elif acquisition_fkt == "Entropy": a = Entropy(model, X_upper=task.X_upper, X_lower=task.X_lower) elif acquisition_fkt == "EntropyMC": a = EntropyMC( model, X_upper=task.X_upper, X_lower=task.X_lower, ) else: logger.error("ERROR: %s is not a" "valid acquisition function!" % (acquisition_fkt)) return None if maximizer == "cmaes": max_fkt = cmaes.CMAES(a, task.X_lower, task.X_upper) elif maximizer == "direct": max_fkt = direct.Direct(a, task.X_lower, task.X_upper) elif maximizer == "stochastic_local_search": max_fkt = stochastic_local_search.StochasticLocalSearch( a, task.X_lower, task.X_upper) elif maximizer == "grid_search": max_fkt = grid_search.GridSearch(a, task.X_lower, task.X_upper) else: logger.error("ERROR: %s is not a valid function" "to maximize the acquisition function!" % (acquisition_fkt)) return None bo = BayesianOptimization(acquisition_func=a, model=model, maximize_func=max_fkt, task=task) best_x, f_min = bo.run(num_iterations) return best_x, f_min
import numpy as np from robo.models.gaussian_process_mcmc import GaussianProcessMCMC from robo.acquisition.ei import EI from robo.maximizers.direct import Direct from robo.task.controlling_tasks.walker import Walker from robo.solver.bayesian_optimization import BayesianOptimization from robo.priors.default_priors import DefaultPrior from robo.acquisition.integrated_acquisition import IntegratedAcquisition task = Walker() test = '/test' kernel = 1 * george.kernels.Matern52Kernel(np.ones([task.n_dims]), ndim=task.n_dims) prior = DefaultPrior(len(kernel) + 1) model = GaussianProcessMCMC(kernel, prior=prior, chain_length=100, burnin_steps=200, n_hypers=8) ei = EI(model, task.X_lower, task.X_upper) acquisition_func = IntegratedAcquisition(model, ei, task.X_lower, task.X_upper) maximizer = Direct(acquisition_func, task.X_lower, task.X_upper) bo = BayesianOptimization(acquisition_func=acquisition_func, model=model, maximize_func=maximizer, task=task,
def bayesian_optimization(objective_function, lower, upper, num_iterations=30, maximizer="direct", acquisition_func="log_ei", model="gp_mcmc", n_init=3, rng=None): """ General interface for Bayesian optimization for global black box optimization problems. Parameters ---------- objective_function: function The objective function that is minimized. This function gets a numpy array (D,) as input and returns the function value (scalar) lower: np.ndarray (D,) The lower bound of the search space upper: np.ndarray (D,) The upper bound of the search space num_iterations: int The number of iterations (initial design + BO) maximizer: {"direct", "cmaes"} Defines how the acquisition function is maximized. NOTE: "cmaes" only works in D > 1 dimensions acquisition_func: {"ei", "log_ei", "lcb", "pi"} The acquisition function model: {"gp", "gp_mcmc"} The model for the objective function. n_init: int Number of points for the initial design. Make sure that it is <= num_iterations. rng: numpy.random.RandomState Random number generator Returns ------- dict with all results """ assert upper.shape[0] == lower.shape[0] assert n_init <= num_iterations, "Number of initial design point has to be <= than the number of iterations" if rng is None: rng = np.random.RandomState(np.random.randint(0, 10000)) cov_amp = 2 n_dims = lower.shape[0] initial_ls = np.ones([n_dims]) exp_kernel = george.kernels.Matern52Kernel(initial_ls, ndim=n_dims) kernel = cov_amp * exp_kernel prior = DefaultPrior(len(kernel) + 1) n_hypers = 3 * len(kernel) if n_hypers % 2 == 1: n_hypers += 1 if model == "gp": gp = GaussianProcess(kernel, prior=prior, rng=rng, normalize_output=True, normalize_input=True, lower=lower, upper=upper) elif model == "gp_mcmc": gp = GaussianProcessMCMC(kernel, prior=prior, n_hypers=n_hypers, chain_length=200, burnin_steps=100, normalize_input=True, normalize_output=True, rng=rng, lower=lower, upper=upper) else: print("ERROR: %s is not a valid model!" % model) return if acquisition_func == "ei": a = EI(gp) elif acquisition_func == "log_ei": a = LogEI(gp) elif acquisition_func == "pi": a = PI(gp) elif acquisition_func == "lcb": a = LCB(gp) else: print("ERROR: %s is not a valid acquisition function!" % acquisition_func) return if model == "gp": acquisition_func = a elif model == "gp_mcmc": acquisition_func = MarginalizationGPMCMC(a) if maximizer == "cmaes": max_func = CMAES(acquisition_func, lower, upper, verbose=False, rng=rng) elif maximizer == "direct": max_func = Direct(acquisition_func, lower, upper, verbose=False) else: print( "ERROR: %s is not a valid function to maximize the acquisition function!" % maximizer) return bo = BayesianOptimization(objective_function, lower, upper, acquisition_func, gp, max_func, initial_points=n_init, rng=rng) x_best, f_min = bo.run(num_iterations) results = dict() results["x_opt"] = x_best results["f_opt"] = f_min results["incumbents"] = [inc for inc in bo.incumbents] results["incumbent_values"] = [val for val in bo.incumbents_values] results["runtime"] = bo.runtime results["overhead"] = bo.time_overhead return results
def build_prior(kernel): """Build default GP prior based on kernel""" return DefaultPrior(len(kernel) + 1, numpy.random.RandomState(None))
def build_model(lower, upper, model_type="gp_mcmc", model_seed=1, prior_seed=1): """ General interface for Bayesian optimization for global black box optimization problems. Parameters ---------- lower: numpy.ndarray (D,) The lower bound of the search space upper: numpy.ndarray (D,) The upper bound of the search space model_type: {"gp", "gp_mcmc", "rf", "bohamiann", "dngo"} The model for the objective function. model_seed: int Seed for random number generator of the model prior_seed: int Seed for random number generator of the prior Returns ------- Model """ assert upper.shape[0] == lower.shape[0], "Dimension miss match" assert numpy.all(lower < upper), "Lower bound >= upper bound" cov_amp = 2 n_dims = lower.shape[0] initial_ls = numpy.ones([n_dims]) exp_kernel = george.kernels.Matern52Kernel(initial_ls, ndim=n_dims) kernel = cov_amp * exp_kernel prior = DefaultPrior(len(kernel) + 1, numpy.random.RandomState(prior_seed)) n_hypers = 3 * len(kernel) if n_hypers % 2 == 1: n_hypers += 1 # NOTE: Some models do not support RNG properly and rely on global RNG state # so we need to seed here as well... numpy.random.seed(model_seed) model_rng = numpy.random.RandomState(model_seed) if model_type == "gp": model = GaussianProcess(kernel, prior=prior, rng=model_rng, normalize_output=False, normalize_input=True, lower=lower, upper=upper) elif model_type == "gp_mcmc": model = GaussianProcessMCMC(kernel, prior=prior, n_hypers=n_hypers, chain_length=200, burnin_steps=100, normalize_input=True, normalize_output=False, rng=model_rng, lower=lower, upper=upper) elif model_type == "rf": model = RandomForest(rng=model_rng) elif model_type == "bohamiann": model = WrapperBohamiann() elif model_type == "dngo": from pybnn.dngo import DNGO model = DNGO() else: raise ValueError("'{}' is not a valid model".format(model_type)) return model