def test_montecarlo_sensitivity(): ishigami = Ishigami(a=5, b=0.1) space = ParameterSpace([ ContinuousParameter('x1', -np.pi, np.pi), ContinuousParameter('x2', -np.pi, np.pi), ContinuousParameter('x3', -np.pi, np.pi) ]) num_mc = 1000 np.random.seed(0) senstivity_ishigami = MonteCarloSensitivity(ishigami.fidelity1, space) senstivity_ishigami.generate_samples(1) main_sample = np.array([[3.10732573, -1.25504469, -2.66820221], [-1.32105416, -1.45224686, -2.06642419], [2.41144646, -1.98949844, 1.66646315]]) fixing_sample = np.array([[-2.93034587, -2.62148462, 1.71694805], [-2.70120457, 2.60061313, 3.00826754], [-2.56283615, 2.53817347, -1.00496868]]) main_effects, total_effects, total_variance = senstivity_ishigami.compute_effects( main_sample=main_sample, fixing_sample=fixing_sample, num_monte_carlo_points=num_mc) keys = space.parameter_names assert (all(k in main_effects for k in keys)) assert (all(k in total_effects for k in keys))
def space(): space = ParameterSpace([ ContinuousParameter('x1', 0, 1), ContinuousParameter('x2', 0, 1), ContinuousParameter('x3', 0, 1) ]) return space
def catg_space(): return ParameterSpace([ ContinuousParameter("x1", 0, 15), CategoricalParameter("x2", OneHotEncoding(["A", "B", "C", "D"])), CategoricalParameter("x3", OneHotEncoding([1, 2, 3, 4, 5])), ContinuousParameter("x4", -2, 3), ])
def branin_function(): """ Two-dimensional Branin, often used as an optimization benchmark. Based on: https://www.sfu.ca/~ssurjano/branin.html .. math:: f(\mathbf{x}) = (x_2 - b x_1 ^ 2 + c x_1 - r) ^ 2 + s(1 - t) \cos(x_1) + s where: .. math:: b = 5.1 / (4 \pi ^ 2) c = 5 /\pi r = 6 s = 10 t = 1 / (8\pi) """ parameter_space = ParameterSpace( [ContinuousParameter("x1", -5, 10), ContinuousParameter("x2", 0, 15)]) return _branin, parameter_space
def catg_space(): return ParameterSpace([ ContinuousParameter('x1', 0, 15), CategoricalParameter('x2', OneHotEncoding([0, 1, 2, 3])), CategoricalParameter('x3', OneHotEncoding([1, 2, 3, 4, 5])), ContinuousParameter('x4', -2, 3) ])
def space(): space = ParameterSpace([ ContinuousParameter("x1", 0, 1), ContinuousParameter("x2", 0, 1), ContinuousParameter("x3", 0, 1) ]) return space
def test_integrated_variance_acquisition(model): space = ParameterSpace( [ContinuousParameter('x1', 0, 1), ContinuousParameter('x2', 0, 1)]) acquisition = IntegratedVarianceReduction(model, space) x_test = np.random.rand(10, 2) result = acquisition.evaluate(x_test) assert (result.shape == (10, 1)) assert (np.all(result > 0))
def test_integrated_variance_fails_with_out_of_domain_test_points(model): """ Checks that if the user supplies x_monte_carlo to the function, and they are out of the domain of the parameter space a ValueError is raised. """ space = ParameterSpace( [ContinuousParameter('x1', 0, 1), ContinuousParameter('x2', 0, 1)]) x_monte_carlo = np.array([[0.5, 20.], [0.2, 0.3]]) with pytest.raises(ValueError): IntegratedVarianceReduction(model, space, x_monte_carlo)
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_continuous_parameter(): param = ContinuousParameter('x', 1, 10) assert param.name == 'x' assert param.check_in_domain(np.array([1, 3])) assert param.check_in_domain(np.array([[1], [3]])) assert param.check_in_domain(3) assert not param.check_in_domain(0.9) assert not param.check_in_domain(0) with pytest.raises(ValueError): # too many columns param.check_in_domain(np.array([[1, 0], [0, 2]])) with pytest.raises(ValueError): # not a 1d/2d array param.check_in_domain(np.array([[[1]]]))
def sixhumpcamel_function(): """ Two-dimensional SixHumpCamel function, often used as an optimization benchmark. Based on: https://www.sfu.ca/~ssurjano/camel6.html .. math:: f(\mathbf{x}) = \left(4-2.1x_1^2 = \frac{x_1^4}{3} \right)x_1^2 + x_1x_2 + (-4 +4x_2^2)x_2^2 """ parameter_space = ParameterSpace([ContinuousParameter("x1", -2, 2), ContinuousParameter("x2", -1, 1)]) return _sixhumpcamel, parameter_space
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)
def multi_fidelity_borehole_function(high_noise_std_deviation=0, low_noise_std_deviation=0): """ Two level borehole function. The Borehole function models water flow through a borehole. Its simplicity and quick evaluation makes it a commonly used function for testing a wide variety of methods in computer experiments. See reference for equations: https://www.sfu.ca/~ssurjano/borehole.html :param high_noise_std_deviation: Standard deviation of Gaussian observation noise on high fidelity observations. Defaults to zero. :param low_noise_std_deviation: Standard deviation of Gaussian observation noise on low fidelity observations. Defaults to zero. :return: Tuple of user function object and parameter space """ parameter_space = ParameterSpace([ ContinuousParameter('borehole_radius', 0.05, 0.15), ContinuousParameter('radius_of_influence', 100, 50000), ContinuousParameter('upper_aquifer_transmissivity', 63070, 115600), ContinuousParameter('upper_aquifer_head', 990, 1110), ContinuousParameter('lower_aquifer_transmissivity', 63.1, 116), ContinuousParameter('lower_aquifer_head', 700, 820), ContinuousParameter('borehole_length', 1120, 1680), ContinuousParameter('hydraulic_conductivity', 9855, 12045), InformationSourceParameter(2)]) user_function = MultiSourceFunctionWrapper([ lambda x: borehole_low(x, low_noise_std_deviation), lambda x: borehole_high(x, high_noise_std_deviation)]) return user_function, parameter_space
def get_validation_tasks_per_cache_size(trace_file, cache_type, cache_size, parameters, args_global, df): n_req = parameters['n_req'] n_validation = int(n_req * args_global['ratio_validation']) n_iteration = args_global['n_iteration'] n_beam = args_global['n_beam'] if len(df) == 0 or len(df[df['cache_size'] == cache_size]) == 0: # init value next_windows = np.linspace(1, int(0.4 * n_validation), args_global['n_beam'] + 1, dtype=int)[1:] tasks = [] for memory_window in next_windows: # override n_early stop task = _get_task(trace_file, cache_type, cache_size, parameters, n_validation, memory_window) tasks.append(task) return tasks # as emukit output is random, don't assume same points each time, as long as # point is enough df1 = df[df['cache_size'] == cache_size] if len(df1) >= n_iteration * n_beam: return [] # next round # window at most 40% of length # add xs, ys in a consistent order otherwise the results will be difference parameter_space = ParameterSpace([ContinuousParameter('x', 1, int(0.4 * n_validation))]) bo = GPBayesianOptimization(variables_list=parameter_space.parameters, X=df1['memory_window'].values.reshape(-1, 1), Y=df1['byte_miss_ratio'].values.reshape(-1, 1), batch_size=args_global['n_beam']) next_windows = bo.suggest_new_locations().reshape(-1).astype(int) tasks = [] for memory_window in next_windows: task = _get_task(trace_file, cache_type, cache_size, parameters, n_validation, memory_window) tasks.append(task) return tasks
def multi_source_entropy_search_acquisition(gpy_model): space = ParameterSpace( [ContinuousParameter("x1", 0, 1), InformationSourceParameter(2)]) return MultiInformationSourceEntropySearch(gpy_model, space, num_representer_points=10)
def initialize(self): parameter_space = ParameterSpace([ ContinuousParameter("x%d" % index, bounds[0], bounds[1]) for index, bounds in enumerate(self.problem.bounds) ] + [InformationSourceParameter(len(self.problem.fidelities))]) # Obtain initial sample design = LatinDesign(parameter_space) initial_parameters = design.get_samples(self.initial_sample_count) initial_response = self._evaluate_batch(initial_parameters) kernels = [GPy.kern.RBF(1)] * len(self.problem.fidelities) kernel = emukit.multi_fidelity.kernels.LinearMultiFidelityKernel(kernels) model = GPyLinearMultiFidelityModel( initial_parameters, initial_response, kernel, n_fidelities = len(self.problem.fidelities) ) model = GPyMultiOutputWrapper(model, len(self.problem.fidelities)) acquisition = NegativeLowerConfidenceBound(model) self.loop = BayesianOptimizationLoop( model = model, space = parameter_space, acquisition = acquisition, batch_size = self.batch_size )