Standard Bayesian optimization
- Driver:
Download script: vanilla_bayesian_optimization.m
The target of the study is to run a standard Bayesian optimization of a scalar function. As an example, the 2D Rastrigin function on a circular domain is minimized,
\[ \begin{align}\begin{aligned}&\text{min.}\,& f(x_1,x_2) = 2\cdot10 + \sum_{i=1,2} \left(x_i^2 - 10\cos(2\pi x_i)\right)\\&\text{s.t.}\,& \sqrt{x_1^2 + x_2^2} \leq 1.5.\end{aligned}\end{align} \]
1client = jcmoptimizer.Client();
2
3% Definition of the search domain
4design_space = {...
5 struct('name', 'x1', 'type', 'continuous', 'domain', [-1.5,1.5]), ...
6 struct('name', 'x2', 'type', 'continuous', 'domain', [-1.5,1.5]), ...
7};
8
9% Definition of fixed environment parameter
10environment = {...
11 struct('name', 'radius', 'type', 'fixed', 'domain', 1.5) ...
12};
13
14% Definition of a constraint on the search domain
15constraints = {...
16 struct('name', 'circle', 'expression', 'sqrt(x1^2 + x2^2) <= radius')...
17};
18
19 % Creation of the study object with study_id 'vanilla_bayesian_optimization'
20study = client.create_study( ...
21 'design_space', design_space, ...
22 'environment', environment, ...
23 'constraints', constraints,...
24 'driver','BayesianOptimization',...
25 'study_name','Standard Bayesian optimization',...
26 'study_id', 'vanilla_bayesian_optimization');
27
28% Evaluation of the black-box function for specified design parameters
29function observation = evaluate(study, sample)
30
31 pause(2); % make objective expensive
32 observation = study.new_observation();
33 x1 = sample.x1;
34 x2 = sample.x2;
35 observation.add(10*2 ...
36 + (x1.^2-10*cos(2*pi*x1)) ...
37 + (x2.^2-10*cos(2*pi*x2)) ...
38 );
39end
40
41% Run the minimization
42study.set_evaluator(@evaluate);
43study.run();
44
45
46best = study.driver.best_sample;
47fprintf('Best sample at x1=%0.3e, x2=%0.3e\n', best.x1, best.x2)
48% print information on all found local minima
49minima = study.driver.get_minima('num_initial_samples', 20);
50array2table(cell2mat(struct2cell(minima)).','VariableNames', fieldnames(minima))
51
52% get information on best value under uncertain inputs with standard deviation 0.1
53pd = struct();
54pd.distributions = {...
55 struct('type', 'normal', 'parameter', 'x1', 'mean', best.x1, 'stddev', 0.01), ...
56 struct('type', 'normal', 'parameter', 'x2', 'mean', best.x2, 'stddev', 0.02) ...
57 };
58study.configure('parameter_distribution', pd);
59
60stats = study.driver.get_statistics('quantiles', [0.5]);
61fprintf("Objective mean %f, standard deviation %f, median %f\n", ...
62 stats.mean(1), sqrt(stats.variance(1)), stats.quantiles(1, 1));
63client.shutdown_server();