-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathacquisition.m
101 lines (83 loc) · 2.85 KB
/
acquisition.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
classdef acquisition
properties
method = 'lbfgs'
ncandidates = optim.AF_ncandidates
initial_guess
utility
dims = [];
end
methods
function AF = define_AF()
end
function [new_x,new_x_norm, L] = acquire(AF)
[new_x_norm, L] = AF.optimize_AF(@(x) utility(theta, xtrain_norm, x, ctrain, model, post, e), model.lb_norm, model.ub_norm);
new_x = new_x_norm.*(model.ub-model.lb) + model.lb;
end
function [best_x, best_val] = optimize_AF(AF, utility, lb, ub)
% To optimize acquisition functions
%% Multistart seach with minFunc
best_x= [];
x0 = lb;
f = @(x) subfun(x, utility,dims, x0, objective);
if ~isempty(dims)
lb = lb(dims);
ub = ub(dims);
end
% Batch initialization for multi-start optimization : select starting points using the method by
% Balandat et al 2020 (appendix F).
D = size(lb,1);
p = haltonset(D,'Skip',1e3,'Leap',1e2);
p = scramble(p,'RR2');
q = qrandstream(p);
n0 = 500;
sampSize = 1;
X = zeros(D, n0);
v = zeros(1,n0);
for i = 1:n0
X(:,i) = qrand(q,sampSize);
v(i) = utility(X(:,i));
end
temperature = 1;
p = exp(temperature*zscore(v));
p = p/sum(p);
starting_points = X(:,randsample(n0, ncandidates, true, p));
%starting_points = rand_interval(lb, ub, 'nsamples', ncandidates);
if ~isempty(init_guess)
starting_points(:,1)= init_guess;
end
best_val=inf;
options.verbose = 0;
x = [];
for k = 1:ncandidates
try
x = minConf_TMP(@(x)utility(x), starting_points(:,k), lb(:), ub(:), options);
if any(isnan(x(:)))
error('x is NaN')
end
val = utility(x);
if val < best_val
best_x = x;
best_val = val;
end
catch e %e is an MException struct
fprintf(1,e.message);
end
end
if isempty(x)
error('Optimization failed')
end
best_val = - best_val;
end
function [f, df] = subfun(x, fun,dims, x0)
if ~isempty(dims)
x0(dims) = x;
[f, df] = fun(x0);
df = df(:,dims);
else
[f, df] = fun(x);
end
f = -f;
df = -df;
end
end
end