-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoptimize_AF.m
106 lines (88 loc) · 2.1 KB
/
optimize_AF.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
102
103
104
105
106
function [best_x, best_val] = optimize_AF(fun, lb, ub, ncandidates, init_guess, options, varargin)
% To optimize acquisition functions
opts = namevaluepairtostruct(struct( ...
'dims', [], ...
'objective', 'max' ...
), varargin);
UNPACK_STRUCT(opts, false)
%% Multistart seach with minFunc
s.verbose = 0;
DEFAULT('ncandidates',10);
DEFAULT('options', s);
best_x= [];
x0 = lb;
f = @(x) subfun(x, fun,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(x0,1);
d = size(lb,1);
if isempty(dims)
dims = 1:D;
end
p = haltonset(d,'Skip',1e3,'Leap',1e2);
p = scramble(p,'RR2');
q = qrandstream(p);
n0 = 500;
sampSize = 1;
X = x0.*ones(1, n0);
v = zeros(1,n0);
for i = 1:n0
X(dims,i) = qrand(q,sampSize);
v(i) = fun(X(:,i));
end
temperature = 1;
p = exp(temperature*zscore(v));
p = p/sum(p);
starting_points = X(dims,randsample(n0, ncandidates, true, p));
%starting_points = rand_interval(lb, ub, 'nsamples', ncandidates);
if ~isempty(init_guess)
starting_points(:,1)= init_guess;
end
if strcmp(objective, 'max')
best_val=-inf;
elseif strcmp(objective, 'min')
best_val=inf;
end
x = [];
for k = 1:ncandidates
try
x = minConf_TMP(@(x)f(x), starting_points(:,k), lb(:), ub(:), options);
if any(isnan(x(:)))
error('x is NaN')
end
val = f(x);
if strcmp(objective, 'max')
val = -val;
end
if (strcmp(objective, 'min') && val < best_val) || (strcmp(objective, 'max') && best_val < 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
x0(dims) = best_x;
best_x = x0;
end
function [f, df] = subfun(x, fun,dims, x0, objective)
if ~isempty(dims)
x0(dims) = x;
[f, df] = fun(x0);
df = df(dims);
else
[f, df] = fun(x);
df = df(:);
end
if strcmp(objective, 'max')
f = -f;
df = -df;
end
end