-
Notifications
You must be signed in to change notification settings - Fork 1
/
GPSO_Tree.m
362 lines (296 loc) · 11.1 KB
/
GPSO_Tree.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
classdef GPSO_Tree < handle
%
% Free software provided under AGPLv3 license (see README).
% Copyright Jonathan Hadida ([email protected]), July 2017.
properties (SetAccess = private)
level % struct-array storing tree contents level by level
Nl,Ns % number of leaves/splits
end
properties (Transient,Dependent)
depth
end
methods
function self = GPSO_Tree()
self.clear();
end
function self=clear(self)
self.level = [];
self.Nl = 0;
self.Ns = 0;
end
% serialise data to be saved
function D = serialise(self)
F = {'level','Nl','Ns'};
n = numel(F);
D = struct();
for i = 1:n
f = F{i};
D.(f) = self.(f);
end
D.version = '0.2';
end
function self=unserialise(self,D)
F = {'level','Nl','Ns'};
n = numel(F);
for i = 1:n
f = F{i};
self.(f) = D.(f);
end
switch D.version
case '0.1'
% used to store level and index, but level is useless since it's always the one just above
for h = 1:self.depth
assert( all( self.level(h).parent(:,1) == h-1 ), 'Bad ancestry record.' );
self.level(h).parent = self.level(h).parent(:,2)';
end
if self.depth
self.level(1).parent = 0; % root index is 0
end
end
end
function self=init(self,ndim,rid)
%
% ndim: dimensionality of search space
% rid: storage index of tree root (centre of hyperdomain)
%
if nargin < 3, rid=1; end
% initialise tree
T.parent = 0;
T.lower = zeros(1,ndim);
T.upper = ones(1,ndim);
T.samp = rid;
T.leaf = true;
self.level = T;
self.Nl = 1;
self.Ns = 0;
end
function d = get.depth(self)
d = numel(self.level);
end
function w = width(self,h)
w = numel(self.level(h).samp);
end
% quick access
function p = parent(self,h,k)
p = self.level(h).parent(k);
end
function l = lower(self,h,k)
l = self.level(h).lower(k,:);
end
function u = upper(self,h,k)
u = self.level(h).upper(k,:);
end
function s = samp(self,h,k)
s = self.level(h).samp(k);
end
function l = leaf(self,h,k)
l = self.level(h).leaf(k);
end
% works with k vector, but h should be scalar
% not super efficient, don't use too often
function n = node(self,h,k)
n.parent = self.parent(h,k);
n.lower = self.lower(h,k);
n.upper = self.upper(h,k);
n.samp = self.samp(h,k);
n.leaf = self.leaf(h,k);
end
function child=split(self,h,k,srgt,xmet,xprm)
%
% h,k: level+id of node to split
% srgt: surrogate object
% xmet,xprm: exploration options
%
% make sure it's a leaf
assert( self.leaf(h,k), '[bug] Splitting non-leaf node.' );
% select exploration method
switch lower(xmet)
case {'tree','grow'}
vcat = @(x) vertcat(x.coord);
xfun = @(node) vcat(self.grow(node,xprm));
case {'samp','urand','sample'}
xfun = @(node) self.sample(node,xprm);
otherwise
error( 'Unknown exploration method %s', xmet );
end
% children are in the next level
m = h+1;
if m > self.depth % initialise if necessary
self.level(m).leaf = true(0);
end
% split node along largest dimension
parent = self.node(h,k);
child = recursive_split( parent );
% evaluate each new leaf
nc = 3; % == numel(child)
varsigma = srgt.get_varsigma();
[~,child(1).best] = srgt.gp_eval( xfun(child(1)), varsigma );
[~,child(2).best] = srgt.gp_eval( xfun(child(2)), varsigma );
[~,child(3).best] = srgt.gp_eval( xfun(child(3)), varsigma );
% insert children into surrogate
sid = srgt.append( vertcat(child.coord), vertcat(child.best), true );
% insert children into tree
self.level(m).parent = [self.level(m).parent, k*ones(1,nc)];
self.level(m).lower = [self.level(m).lower; vertcat(child.lower)];
self.level(m).upper = [self.level(m).upper; vertcat(child.upper)];
self.level(m).samp = [self.level(m).samp, sid];
self.level(m).leaf = [self.level(m).leaf, true(1,nc)];
% parent is no longer a leaf
self.level(h).leaf(k) = false;
% update split+leaf counts
self.Ns = self.Ns+1;
self.Nl = self.Nl+nc-1;
end
function children = grow(self,node,d)
%
% node: node structure, or [h,k] vector
% d: depth of the subtree to grow
% (WARNING: tree grows exponentially fast!)
%
% Grow tree from node (h,k) without saving anything.
% Return a struct-array of children nodes with fields {lower,upper,coord}.
%
% NOTE: coordinates are NORMALISED here
%
dk.assert( d <= 8, [ ...
'This is safeguard error to prevent deep tree explorations.\n' ...
'If you meant to set the option xmet="tree" with a depth of %d (%d samples),\n' ...
'then please comment this message in the method grow.\n' ...
], d, 3^d );
if ~isstruct(node)
node = self.node( node(1), node(2) );
end
node.coord = (node.lower + node.upper)/2;
children = recursive_split( node, d );
end
function points = sample(self,node,ns)
%
% node: node structure, or [h,k] vector
% ns: number of random points to sample
%
% Sample n points within node (h,k) uniformly randomly.
%
% NOTE: coordinates are NORMALISED here
%
if ~isstruct(node)
node = self.node( node(1), node(2) );
end
nd = numel(node.upper);
delta = node.upper - node.lower;
points = bsxfun( @times, rand(ns,nd), delta );
points = bsxfun( @plus, points, node.lower );
end
function T = export_compact(self)
%
% Export tree as a structure with fields:
%
% parent Index of the parent node (in that array, not in this object).
% children Zero for leaf-nodes, otherwise index of first child.
% sample Index of the corresponding surrogate sample.
% depth Depth of the node in the tree.
% order Index of youth (higher values mean younger).
%
% Note: children are necessarily next to each other, so if C is the index of the first child,
% then the other children are C+1 and C+2.
%
% JH
d = self.depth; % tree depth
w = arrayfun( @(x) numel(x.samp), self.level ); % width of each level
n = sum(w); % total number of nodes
% allocate output
T = zeros(1,n);
T = struct( 'n', n, 'd', d, ...
'parent', T, 'children', T, 'sample', T, 'depth', T, 'order', T ...
);
% first pass:
% set depth, sample indices, and re-index parents
b = 1;
e = 0;
for h = 1:d
o = b-1;
b = e+1;
e = b+w(h)-1;
T.parent(b:e) = o + self.level(h).parent;
T.sample(b:e) = self.level(h).samp;
T.depth(b:e) = h;
end
% second pass: set children
for i = 1:n
p = T.parent(i);
if p>0 && T.children(p)==0
T.children(p) = i;
end
end
% third pass: set order
for i = 2:3:n
k = i + [0 1 2];
T.order(k) = max(T.sample(k));
end
T.order(1) = T.sample(1); % set the root
% re-index the order
u = unique(T.order);
r(u) = 1:numel(u);
T.order = r(T.order);
end
function T = export_dkTree(self)
%
% Export as dk.ds.Tree instance.
C = self.export_compact();
C.index = ones(1,C.n);
T = dk.ds.Tree( struct('sid', C.sample(1), 'order', C.order(1)) );
for d = 2:C.d
k = find(C.depth == d);
n = numel(k);
for i = 1:n
ki = k(i);
pi = C.parent(ki);
C.index(ki) = T.add_node( C.index(pi), 'sid', C.sample(ki), 'order', C.order(ki) );
end
end
end
end
end
%
% node.lower node.upper
% Lvl \ /
% h: =---------node----------=
%
%
% h+1: =---L---=---M---=---R---=
% / | | \
% Pmin Lmax Rmin Pmax
%
function children = recursive_split(node,count)
if nargin < 2, count=1; end
% bounds of parent node
Pmin = node.lower;
Pmax = node.upper;
% halting condition
if count == 0
children = node;
return;
end
% barycenter of children subintervals
M = (Pmin + Pmax) / 2;
L = M;
R = M;
[~,s] = max( Pmax - Pmin ); % split along largest dimension
L(s) = (5*Pmin(s) + Pmax(s))/6;
R(s) = ( Pmin(s) + 5*Pmax(s))/6;
% compute bounds of children
Lmax = Pmax;
Rmin = Pmin;
Mmin = Pmin;
Mmax = Pmax;
Lmax(s) = (2*Pmin(s) + Pmax(s))/3.0;
Rmin(s) = ( Pmin(s) + 2*Pmax(s))/3.0;
Mmin(s) = Lmax(s);
Mmax(s) = Rmin(s);
% pack as struct-array
make_node = @(ll,uu,xx) struct('lower',ll,'upper',uu,'coord',xx,'sdim',s);
children = [ ... WARNING: Order matters!
recursive_split(make_node(Pmin,Lmax,L), count-1), ... left
recursive_split(make_node(Rmin,Pmax,R), count-1), ... right
recursive_split(make_node(Mmin,Mmax,M), count-1) ... middle
];
end