Skip to content

Commit 5aaa3d2

Browse files
committed
fix giving bad seeds to l-bfgs-b
1 parent 2600edb commit 5aaa3d2

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

bayes_opt/acquisition.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ def _random_sample_minimize(
312312
x_min = x_tries[ys.argmin()]
313313
min_acq = ys.min()
314314
if n_x_seeds != 0:
315-
idxs = np.argsort(ys)[-n_x_seeds:]
315+
idxs = np.argsort(ys)[:n_x_seeds]
316316
x_seeds = x_tries[idxs]
317317
else:
318318
x_seeds = []

tests/test_acquisition.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def target_func():
4343

4444
@pytest.fixture
4545
def random_state():
46-
return np.random.RandomState()
46+
return np.random.RandomState(0)
4747

4848

4949
@pytest.fixture
@@ -99,13 +99,20 @@ def test_acquisition_optimization(gp, target_space):
9999
acq.suggest(gp=gp, target_space=target_space, n_random=0, n_smart=0)
100100

101101

102-
def test_acquisition_optimization_only_random(gp, target_space):
102+
def test_acquisition_optimization_only_random(gp, target_space, random_state):
103103
acq = MockAcquisition()
104104
target_space.register(params={"x": 2.5, "y": 0.5}, target=3.0)
105-
res = acq.suggest(gp=gp, target_space=target_space, n_smart=0, n_random=10_000)
105+
res = acq.suggest(gp=gp, target_space=target_space, n_smart=0, n_random=10_000, random_state=random_state)
106106
# very lenient comparison as we're just considering random samples
107107
assert np.array([3.0, 1.0]) == pytest.approx(res, abs=1e-1, rel=1e-1)
108108

109+
# make sure that the best random sample is in the seeds
110+
acq_f = acq._get_acq(gp=gp, constraint=target_space.constraint)
111+
x_min, _, x_seeds = acq._random_sample_minimize(
112+
acq_f, target_space, random_state=random_state, n_random=10_000, n_x_seeds=3
113+
)
114+
assert x_min in x_seeds
115+
109116

110117
def test_acquisition_optimization_only_l_bfgs_b(gp, target_space):
111118
acq = MockAcquisition()

0 commit comments

Comments
 (0)