Standard Bayesian optimization

Driver:

BayesianOptimization

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