-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbayes_op.py
1139 lines (1021 loc) · 48.6 KB
/
bayes_op.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import numpy as np
import torch
from gp_utils import BoTorchGP
from botorch.acquisition.analytic import ExpectedImprovement, ProbabilityOfImprovement
from botorch.optim.initializers import initialize_q_batch_nonneg
from sampling import EfficientThompsonSampler
import sobol_seq
'''
This script implements all Bayesian Optimization methods we compared in the paper.
'''
class UCBwLP():
def __init__(self, env, initial_temp = None, beta = None, lipschitz_constant = 1, num_of_starts = 75, num_of_optim_epochs = 150, \
hp_update_frequency = None):
'''
Upper Confidence Bound with Local Penalisation, see:
Gonzalez, J., Dai, Z., Hennig, P., and Lawrence, N.
Batch Bayesian Optimization via Local Penalization.
In Proceedings of the 19th International Conference on Artificial Intelligence and Statistics,
pp. 648-657, 09-11 May 2016.
Takes as inputs:
env - optimization environment
beta - parameter of UCB bayesian optimization, default uses 0.2 * self.dim * np.log(2 * (self.env.t + 1))
lipschitz_consant - initial lipschitz_consant, will be re-estimated at every step
num_of_starts - number of multi-starts for optimizing the acquisition function, default is 75
num_of_optim_epochs - number of epochs for optimizing the acquisition function, default is 150
hp_update_frequency - how ofter should GP hyper-parameters be re-evaluated, default is None
'''
# initialise the environment
self.env = env
self.t_dim = self.env.t_dim
self.x_dim = self.env.x_dim
if self.x_dim is None:
self.dim = self.t_dim
else:
self.dim = self.t_dim + self.x_dim
# gp hyperparams
self.set_hyperparams()
# values of LP
if beta == None:
self.fixed_beta = False
self.beta = float(0.2 * self.dim * np.log(2 * (self.env.t + 1)))
else:
self.fixed_beta = True
self.beta = beta
# parameters of the method
self.lipschitz_constant = lipschitz_constant
self.max_value = 0
# initalise grid to select lipschitz constant
self.num_of_grad_points = 50 * self.dim
self.lipschitz_grid = sobol_seq.i4_sobol_generate(self.dim, self.num_of_grad_points)
# do we require transform?
if (self.env.function.name in ['Perm10D', 'Ackley4D', 'SnarBenchmark']) & (self.env.max_batch_size > 1):
self.soft_plus_transform = True
else:
self.soft_plus_transform = False
# optimisation parameters
self.num_of_starts = num_of_starts
self.num_of_optim_epochs = num_of_optim_epochs
# hp hyperparameters update frequency
self.hp_update_frequency = hp_update_frequency
# initial temperature, not needed I think
if initial_temp is not None:
self.initial_temp = initial_temp
else:
self.initial_temp = np.zeros((1, self.t_dim))
# define domain
self.domain = np.zeros((self.t_dim,))
self.domain = np.stack([self.domain, np.ones(self.t_dim, )], axis=1)
self.initialise_stuff()
def set_hyperparams(self, constant = None, lengthscale = None, noise = None, mean_constant = None, constraints = False):
'''
This function is used to set the hyper-parameters of the GP.
INPUTS:
constant: positive float, multiplies the RBF kernel and defines the initital variance
lengthscale: tensor of positive floats of length (dim), defines the kernel of the rbf kernel
noise: positive float, noise assumption
mean_constant: float, value of prior mean
constraints: boolean, if True, we will apply constraints from paper based on the given hyperparameters
'''
if constant == None:
self.constant = 0.6
self.length_scale = torch.tensor([0.15] * self.dim)
self.noise = 1e-4
self.mean_constant = 0
else:
self.constant = constant
self.length_scale = lengthscale
self.noise = noise
self.mean_constant = mean_constant
self.gp_hyperparams = (self.constant, self.length_scale, self.noise, self.mean_constant)
# check if we want our constraints based on these hyperparams
if constraints is True:
self.model.define_constraints(self.length_scale, self.mean_constant, self.constant)
def initialise_stuff(self):
# list of queries
self.queried_batch = []
# list of queries and observations
self.X = []
self.Y = []
# define model
self.model = BoTorchGP(lengthscale_dim = self.dim)
# current temperature
self.current_temp = self.initial_temp
# budget
self.budget = self.env.budget
# time
self.current_time = 0
# initialise new_obs
self.new_obs = None
def run_optim(self, verbose = False):
'''
Runs the whole optimisation procedure, returns all queries and evaluations
'''
self.env.initialise_optim()
while self.current_time <= self.budget:
self.optim_loop()
if verbose:
print(f'Current time-step: {self.current_time}')
# obtain all queries
X, Y = self.env.finished_with_optim()
# reformat all queries before returning
X_out = X[0]
for x in X[1:]:
X_out = np.concatenate((X_out, x), axis = 0)
return X_out, Y
def optim_loop(self):
'''
Performs a single loop of the optimisation
'''
# check if we need to update beta
if self.fixed_beta == False:
self.beta = float(0.2 * self.dim * np.log(2 * (self.env.t + 1)))
# optimise acquisition function to obtain new query
new_T, new_X = self.optimise_af()
# check if all variables incur input cost
if self.x_dim == None:
query = new_T
else:
query = np.concatenate((new_T, new_X), axis = 1)
# reformat query
query = list(query.reshape(-1))
# step forward in environment
obtain_query, self.new_obs = self.env.step(new_T, new_X)
# append query to queried batch
self.queried_batch.append(query)
# update model if there are new observations
if self.new_obs is not None:
self.X.append(list(obtain_query.reshape(-1)))
self.Y.append(self.new_obs)
# redefine new maximum value
self.max_value = float(max(self.max_value, float(self.new_obs)))
self.update_model()
# update hyperparams if needed
if (self.hp_update_frequency is not None) & (len(self.X) > 0):
if len(self.X) % self.hp_update_frequency == 0:
self.model.optim_hyperparams()
self.gp_hyperparams = self.model.current_hyperparams()
print(f'New hyperparams: {self.model.current_hyperparams()}')
# update current temperature and time
self.current_temp = new_T
self.current_time = self.current_time + 1
def update_model(self):
'''
This function updates the GP model
'''
if self.new_obs is not None:
# fit new model
self.model.fit_model(self.X, self.Y, previous_hyperparams=self.gp_hyperparams)
# we also update our estimate of the lipschitz constant, since we have a new model
# define the grid over which we will calculate gradients
grid = torch.tensor(self.lipschitz_grid, requires_grad = True).double()
# we only do this if we are in asynchronous setting, otherwise this should behave as normal UCB algorithm
if self.env.max_batch_size > 1:
# calculate mean of the GP
mean, _ = self.model.posterior(grid)
# calculate the gradient of the mean
external_grad = torch.ones(self.num_of_grad_points)
mean.backward(gradient = external_grad)
mu_grads = grid.grad
# find the norm of all the mean gradients
mu_norm = torch.norm(mu_grads, dim = 1)
# choose the largest one as our estimate
self.lipschitz_constant = max(mu_norm).item()
def build_af(self, X):
'''
This takes input locations, X, and returns the value of the acquisition function
'''
# check the batch of points being evaluated
batch = self.env.temperature_list
# if there are no new observations return the prior
if self.new_obs is not None:
mean, std = self.model.posterior(X)
else:
mean, std = torch.tensor(self.mean_constant), torch.tensor(self.constant)
# calculate upper confidence bound
ucb = mean + self.beta * std
# apply softmax transform if necessary
if self.soft_plus_transform:
ucb = torch.log(1 + torch.exp(ucb))
# penalize acquisition function, loop through batch of evaluations
for i, penalty_point in enumerate(batch):
# add x-variables if needed
if self.env.x_dim is not None:
query_x = self.env.batch[i]
penalty_point = np.concatenate((penalty_point, query_x.reshape(1, -1)), axis = 1).reshape(1, -1)
# re-define penalty point as tensor
penalty_point = torch.tensor(penalty_point)
# define the value that goes inside the erfc
norm = torch.norm(penalty_point - X, dim = 1)
# calculate z-value
z = self.lipschitz_constant * norm - self.max_value + mean
z = z / (std * np.sqrt(2))
# define penaliser
penaliser = 0.5 * torch.erfc(-1*z)
# penalise ucb
ucb = ucb * penaliser
# return acquisition function
return ucb
def optimise_af(self):
'''
This function optimizes the acquisition function, and returns the next query point
'''
# if time is zero, pick point at random
if self.current_time == 0:
new_T = np.random.uniform(size = self.t_dim).reshape(1, -1)
if self.x_dim is not None:
new_X = np.random.uniform(size = self.x_dim).reshape(1, -1)
else:
new_X = None
return new_T, new_X
# optimisation bounds
bounds = torch.stack([torch.zeros(self.dim), torch.ones(self.dim)])
# random initialization, multiply by 100
X = torch.rand(100 * self.num_of_starts, self.dim).double()
X.requires_grad = True
# define optimiser
optimiser = torch.optim.Adam([X], lr = 0.0001)
af = self.build_af(X)
# do the optimisation
for _ in range(self.num_of_optim_epochs):
# set zero grad
optimiser.zero_grad()
# losses for optimiser
losses = -self.build_af(X)
loss = losses.sum()
loss.backward()
# optim step
optimiser.step()
# make sure we are still within the bounds
for j, (lb, ub) in enumerate(zip(*bounds)):
X.data[..., j].clamp_(lb, ub)
# find the best start
best_start = torch.argmax(-losses)
# corresponding best input
best_input = X[best_start, :].detach()
# return the next query point
if self.x_dim is not None:
best = best_input.detach().numpy().reshape(1, -1)
new_T = best[0, :self.t_dim].reshape(1, -1)
new_X = best[0, self.t_dim:].reshape(1, -1)
else:
new_T = best_input.detach().numpy().reshape(1, -1)
new_X = None
return new_T, new_X
class ThompsonSampling():
'''
Method of Thompson Sampling for Bayesian Optimization, see the paper:
Kandasamy, K., Krishnamurthy, A., Schneider, J., and Poczos, B.
Parallelised BayesianOptimisation via Thompson Sampling.
In Proceedings of the Twenty-First International Conference on Artificial Intelligence and Statistics,
pp. 133-142, 2018.
'''
def __init__(self, env, initial_temp = None, num_of_starts = 75, num_of_optim_epochs = 150, \
hp_update_frequency = None):
'''
Takes as inputs:
env - optimization environment
beta - parameter of UCB bayesian optimization, default uses 0.2 * self.dim * np.log(2 * (self.env.t + 1))
lipschitz_consant - initial lipschitz_consant, will be re-estimated at every step
num_of_starts - number of multi-starts for optimizing the acquisition function, default is 75
num_of_optim_epochs - number of epochs for optimizing the acquisition function, default is 150
hp_update_frequency - how ofter should GP hyper-parameters be re-evaluated, default is None
'''
self.env = env
self.t_dim = self.env.t_dim
self.x_dim = self.env.x_dim
if self.x_dim is None:
self.dim = self.t_dim
else:
self.dim = self.t_dim + self.x_dim
# gp hyperparams
self.set_hyperparams()
# optimisation parameters
self.num_of_starts = num_of_starts
self.num_of_optim_epochs = num_of_optim_epochs
# hp hyperparameters update frequency
self.hp_update_frequency = hp_update_frequency
# initial temperature, not needed I think
if initial_temp is not None:
self.initial_temp = initial_temp
else:
self.initial_temp = np.zeros((1, self.t_dim))
# define domain
self.domain = np.zeros((self.t_dim,))
self.domain = np.stack([self.domain, np.ones(self.t_dim, )], axis=1)
self.initialise_stuff()
def set_hyperparams(self, constant = None, lengthscale = None, noise = None, mean_constant = None, constraints = False):
'''
This function is used to set the hyper-parameters of the GP.
INPUTS:
constant: positive float, multiplies the RBF kernel and defines the initital variance
lengthscale: tensor of positive floats of length (dim), defines the kernel of the rbf kernel
noise: positive float, noise assumption
mean_constant: float, value of prior mean
constraints: boolean, if True, we will apply constraints from paper based on the given hyperparameters
'''
if constant == None:
self.constant = 0.6
self.length_scale = torch.tensor([0.15] * self.dim)
self.noise = 1e-4
self.mean_constant = 0
else:
self.constant = constant
self.length_scale = lengthscale
self.noise = noise
self.mean_constant = mean_constant
self.gp_hyperparams = (self.constant, self.length_scale, self.noise, self.mean_constant)
# check if we want our constraints based on these hyperparams
if constraints is True:
self.model.define_constraints(self.length_scale, self.mean_constant, self.constant)
def initialise_stuff(self):
# list of queries
self.queried_batch = []
# list of queries and observations
self.X = []
self.Y = []
# define model
self.model = BoTorchGP(lengthscale_dim = self.dim)
# current temperature
self.current_temp = self.initial_temp
# budget
self.budget = self.env.budget
# time
self.current_time = 0
# initialise new_obs
self.new_obs = None
def run_optim(self, verbose = False):
'''
Runs the whole optimisation procedure, returns all queries and evaluations
'''
self.env.initialise_optim()
while self.current_time <= self.budget:
self.optim_loop()
if verbose:
print(f'Current time-step: {self.current_time}')
# obtain all inputs and outputs
X, Y = self.env.finished_with_optim()
# reformat the output
X_out = X[0]
for x in X[1:]:
X_out = np.concatenate((X_out, x), axis = 0)
return X_out, Y
def optim_loop(self):
'''
Performs a single loop of the optimisation
'''
# if there are no observations, sample uniformly
if len(self.X) == 0:
query = np.random.uniform(size = (1, self.dim))
else:
# define the samples
sampler = EfficientThompsonSampler(self.model, num_of_multistarts = self.num_of_starts, \
num_of_bases = 1024, \
num_of_samples = 1)
# create samples
sampler.create_sample()
# optimise samples
samples = sampler.generate_candidates()
query = samples.numpy()
# check if there are x-variables to get formatting right
if self.x_dim == None:
new_T = query
new_X = None
else:
new_T = query[0, :self.t_dim]
new_X = query[0, self.t_dim:]
# reformat query
query = list(query.reshape(-1))
# carry out step in environment
obtain_query, self.new_obs = self.env.step(new_T, new_X)
# add query to queried batch
self.queried_batch.append(query)
# update model
if self.new_obs is not None:
self.X.append(list(obtain_query.reshape(-1)))
self.Y.append(self.new_obs)
self.update_model()
# update hyperparams if needed
if (self.hp_update_frequency is not None) & (len(self.X) > 0):
if len(self.X) % self.hp_update_frequency == 0:
self.model.optim_hyperparams()
self.gp_hyperparams = self.model.current_hyperparams()
print(f'New hyperparams: {self.model.current_hyperparams()}')
# update current temperature and time
self.current_temp = new_T
self.current_time = self.current_time + 1
def update_model(self):
# updates model
if self.new_obs is not None:
self.model.fit_model(self.X, self.Y, previous_hyperparams=self.gp_hyperparams)
class oneExpectedImprovement():
def __init__(self, env, initial_temp = None, num_of_starts = 75, num_of_optim_epochs = 150, \
hp_update_frequency = None):
'''
Expected Improvement in a sequential setting i.e. one query per iteration. See paper:
Mockus, J., Tiesis, V., and Zilinskas, A.
The application of Bayesian methods for seeking the extremum.
Towards Global Optimization, 2:117-129, 09 2014.
Inputs:
num_of_starts - number of multi-starts to optimize acquisition function
num_of_optim_epochs - number of epochs to optimize acquisition function
hp_update_frequency - how frequently to optimize the hyper-parameters
'''
self.env = env
self.t_dim = self.env.t_dim
self.x_dim = self.env.x_dim
if self.x_dim is None:
self.dim = self.t_dim
else:
self.dim = self.t_dim + self.x_dim
assert self.env.max_batch_size == 1, 'Expected Improvement Requires Sequential Data!'
self.set_hyperparams()
# optimisation parameters
self.num_of_starts = num_of_starts
self.num_of_optim_epochs = num_of_optim_epochs
# hp hyperparameters update frequency
self.hp_update_frequency = hp_update_frequency
# initial temperature, not needed I think
if initial_temp is not None:
self.initial_temp = initial_temp
else:
self.initial_temp = np.zeros((1, self.t_dim))
# define domain
self.domain = np.zeros((self.t_dim,))
self.domain = np.stack([self.domain, np.ones(self.t_dim, )], axis=1)
# initialize max value observed
self.max_value = -100
self.initialise_stuff()
def set_hyperparams(self, constant = None, lengthscale = None, noise = None, mean_constant = None, constraints = False):
'''
This function is used to set the hyper-parameters of the GP.
INPUTS:
constant: positive float, multiplies the RBF kernel and defines the initital variance
lengthscale: tensor of positive floats of length (dim), defines the kernel of the rbf kernel
noise: positive float, noise assumption
mean_constant: float, value of prior mean
constraints: boolean, if True, we will apply constraints from paper based on the given hyperparameters
'''
if constant == None:
self.constant = 0.6
self.length_scale = torch.tensor([0.15] * self.dim)
self.noise = 1e-4
self.mean_constant = 0
else:
self.constant = constant
self.length_scale = lengthscale
self.noise = noise
self.mean_constant = mean_constant
self.gp_hyperparams = (self.constant, self.length_scale, self.noise, self.mean_constant)
# check if we want our constraints based on these hyperparams
if constraints is True:
self.model.define_constraints(self.length_scale, self.mean_constant, self.constant)
def initialise_stuff(self):
# list of queries
self.queried_batch = []
# list of queries and observations
self.X = []
self.Y = []
# define model
self.model = BoTorchGP(lengthscale_dim = self.dim)
# current temperature
self.current_temp = self.initial_temp
# budget
self.budget = self.env.budget
# time
self.current_time = 0
# initialise new_obs
self.new_obs = None
def run_optim(self, verbose = False):
'''
Runs the whole optimisation procedure, returns all queries and evaluations
'''
self.env.initialise_optim()
while self.current_time <= self.budget:
self.optim_loop()
if verbose:
print(f'Current time-step: {self.current_time}')
# obtain all queries and observations
X, Y = self.env.finished_with_optim()
# reformat queries
X_out = X[0]
for x in X[1:]:
X_out = np.concatenate((X_out, x), axis = 0)
return X_out, Y
def optim_loop(self):
'''
Performs a single loop of the optimisation
'''
# optimise acquisition function to obtain new queries
new_T, new_X = self.optimise_af()
# check if we have x-variables (i.e. variables with no input cost)
if self.x_dim == None:
query = new_T
else:
query = np.concatenate((new_T, new_X), axis = 1)
# reformat query
query = list(query.reshape(-1))
# step in environment
obtain_query, self.new_obs = self.env.step(new_T, new_X)
# add query to queried batch
self.queried_batch.append(query)
# update model
if self.new_obs is not None:
self.X.append(list(obtain_query.reshape(-1)))
self.Y.append(self.new_obs)
self.max_value = float(max(self.max_value, float(self.new_obs)))
self.update_model()
# update hyperparams if needed
if (self.hp_update_frequency is not None) & (len(self.X) > 0):
if len(self.X) % self.hp_update_frequency == 0:
self.model.optim_hyperparams()
self.gp_hyperparams = self.model.current_hyperparams()
print(f'New hyperparams: {self.model.current_hyperparams()}')
# update temperature and time
self.current_temp = new_T
self.current_time = self.current_time + 1
def update_model(self):
# update gp model
if self.new_obs is not None:
self.model.fit_model(self.X, self.Y, previous_hyperparams=self.gp_hyperparams)
def build_af(self, X):
# build acquisition function using BoTorch Expected Improvement
EI = ExpectedImprovement(self.model.model, best_f = self.max_value)
return EI(X.unsqueeze(1))
def optimise_af(self):
# if time is zero, pick point at random
if self.current_time == 0:
new_T = np.random.uniform(size = self.t_dim).reshape(1, -1)
if self.x_dim is not None:
new_X = np.random.uniform(size = self.x_dim).reshape(1, -1)
else:
new_X = None
return new_T, new_X
if self.env.function.grid_search == True:
grid_to_search = self.env.function.grid_to_search
idx_rand = torch.randperm(len(grid_to_search))[:self.max_grid_search_size]
self.grid_to_search_sample = grid_to_search[idx_rand, :]
af_in_grid = self.build_af(self.grid_to_search_sample)
max_idx = torch.argmax(af_in_grid)
best_input = self.grid_to_search_sample[max_idx, :]
if self.x_dim is not None:
new_T = best_input[:self.t_dim].detach().numpy().reshape(1, -1)
new_X = best_input[self.t_dim:].detach().numpy().reshape(1, -1)
else:
new_T = best_input.detach().numpy().reshape(1, -1)
new_X = None
# return next query
return new_T, new_X
# optimisation bounds
bounds = torch.stack([torch.zeros(self.dim), torch.ones(self.dim)])
# random initialization
Xraw = torch.rand(100 * self.num_of_starts, self.dim)
Yraw = self.build_af(Xraw)
# use BoTorch initializer
X = initialize_q_batch_nonneg(Xraw, Yraw, self.num_of_starts)
X.requires_grad = True
# define optimizer
optimiser = torch.optim.Adam([X], lr = 0.0001)
# do the optimization
for _ in range(self.num_of_optim_epochs):
# set zero grad
optimiser.zero_grad()
# losses for optimizer
losses = -self.build_af(X)
loss = losses.sum()
loss.backward()
# optim step
optimiser.step()
# make sure we are still within the bounds
for j, (lb, ub) in enumerate(zip(*bounds)):
X.data[..., j].clamp_(lb, ub) # need to do this on the data not X itself
# obtain the best start
best_start = torch.argmax(-losses)
best_input = X[best_start, :].detach()
if self.x_dim is not None:
new_T = best_input[:self.t_dim].detach().numpy().reshape(1, -1)
new_X = best_input[self.t_dim:].detach().numpy().reshape(1, -1)
else:
new_T = best_input.detach().numpy().reshape(1, -1)
new_X = None
# return next query
return new_T, new_X
class oneProbabilityOfImprovement(oneExpectedImprovement):
'''
One Probability of Improvement, see paper:
Kushner, H. J.
A New Method of Locating the Maximum Point of an Arbitrary Multipeak Curve in the Presence of Noise.
Journal of Basic Engineering, 86:97-106, 1964.
'''
def __init__(self, env, initial_temp=None, beta=1.96, lipschitz_constant=20, num_of_starts=75, num_of_optim_epochs=150, hp_update_frequency=None):
'''
Inputs:
num_of_starts - number of multi-starts to optimize acquisition function
num_of_optim_epochs - number of epochs to optimize acquisition function
hp_update_frequency - how frequently to optimize the hyper-parameters
'''
# use Expected Improvement class, only change the acquisition function
super().__init__(env, initial_temp=initial_temp, num_of_starts=num_of_starts, num_of_optim_epochs=num_of_optim_epochs, hp_update_frequency=hp_update_frequency)
def build_af(self, X):
# Probability of Improvement using BoTorch
PI = ProbabilityOfImprovement(self.model.model, best_f = self.max_value)
return PI(X.unsqueeze(1))
class EIperUnitCost(oneExpectedImprovement):
'''
We consider the Expected Improvement per unit cost:
AF_t(x) = EI(x) / (C(x_{t-1}, x) + c)
where c > 0 is chosen to avoid division by zero.
See paper intoduction of paper:
Lee, Eric Hans, et al. "Cost-aware Bayesian optimization." arXiv preprint arXiv:2003.10870 (2020).
'''
def __init__(self, env, initial_temp=None, beta=1.96, lipschitz_constant=20, num_of_starts=75, num_of_optim_epochs=150, hp_update_frequency=None, cost_constant = 1, cost_equation = None, max_grid_search_size = 1000):
'''
Inputs:
num_of_starts - number of multi-starts to optimize acquisition function
num_of_optim_epochs - number of epochs to optimize acquisition function
hp_update_frequency - how frequently to optimize the hyper-parameters
cost_constant - parameter to avoid division by zero, see SnAKe paper for description
cost_equation - equation that builds cost matrix
max_grid_search_size - maximum size of the grid over which to search, in case we are doing grid search
'''
# use Expected Improvement class, only change the acquisition function
super().__init__(env, initial_temp=initial_temp, num_of_starts=num_of_starts, num_of_optim_epochs=num_of_optim_epochs, hp_update_frequency=hp_update_frequency)
self.cost_constant = cost_constant
if cost_equation is None:
self.cost_equation = lambda x, y: torch.norm(x - y, dim = 1)
else:
self.cost_equation = cost_equation
# grid search parameters
self.max_grid_search_size = max_grid_search_size
# check if we need to initialize a search grid
if self.env.function.grid_search is True:
# initialize grid to search
grid_to_search = self.env.function.grid_to_search
idx_rand = torch.randperm(len(grid_to_search))[:self.max_grid_search_size]
self.grid_to_search_sample = grid_to_search[idx_rand, :]
def build_af(self, X):
# Probability of Improvement using BoTorch
current_x = torch.tensor(self.current_temp).double()
cost = self.cost_equation(current_x, X[:, :self.t_dim].double()) + self.cost_constant
EI = ExpectedImprovement(self.model.model, best_f = self.max_value)
return EI(X.unsqueeze(1)) / cost.reshape(-1)
class TruncatedExpectedImprovement(oneExpectedImprovement):
'''
Truncated Expected Improvement as introduced in:
Samaniego, Federico Peralta, et al. "A bayesian optimization approach for water resources
monitoring through an autonomous surface vehicle: The ypacarai lake case study."
IEEE Access 9 (2021): 9163-9179.
'''
def __init__(self, env, initial_temp=None, num_of_starts=75, num_of_optim_epochs=150, hp_update_frequency=None, max_grid_search_size = 1000):
'''
Takes as inputs:
env - optimization environment
initial_temp - initial optimization cost
num_of_starts - number of multi-starts for optimizing the acquisition function, default is 75
num_of_optim_epochs - number of epochs for optimizing the acquisition function, default is 150
hp_update_frequency - how ofter should GP hyper-parameters be re-evaluated, default is None
max_grid_search_size - maximum size of the grid over which to search, in case we are doing grid search
'''
super().__init__(env, initial_temp, num_of_starts, num_of_optim_epochs, hp_update_frequency)
# grid search parameters
self.max_grid_search_size = max_grid_search_size
# check if we need to initialize a search grid
if self.env.function.grid_search is True:
# initialize grid to search
grid_to_search = self.env.function.grid_to_search
idx_rand = torch.randperm(len(grid_to_search))[:self.max_grid_search_size]
self.grid_to_search_sample = grid_to_search[idx_rand, :]
def optim_loop(self):
'''
Performs a single loop of the optimisation
'''
# optimise acquisition function to obtain new queries
new_T, new_X = self.optimise_af()
# check smallest lengthscale for jump
jump_lengthscale = float(torch.min(self.gp_hyperparams[1])) * np.sqrt(self.dim)
distance_to_query = np.linalg.norm(new_T - self.current_temp)
if distance_to_query > jump_lengthscale:
new_T = self.current_temp + (new_T - self.current_temp) / distance_to_query * jump_lengthscale
if self.env.function.grid_search is True:
distances_to_grid = np.sum((self.grid_to_search_sample - new_T).numpy()**2, axis = 1)
idx_min = np.argmin(distances_to_grid)
new_T = self.grid_to_search_sample[idx_min, :].numpy().reshape(1, -1)
# check if we have x-variables (i.e. variables with no input cost)
if self.x_dim == None:
query = new_T
else:
query = np.concatenate((new_T, new_X), axis = 1)
# reformat query
query = list(query.reshape(-1))
# step in environment
obtain_query, self.new_obs = self.env.step(new_T, new_X)
# add query to queried batch
self.queried_batch.append(query)
# update model
if self.new_obs is not None:
self.X.append(list(obtain_query.reshape(-1)))
self.Y.append(self.new_obs)
self.max_value = float(max(self.max_value, float(self.new_obs)))
self.update_model()
# update hyperparams if needed
if (self.hp_update_frequency is not None) & (len(self.X) > 0):
if len(self.X) % self.hp_update_frequency == 0:
self.model.optim_hyperparams()
self.gp_hyperparams = self.model.current_hyperparams()
print(f'New hyperparams: {self.model.current_hyperparams()}')
# update temperature and time
self.current_temp = new_T
self.current_time = self.current_time + 1
def optimise_af(self):
# if time is zero, pick point at random
if self.current_time == 0:
new_T = np.random.uniform(size = self.t_dim).reshape(1, -1)
if self.x_dim is not None:
new_X = np.random.uniform(size = self.x_dim).reshape(1, -1)
else:
new_X = None
return new_T, new_X
if self.env.function.grid_search == True:
grid_to_search = self.env.function.grid_to_search
idx_rand = torch.randperm(len(grid_to_search))[:self.max_grid_search_size]
self.grid_to_search_sample = grid_to_search[idx_rand, :]
af_in_grid = self.build_af(self.grid_to_search_sample)
max_idx = torch.argmax(af_in_grid)
best_input = self.grid_to_search_sample[max_idx, :]
if self.x_dim is not None:
new_T = best_input[:self.t_dim].detach().numpy().reshape(1, -1)
new_X = best_input[self.t_dim:].detach().numpy().reshape(1, -1)
else:
new_T = best_input.detach().numpy().reshape(1, -1)
new_X = None
# return next query
return new_T, new_X
# optimisation bounds
bounds = torch.stack([torch.zeros(self.dim), torch.ones(self.dim)])
# random initialization
Xraw = torch.rand(100 * self.num_of_starts, self.dim)
Yraw = self.build_af(Xraw)
# use BoTorch initializer
X = initialize_q_batch_nonneg(Xraw, Yraw, self.num_of_starts)
X.requires_grad = True
# define optimizer
optimiser = torch.optim.Adam([X], lr = 0.0001)
# do the optimization
for _ in range(self.num_of_optim_epochs):
# set zero grad
optimiser.zero_grad()
# losses for optimizer
losses = -self.build_af(X)
loss = losses.sum()
loss.backward()
# optim step
optimiser.step()
# make sure we are still within the bounds
for j, (lb, ub) in enumerate(zip(*bounds)):
X.data[..., j].clamp_(lb, ub) # need to do this on the data not X itself
# obtain the best start
best_start = torch.argmax(-losses)
best_input = X[best_start, :].detach()
if self.x_dim is not None:
new_T = best_input[:self.t_dim].detach().numpy().reshape(1, -1)
new_X = best_input[self.t_dim:].detach().numpy().reshape(1, -1)
else:
new_T = best_input.detach().numpy().reshape(1, -1)
new_X = None
# return next query
return new_T, new_X
class EIpuLP(UCBwLP):
'''
Gonzalez, J., Dai, Z., Hennig, P., and Lawrence, N.
Batch Bayesian Optimization via Local Penalization.
In Proceedings of the 19th International Conference on Artificial Intelligence and Statistics,
pp. 648-657, 09-11 May 2016.
Lee, Eric Hans, et al. "Cost-aware Bayesian optimization." arXiv preprint arXiv:2003.10870 (2020).
Takes as inputs:
env - optimization environment
beta - parameter of UCB bayesian optimization, default uses 0.2 * self.dim * np.log(2 * (self.env.t + 1))
lipschitz_consant - initial lipschitz_consant, will be re-estimated at every step
num_of_starts - number of multi-starts for optimizing the acquisition function, default is 75
num_of_optim_epochs - number of epochs for optimizing the acquisition function, default is 150
hp_update_frequency - how ofter should GP hyper-parameters be re-evaluated, default is None
cost_constant - parameter to avoid division by zero, see SnAKe paper for description
cost_equation - equation that builds cost matrix
'''
def __init__(self, env, initial_temp=None, beta=None, lipschitz_constant=1, num_of_starts=75, num_of_optim_epochs=150, hp_update_frequency=None, cost_constant = 1, cost_equation = None):
super().__init__(env, initial_temp, beta, lipschitz_constant, num_of_starts, num_of_optim_epochs, hp_update_frequency)
# initialize cost constant
self.cost_constant = cost_constant
# max value for EI
self.max_value = 0
# initialize cost equation
if cost_equation is None:
self.cost_equation = lambda x, y: torch.norm(x - y)
else:
self.cost_equation = cost_equation
def build_af(self, X):
'''
This takes input locations, X, and returns the value of the acquisition function
'''
# check the batch of points being evaluated
batch = self.env.temperature_list
# if there are no new observations return the prior
if self.new_obs is not None:
# get expected improvement
EI = ExpectedImprovement(self.model.model, self.max_value)
af = EI(X.unsqueeze(1))
# get mean and standard deviation
mean, std = self.model.posterior(X)
else:
af = torch.tensor(self.mean_constant) - self.max_value
mean, std = torch.tensor(self.mean_constant), torch.tensor(self.constant)
# add cost
current_x = torch.tensor(self.current_temp)
cost = self.cost_equation(current_x, X[:, :self.t_dim]) + self.cost_constant
af = af / cost
# penalize acquisition function, loop through batch of evaluations
for i, penalty_point in enumerate(batch):
# add x-variables if needed
if self.env.x_dim is not None:
query_x = self.env.batch[i]
penalty_point = np.concatenate((penalty_point, query_x.reshape(1, -1)), axis = 1).reshape(1, -1)
# re-define penalty point as tensor
penalty_point = torch.tensor(penalty_point)
# define the value that goes inside the erfc
norm = torch.norm(penalty_point - X, dim = 1)
# calculate z-value
z = self.lipschitz_constant * norm - self.max_value + mean
z = z / (std * np.sqrt(2))
# define penaliser
penaliser = 0.5 * torch.erfc(-1*z)
# penalise ucb
af = af * penaliser
# return acquisition function
return af
class MultiObjectiveEIpu(EIperUnitCost):
'''
Multi-objective version of EIpu. We do this by changing to the next objective after we incur a cost of 'cost_switch'.
See normal EIpu for explanation of other variables.
'''
def __init__(self, env, initial_temp=None, beta=None, lipschitz_constant=1, num_of_starts=75, num_of_optim_epochs=150, hp_update_frequency=None, cost_constant=1, cost_equation=None, cost_switch = .75):
# number of objectives to maximize
self.num_of_objectives = env.num_of_objectives
super().__init__(env, initial_temp, beta, lipschitz_constant, num_of_starts, num_of_optim_epochs, hp_update_frequency, cost_constant, cost_equation)
self.cost_switch = cost_switch
self.X = []
self.Y = [[] for _ in range(self.num_of_objectives)]
# initialize max value
self.max_value = [0 for _ in range(self.num_of_objectives)]
# define model
self.model = [BoTorchGP(lengthscale_dim = self.dim) for _ in range(self.num_of_objectives)]
self.set_hyperparams()
# initialize cost
self.current_cost = 0
def set_hyperparams(self, constant = None, lengthscale = None, noise = None, mean_constant = None, constraints = False):
'''
This function is used to set the hyper-parameters of the GP.
INPUTS:
constant: positive float, multiplies the RBF kernel and defines the initital variance
lengthscale: tensor of positive floats of length (dim), defines the kernel of the rbf kernel
noise: positive float, noise assumption
mean_constant: float, value of prior mean
constraints: boolean, if True, we will apply constraints from paper based on the given hyperparameters
'''
if constant == None:
self.constant = 0.6
self.length_scale = torch.tensor([0.15] * self.dim)
self.noise = 1e-4
self.mean_constant = 0
else:
self.length_scale = lengthscale
self.noise = noise
self.constant = constant
self.mean_constant = mean_constant