-
Notifications
You must be signed in to change notification settings - Fork 0
/
bo.py
374 lines (302 loc) · 12.8 KB
/
bo.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
363
364
365
366
367
368
369
370
371
372
373
374
import os, shutil
import traceback
import math
import numpy as np
from dataclasses import dataclass
from dataclasses import field
import torch
import torch.nn.functional as F
from botorch.acquisition import qExpectedImprovement
from botorch.acquisition import qUpperConfidenceBound
from botorch.acquisition import UpperConfidenceBound
from botorch.fit import fit_gpytorch_model
from botorch.generation import MaxPosteriorSampling
from botorch.models import FixedNoiseGP, SingleTaskGP
from botorch.optim import optimize_acqf
from botorch.utils.transforms import normalize
from botorch.utils.transforms import unnormalize
from torch.quasirandom import SobolEngine
import gpytorch
from gpytorch.constraints import Interval
from gpytorch.likelihoods import GaussianLikelihood
from gpytorch.mlls import ExactMarginalLogLikelihood
from gpytorch.priors import HorseshoePrior
from typing import List, Optional, Tuple
from botorch.test_functions.base import BaseTestProblem
from torch import Tensor
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
def get_initial_points(dim, n_pts):
#sobol = SobolEngine(dimension=dim, scramble=True, seed=0)
sobol = SobolEngine(dimension=dim, scramble=True)
X_init = sobol.draw(n=n_pts).to(dtype=dtype, device=device)
return X_init
#test = get_initial_points(3, 5)
#print(test)
#fun_V = VASP(dim=3, negate=False).to(dtype=dtype, device=device)
#fun_V.bounds[0, :] = torch.tensor([-0.002,-0.002,-0.002])
#fun_V.bounds[1, :] = torch.tensor([0.002, 0.002, 0.002])
#dum = unnormalize(test, fun_V.bounds)
#print(dum)
def read_amp(root_path):
with open(root_path+'dict_candi') as f:
candi_lines = f.readlines()
_X = []
_Y = []
for i in candi_lines:
dum = []
info = i.split()
file_name = info[0]
x = np.array([float(num) for num in info[1].split('_') if num])
x_term = convert_amp(x)
for sublist in x_term:
for item in sublist:
dum.append(item)
y = float(info[2])
_X.append(dum)
_Y.append(y)
#candi_dict[file_name] = [x_term, y]
X = np.array(_X); Y = np.array(_Y)
return [X, Y]
def convert_amp(x):
ux = x[0]
uy = x[1]
uz = x[2]
# ux = 0
# uy = 0
# uz = 0
n1 = x[3]
n2 = x[4]
n3 = x[5]
# n4 = 0
# n5 = 0
# n6 = 0
n4 = x[6]*2
n5 = x[7]*2
n6 = x[8]*2
#soft = [ux**2+uy**2+uz**2, (ux**2+uy**2+uz**2)**2, ux**2*uy**2+uy**2*uz**2+uz**2*ux**2, (ux**2+uy**2+uz**2)**3, ux**4*(uy**2+uz**2) + uy**4*(ux**2+uz**2) + uz**4*(ux**2+uy**2), ux**2*uy**2*uz**2, (ux**2+uy**2+uz**2)**4]
soft = [ux**2+uy**2+uz**2, (ux**2+uy**2+uz**2)**2, ux**2*uy**2+uy**2*uz**2+uz**2*ux**2]
elas = [0.5*(n1**2+n2**2+n3**2), n1*n2+n2*n3+n3*n1, 0.5*(n4**2+n5**2+n6**2)]
#elas = [0.5*(n1**2+n2**2+n3**2)*127, (n1*n2+n2*n3+n3*n1)*40, 0.5*(n4**2+n5**2+n6**2)*50]
inter = [0.5*(n1*ux**2+n2*uy**2+n3*uz**2), 0.5*(n1*(uy**2+uz**2)+n2*(uz**2+ux**2)+n3*(ux**2+uy**2)), n4*uy*uz+n5*uz*ux+n6*ux*uy]
# dummy = [n1+n2+n3]
#dummy = [n1+n2+n3, n1*ux+n2*uy+n3*uz, n1*(uy+uz)+n2*(uz+ux)+n3*(ux+uy)]
#dummy = [n1+n2+n3, ux*uy+uy*uz+uz*ux]
#dummy = [n1+n2+n3, n1*ux+n2*uy+n3*uz, n1*(uy+uz)+n2*(uz+ux)+n3*(ux+uy), n4*(uy*uz)**2+n5*(uz*ux)**2+n6*(ux*uy)**2]
#x_term = torch.tensor([soft[0], soft[1], soft[2], elas[0], elas[1], elas[2], inter[0], inter[1], inter[2]], dtype=dtype, device=device)
return [soft, elas, inter]
def read_cof(root_path):
with open(root_path+'results.txt') as f:
cof_lines = f.readlines()
info = cof_lines[-1].split()
cof = np.array([float(num) for num in info[0].split('_') if num])
return cof
def Linear_search(init_number):
if restart is True:
L_points = read_amp(root_path)
#print("L_points[0]", L_points[0])
lin = LinearRegression()
reg = lin.fit(L_points[0], L_points[1])
#info = Linear_fitting(torch.tensor(L_points[0], dtype=dtype, device=device), torch.tensor(L_points[1], dtype=dtype, device=device), lr=0.1, epochs_num=20000, method = 'SGD')
#info = Linear_fitting(L_points[0], L_points[1], lr, epochs_num, method = 'Adam')
if restart is not True:
print('Intialization')
X_turbo_V = get_initial_points(dim_V, init_number)
Y_turbo_V = calculate_Vasp(unnormalize(X_turbo_V, fun_V.bounds)).unsqueeze(-1)
L_points = read_amp(root_path)
lin = LinearRegression()
reg = lin.fit(L_points[0], L_points[1])
#info = Linear_fitting(L_points[0], L_points[1], lr, epochs_num, method = 'Adam')
#info = Linear_fitting(torch.tensor(L_points[0], dtype=dtype, device=device), torch.tensor(L_points[1], dtype=dtype, device=device), lr=0.1, epochs_num=20000, method = 'SGD')
#return {'X_best': info['Parameters'], 'Best_value': info['Loss']}
return {'Coefficient': reg.coef_, 'Intercept': reg.intercept_}
def BO_search_Vasp(acqf = 'qucb', hpar = 0.5):
batch_size = 2
n_init = 8 # 2*dim, which corresponds to 5 batches of 4
###### get Vasp parameters######
turbo_info = restarts_BO_V(root_path)
X_turbo_V = turbo_info[0]
Value_true_turbo_V = turbo_info[1]
Value_fit_turbo_V = eval_objective_Vasp(X_turbo_V).unsqueeze(-1)
Y_turbo_V = torch.abs(torch.sub(Value_true_turbo_V, Value_fit_turbo_V))
state = TurboState(dim_V, batch_size=batch_size, length=10, length_min=9, length_max=40, success_tolerance=2)
NUM_RESTARTS = 10 if not SMOKE_TEST else 2
RAW_SAMPLES = 4096 if not SMOKE_TEST else 4
N_CANDIDATES = min(5000, max(2000, 200 * dim_V)) if not SMOKE_TEST else 4
#N_CANDIDATES = max(5000, max(2000, 200 * dim)) if not SMOKE_TEST else 4
try:
# Fit a GP model
train_Y_V = (Y_turbo_V - Y_turbo_V.mean()) / Y_turbo_V.std()
likelihood = GaussianLikelihood(noise_constraint=Interval(1e-7, 1e-2))
model = SingleTaskGP(X_turbo_V, train_Y_V, likelihood=likelihood)
mll = ExactMarginalLogLikelihood(model.likelihood, model)
fit_gpytorch_model(mll)
if acqf == 'ucb':
batch_size = 1
# Create a batch
X_info = generate_batch(
state=state,
model=model,
X=X_turbo_V,
Y=train_Y_V,
batch_size=batch_size,
n_candidates=N_CANDIDATES,
num_restarts=NUM_RESTARTS,
raw_samples=RAW_SAMPLES,
acqf=acqf,
hpar=hpar
)
X_next = X_info['X_next']
acq = X_info['acq'].numpy()
#print(acq)
print('\n--------------------------------------------\n')
print(f"{len(X_turbo_V)}) X_best_V: {unnormalize(X_next, fun_V.bounds)}")
return {'X_best': unnormalize(X_next, fun_V.bounds),'acq':acq}
except Exception as e:
print('Error returned')
traceback.print_exc()
finally:
return {'X_best': unnormalize(X_next, fun_V.bounds),'acq':acq}
fun_V = VASP(dim=9, negate=False).to(dtype=dtype, device=device)
fun_V.bounds[0, :] = torch.tensor([0.000, 0.000, 0.000, -0.015, -0.015, -0.015, -0.008, -0.008, -0.008])
fun_V.bounds[1, :] = torch.tensor([0.175, 0.175, 0.175, 0.015, 0.015, 0.015, 0.008, 0.008, 0.008])
print(fun_V.bounds)
dim_V = fun_V.dim
def eval_objective_Vasp(x):
"""This is a helper function we use to unnormalize and evalaute a point"""
return fun_V(unnormalize(x, fun_V.bounds))
@dataclass
class TurboState:
dim: int
batch_size: int
length: float = 10
length_min: float = 0.1 ** 7
length_max: float = 100
failure_counter: int = 0
failure_tolerance: int = float("nan") # Note: Post-initialized
success_counter: int = 0
success_tolerance: int = 3 # Note: The original paper uses 3
best_value: float = -float("inf")
#best_n_value: List[float]=field(default_factory=list)
restart_triggered: bool = False
def __post_init__(self):
self.failure_tolerance = math.ceil(
max([4.0 / self.batch_size, float(self.dim) / self.batch_size])
)
def update_state(state, Y_next):
if max(Y_next) > state.best_value + 1e-3 * math.fabs(state.best_value):
state.success_counter += 1
state.failure_counter = 0
else:
state.success_counter = 0
state.failure_counter += 1
if state.success_counter == state.success_tolerance: # Expand trust region
state.length = min(2.0 * state.length, state.length_max)
state.success_counter = 0
elif state.failure_counter == state.failure_tolerance: # Shrink trust region
state.length /= 2.0
state.failure_counter = 0
state.best_value = max(state.best_value, max(Y_next).item())
#Y_top = torch.topk(Y_next.squeeze(-1), k=math.ceil(state.batch_size/4), dim=0, largest=True)[0]
#print(state.best_n_value)
#state.best_n_value = torch.topk(torch.cat((torch.tensor(state.best_n_value, dtype=dtype, device=device), Y_top),0), k=math.ceil(state.batch_size/4), dim=0, largest=True)[0].cpu().tolist()
#numpy.maximum(state.best_n_value, torch.topk(Y_next.squeeze(-1), k=math.ceil(state.batch_size/4), dim=0, largest=True)[0].cpu())
#print(state.best_value, state.best_n_value)
if state.length < state.length_min:
state.restart_triggered = True
return state
def restarts_BO_V(root_path, file='dict_candi'):
with open(root_path+file, 'r') as f:
candi_lines = f.readlines()
candi_dict = {}
for i in candi_lines:
info = i.split()
file_name = info[0]
x = [float(num) for num in info[1].split('_') if num]
y = float(info[2])
candi_dict[file_name] = [x, y]
folders = os.popen('ls '+path).read()
#print(folders)
tensor_x = []
tensor_y = []
for i in candi_dict:
x = candi_dict[i][0]
y = candi_dict[i][1]
tensor_x.append(x)
tensor_y.append(-y)
X = normalize(torch.tensor(tensor_x, dtype=dtype, device=device), fun_V.bounds)
Y = torch.tensor(tensor_y, dtype=dtype, device=device).unsqueeze(-1)
return [X, Y]
def generate_batch(
state,
model, # GP model
X, # Evaluated points on the domain [0, 1]^d
Y, # Function values
batch_size,
n_candidates=None, # Number of candidates for Thompson sampling
num_restarts=2,
raw_samples=512,
acqf="qucb", # "ei" or "ts"
hpar=10000,
):
assert acqf in ("ts", "ei", "ucb","qucb")
assert X.min() >= 0.0 and X.max() <= 1.0 and torch.all(torch.isfinite(Y))
if n_candidates is None:
n_candidates = min(5000, max(2000, 200 * X.shape[-1]))
# Scale the TR to be proportional to the lengthscales
x_center = X[Y.argmax(), :].clone()
weights = model.covar_module.base_kernel.lengthscale.squeeze().detach()
weights = weights / weights.mean()
weights = weights / torch.prod(weights.pow(1.0 / len(weights)))
tr_lb = torch.clamp(x_center - weights * state.length / 2.0, 0.0, 1.0)
tr_ub = torch.clamp(x_center + weights * state.length / 2.0, 0.0, 1.0)
if acqf == "ts":
dim = X.shape[-1]
sobol = SobolEngine(dim, scramble=True)
pert = sobol.draw(n_candidates).to(dtype=dtype, device=device)
pert = tr_lb + (tr_ub - tr_lb) * pert
# Create a perturbation mask
prob_perturb = min(20.0 / dim, 1.0)
mask = (
torch.rand(n_candidates, dim, dtype=dtype, device=device)
<= prob_perturb
)
ind = torch.where(mask.sum(dim=1) == 0)[0]
mask[ind, torch.randint(0, dim - 1, size=(len(ind),), device=device)] = 1
# Create candidate points from the perturbations and the mask
X_cand = x_center.expand(n_candidates, dim).clone()
X_cand[mask] = pert[mask]
# Sample on the candidate points
thompson_sampling = MaxPosteriorSampling(model=model, replacement=False)
X_next = thompson_sampling(X_cand, num_samples=batch_size)
elif acqf == "ei":
ei = qExpectedImprovement(model, train_Y.max(), maximize=True)
X_next, acq_value = optimize_acqf(
ei,
bounds=torch.stack([tr_lb, tr_ub]),
q=batch_size,
num_restarts=num_restarts,
raw_samples=raw_samples,
)
elif acqf == "qucb":
qucb = qUpperConfidenceBound(model, hpar)
X_next, acq_value = optimize_acqf(
qucb,
bounds=torch.stack([tr_lb, tr_ub]),
q=batch_size,
num_restarts=num_restarts,
raw_samples=raw_samples,
)
elif acqf == "ucb":
ucb = UpperConfidenceBound(model, beta = hpar)
#ucb = UpperConfidenceBound_spec(model, beta = hpar)
X_next, acq_value = optimize_acqf(
ucb,
bounds=torch.stack([tr_lb, tr_ub]),
q=batch_size,
num_restarts=num_restarts,
raw_samples=raw_samples,
)
return {'X_next': X_next, 'acq':acq_value}
if __name == "__main__":
print('BO test')