From a58ecaf65e494a6a94788a8905469d20289cd558 Mon Sep 17 00:00:00 2001
From: till-m <36440677+till-m@users.noreply.github.com>
Date: Wed, 24 Jan 2024 22:28:19 +0100
Subject: [PATCH] Domain reduction, Sphinx docs (#455)
* Fixes issue-436: Constrained optimization does not allow duplicate points (#437)
* Update docs of `bayesian_optimization.py` and `observer.py`.
* Fix minor style issue in module docstring
* Update docs of `__init__.py` and `events.py`.
* Fix minor style issue in class docstring
* Add workflow to check docstrings
* Update bayes_opt/bayesian_optimization.py
Co-authored-by: Leandro Braga <18340809+leandrobbraga@users.noreply.github.com>
* Improve acq_max seeding of L-BFGS-B optimization (#297)
* bounds_transformer could bypass global_bounds due to the test logic within _trim function in domain_reduction.py (#441)
* Update trim bounds in domain_reduction.py
Previously, when the new upper limit was less than the original lower limit, the new_bounds could bypass the global_bounds.
* Update test_seq_domain_red.py
Added test cases to catch an error when both bounds of new_bounds exceeded the global_bounds
* Update domain_reduction.py
_trim function now avoids an error when both bounds for a given parameter in new_bounds exceed the global_bounds
* Update domain_reduction.py comments
* fixed English in domain_reduction.py
* use numpy to sort bounds, boundary exceeded warn.
* simple sort test added
* domain_red windows target_space to global_bounds
Added windowing function to improve the convergence of optimizers that use domain_reduction. Improved comments and documentation.
* target_space.max respects bounds; SDRT warnings
* Remove unused function.
This function was used to prototype a solution. It should not have been pushed and can be removed.
* Updated target_space.py docstrings
* Update tests/test_target_space.py
Co-authored-by: till-m <36440677+till-m@users.noreply.github.com>
* Added pbound warnings, updated various tests.
* updated line spacing for consistency and style
* added pbound test condition
---------
Co-authored-by: till-m <36440677+till-m@users.noreply.github.com>
* DomainReduction docs, docstyle
* Add missing doc dependency
---------
Co-authored-by: YoungJae Bae <57710489+YoungJaeBae@users.noreply.github.com>
Co-authored-by: Leandro Braga <18340809+leandrobbraga@users.noreply.github.com>
Co-authored-by: ptapping <63924582+ptapping@users.noreply.github.com>
Co-authored-by: Edgar <50716923+perezed00@users.noreply.github.com>
---
.github/workflows/build_docs.yml | 2 +
.gitignore | 4 +-
README.md | 137 +++++++++--------
bayes_opt/constraint.py | 2 +-
bayes_opt/domain_reduction.py | 141 +++++++++++++-----
bayes_opt/target_space.py | 129 ++++++++++------
bayes_opt/util.py | 3 +-
docsrc/Makefile | 2 +
docsrc/conf.py | 5 +-
docsrc/func.ico | Bin 0 -> 136207 bytes
docsrc/index.rst | 1 +
docsrc/quickstart.rst | 2 +
examples/domain_reduction.ipynb | 2 +-
.../bayesian_optimization.gif | Bin
{examples => static}/bo_example.png | Bin
{examples => static}/func.png | Bin
{examples => static}/sdr.png | Bin
tests/test_logs_bounds.log | 5 +
tests/test_seq_domain_red.py | 51 ++++++-
tests/test_target_space.py | 38 ++++-
tests/test_util.py | 22 ++-
21 files changed, 386 insertions(+), 160 deletions(-)
create mode 100644 docsrc/func.ico
create mode 100644 docsrc/quickstart.rst
rename {examples => static}/bayesian_optimization.gif (100%)
rename {examples => static}/bo_example.png (100%)
rename {examples => static}/func.png (100%)
rename {examples => static}/sdr.png (100%)
create mode 100644 tests/test_logs_bounds.log
diff --git a/.github/workflows/build_docs.yml b/.github/workflows/build_docs.yml
index 07282413b..a57745b01 100644
--- a/.github/workflows/build_docs.yml
+++ b/.github/workflows/build_docs.yml
@@ -29,6 +29,8 @@ jobs:
pip install nbsphinx
pip install sphinx_rtd_theme
pip install jupyter
+ pip install numpydoc
+ pip install myst-parser
- name: Install package
run: |
pip install -e .
diff --git a/.gitignore b/.gitignore
index 3f4f6d54e..6556c710e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -32,4 +32,6 @@ venv.bak/
docs/*
docsrc/.ipynb_checkpoints/*
-docsrc/*.ipynb
\ No newline at end of file
+docsrc/*.ipynb
+docsrc/static/*
+docsrc/README.md
diff --git a/README.md b/README.md
index ac94cc649..652dc9a18 100644
--- a/README.md
+++ b/README.md
@@ -1,16 +1,18 @@
-
+
# Bayesian Optimization
-![tests](https://github.com/fmfn/BayesianOptimization/actions/workflows/run_tests.yml/badge.svg)
-[![Codecov](https://codecov.io/github/fmfn/BayesianOptimization/badge.svg?branch=master&service=github)](https://codecov.io/github/fmfn/BayesianOptimization?branch=master)
+![tests](https://github.com/bayesian-optimization/BayesianOptimization/actions/workflows/run_tests.yml/badge.svg)
+[![Codecov](https://codecov.io/github/bayesian-optimization/BayesianOptimization/badge.svg?branch=master&service=github)](https://codecov.io/github/bayesian-optimization/BayesianOptimization?branch=master)
[![Pypi](https://img.shields.io/pypi/v/bayesian-optimization.svg)](https://pypi.python.org/pypi/bayesian-optimization)
Pure Python implementation of bayesian global optimization with gaussian
processes.
+## Installation
+
* PyPI (pip):
```console
@@ -30,25 +32,18 @@ suited for optimization of high cost functions, situations where the balance
between exploration and exploitation is important.
## Quick Start
-See below for a quick tour over the basics of the Bayesian Optimization package. More detailed information, other advanced features, and tips on usage/implementation can be found in the [examples](https://github.com/fmfn/BayesianOptimization/tree/master/examples) folder. I suggest that you:
-- Follow the
-[basic tour notebook](https://github.com/fmfn/BayesianOptimization/blob/master/examples/basic-tour.ipynb)
-to learn how to use the package's most important features.
-- Take a look at the
-[advanced tour notebook](https://github.com/fmfn/BayesianOptimization/blob/master/examples/advanced-tour.ipynb)
-to learn how to make the package more flexible, how to deal with categorical parameters, how to use observers, and more.
-- Check out this
-[notebook](https://github.com/fmfn/BayesianOptimization/blob/master/examples/visualization.ipynb)
-with a step by step visualization of how this method works.
-- To understand how to use bayesian optimization when additional constraints are present, see the
-[constrained optimization notebook](https://github.com/fmfn/BayesianOptimization/blob/master/examples/constraints.ipynb).
-- Explore this [notebook](https://github.com/fmfn/BayesianOptimization/blob/master/examples/exploitation_vs_exploration.ipynb)
+See below for a quick tour over the basics of the Bayesian Optimization package. More detailed information, other advanced features, and tips on usage/implementation can be found in the [examples](http://bayesian-optimization.github.io/BayesianOptimization/examples.html) folder. I suggest that you:
+- Follow the [basic tour notebook](http://bayesian-optimization.github.io/BayesianOptimization/basic-tour.html) to learn how to use the package's most important features.
+- Take a look at the [advanced tour notebook](http://bayesian-optimization.github.io/BayesianOptimization/advanced-tour.html) to learn how to make the package more flexible, how to deal with categorical parameters, how to use observers, and more.
+- Check out this [notebook](http://bayesian-optimization.github.io/BayesianOptimization/visualization.html) with a step by step visualization of how this method works.
+- To understand how to use bayesian optimization when additional constraints are present, see the [constrained optimization notebook](http://bayesian-optimization.github.io/BayesianOptimization/constraints.html).
+- Explore this [notebook](http://bayesian-optimization.github.io/BayesianOptimization/exploitation_vs_exploration.html)
exemplifying the balance between exploration and exploitation and how to
control it.
-- Go over this [script](https://github.com/fmfn/BayesianOptimization/blob/master/examples/sklearn_example.py)
+- Go over this [script](https://github.com/bayesian-optimization/BayesianOptimization/blob/master/examples/sklearn_example.py)
for examples of how to tune parameters of Machine Learning models using cross validation and bayesian optimization.
-- Explore the [domain reduction notebook](https://github.com/fmfn/BayesianOptimization/blob/master/examples/domain_reduction.ipynb) to learn more about how search can be sped up by dynamically changing parameters' bounds.
-- Finally, take a look at this [script](https://github.com/fmfn/BayesianOptimization/blob/master/examples/async_optimization.py)
+- Explore the [domain reduction notebook](http://bayesian-optimization.github.io/BayesianOptimization/domain_reduction.html) to learn more about how search can be sped up by dynamically changing parameters' bounds.
+- Finally, take a look at this [script](https://github.com/bayesian-optimization/BayesianOptimization/blob/master/examples/async_optimization.py)
for ideas on how to implement bayesian optimization in a distributed fashion using this package.
@@ -56,11 +51,11 @@ for ideas on how to implement bayesian optimization in a distributed fashion usi
Bayesian optimization works by constructing a posterior distribution of functions (gaussian process) that best describes the function you want to optimize. As the number of observations grows, the posterior distribution improves, and the algorithm becomes more certain of which regions in parameter space are worth exploring and which are not, as seen in the picture below.
-![BayesianOptimization in action](./examples/bo_example.png)
+![BayesianOptimization in action](./static/bo_example.png)
As you iterate over and over, the algorithm balances its needs of exploration and exploitation taking into account what it knows about the target function. At each step a Gaussian Process is fitted to the known samples (points previously explored), and the posterior distribution, combined with a exploration strategy (such as UCB (Upper Confidence Bound), or EI (Expected Improvement)), are used to determine the next point that should be explored (see the gif below).
-![BayesianOptimization in action](./examples/bayesian_optimization.gif)
+![BayesianOptimization in action](./static/bayesian_optimization.gif)
This process is designed to minimize the number of steps required to find a combination of parameters that are close to the optimal combination. To do so, this method uses a proxy optimization problem (finding the maximum of the acquisition function) that, albeit still a hard problem, is cheaper (in the computational sense) and common tools can be employed. Therefore Bayesian Optimization is most adequate for situations where sampling the function to be optimized is a very expensive endeavor. See the references for a proper discussion of this method.
@@ -68,10 +63,9 @@ This project is under active development, if you find a bug, or anything that
needs correction, please let me know.
-Basic tour of the Bayesian Optimization package
-===============================================
+## Basic tour of the Bayesian Optimization package
-## 1. Specifying the function to be optimized
+### 1. Specifying the function to be optimized
This is a function optimization package, therefore the first and most important ingredient is, of course, the function to be optimized.
@@ -89,7 +83,7 @@ def black_box_function(x, y):
return -x ** 2 - (y - 1) ** 2 + 1
```
-## 2. Getting Started
+### 2. Getting Started
All we need to get started is to instantiate a `BayesianOptimization` object specifying a function to be optimized `f`, and its parameters with their corresponding bounds, `pbounds`. This is a constrained optimization technique, so you must specify the minimum and maximum values that can be probed for each parameter in order for it to work
@@ -160,7 +154,7 @@ for i, res in enumerate(optimizer.res):
```
-### 2.1 Changing bounds
+#### 2.1 Changing bounds
During the optimization process you may realize the bounds chosen for some parameters are not adequate. For these situations you can invoke the method `set_bounds` to alter them. You can pass any combination of **existing** parameters and their associated new bounds.
@@ -183,17 +177,17 @@ optimizer.maximize(
| 10 | -1.762 | 1.442 | 0.1735 |
=================================================
-### 2.2 Sequential Domain Reduction
+#### 2.2 Sequential Domain Reduction
Sometimes the initial boundaries specified for a problem are too wide, and adding points to improve the response surface in regions of the solution domain is extraneous. Other times the cost function is very expensive to compute, and minimizing the number of calls is extremely beneficial.
When it's worthwhile to converge on an optimal point quickly rather than try to find the optimal point, contracting the domain around the current optimal value as the search progresses can speed up the search progress considerably. Using the `SequentialDomainReductionTransformer` the bounds of the problem can be panned and zoomed dynamically in an attempt to improve convergence.
-![sequential domain reduction](./examples/sdr.png)
+![sequential domain reduction](./static/sdr.png)
-An example of using the `SequentialDomainReductionTransformer` is shown in the [domain reduction notebook](https://github.com/fmfn/BayesianOptimization/blob/master/examples/domain_reduction.ipynb). More information about this method can be found in the paper ["On the robustness of a simple domain reduction scheme for simulation‐based optimization"](http://www.truegrid.com/srsm_revised.pdf).
+An example of using the `SequentialDomainReductionTransformer` is shown in the [domain reduction notebook](http://bayesian-optimization.github.io/BayesianOptimization/domain_reduction.html). More information about this method can be found in the paper ["On the robustness of a simple domain reduction scheme for simulation‐based optimization"](http://www.truegrid.com/srsm_revised.pdf).
-## 3. Guiding the optimization
+### 3. Guiding the optimization
It is often the case that we have an idea of regions of the parameter space where the maximum of our function might lie. For these situations the `BayesianOptimization` object allows the user to specify points to be probed. By default these will be explored lazily (`lazy=True`), meaning these points will be evaluated only the next time you call `maximize`. This probing process happens before the gaussian process takes over.
@@ -221,11 +215,11 @@ optimizer.maximize(init_points=0, n_iter=0)
=================================================
-## 4. Saving, loading and restarting
+### 4. Saving, loading and restarting
By default you can follow the progress of your optimization by setting `verbose>0` when instantiating the `BayesianOptimization` object. If you need more control over logging/alerting you will need to use an observer. For more information about observers checkout the advanced tour notebook. Here we will only see how to use the native `JSONLogger` object to save to and load progress from files.
-### 4.1 Saving progress
+#### 4.1 Saving progress
```python
@@ -255,7 +249,7 @@ optimizer.maximize(
By default the previous data in the json file is removed. If you want to keep working with the same logger, the `reset` parameter in `JSONLogger` should be set to False.
-### 4.2 Loading progress
+#### 4.2 Loading progress
Naturally, if you stored progress you will be able to load that onto a new instance of `BayesianOptimization`. The easiest way to do it is by invoking the `load_logs` function, from the `util` submodule.
@@ -277,54 +271,71 @@ load_logs(new_optimizer, logs=["./logs.log"]);
## Next Steps
-This introduction covered the most basic functionality of the package. Checkout the [basic-tour](https://github.com/fmfn/BayesianOptimization/blob/master/examples/basic-tour.ipynb) and [advanced-tour](https://github.com/fmfn/BayesianOptimization/blob/master/examples/advanced-tour.ipynb) notebooks in the example folder, where you will find detailed explanations and other more advanced functionality. Also, browse the examples folder for implementation tips and ideas.
-
-Installation
-============
-
-### Installation
-
-The latest release can be obtained by two ways:
-
-* With PyPI (pip):
-
- pip install bayesian-optimization
+This introduction covered the most basic functionality of the package. Checkout the [basic-tour](http://bayesian-optimization.github.io/BayesianOptimization/basic-tour.html) and [advanced-tour](http://bayesian-optimization.github.io/BayesianOptimization/advanced-tour.html), where you will find detailed explanations and other more advanced functionality. Also, browse the [examples](http://bayesian-optimization.github.io/BayesianOptimization/examples.html) for implementation tips and ideas.
-* With conda (from conda-forge channel):
-
- conda install -c conda-forge bayesian-optimization
-
-The bleeding edge version can be installed with:
-
- pip install git+https://github.com/fmfn/BayesianOptimization.git
+## Installing from GitHub
+To install directly from master, simply run
+```
+pip install https://github.com/bayesian-optimization/BayesianOptimization/archive/master.zip
+```
+Please note that these builds are not necessarily stable.
-If you prefer, you can clone it and run the setup.py file. Use the following
-commands to get a copy from Github and install all dependencies:
+If you prefer, you can also clone the repository and run `setup.py`:
+```
+git clone https://github.com/bayesian-optimization/BayesianOptimization.git
+cd BayesianOptimization
+python setup.py install
+```
+
- git clone https://github.com/fmfn/BayesianOptimization.git
- cd BayesianOptimization
- python setup.py install
+## Minutiae
-Citation
-============
+### Citation
-If you used this package in your research and is interested in citing it here's how you do it:
+If you used this package in your research, please cite it:
```
@Misc{,
author = {Fernando Nogueira},
title = {{Bayesian Optimization}: Open source constrained global optimization tool for {Python}},
year = {2014--},
- url = " https://github.com/fmfn/BayesianOptimization"
+ url = " https://github.com/bayesian-optimization/BayesianOptimization"
+}
+```
+If you used any of the advanced functionalities, please additionally cite the corresponding publication:
+
+For the `SequentialDomainTransformer`:
+```
+@article{
+ author = {Stander, Nielen and Craig, Kenneth},
+ year = {2002},
+ month = {06},
+ pages = {},
+ title = {On the robustness of a simple domain reduction scheme for simulation-based optimization},
+ volume = {19},
+ journal = {International Journal for Computer-Aided Engineering and Software (Eng. Comput.)},
+ doi = {10.1108/02644400210430190}
+}
+```
+
+For constrained optimization:
+```
+@inproceedings{gardner2014bayesian,
+ title={Bayesian optimization with inequality constraints.},
+ author={Gardner, Jacob R and Kusner, Matt J and Xu, Zhixiang Eddie and Weinberger, Kilian Q and Cunningham, John P},
+ booktitle={ICML},
+ volume={2014},
+ pages={937--945},
+ year={2014}
}
```
-# Dependencies
+### Dependencies
* Numpy
* Scipy
* Scikit-learn
-# References:
+### References:
* http://papers.nips.cc/paper/4522-practical-bayesian-optimization-of-machine-learning-algorithms.pdf
* http://arxiv.org/pdf/1012.2599v1.pdf
* http://www.gaussianprocess.org/gpml/
diff --git a/bayes_opt/constraint.py b/bayes_opt/constraint.py
index 5ea333525..10142e43b 100644
--- a/bayes_opt/constraint.py
+++ b/bayes_opt/constraint.py
@@ -81,7 +81,7 @@ def eval(self, **kwargs):
Raises
------
TypeError
- If the kwargs keys don't match the function argument names.
+ If the kwargs' keys don't match the function argument names.
"""
try:
return self.fun(**kwargs)
diff --git a/bayes_opt/domain_reduction.py b/bayes_opt/domain_reduction.py
index f4b86ffc8..f1d31a418 100644
--- a/bayes_opt/domain_reduction.py
+++ b/bayes_opt/domain_reduction.py
@@ -1,26 +1,37 @@
+"""Implement domain transformation.
+
+In particular, this provides a base transformer class and a sequential domain
+reduction transformer as based on Stander and Craig's "On the robustness of a
+simple domain reduction scheme for simulation-based optimization"
+"""
from typing import Optional, Union, List
import numpy as np
from .target_space import TargetSpace
+from warnings import warn
class DomainTransformer():
- '''The base transformer class'''
+ """Base class."""
- def __init__(self, **kwargs):
+ def __init__(self, **kwargs) -> None:
+ """To override with specific implementation."""
pass
- def initialize(self, target_space: TargetSpace):
+ def initialize(self, target_space: TargetSpace) -> None:
+ """To override with specific implementation."""
raise NotImplementedError
- def transform(self, target_space: TargetSpace):
+ def transform(self, target_space: TargetSpace) -> dict:
+ """To override with specific implementation."""
raise NotImplementedError
class SequentialDomainReductionTransformer(DomainTransformer):
- """
+ """Reduce the searchable space.
+
A sequential domain reduction transformer based on the work by Stander, N. and Craig, K:
- "On the robustness of a simple domain reduction scheme for simulation‐based optimization"
+ "On the robustness of a simple domain reduction scheme for simulation-based optimization"
"""
def __init__(
@@ -36,7 +47,14 @@ def __init__(
self.minimum_window_value = minimum_window
def initialize(self, target_space: TargetSpace) -> None:
- """Initialize all of the parameters"""
+ """Initialize all of the parameters.
+
+ Parameters
+ ----------
+ target_space : TargetSpace
+ TargetSpace this DomainTransformer operates on.
+ """
+ # Set the original bounds
self.original_bounds = np.copy(target_space.bounds)
self.bounds = [self.original_bounds]
@@ -47,6 +65,7 @@ def initialize(self, target_space: TargetSpace) -> None:
else:
self.minimum_window = [self.minimum_window_value] * len(target_space.bounds)
+ # Set initial values
self.previous_optimal = np.mean(target_space.bounds, axis=1)
self.current_optimal = np.mean(target_space.bounds, axis=1)
self.r = target_space.bounds[:, 1] - target_space.bounds[:, 0]
@@ -72,14 +91,12 @@ def initialize(self, target_space: TargetSpace) -> None:
self._window_bounds_compatibility(self.original_bounds)
def _update(self, target_space: TargetSpace) -> None:
-
+ """Update contraction rate, window size, and window center."""
# setting the previous
self.previous_optimal = self.current_optimal
self.previous_d = self.current_d
-
- self.current_optimal = target_space.params[
- np.argmax(target_space.target)
- ]
+
+ self.current_optimal = target_space.params_to_array(target_space.max()['params'])
self.current_d = 2.0 * (self.current_optimal -
self.previous_optimal) / self.r
@@ -97,32 +114,82 @@ def _update(self, target_space: TargetSpace) -> None:
self.r = self.contraction_rate * self.r
def _trim(self, new_bounds: np.array, global_bounds: np.array) -> np.array:
- for i, variable in enumerate(new_bounds):
- if variable[0] < global_bounds[i, 0]:
- variable[0] = global_bounds[i, 0]
- if variable[1] > global_bounds[i, 1]:
- variable[1] = global_bounds[i, 1]
- for i, entry in enumerate(new_bounds):
- if entry[0] > entry[1]:
- new_bounds[i, 0] = entry[1]
- new_bounds[i, 1] = entry[0]
- window_width = abs(entry[0] - entry[1])
- if window_width < self.minimum_window[i]:
- dw = (self.minimum_window[i] - window_width) / 2.0
- left_expansion_space = abs(global_bounds[i, 0] - entry[0]) # should be non-positive
- right_expansion_space = abs(global_bounds[i, 1] - entry[1]) # should be non-negative
- # conservative
- dw_l = min(dw, left_expansion_space)
- dw_r = min(dw, right_expansion_space)
- # this crawls towards the edge
- ddw_r = dw_r + max(dw - dw_l, 0)
- ddw_l = dw_l + max(dw - dw_r, 0)
- new_bounds[i, 0] -= ddw_l
- new_bounds[i, 1] += ddw_r
+ """
+ Adjust the new_bounds and verify that they adhere to global_bounds and minimum_window.
+
+ Parameters
+ ----------
+ new_bounds : np.array
+ The proposed new_bounds that (may) need adjustment.
+
+ global_bounds : np.array
+ The maximum allowable bounds for each parameter.
+
+ Returns
+ -------
+ new_bounds : np.array
+ The adjusted bounds after enforcing constraints.
+ """
+ #sort bounds
+ new_bounds = np.sort(new_bounds)
+
+ # Validate each parameter's bounds against the global_bounds
+ for i, pbounds in enumerate(new_bounds):
+ # If the one of the bounds is outside the global bounds, reset the bound to the global bound
+ # This is expected to happen when the window is near the global bounds, no warning is issued
+ if (pbounds[0] < global_bounds[i, 0]):
+ pbounds[0] = global_bounds[i, 0]
+
+ if (pbounds[1] > global_bounds[i, 1]):
+ pbounds[1] = global_bounds[i, 1]
+
+ # If a lower bound is greater than the associated global upper bound, reset it to the global lower bound
+ if (pbounds[0] > global_bounds[i, 1]):
+ pbounds[0] = global_bounds[i, 0]
+ warn("\nDomain Reduction Warning:\n"+
+ "A parameter's lower bound is greater than the global upper bound."+
+ "The offensive boundary has been reset."+
+ "Be cautious of subsequent reductions.", stacklevel=2)
+
+ # If an upper bound is less than the associated global lower bound, reset it to the global upper bound
+ if (pbounds[1] < global_bounds[i, 0]):
+ pbounds[1] = global_bounds[i, 1]
+ warn("\nDomain Reduction Warning:\n"+
+ "A parameter's lower bound is greater than the global upper bound."+
+ "The offensive boundary has been reset."+
+ "Be cautious of subsequent reductions.", stacklevel=2)
+
+ # Adjust new_bounds to ensure they respect the minimum window width for each parameter
+ for i, pbounds in enumerate(new_bounds):
+ current_window_width = abs(pbounds[0] - pbounds[1])
+
+ # If the window width is less than the minimum allowable width, adjust it
+ # Note that when minimum_window < width of the global bounds one side always has more space than required
+ if current_window_width < self.minimum_window[i]:
+ width_deficit = (self.minimum_window[i] - current_window_width) / 2.0
+ available_left_space = abs(global_bounds[i, 0] - pbounds[0])
+ available_right_space = abs(global_bounds[i, 1] - pbounds[1])
+
+ # determine how much to expand on the left and right
+ expand_left = min(width_deficit, available_left_space)
+ expand_right = min(width_deficit, available_right_space)
+
+ # calculate the deficit on each side
+ expand_left_deficit = width_deficit - expand_left
+ expand_right_deficit = width_deficit - expand_right
+
+ # shift the deficit to the side with more space
+ adjust_left = expand_left + max(expand_right_deficit, 0)
+ adjust_right = expand_right + max(expand_left_deficit, 0)
+
+ # adjust the bounds
+ pbounds[0] -= adjust_left
+ pbounds[1] += adjust_right
+
return new_bounds
def _window_bounds_compatibility(self, global_bounds: np.array) -> bool:
- """Checks if global bounds are compatible with the minimum window sizes."""
+ """Check if global bounds are compatible with the minimum window sizes."""
for i, entry in enumerate(global_bounds):
global_window_width = abs(entry[1] - entry[0])
if global_window_width < self.minimum_window[i]:
@@ -133,7 +200,7 @@ def _create_bounds(self, parameters: dict, bounds: np.array) -> dict:
return {param: bounds[i, :] for i, param in enumerate(parameters)}
def transform(self, target_space: TargetSpace) -> dict:
-
+ """Transform the bounds of the target space."""
self._update(target_space)
new_bounds = np.array(
@@ -143,6 +210,6 @@ def transform(self, target_space: TargetSpace) -> dict:
]
).T
- self._trim(new_bounds, self.original_bounds)
+ new_bounds = self._trim(new_bounds, self.original_bounds)
self.bounds.append(new_bounds)
return self._create_bounds(target_space.keys, new_bounds)
diff --git a/bayes_opt/target_space.py b/bayes_opt/target_space.py
index 195474cb2..bf727c5c2 100644
--- a/bayes_opt/target_space.py
+++ b/bayes_opt/target_space.py
@@ -1,6 +1,7 @@
"""Manages the optimization domain and holds points."""
import numpy as np
from colorama import Fore
+from warnings import warn
from .util import ensure_rng, NotUniqueError
@@ -38,9 +39,11 @@ class TargetSpace():
>>> return p1 + p2
>>> pbounds = {'p1': (0, 1), 'p2': (1, 100)}
>>> space = TargetSpace(target_func, pbounds, random_state=0)
- >>> x = space.random_points(1)[0]
- >>> y = space.register_point(x)
- >>> assert self.max_point()['max_val'] == y
+ >>> x = np.array([4 , 5])
+ >>> y = target_func(x)
+ >>> space.register(x, y)
+ >>> assert self.max()['target'] == 9
+ >>> assert self.max()['params'] == {'p1': 1.0, 'p2': 2.0}
"""
def __init__(self, target_func, pbounds, constraint=None, random_state=None,
@@ -178,6 +181,30 @@ def constraint_values(self):
return self._constraint_values
+ @property
+ def mask(self):
+ """Return a boolean array of valid points.
+
+ Points are valid if they satisfy both the constraint and boundary conditions.
+
+ Returns
+ -------
+ np.ndarray
+ """
+ mask = np.ones_like(self.target, dtype=bool)
+
+ # mask points that don't satisfy the constraint
+ if self._constraint is not None:
+ mask &= self._constraint.allowed(self._constraint_values)
+
+ # mask points that are outside the bounds
+ if self._bounds is not None:
+ within_bounds = np.all((self._bounds[:, 0] <= self._params) &
+ (self._params <= self._bounds[:, 1]), axis=1)
+ mask &= within_bounds
+
+ return mask
+
def params_to_array(self, params):
"""Convert a dict representation of parameters into an array version.
@@ -260,13 +287,14 @@ def register(self, params, target, constraint_value=None):
Examples
--------
+ >>> target_func = lambda p1, p2: p1 + p2
>>> pbounds = {'p1': (0, 1), 'p2': (1, 100)}
- >>> space = TargetSpace(lambda p1, p2: p1 + p2, pbounds)
+ >>> space = TargetSpace(target_func, pbounds)
>>> len(space)
0
>>> x = np.array([0, 0])
>>> y = 1
- >>> space.add_observation(x, y)
+ >>> space.register(x, y)
>>> len(space)
1
"""
@@ -281,6 +309,11 @@ def register(self, params, target, constraint_value=None):
raise NotUniqueError(f'Data point {x} is not unique. You can set'
' "allow_duplicate_points=True" to avoid this error')
+ # if x is not within the bounds of the parameter space, warn the user
+ if self._bounds is not None:
+ if not np.all((self._bounds[:, 0] <= x) & (x <= self._bounds[:, 1])):
+ warn(f'\nData point {x} is outside the bounds of the parameter space. ', stacklevel=2)
+
self._params = np.concatenate([self._params, x.reshape(1, -1)])
self._target = np.concatenate([self._target, [target]])
@@ -313,6 +346,15 @@ def probe(self, params):
-------
y : float
target function value.
+
+ Example
+ -------
+ >>> target_func = lambda p1, p2: p1 + p2
+ >>> pbounds = {'p1': (0, 1), 'p2': (1, 100)}
+ >>> space = TargetSpace(target_func, pbounds)
+ >>> space.probe([1, 5])
+ >>> assert self.max()['target'] == 6
+ >>> assert self.max()['params'] == {'p1': 1.0, 'p2': 5.0}
"""
x = self._as_array(params)
params = dict(zip(self._keys, x))
@@ -327,19 +369,20 @@ def probe(self, params):
return target, constraint_value
def random_sample(self):
- """Create random points within the bounds of the space.
+ """
+ Sample a random point from within the bounds of the space.
Returns
-------
- data: np.ndarray
- [num x dim] array points with dimensions corresponding to `self._keys`
+ data: ndarray
+ [1 x dim] array with dimensions corresponding to `self._keys`
Examples
--------
>>> target_func = lambda p1, p2: p1 + p2
>>> pbounds = {'p1': (0, 1), 'p2': (1, 100)}
>>> space = TargetSpace(target_func, pbounds, random_state=0)
- >>> space.random_points(1)
+ >>> space.random_sample()
array([[ 55.33253689, 0.54488318]])
"""
data = np.empty((1, self.dim))
@@ -348,61 +391,48 @@ def random_sample(self):
return data.ravel()
def _target_max(self):
- """Get maximum target value found.
+ """Get the maximum target value within the current parameter bounds.
If there is a constraint present, the maximum value that fulfills the
- constraint is returned.
+ constraint within the parameter bounds is returned.
-
Returns
-------
- dict | None
- The maximum allowed point's target function value.
- Returns None if there is no (allowed) maximum.
+ max: float
+ The maximum target value.
+
"""
if len(self.target) == 0:
return None
- if self._constraint is None:
- return self.target.max()
-
- allowed = self._constraint.allowed(self._constraint_values)
- if allowed.any():
- return self.target[allowed].max()
+ if len(self.target[self.mask]) == 0:
+ return None
- return None
+ return self.target[self.mask].max()
def max(self):
"""Get maximum target value found and corresponding parameters.
-
+
If there is a constraint present, the maximum value that fulfills the
- constraint is returned.
+ constraint within the parameter bounds is returned.
Returns
-------
- dict | None
- A dictionary containing the maximum allowed point's target function
- value, parameters, and, if applicable, constraint function value.
- Returns None if there is no (allowed) maximum.
+ res: dict
+ A dictionary with the keys 'target' and 'params'. The value of
+ 'target' is the maximum target value, and the value of 'params' is
+ a dictionary with the parameter names as keys and the parameter
+ values as values.
+
"""
target_max = self._target_max()
-
if target_max is None:
return None
- if self._constraint is not None:
- allowed = self._constraint.allowed(self._constraint_values)
-
- target = self.target[allowed]
- params = self.params[allowed]
- constraint_values = self.constraint_values[allowed]
- else:
- target = self.target
- params = self.params
-
- target_max_idx = np.where(target == target_max)[0][0]
-
-
+ target = self.target[self.mask]
+ params = self.params[self.mask]
+ target_max_idx = np.argmax(target)
+
res = {
'target': target_max,
'params': dict(
@@ -411,6 +441,7 @@ def max(self):
}
if self._constraint is not None:
+ constraint_values = self.constraint_values[self.mask]
res['constraint'] = constraint_values[target_max_idx]
return res
@@ -420,9 +451,17 @@ def res(self):
Returns
-------
- dict
- A dictionary containing the target function values, parameters,
- and, if applicable, constraint function values and allowedness.
+ res: list
+ A list of dictionaries with the keys 'target', 'params', and
+ 'constraint'. The value of 'target' is the target value, the value
+ of 'params' is a dictionary with the parameter names as keys and the
+ parameter values as values, and the value of 'constraint' is the
+ constraint fulfillment.
+
+ Notes
+ -----
+ Does not report if points are within the bounds of the parameter space.
+
"""
if self._constraint is None:
params = [dict(zip(self.keys, p)) for p in self.params]
diff --git a/bayes_opt/util.py b/bayes_opt/util.py
index 417e307f7..a90f344d7 100644
--- a/bayes_opt/util.py
+++ b/bayes_opt/util.py
@@ -8,7 +8,7 @@
def acq_max(ac, gp, y_max, bounds, random_state, constraint=None, n_warmup=10000, n_iter=10, y_max_params=None):
"""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` (10) random starting points.
@@ -106,7 +106,6 @@ def adjusted_ac(x):
x_seeds = random_state.uniform(bounds[:, 0], bounds[:, 1],
size=(1+n_iter+int(not y_max_params is None),
bounds.shape[0]))
-
x_seeds[0] = x_max
if not y_max_params is None:
# Add the provided best sample to the seeds so that the optimization
diff --git a/docsrc/Makefile b/docsrc/Makefile
index e0e46fb58..c3886de30 100644
--- a/docsrc/Makefile
+++ b/docsrc/Makefile
@@ -15,6 +15,8 @@ BUILDDIR = ../docs
.PHONY: help Makefile
github:
+ @cp ../README.md .
+ @cp -r ../static .
@make html
@cp -a ../docs/html/. ../docs
diff --git a/docsrc/conf.py b/docsrc/conf.py
index b758e36f4..159db2748 100644
--- a/docsrc/conf.py
+++ b/docsrc/conf.py
@@ -42,7 +42,9 @@
'sphinx.ext.githubpages',
'nbsphinx',
'IPython.sphinxext.ipython_console_highlighting',
- 'sphinx.ext.mathjax']
+ 'sphinx.ext.mathjax',
+ 'myst_parser',
+ 'numpydoc']
source_suffix = {
'.rst': 'restructuredtext',
@@ -63,6 +65,7 @@
# a list of builtin themes.
#
html_theme = 'sphinx_rtd_theme'
+html_favicon = 'func.ico'
# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
diff --git a/docsrc/func.ico b/docsrc/func.ico
new file mode 100644
index 0000000000000000000000000000000000000000..f294702610b6d5db3f5c55d0e2b71ea6ec497bc5
GIT binary patch
literal 136207
zcmd3Lg;yMH%rB*d7B60;I19VzqQ#55yBBvaZY}P*xWnS^4#nNwi@4$I7GPru6J-`a5+(MaHenTU%&pBrv3B|?qf6@-1qPQmllG9
zV@i4l$ISe{bP+Wiochw6B-#I^Kf}SjzPy9`{Pz4$pZNzkxIzqgxKIT-aWrH?*!`|@1*A=5$>i32Gk4^
zpmGrS+x>{&jxM&cVUd|Bji9AOK5qf_2*5@#G*SbG*2PPQ9JfG6KA}{Of~o
z(d*?ErL5~5m*;8i#IjX+`~323kz$@$Bq6)y|E~+OA{9_V-df0;iNs%p#9QCJnflFR
z_QRiv!A#GSspo%r;;jVimTgobQ7-R&)&{R{mfH>dB`6H~k!XmI_hyUEbBiq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