From 3e98e12fa9742d79e4c2e15384419a57e3a2d1c9 Mon Sep 17 00:00:00 2001 From: Patrick Tapping Date: Thu, 25 Nov 2021 14:35:49 +1030 Subject: [PATCH 1/4] acq_max includes best random sample and y_max params in x_seeds for L-BFGS-B --- bayes_opt/bayesian_optimization.py | 3 ++- bayes_opt/util.py | 17 +++++++++++++---- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/bayes_opt/bayesian_optimization.py b/bayes_opt/bayesian_optimization.py index 89ad98943..0937969f1 100644 --- a/bayes_opt/bayesian_optimization.py +++ b/bayes_opt/bayesian_optimization.py @@ -184,7 +184,8 @@ def suggest(self, utility_function): gp=self._gp, y_max=self._space.target.max(), bounds=self._space.bounds, - random_state=self._random_state + random_state=self._random_state, + y_max_params=self.space.params[self.space.target.argmax()] ) return self._space.array_to_params(suggestion) diff --git a/bayes_opt/util.py b/bayes_opt/util.py index a936221d8..bb8edd825 100644 --- a/bayes_opt/util.py +++ b/bayes_opt/util.py @@ -4,13 +4,13 @@ from scipy.optimize import minimize -def acq_max(ac, gp, y_max, bounds, random_state, n_warmup=10000, n_iter=10): +def acq_max(ac, gp, y_max, bounds, random_state, n_warmup=10000, n_iter=10, y_max_params=None): """ A function to find the maximum of the acquisition function It uses a combination of random sampling (cheap) and the 'L-BFGS-B' optimization method. First by sampling `n_warmup` (1e5) points at random, - and then running L-BFGS-B from `n_iter` (250) random starting points. + and then running L-BFGS-B from `n_iter` (10) random starting points. Parameters ---------- @@ -47,9 +47,18 @@ def acq_max(ac, gp, y_max, bounds, random_state, n_warmup=10000, n_iter=10): x_max = x_tries[ys.argmax()] max_acq = ys.max() - # Explore the parameter space more throughly + # Explore the parameter space more thoroughly x_seeds = random_state.uniform(bounds[:, 0], bounds[:, 1], - size=(n_iter, bounds.shape[0])) + size=(1+n_iter+int(not y_max_params is None), + bounds.shape[0])) + # Add the best candidate from the random sampling to the seeds so that the + # optimization algorithm can try to walk up to that particular local maxima + x_seeds[0] = x_max + if not y_max_params is None: + # Add the provided best sample to the seeds so that the optimization + # algorithm is aware of it and will attempt to find its local maxima + x_seeds[1] = y_max_params + for x_try in x_seeds: # Find the minimum of minus the acquisition function res = minimize(lambda x: -ac(x.reshape(1, -1), gp=gp, y_max=y_max), From db1e0df16dc0fa1e78bae7dae052edda0e2fd3df Mon Sep 17 00:00:00 2001 From: ptapping <63924582+ptapping@users.noreply.github.com> Date: Thu, 7 Dec 2023 17:11:26 +1030 Subject: [PATCH 2/4] Remove errant comma introduced from merge --- bayes_opt/bayesian_optimization.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bayes_opt/bayesian_optimization.py b/bayes_opt/bayesian_optimization.py index 09eea10be..9740d3fa7 100644 --- a/bayes_opt/bayesian_optimization.py +++ b/bayes_opt/bayesian_optimization.py @@ -229,7 +229,7 @@ def suggest(self, utility_function): constraint=self.constraint, y_max=self._space._target_max(), bounds=self._space.bounds, - random_state=self._random_state,, + random_state=self._random_state, y_max_params=self._space.params[self._space.target.argmax()) return self._space.array_to_params(suggestion) From 6ca07d01b3caa1722965ca5a8ff4651962c10e51 Mon Sep 17 00:00:00 2001 From: Patrick Tapping Date: Thu, 7 Dec 2023 17:14:07 +1030 Subject: [PATCH 3/4] Add docstring for new parameter in acq_max --- bayes_opt/util.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/bayes_opt/util.py b/bayes_opt/util.py index ad05c99ac..48d8f3c17 100644 --- a/bayes_opt/util.py +++ b/bayes_opt/util.py @@ -40,6 +40,9 @@ def acq_max(ac, gp, y_max, bounds, random_state, constraint=None, n_warmup=10000 :param n_iter: number of times to run scipy.minimize + :param y_max_params: + Function parameters that produced the maximum known value given by `y_max`. + Returns ------- :return: x_max, The arg max of the acquisition function. From c03dee0388cae9ea2012748c09d8f5e4fd6aff1e Mon Sep 17 00:00:00 2001 From: Patrick Tapping Date: Thu, 7 Dec 2023 17:18:44 +1030 Subject: [PATCH 4/4] Fix missing bracket --- bayes_opt/bayesian_optimization.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bayes_opt/bayesian_optimization.py b/bayes_opt/bayesian_optimization.py index 9740d3fa7..df2000291 100644 --- a/bayes_opt/bayesian_optimization.py +++ b/bayes_opt/bayesian_optimization.py @@ -230,7 +230,7 @@ def suggest(self, utility_function): y_max=self._space._target_max(), bounds=self._space.bounds, random_state=self._random_state, - y_max_params=self._space.params[self._space.target.argmax()) + y_max_params=self._space.params[self._space.target.argmax()]) return self._space.array_to_params(suggestion)