Skip to content

Commit bc9af23

Browse files
authored
docs: Add acquisition functions (#498)
* docs: Add acquisition functions * Make `_update_params` subclass specific
1 parent a6697aa commit bc9af23

File tree

4 files changed

+48
-36
lines changed

4 files changed

+48
-36
lines changed

bayes_opt/acquisition.py

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,7 @@
1212
from typing import Callable, List, Union, Tuple
1313
from copy import deepcopy
1414
from numbers import Number
15-
16-
class ConstraintNotSupportedError(Exception):
17-
"""Raised when constrained optimization is not supported."""
18-
19-
pass
20-
21-
22-
class NoValidPointRegisteredError(Exception):
23-
"""Raised when an acquisition function depends on previous points but none are registered."""
24-
25-
pass
26-
27-
28-
class TargetSpaceEmptyError(Exception):
29-
"""Raised when the target space is empty."""
30-
31-
pass
15+
from .util import ConstraintNotSupportedError, NoValidPointRegisteredError, TargetSpaceEmptyError
3216

3317

3418
class AcquisitionFunction(abc.ABC):
@@ -102,9 +86,7 @@ def suggest(self, gp: GaussianProcessRegressor, target_space: TargetSpace, n_ran
10286
self._fit_gp(gp=gp, target_space=target_space)
10387

10488
acq = self._get_acq(gp=gp, constraint=target_space.constraint)
105-
x_max = self._acq_min(acq, target_space.bounds, n_random=n_random, n_l_bfgs_b=n_l_bfgs_b)
106-
self._update_params()
107-
return x_max
89+
return self._acq_min(acq, target_space.bounds, n_random=n_random, n_l_bfgs_b=n_l_bfgs_b)
10890

10991
def _get_acq(self, gp: GaussianProcessRegressor, constraint: Union[ConstraintModel, None] = None) -> Callable:
11092
"""Prepare the acquisition function for minimization.
@@ -269,10 +251,6 @@ def _l_bfgs_b_minimize(self, acq: Callable, bounds: np.ndarray, n_x_seeds:int=10
269251
# point technicalities this is not always the case.
270252
return np.clip(x_min, bounds[:, 0], bounds[:, 1]), min_acq
271253

272-
def _update_params(self) -> None:
273-
"""Update the parameters of the acquisition function."""
274-
pass
275-
276254

277255
class UpperConfidenceBound(AcquisitionFunction):
278256
r"""Upper Confidence Bound acquisition function.
@@ -357,10 +335,15 @@ def suggest(self, gp: GaussianProcessRegressor, target_space: TargetSpace, n_ran
357335
+ "does not support constrained optimization."
358336
)
359337
raise ConstraintNotSupportedError(msg)
360-
return super().suggest(gp=gp, target_space=target_space, n_random=n_random, n_l_bfgs_b=n_l_bfgs_b, fit_gp=fit_gp)
338+
x_max = super().suggest(gp=gp, target_space=target_space, n_random=n_random, n_l_bfgs_b=n_l_bfgs_b, fit_gp=fit_gp)
339+
self.decay_exploration()
340+
return x_max
361341

362-
def _update_params(self) -> None:
363-
"""Update the parameters of the acquisition function."""
342+
def decay_exploration(self) -> None:
343+
"""Decay kappa by a constant rate.
344+
345+
Adjust exploration/exploitation trade-off by reducing kappa.
346+
"""
364347
if self.exploration_decay is not None:
365348
if self.exploration_decay_delay is None or self.exploration_decay_delay <= self.i:
366349
self.kappa = self.kappa*self.exploration_decay
@@ -463,10 +446,15 @@ def suggest(self, gp: GaussianProcessRegressor, target_space: TargetSpace, n_ran
463446
)
464447
raise NoValidPointRegisteredError(msg)
465448
self.y_max = y_max
466-
return super().suggest(gp=gp, target_space=target_space, n_random=n_random, n_l_bfgs_b=n_l_bfgs_b, fit_gp=fit_gp)
449+
x_max = super().suggest(gp=gp, target_space=target_space, n_random=n_random, n_l_bfgs_b=n_l_bfgs_b, fit_gp=fit_gp)
450+
self.decay_exploration()
451+
return x_max
452+
453+
def decay_exploration(self) -> None:
454+
r"""Decay xi by a constant rate.
467455
468-
def _update_params(self) -> None:
469-
"""Update the parameters of the acquisition function."""
456+
Adjust exploration/exploitation trade-off by reducing xi.
457+
"""
470458
if self.exploration_decay is not None:
471459
if self.exploration_decay_delay is None or self.exploration_decay_delay <= self.i:
472460
self.xi = self.xi*self.exploration_decay
@@ -578,10 +566,15 @@ def suggest(self, gp: GaussianProcessRegressor, target_space: TargetSpace, n_ran
578566
raise NoValidPointRegisteredError(msg)
579567
self.y_max = y_max
580568

581-
return super().suggest(gp=gp, target_space=target_space, n_random=n_random, n_l_bfgs_b=n_l_bfgs_b, fit_gp=fit_gp)
569+
x_max = super().suggest(gp=gp, target_space=target_space, n_random=n_random, n_l_bfgs_b=n_l_bfgs_b, fit_gp=fit_gp)
570+
self.decay_exploration()
571+
return x_max
572+
573+
def decay_exploration(self) -> None:
574+
r"""Decay xi by a constant rate.
582575
583-
def _update_params(self) -> None:
584-
"""Update the parameters of the acquisition function."""
576+
Adjust exploration/exploitation trade-off by reducing xi.
577+
"""
585578
if self.exploration_decay is not None:
586579
if self.exploration_decay_delay is None or self.exploration_decay_delay <= self.i:
587580
self.xi = self.xi*self.exploration_decay

bayes_opt/util.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,24 @@ class NotUniqueError(Exception):
77
"""A point is non-unique."""
88

99

10+
class ConstraintNotSupportedError(Exception):
11+
"""Raised when constrained optimization is not supported."""
12+
13+
pass
14+
15+
16+
class NoValidPointRegisteredError(Exception):
17+
"""Raised when an acquisition function depends on previous points but none are registered."""
18+
19+
pass
20+
21+
22+
class TargetSpaceEmptyError(Exception):
23+
"""Raised when the target space is empty."""
24+
25+
pass
26+
27+
1028
def load_logs(optimizer, logs):
1129
"""Load previous ...
1230

docsrc/code_docs.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ Bayesian Optimization
99
.. automodule:: bayes_opt.bayesian_optimization
1010
:members:
1111

12-
Acquisition function
13-
--------------------
12+
Acquisition Functions
13+
---------------------
1414

15-
.. autoclass:: bayes_opt.util.UtilityFunction
15+
.. automodule:: bayes_opt.acquisition
1616
:members:
1717

1818
Target Space

docsrc/examples.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Examples
88
/basic-tour
99
/advanced-tour
1010
/exploitation_vs_exploration
11+
/acquisition_functions
1112
/visualization
1213
/constraints
1314
/domain_reduction

0 commit comments

Comments
 (0)