bayesian-optimization / BayesianOptimization

A Python implementation of global optimization with gaussian processes.
https://bayesian-optimization.github.io/BayesianOptimization/index.html
MIT License
7.81k stars 1.53k forks source link

Domain reduction with the Suggest-Evaluate-Register Paradigm #480

Closed AArriandiaga closed 2 months ago

AArriandiaga commented 2 months ago

The domain reduction with the Suggest-Evaluate-Register Paradigm has no effect; the solutions are the same using or not using domain reduction. The effects can be seen with the maximize function but not with the Suggest-Evaluate-Register Paradigm. Thus, is it not possible to use domain reduction with the Suggest-Evaluate-Register Paradigm?

till-m commented 2 months ago

Hi @AArriandiaga,

I'm not too familiar with the domain reduction. I think you would need to manually execute something like this line, but then it should work.

bwheelz36 commented 2 months ago

Hey @AArriandiaga

@till-m is correct. I put together an end to end example that combines the maximise loop with the domain transformer below. Let us know how this works. If you'd like, it would be helpful you could add this to the domain transformer notebook and raise a PR :-)

import numpy as np
from bayes_opt import BayesianOptimization
from bayes_opt import SequentialDomainReductionTransformer
from bayes_opt import UtilityFunction
from matplotlib import pyplot as plt

def ackley(**kwargs):
    x = np.fromiter(kwargs.values(), dtype=float)
    arg1 = -0.2 * np.sqrt(0.5 * (x[0] ** 2 + x[1] ** 2))
    arg2 = 0.5 * (np.cos(2. * np.pi * x[0]) + np.cos(2. * np.pi * x[1]))
    return -1.0 * (-20. * np.exp(arg1) - np.exp(arg2) + 20. + np.e)

pbounds = {'x': (-5, 5), 'y': (-5, 5)}
bounds_transformer = SequentialDomainReductionTransformer(minimum_window=0.5)
utility = UtilityFunction(kind="ucb", kappa=2.5, xi=0.0)
mutating_optimizer = BayesianOptimization(
    f=ackley,
    pbounds=pbounds,
    verbose=0,
    random_state=1,
    bounds_transformer=bounds_transformer)

n_iterations = 50
for _ in range(50):
    next_point = mutating_optimizer.suggest(utility)
    target = ackley(**next_point)
    mutating_optimizer.register(params=next_point, target=target)
    mutating_optimizer.set_bounds(bounds_transformer.transform(mutating_optimizer.space))

x_min_bound = [b[0][0] for b in bounds_transformer.bounds]
x_max_bound = [b[0][1] for b in bounds_transformer.bounds]
x = [x[0] for x in mutating_optimizer.space.params]
bounds_transformers_iteration = list(range(0, len(x)))

image

AArriandiaga commented 2 months ago

Hi @till-m and @bwheelz36

I'll try today. Thank you so much to both.