bayesian-optimization / BayesianOptimization

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

Doesn't work with *args or **kwargs in case you want to include many inputs #349

Closed abdullahmsalama closed 2 years ago

abdullahmsalama commented 2 years ago

Doesn't work with *args or *kwargs. I want to be able to optimize around 300 variables, I cannot just write them in the function input as they exceed python maximum, but when I try to add args, it conflicts because the input variable to be explicitly mentioned in pbounds. So I receive one of two errors:

1- "TypeError: cost_function() got an unexpected keyword argument 'input1'" or 2- "TypeError: cost_function() keywords must be strings"

To Reproduce

from bayes_opt import BayesianOptimization
from past.builtins import xrange

#cost_function (to be maximized)
def cost_function(*args):
    sum = 0
    for x in args:
        sum = sum + x
     return sum

# Bounded region of parameter space
pbounds = {}
for i in xrange(10):
    pbounds['input' + str(i + 1)] = (0, 100)

optimizer = BayesianOptimization(
    f = cost_function,\
    pbounds = pbounds,\
    random_state = 1,
)

optimizer.maximize(
    init_points = 2,
    n_iter = 1,
)
till-m commented 2 years ago

While I think that this is a valid point in general and the issue should probably be adressed, please note that (at least to the extent of my knowledge) BO does not perform particularly well when optimizing functions over high-dimensional spaces and it may be worth considering other approaches instead.

abdullahmsalama commented 2 years ago

100% agree, but it depends on how good of an optimization you are looking for, or that is my opinion :)

On Tue 23. Aug 2022 at 17:00, till-m @.***> wrote:

While I think that this is a valid point in general and the issue should probably be adressed, please note that (at least to the extent of my knowledge) BO does not perform particularly well when optimizing functions over high-dimensional spaces and it may be worth considering other approaches instead.

— Reply to this email directly, view it on GitHub https://github.com/fmfn/BayesianOptimization/issues/349#issuecomment-1224198362, or unsubscribe https://github.com/notifications/unsubscribe-auth/AGUAR4OPHPAKDWS2VGAHZD3V2TRPTANCNFSM57LIW64A . You are receiving this because you authored the thread.Message ID: @.***>

bwheelz36 commented 2 years ago

Hey, I can get this to run by changing the objective function as below. Does this solve this problem?

def cost_function(**kwargs):
    sum = 0
    for x in args:
        sum = sum + kwargs[x]
    return sum
abdullahmsalama commented 2 years ago

Yes, that solved it, it worked with **kwargs, I don't know how I missed it but I glad it works. Thanks @bwheelz36!

Deepyman commented 1 year ago

Yes, that solved it, it worked with **kwargs, I don't know how I missed it but I glad it works. Thanks @bwheelz36!

Hi, did you solve your 300 variables problem? The dimension is too high.....

abdullahmsalama commented 1 year ago

@Deepyman, no, I was just trying something out with the function. But ofc the dimension is too high so it takes ages.