Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve acq_max seeding of L-BFGS-B optimization #297

Merged
merged 5 commits into from
Dec 7, 2023

Conversation

ptapping
Copy link
Contributor

The utils.acq_max() function attempts to find the maximum of the function space by first random sampling, then performing a L-BFGS-B optimization starting from some number of random points in the parameter space. This function is used by the optimizer.suggest() routine.

I was using some code like this to do a quick-and-dirty estimate of the current optimal parameters, which should simply find the maximum value of the currently fitted Gaussian process model function:

suggestion = optimizer.suggest(UtilityFunction(kind='ucb', kappa=0.0, xi=0.0))
print(f"Best guess: {suggestion} = {optimizer._gp.predict([[suggestion['x'], suggestion['y']]])[0]}")

This worked most of the time, but I noticed that the suggestion is sometimes worse than the current best result in optimizer.res. In other words, the returned "best" y value can actually be worse than the best known y_max which is passed in as a parameter!

It turns out while suggest() passes acq_max() the best y_max value it has found so far, it doesn't let it know what set of parameters were used to obtain it. This commit passes in these parameters to the acq_max() function where they are added to the list of seeds used for the L-BFGS-B optimization routine. This ensures that the returned best value is at least as good as y_max, but also means the optimization routine should be able to "walk up" the local maximum around the current best y_max.

The changes to acq_max() uses a new keyword parameter with a default of None which retains API backwards compatibility.

In addition, the best candidate from the random sampling phase is also added to the list of seeds for the L-BFGS-B optimization routine, allowing the algorithm to "polish" that result by walking up its local maximum as well.

@matheus695p
Copy link

TOP !

@bwheelz36
Copy link
Collaborator

@ptapping

  • sorry about slow response; I'm trying to work through all the open issues and pull requests....
  • could you please make any random commit (e.g. add a space or a comment) to this branch, which will trigger the (newly working) CI to run (actually - can you add the new parameter to the docstring?)
  • suggest is also called from maximize - in your opinion should this behavior be coded into maximize as well? Probably right?

@ptapping
Copy link
Contributor Author

ptapping commented Jul 6, 2022

Hi @bwheelz36

Good stuff. I'm on a bit of a holiday at the moment, but will take a look at this soon when I'm back in front of my real computer.

@ptapping
Copy link
Contributor Author

ptapping commented Dec 7, 2023

Hi @bwheelz36 ,

Better late than never! I finally updated this pull request to sync with the changes in master over the last two years. My testing seems to indicate it all still works as expected.

The docstring is added to the acq_max() function.

I don't think any other changes are required inside of maximize() as the suggest() call takes care of everything transparently.

Copy link

codecov bot commented Dec 7, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (11a0c6a) 98.42% compared to head (c03dee0) 98.43%.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #297   +/-   ##
=======================================
  Coverage   98.42%   98.43%           
=======================================
  Files           8        8           
  Lines         573      576    +3     
  Branches       84       85    +1     
=======================================
+ Hits          564      567    +3     
  Misses          5        5           
  Partials        4        4           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@brendan-whelan-seetreat
Copy link

Looks good to me, @till-m unless you have any concerns I can merge?

@till-m
Copy link
Member

till-m commented Dec 7, 2023

LGTM :)

@bwheelz36 bwheelz36 merged commit 8cf4531 into bayesian-optimization:master Dec 7, 2023
7 checks passed
till-m added a commit that referenced this pull request Dec 10, 2023
* Improve acq_max seeding of L-BFGS-B optimization (#297)

---------

Co-authored-by: ptapping <63924582+ptapping@users.noreply.github.com>
till-m added a commit that referenced this pull request Jan 24, 2024
* 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>
till-m added a commit that referenced this pull request Feb 26, 2024
* Replace custom colour implementation, add docs for `logger.py`, `util.py` (#435)

* Replace custom colour implementation, add docs for `logger.py`, `util.py`

* minor typo/syntax fixes

* User `or` to separate different possible types

* Update docs & linting for `constraints.py`, `target_space.py` (#440)

* Run tests on any PR

* Update docs, linting

* Update bayes_opt/constraint.py

Co-authored-by: Leandro Braga <18340809+leandrobbraga@users.noreply.github.com>

* Rename mislabelled parameters

---------

Co-authored-by: Leandro Braga <18340809+leandrobbraga@users.noreply.github.com>

* Update various docstrings, add workflow to check docstrings (#445)

* 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>

---------

Co-authored-by: YoungJae Bae <57710489+YoungJaeBae@users.noreply.github.com>
Co-authored-by: Leandro Braga <18340809+leandrobbraga@users.noreply.github.com>

* Pydocstyle (#453)

* Improve acq_max seeding of L-BFGS-B optimization (#297)

---------

Co-authored-by: ptapping <63924582+ptapping@users.noreply.github.com>

* 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>

* Small fixes, minor cosmetic changes

* Add some more docs to target space and constraint, cosmetic changes

* Remove duplicate code snippet

* Remove numpydoc + adjust "*" formatting accordingly

* Explicitly add D417, adjust code accordingly

* Adjust `TargetSpace.probe()` behaviour to be in line with docstring.

* Update bayes_opt/target_space.py

Co-authored-by: Edgar <50716923+perezed00@users.noreply.github.com>

* Update README.md

---------

Co-authored-by: Leandro Braga <18340809+leandrobbraga@users.noreply.github.com>
Co-authored-by: YoungJae Bae <57710489+YoungJaeBae@users.noreply.github.com>
Co-authored-by: ptapping <63924582+ptapping@users.noreply.github.com>
Co-authored-by: Edgar <50716923+perezed00@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants