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