Skip to content

Commit 17dfc06

Browse files
author
brendan
committed
remove the weird hashing behavior in maximize
1 parent bae63f6 commit 17dfc06

File tree

3 files changed

+57
-22
lines changed

3 files changed

+57
-22
lines changed

bayes_opt/bayesian_optimization.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ def dispatch(self, event):
6262
callback(event, self)
6363

6464

65+
66+
6567
class BayesianOptimization(Observable):
6668
"""
6769
This class takes the function to optimize as well as the parameters bounds

bayes_opt/target_space.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -226,20 +226,16 @@ def probe(self, params):
226226
target function value.
227227
"""
228228
x = self._as_array(params)
229+
params = dict(zip(self._keys, x))
230+
target = self.target_func(**params)
229231

230-
try:
231-
return self._cache[_hashable(x)]
232-
except KeyError:
233-
params = dict(zip(self._keys, x))
234-
target = self.target_func(**params)
235-
236-
if self._constraint is None:
237-
self.register(x, target)
238-
return target
239-
else:
240-
constraint_value = self._constraint.eval(**params)
241-
self.register(x, target, constraint_value)
242-
return target, constraint_value
232+
if self._constraint is None:
233+
self.register(x, target)
234+
return target
235+
else:
236+
constraint_value = self._constraint.eval(**params)
237+
self.register(x, target, constraint_value)
238+
return target, constraint_value
243239

244240
def random_sample(self):
245241
"""

examples/test.py

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,56 @@
11
import numpy as np
22
from bayes_opt import BayesianOptimization
33
from bayes_opt import UtilityFunction
4+
from bayes_opt.logger import ScreenLogger
45

56

67
def f(x):
78
return np.exp(-(x - 2) ** 2) + np.exp(-(x - 6) ** 2 / 10) + 1/ (x ** 2 + 1)
89

10+
def black_box_function(x, y):
11+
return -x ** 2 - (y - 1) ** 2 + 1
12+
13+
914

1015
if __name__ == '__main__':
11-
optimizer = BayesianOptimization(f=None, pbounds={'x': (-2, 2)}, verbose=2, random_state=1, allow_duplicate_points=True)
12-
optimizer.set_gp_params(normalize_y=True, alpha=2.5e-3, n_restarts_optimizer=20) # tuning of the gaussian parameters...
13-
utility = UtilityFunction(kind="ucb", kappa=5, xi=1) # kappa determines explore/Exploitation ratio
14-
for point in range(20):
15-
next_point_to_probe = optimizer.suggest(utility)
16-
NextPointValues = np.array(list(next_point_to_probe.values()))
17-
mean,std = optimizer._gp.predict(NextPointValues.reshape(1, -1),return_std=True)
18-
target = f(**next_point_to_probe)
19-
optimizer.register(params=next_point_to_probe, target=target)
16+
repeat_points_version=True
17+
if repeat_points_version:
18+
optimizer = BayesianOptimization(f=f, pbounds={'x': (-2, 2)}, verbose=2, random_state=1, allow_duplicate_points=True)
19+
# logger = ScreenLogger()
20+
# optimizer.subscribe(Events.OPTIMIZATION_STEP, logger)
21+
optimizer.set_gp_params(normalize_y=True, alpha=2.5e-3, n_restarts_optimizer=20) # tuning of the gaussian parameters...
22+
utility = UtilityFunction(kind="ucb", kappa=5, xi=1) # kappa determines explore/Exploitation ratio
23+
# optimizer.maximize(
24+
# init_points=2,
25+
# n_iter=20,
26+
# )
27+
28+
optimizer.maximize(init_points=2,
29+
n_iter=25,
30+
acq='ucb',
31+
kappa=5,
32+
xi=1,
33+
**{'normalize_y':True, 'alpha':2.5e-3, 'n_restarts_optimizer':20})
34+
# for point in range(20):
35+
# next_point_to_probe = optimizer.suggest(utility)
36+
# NextPointValues = np.array(list(next_point_to_probe.values()))
37+
# mean,std = optimizer._gp.predict(NextPointValues.reshape(1, -1),return_std=True)
38+
# target = f(**next_point_to_probe)
39+
# optimizer.register(params=next_point_to_probe, target=target)
40+
else:
41+
42+
# Bounded region of parameter space
43+
pbounds = {'x': (2, 4), 'y': (-3, 3)}
44+
45+
optimizer = BayesianOptimization(
46+
f=black_box_function,
47+
pbounds=pbounds,
48+
verbose=2, # verbose = 1 prints only when a maximum is observed, verbose = 0 is silent
49+
random_state=1,
50+
)
51+
52+
optimizer.maximize(
53+
init_points=2,
54+
n_iter=3,
55+
)
56+

0 commit comments

Comments
 (0)