def run_optimisation(current_range, freq_range, power_range): parameter_space = ParameterSpace([\ ContinuousParameter('current', current_range[0], current_range[1]), \ ContinuousParameter('freq', freq_range[0], freq_range[1]), \ ContinuousParameter('power', power_range[0], power_range[1]) ]) def function(X): current = X[:, 0] freq = X[:, 1] power = X[:, 2] out = np.zeros((len(current), 1)) for g in range(len(current)): ''' Set JPA Current, Frequency & Power ''' out[g, 0] = -get_SNR( plot=False)[-1] #Negative as want to maximise SNR return out num_data_points = 10 design = RandomDesign(parameter_space) X = design.get_samples(num_data_points) Y = function(X) model_gpy = GPRegression(X, Y) model_gpy.optimize() model_emukit = GPyModelWrapper(model_gpy) exp_imprv = ExpectedImprovement(model=model_emukit) optimizer = GradientAcquisitionOptimizer(space=parameter_space) point_calc = SequentialPointCalculator(exp_imprv, optimizer) coords = [] min = [] bayesopt_loop = BayesianOptimizationLoop(model=model_emukit, space=parameter_space, acquisition=exp_imprv, batch_size=1) stopping_condition = FixedIterationsStoppingCondition(i_max=100) bayesopt_loop.run_loop(q, stopping_condition) coord_results = bayesopt_loop.get_results().minimum_location min_value = bayesopt_loop.get_results().minimum_value step_results = bayesopt_loop.get_results().best_found_value_per_iteration print(coord_results) print(min_value) return coord_results, abs(min_value)
def test_loop(): n_iterations = 5 x_init = np.random.rand(5, 1) y_init = np.random.rand(5, 1) # Make GPy model gpy_model = GPy.models.GPRegression(x_init, y_init) model = GPyModelWrapper(gpy_model) space = ParameterSpace([ContinuousParameter('x', 0, 1)]) acquisition = ExpectedImprovement(model) # Make loop and collect points bo = BayesianOptimizationLoop(model=model, space=space, acquisition=acquisition) bo.run_loop(UserFunctionWrapper(f), FixedIterationsStoppingCondition(n_iterations)) # Check we got the correct number of points assert bo.loop_state.X.shape[0] == n_iterations + 5 # Check the obtained results results = bo.get_results() assert results.minimum_location.shape[0] == 1 assert results.best_found_value_per_iteration.shape[0] == n_iterations + 5
def bayesian_opt(): # 2. ranges of the synth parameters syn1 = syn2 = syn3 = syn4 = syn5 = np.arange(158) syn6 = np.arange(6000) syn7 = np.arange(1000) syn8 = np.arange(700) # 2. synth paramters ranges into an 8D parameter space # parameter_space = ParameterSpace( # [ContinuousParameter('x1', 0., 157.)]) # parameter_space = ParameterSpace( # [DiscreteParameter('x8', syn8)]) parameter_space = ParameterSpace( [ContinuousParameter('x1', 0., 157.), ContinuousParameter('x2', 0., 157.), ContinuousParameter('x3', 0., 157.), ContinuousParameter('x4', 0., 157.), ContinuousParameter('x5', 0., 157.), ContinuousParameter('x6', 0., 5999.), ContinuousParameter('x7', 0., 999.), ContinuousParameter('x8', 0., 699.)]) # parameter_space = ParameterSpace( # [DiscreteParameter('x1', syn1), DiscreteParameter('x2', syn2), DiscreteParameter('x3', syn3), # DiscreteParameter('x4', syn4), DiscreteParameter('x5', syn5), DiscreteParameter('x6', syn6), # DiscreteParameter('x7', syn1), DiscreteParameter('x8', syn8)]) # 3. collect random points design = RandomDesign(parameter_space) X = design.get_samples(num_data_points) # X is a numpy array print("X=", X) # [is the below needed?] # UserFunction.evaluate(training_function, X) # I put UserFunctionWrapper in line 94 # 4. define training_function as Y Y = training_function(X) # [is this needed?] # loop_state = create_loop_state(X, Y) # 5. train and wrap the model in Emukit model_gpy = GPRegression(X, Y, normalizer=True) model_emukit = GPyModelWrapper(model_gpy) expected_improvement = ExpectedImprovement(model=model_emukit) bayesopt_loop = BayesianOptimizationLoop(model=model_emukit, space=parameter_space, acquisition=expected_improvement, batch_size=5) max_iterations = 15 bayesopt_loop.run_loop(training_function, max_iterations) model_gpy.plot() plt.show() results = bayesopt_loop.get_results() # bayesopt_loop.loop_state.X print("X: ", bayesopt_loop.loop_state.X) print("Y: ", bayesopt_loop.loop_state.Y) print("cost: ", bayesopt_loop.loop_state.cost)
exp_imprv = ExpectedImprovement(model = model_emukit) optimizer = GradientAcquisitionOptimizer(space = parameter_space) point_calc = SequentialPointCalculator(exp_imprv,optimizer) # Bayesian optimisation routine bayesopt_loop = BayesianOptimizationLoop(model = model_emukit, space = parameter_space, acquisition=exp_imprv, batch_size=1) stopping_condition = FixedIterationsStoppingCondition(i_max = no_BO_sims) bayesopt_loop.run_loop(q, stopping_condition) # Results of Bayesian optimisation coord_results = bayesopt_loop.get_results().minimum_location min_value = bayesopt_loop.get_results().minimum_value step_results = bayesopt_loop.get_results().best_found_value_per_iteration print(coord_results) print(min_value) # Save the pararmeters of the best resonator results = [coord_results,min_value] results_file = open('results.txt','w') results_file.write(str(results)) results_file.close() # Save the entire results of the model data = model_emukit.model.to_dict() with open('model_data.txt','w') as outfile: json.dump(data,outfile)
def bo_loop(config, image_path, ai_model=None): target_function, space = eval(config.name)() data_dim = config.data_dim num_mix = config.num_mix init_num_data = config.init_num_data interval_std = config.interval_std interval = np.zeros((1, data_dim)) std = np.zeros((1, data_dim)) mean = np.zeros((1, data_dim)) #set up data, scaling for ii in range(data_dim): interval[0, ii] = space.parameters[ii].max - space.parameters[ii].min std[0, ii] = interval[0, ii] / interval_std mean[0, ii] = (space.parameters[ii].max + space.parameters[ii].min) / 2 space.parameters[ii].min = (space.parameters[ii].min - mean[0, ii]) / std[0, ii] space.parameters[ii].max = (space.parameters[ii].max - mean[0, ii]) / std[0, ii] results_list = [None] * config.repeated_runs best_value_per_iter = np.zeros((config.repeated_runs, config.bo_iter)) npr = np.random.RandomState(123) for ii in tqdm(range(config.repeated_runs)): #initialize data points X_init = (npr.rand(init_num_data, data_dim) - 0.5) * interval + mean X_init_norm = (X_init - mean) / std Y_init = target_function(X_init) Y_init_norm, mean_Y, std_Y = standardize(Y_init) # normalized function function_norm = lambda x: (target_function(x * std + mean) - mean_Y ) / std_Y if config.is_GPY: kernel = GPy.kern.RBF(input_dim=data_dim, variance=npr.rand(1), lengthscale=npr.rand(data_dim), ARD=True) for jj in range(num_mix - 1): rbf_new = GPy.kern.RBF(input_dim=data_dim, variance=npr.rand(1), lengthscale=npr.rand(data_dim), ARD=True) kernel = kernel + rbf_new if config.is_sparseGP: z = (np.random.rand(config.num_inducing_pts, data_dim) - 0.5) * interval_std model_gp = GPy.models.SparseGPRegression(X_init_norm, Y_init_norm, kernel, Z=z) else: model_gp = GPy.models.GPRegression(X_init_norm, Y_init_norm, kernel) model_gp.Gaussian_noise.variance = config.epsilon model_gp.Gaussian_noise.variance.fix() model_emukit = GPyModelWrapperTime(model_gp) model_emukit.optimize() else: #Set up Emukit_BO_BQ_GP_Model model_emukit = Emukit_BO_BQ_GP_Model(X_init_norm, Y_init_norm, config, ai_model) model_emukit.optimize() model_emukit.set_kernel() expected_improvement = ExpectedImprovement(model=model_emukit) bayesopt_loop = BayesianOptimizationLoop( model=model_emukit, space=space, acquisition=expected_improvement, batch_size=1) max_iterations = config.bo_iter bayesopt_loop.run_loop(function_norm, max_iterations) results = bayesopt_loop.get_results() #scale back the x and y results_save = edict() results_save.best_found_value_per_iteration = results.best_found_value_per_iteration[ init_num_data:] * std_Y.item() + mean_Y.item() best_value_per_iter[ ii, :] = results_save.best_found_value_per_iteration results_save.minimum_value = results.minimum_value * std_Y.item( ) + mean_Y.item() results_save.minimum_location = results.minimum_location * std.squeeze( 0) + mean.squeeze(0) results_save.time_elapsed = model_emukit.time_count results_list[ii] = results_save best_value_mean = np.mean(best_value_per_iter, 0) best_value_std = np.std(best_value_per_iter, 0) plt.figure(figsize=(12, 8)) plt.fill_between(np.arange(max_iterations) + 1, best_value_mean - 0.2 * best_value_std, best_value_mean + 0.2 * best_value_std, color='red', alpha=0.15) plt.plot(np.arange(max_iterations) + 1, best_value_mean, 'or-', lw=2, label='Best found function value') plt.legend(loc=2, prop={'size': LEGEND_SIZE}) plt.xlabel(r"iteration") plt.ylabel(r"$f(x)$") plt.grid(True) plt.savefig(image_path, format='pdf') return results_list
model_wrapped = GPyModelWrapper(model) target = user_sample_vector acq = L2_LCB(model=model_wrapped, target=target) fit_update = lambda a, b: model.optimize_restarts(verbose=False) bayesopt_loop = BayesianOptimizationLoop(model=model_wrapped, space=parameter_space, acquisition=acq) bayesopt_loop.iteration_end_event.append(fit_update) bayesopt_loop.run_loop(training_function, 5) # 5. train and wrap the model in Emukit # model_gpy = GPRegression(X, Y, normalizer=True) # # model_emukit = GPyModelWrapper(model_gpy) # expected_improvement = ExpectedImprovement(model=model_emukit) # bayesopt_loop = BayesianOptimizationLoop(model=model_emukit, # space=parameter_space, # acquisition=expected_improvement, # batch_size=5) # # max_iterations = 15 # bayesopt_loop.run_loop(training_function, max_iterations) model_wrapped.plot() plt.show() results = bayesopt_loop.get_results() # bayesopt_loop.loop_state.X print("X: ", bayesopt_loop.loop_state.X) print("Y: ", bayesopt_loop.loop_state.Y) print("cost: ", bayesopt_loop.loop_state.cost)