-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathturbo_1.py
362 lines (311 loc) · 15.1 KB
/
turbo_1.py
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
###############################################################################
# Copyright (c) 2019 Uber Technologies, Inc. #
# #
# Licensed under the Uber Non-Commercial License (the "License"); #
# you may not use this file except in compliance with the License. #
# You may obtain a copy of the License at the root directory of this project. #
# #
# See the License for the specific language governing permissions and #
# limitations under the License. #
###############################################################################
import math
import sys
from copy import deepcopy
import gpytorch
import numpy as np
import torch
from torch.quasirandom import SobolEngine
from gp import train_gp
from utils import from_unit_cube, latin_hypercube, to_unit_cube
class Turbo1:
"""The TuRBO-1 algorithm.
Parameters
----------
f : function handle
lb : Lower variable bounds, numpy.array, shape (d,).
ub : Upper variable bounds, numpy.array, shape (d,).
n_init : Number of initial points (2*dim is recommended), int.
max_evals : Total evaluation budget, int.
batch_size : Number of points in each batch, int.
verbose : If you want to print information about the optimization progress, bool.
use_ard : If you want to use ARD for the GP kernel.
max_cholesky_size : Largest number of training points where we use Cholesky, int
n_training_steps : Number of training steps for learning the GP hypers, int
min_cuda : We use float64 on the CPU if we have this or fewer datapoints
device : Device to use for GP fitting ("cpu" or "cuda")
dtype : Dtype to use for GP fitting ("float32" or "float64")
Example usage:
turbo1 = Turbo1(f=f, lb=lb, ub=ub, n_init=n_init, max_evals=max_evals)
turbo1.optimize() # Run optimization
X, fX = turbo1.X, turbo1.fX # Evaluated points
"""
def __init__(
self,
f,
lb,
ub,
n_init,
max_evals,
batch_size=1,
verbose=True,
use_ard=True,
max_cholesky_size=2000,
n_training_steps=50,
min_cuda=1024,
device="cpu",
dtype="float64",
):
# Very basic input checks
assert lb.ndim == 1 and ub.ndim == 1
assert len(lb) == len(ub)
assert np.all(ub > lb)
assert max_evals > 0 and isinstance(max_evals, int)
assert n_init > 0 and isinstance(n_init, int)
assert batch_size > 0 and isinstance(batch_size, int)
assert isinstance(verbose, bool) and isinstance(use_ard, bool)
assert max_cholesky_size >= 0 and isinstance(batch_size, int)
assert n_training_steps >= 30 and isinstance(n_training_steps, int)
assert max_evals > n_init and max_evals > batch_size
assert device == "cpu" or device == "cuda"
assert dtype == "float32" or dtype == "float64"
if device == "cuda":
assert torch.cuda.is_available(), "can't use cuda if it's not available"
# Save function information
self.f = f
self.dim = len(lb)
self.lb = lb
self.ub = ub
# Settings
self.n_init = n_init
self.max_evals = max_evals
self.batch_size = batch_size
self.verbose = verbose
self.use_ard = use_ard
self.max_cholesky_size = max_cholesky_size
self.n_training_steps = n_training_steps
# Hyperparameters
self.mean = np.zeros((0, 1))
self.signal_var = np.zeros((0, 1))
self.noise_var = np.zeros((0, 1))
self.lengthscales = np.zeros((0, self.dim)) if self.use_ard else np.zeros((0, 1))
# Tolerances and counters
# no of candidates
self.n_cand = min(100 * self.dim, 5000)
self.failtol = np.ceil(np.max([4.0 / batch_size, self.dim / batch_size]))
self.succtol = 3
self.n_evals = 0
# distance between elements in batch
self.ele_distance = 1e-2
# Trust region sizes
self.length_min = 0.5 ** 7
self.length_max = 1.6
self.length_init = 0.8
# Save the full history
# observations for whole search space
self.X = np.zeros((0, self.dim))
self.fX = np.zeros((0, 1))
# Device and dtype for GPyTorch
self.min_cuda = min_cuda
self.dtype = torch.float32 if dtype == "float32" else torch.float64
self.device = torch.device("cuda") if device == "cuda" else torch.device("cpu")
if self.verbose:
print("Using dtype = %s \nUsing device = %s" % (self.dtype, self.device))
sys.stdout.flush()
# Initialize parameters
self._restart()
def _restart(self):
self._X = []
self._fX = []
self.failcount = 0
self.succcount = 0
self.length = self.length_init
def _adjust_length(self, fX_next):
# this region is good -> try to explore around
if np.min(fX_next) < np.min(self._fX) - 1e-3 * math.fabs(np.min(self._fX)):
print("good region")
self.succcount += 1
self.failcount = 0
# this region is not good -> try to search in a smaller region
else:
print("bad region")
self.succcount = 0
self.failcount += 1
if self.succcount == self.succtol: # Expand trust region
print("expand region")
self.length = min([2.0 * self.length, self.length_max])
self.succcount = 0
elif self.failcount == self.failtol: # Shrink trust region
print("shrink region")
self.length /= 2.0
self.failcount = 0
def _create_candidates(self, X, fX, length, n_training_steps, hypers):
"""Generate candidates assuming X has been scaled to [0,1]^d."""
# NOTE: X and fX here are local observations of a trusted region
# Pick the center as the point with the smallest function values
# NOTE: This may not be robust to noise, in which case the posterior mean of the GP can be used instead
assert X.min() >= 0.0 and X.max() <= 1.0
# Standardize function values.
mu, sigma = np.median(fX), fX.std()
sigma = 1.0 if sigma < 1e-6 else sigma
fX = (deepcopy(fX) - mu) / sigma
# Figure out what device we are running on
if len(X) < self.min_cuda:
device, dtype = torch.device("cpu"), torch.float64
else:
device, dtype = self.device, self.dtype
# We use CG + Lanczos for training if we have enough data
with gpytorch.settings.max_cholesky_size(self.max_cholesky_size):
X_torch = torch.tensor(X).to(device=device, dtype=dtype)
y_torch = torch.tensor(fX).to(device=device, dtype=dtype)
gp = train_gp(
train_x=X_torch, train_y=y_torch, use_ard=self.use_ard, num_steps=n_training_steps, hypers=hypers
)
# Save state dict
hypers = gp.state_dict()
# Create the trust region boundaries
# in current local observations, select point with minimum function value to be the center of trusted region
x_center = X[fX.argmin().item(), :][None, :]
weights = gp.covar_module.base_kernel.lengthscale.cpu().detach().numpy().ravel()
weights = weights / weights.mean() # This will make the next line more stable
weights = weights / np.prod(np.power(weights, 1.0 / len(weights))) # We now have weights.prod() = 1
lb = np.clip(x_center - weights * length / 2.0, 0.0, 1.0)
ub = np.clip(x_center + weights * length / 2.0, 0.0, 1.0)
# Draw a Sobolev sequence in [lb, ub]
seed = np.random.randint(int(1e6))
sobol = SobolEngine(self.dim, scramble=True, seed=seed)
pert = sobol.draw(self.n_cand).to(dtype=dtype, device=device).cpu().detach().numpy()
pert = lb + (ub - lb) * pert
# Create a perturbation mask
prob_perturb = min(20.0 / self.dim, 1.0)
mask = np.random.rand(self.n_cand, self.dim) <= prob_perturb
ind = np.where(np.sum(mask, axis=1) == 0)[0]
mask[ind, np.random.randint(0, self.dim - 1, size=len(ind))] = 1
# Create candidate points
X_cand = x_center.copy() * np.ones((self.n_cand, self.dim))
X_cand[mask] = pert[mask]
# Figure out what device we are running on
if len(X_cand) < self.min_cuda:
device, dtype = torch.device("cpu"), torch.float64
else:
device, dtype = self.device, self.dtype
# We may have to move the GP to a new device
gp = gp.to(dtype=dtype, device=device)
# We use Lanczos for sampling if we have enough data
with torch.no_grad(), gpytorch.settings.max_cholesky_size(self.max_cholesky_size):
X_cand_torch = torch.tensor(X_cand).to(device=device, dtype=dtype)
y_cand = gp.likelihood(gp(X_cand_torch)).sample(torch.Size([self.batch_size])).t().cpu().detach().numpy()
# Remove the torch variables
del X_torch, y_torch, X_cand_torch, gp
# De-standardize the sampled values
y_cand = mu + sigma * y_cand
return X_cand, y_cand, hypers
def _select_candidates(self, X_cand, y_cand):
"""Select candidates."""
X_next = np.ones((self.batch_size, self.dim))
y_next_old = np.zeros(self.batch_size)
y_next_new = np.zeros(self.batch_size)
# print("len X_cand: {}".format(X_cand.shape))
# print("len y_cand: {}".format(y_cand.shape))
# select <batch_size> points having the smallest predicted function values
for i in range(self.batch_size):
# Pick the best point and make sure we never pick it again
if i == 0:
indbest = np.argmin(y_cand[:, i])
# print("element: {}, indbest: {}".format(i, indbest))
y_next_old[i] = y_cand[indbest, i]
else:
indbest = np.argmin(y_cand[:, i])
# print("element: {}, OLD indbest: {}".format(i, indbest))
y_next_old[i] = y_cand[indbest, i]
# print("y_cand[indbest, i]: {}".format(round(y_cand[indbest, i], 3)))
# print("y_next_new: {}".format(np.around(y_next_new, 3)))
# compute distance
distance = abs(y_cand[indbest, i] - y_next_new)
distance = np.around(distance, 3)
# print("distance: {}".format(distance))
search_all = False
for j in range(y_cand.shape[0]):
if any(distance < self.ele_distance):
# print("very closed to a suggested point, find another point")
# find second smallest
y_cand[indbest, :] = np.inf
indbest = np.argmin(y_cand[:, i])
# (batch_size - 1) points already are inf, for loop will stop
if j == (y_cand.shape[0] - self.batch_size - 1):
indsecondbest = indbest
search_all = True
# print("element: {}, NEW indbest: {}".format(i, indbest))
# compute distance
distance = abs(y_cand[indbest, i] - y_next_new)
distance = np.around(distance, 3)
# print("y_cand[indbest, i]: {}".format(round(y_cand[indbest, i], 3)))
# print("y_next_new: {}".format(np.around(y_next_new, 3)))
# print("distance: {}".format(distance))
else:
break
# print("no of times need to find another point: {}".format(j))
if search_all == True:
print("search all candidates, but cannot find another suggested point")
indbest = indsecondbest
# print("element: {}, NEW indbest: {}".format(i, indbest))
X_next[i, :] = deepcopy(X_cand[indbest, :])
y_next_new[i] = y_cand[indbest, i]
# after selecting this point, we set its function value to be a very high number
# to skip selecting it again
y_cand[indbest, :] = np.inf
# print("X_next: {}".format(np.around(X_next, 2)))
print("y_next_old: {}".format(np.around(y_next_old, 2)))
print("y_next_new: {}".format(np.around(y_next_new, 2)))
return X_next
def optimize(self):
"""Run the full optimization process."""
while self.n_evals < self.max_evals:
if len(self._fX) > 0 and self.verbose:
n_evals, fbest = self.n_evals, self._fX.min()
print(f"{n_evals}) Restarting with fbest = {fbest:.4}")
sys.stdout.flush()
# Initialize parameters
self._restart()
# Generate and evalute initial design points
X_init = latin_hypercube(self.n_init, self.dim)
X_init = from_unit_cube(X_init, self.lb, self.ub)
fX_init = np.array([[self.f(x)] for x in X_init])
# Update budget and set as initial data for this TR
self.n_evals += self.n_init
self._X = deepcopy(X_init)
self._fX = deepcopy(fX_init)
# Append data to the global history
self.X = np.vstack((self.X, deepcopy(X_init)))
self.fX = np.vstack((self.fX, deepcopy(fX_init)))
if self.verbose:
fbest = self._fX.min()
print(f"Starting from fbest = {fbest:.4}")
sys.stdout.flush()
# Thompson sample to get next suggestions
while self.n_evals < self.max_evals and self.length >= self.length_min:
# Warp inputs
X = to_unit_cube(deepcopy(self._X), self.lb, self.ub)
# Standardize values
fX = deepcopy(self._fX).ravel()
# Create th next batch
X_cand, y_cand, _ = self._create_candidates(
X, fX, length=self.length, n_training_steps=self.n_training_steps, hypers={}
)
X_next = self._select_candidates(X_cand, y_cand)
# Undo the warping
X_next = from_unit_cube(X_next, self.lb, self.ub)
# Evaluate batch
fX_next = np.array([[self.f(x)] for x in X_next])
# Update trust region
self._adjust_length(fX_next)
# Update budget and append data
self.n_evals += self.batch_size
self._X = np.vstack((self._X, X_next))
self._fX = np.vstack((self._fX, fX_next))
if self.verbose and fX_next.min() < self.fX.min():
n_evals, fbest = self.n_evals, fX_next.min()
print(f"{n_evals}) New best: {fbest:.4}")
sys.stdout.flush()
# Append data to the global history
self.X = np.vstack((self.X, deepcopy(X_next)))
self.fX = np.vstack((self.fX, deepcopy(fX_next)))