Skip to content

Commit bae63f6

Browse files
author
brendan
committed
fix non unqiue data point warning
1 parent 6de9ba8 commit bae63f6

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

bayes_opt/target_space.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import numpy as np
44
from .util import ensure_rng, NotUniqueError
5+
from .util import Colours
56

67

78
def _hashable(x):
@@ -183,10 +184,11 @@ def register(self, params, target, constraint_value=None):
183184
if x in self:
184185
if self._allow_duplicate_points:
185186
self.n_duplicate_points = self.n_duplicate_points + 1
186-
warnings.warn(f'Data point {x} is not unique. {self.n_duplicate_points} duplicates registered.'
187-
f' Continuing ...')
187+
print(f'{Colours.RED}Data point {x} is not unique. {self.n_duplicate_points} duplicates registered.'
188+
f' Continuing ...{Colours.END}')
188189
else:
189-
raise NotUniqueError('Data point {} is not unique'.format(x))
190+
raise NotUniqueError(f'Data point {x} is not unique. You can set "allow_duplicate_points=True" to '
191+
f'avoid this error')
190192

191193
self._params = np.concatenate([self._params, x.reshape(1, -1)])
192194
self._target = np.concatenate([self._target, [target]])

examples/test.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import numpy as np
2+
from bayes_opt import BayesianOptimization
3+
from bayes_opt import UtilityFunction
4+
5+
6+
def f(x):
7+
return np.exp(-(x - 2) ** 2) + np.exp(-(x - 6) ** 2 / 10) + 1/ (x ** 2 + 1)
8+
9+
10+
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)

0 commit comments

Comments
 (0)