Skip to content

Commit 49a6530

Browse files
committed
added random state
1 parent f243cfc commit 49a6530

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

bayes_opt/bayesian_optimization.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
class BayesianOptimization(object):
1111

12-
def __init__(self, f, pbounds, verbose=1):
12+
def __init__(self, f, pbounds, random_state=None, verbose=1):
1313
"""
1414
:param f:
1515
Function to be maximized.
@@ -25,6 +25,13 @@ def __init__(self, f, pbounds, verbose=1):
2525
# Store the original dictionary
2626
self.pbounds = pbounds
2727

28+
if random_state is None:
29+
self.random_state = np.random.RandomState()
30+
elif isinstance(random_state, int):
31+
self.random_state = np.random.RandomState(random_state)
32+
else:
33+
self.random_state = random_state
34+
2835
# Get the name of the parameters
2936
self.keys = list(pbounds.keys())
3037

@@ -59,6 +66,7 @@ def __init__(self, f, pbounds, verbose=1):
5966
self.gp = GaussianProcessRegressor(
6067
kernel=Matern(nu=2.5),
6168
n_restarts_optimizer=25,
69+
random_state=self.random_state
6270
)
6371

6472
# Utility Function placeholder
@@ -87,7 +95,7 @@ def init(self, init_points):
8795
"""
8896

8997
# Generate random points
90-
l = [np.random.uniform(x[0], x[1], size=init_points)
98+
l = [self.random_state.uniform(x[0], x[1], size=init_points)
9199
for x in self.bounds]
92100

93101
# Concatenate new random points to possible existing
@@ -276,7 +284,8 @@ def maximize(self,
276284
x_max = acq_max(ac=self.util.utility,
277285
gp=self.gp,
278286
y_max=y_max,
279-
bounds=self.bounds)
287+
bounds=self.bounds,
288+
random_state=self.random_state)
280289

281290
# Print new header
282291
if self.verbose:
@@ -293,7 +302,7 @@ def maximize(self,
293302
pwarning = False
294303
if np.any((self.X - x_max).sum(axis=1) == 0):
295304

296-
x_max = np.random.uniform(self.bounds[:, 0],
305+
x_max = self.random_state.uniform(self.bounds[:, 0],
297306
self.bounds[:, 1],
298307
size=self.bounds.shape[0])
299308

@@ -315,7 +324,8 @@ def maximize(self,
315324
x_max = acq_max(ac=self.util.utility,
316325
gp=self.gp,
317326
y_max=y_max,
318-
bounds=self.bounds)
327+
bounds=self.bounds,
328+
random_state=self.random_state)
319329

320330
# Print stuff
321331
if self.verbose:

bayes_opt/helpers.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from scipy.optimize import minimize
77

88

9-
def acq_max(ac, gp, y_max, bounds):
9+
def acq_max(ac, gp, y_max, bounds, random_state):
1010
"""
1111
A function to find the maximum of the acquisition function
1212
@@ -35,14 +35,14 @@ def acq_max(ac, gp, y_max, bounds):
3535
"""
3636

3737
# Warm up with random points
38-
x_tries = np.random.uniform(bounds[:, 0], bounds[:, 1],
38+
x_tries = random_state.uniform(bounds[:, 0], bounds[:, 1],
3939
size=(100000, bounds.shape[0]))
4040
ys = ac(x_tries, gp=gp, y_max=y_max)
4141
x_max = x_tries[ys.argmax()]
4242
max_acq = ys.max()
4343

4444
# Explore the parameter space more throughly
45-
x_seeds = np.random.uniform(bounds[:, 0], bounds[:, 1],
45+
x_seeds = random_state.uniform(bounds[:, 0], bounds[:, 1],
4646
size=(250, bounds.shape[0]))
4747
for x_try in x_seeds:
4848
# Find the minimum of minus the acquisition function

0 commit comments

Comments
 (0)