Repository navigation
Closed
Description
Hi, I am a beginner of Bayesian Optimization. I am learning advanced-tour.ipynb and want to add constraint to Suggest-Evaluate-Register Paradigm, the following is a simple example
def black_box_function(x, y):
return -x ** 2 - (y - 1) ** 2 + 1
def constraint_function(x, y):
return 1 - x - y
constraint = NonlinearConstraint(constraint_function, np.array([0.0]), np.array([1.0]))
optimizer = BayesianOptimization(
f=None,
constraint=constraint,
pbounds={'x': (0, 1), 'y': (0, 1)},
verbose=2
)
utility = UtilityFunction(kind="ucb", kappa=2.5, xi=0.0)
next_point_to_probe = optimizer.suggest(utility)
for _ in range(5):
next_point = optimizer.suggest(utility)
target = black_box_function(**next_point)
optimizer.register(params=next_point, target=target, constraint_value = constraint_function(next_point['x'], next_point['y']))
print(target, next_point)
print(optimizer.max)
When I applied this constrain, I got the error
TypeError Traceback (most recent call last)
/tmp/ipykernel_11843/3829336133.py in <module>
18 next_point = optimizer.suggest(utility)
19 target = black_box_function(**next_point)
---> 20 optimizer.register(params=next_point, target=target, constraint_value = constraint_function(next_point['x'], next_point['y']))
21
22 print(target, next_point)
TypeError: register() got an unexpected keyword argument 'constrain_value'
If I don't give a value to constraint_value optimizer.register(params=next_point, target=target), I got
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/tmp/ipykernel_11843/2609747791.py in <module>
18 next_point = optimizer.suggest(utility)
19 target = black_box_function(**next_point)
---> 20 optimizer.register(params=next_point, target=target)
21
22 print(target, next_point)
~/anaconda3/envs/pinns-tf/lib/python3.7/site-packages/bayes_opt/bayesian_optimization.py in register(self, params, target)
178 def register(self, params, target):
179 """Expect observation with known target"""
--> 180 self._space.register(params, target)
181 self.dispatch(Events.OPTIMIZATION_STEP)
182
TypeError: register() missing 1 required positional argument: 'constraint_value'
I am confused that how I can assign the constraint_value.
Thank you so much!
Metadata
Metadata
Assignees
Labels
No labels
Activity
t-muser commentedon Nov 5, 2022
Hi @Dongzhikang,
Looks like there might be a typo -- I think it should be
constraint_value, notconstrain_value.Let me know if this fixes your problem.
Dongzhikang commentedon Nov 5, 2022
Thanks for quick reply! This is a typo in this issue, not in my code. It still doesn't work for me. Thank you so much!
t-muser commentedon Nov 5, 2022
Hi @Dongzhikang ,
my bad. I can't run your code since I'm on mobile currently.
I think the problem you're experiencing might be fixed in #368, unfortunately that PR is not yet pushed to PyPI/conda.
Can you uninstall the current version of the package and reinstall from this repo and let me know if that fixes the Problem?
Using pip you can install from here by running
pip install git+https://github.com/fmfn/BayesianOptimization. Make sure to uninstall first.Dongzhikang commentedon Nov 6, 2022
Thanks for your suggestion! It works. But I still have a problem. During this update process, I will get some values that are not bounded by my constraint function. Do you have any ideas?
If my constraint is explicit/cheap to compute, are there any approches to solve it?
bwheelz36 commentedon Nov 7, 2022
Hey, glad to hear that solved the error. We will do a new release soon.
For cheap constraints, I suggest encoding directly into the objective.
Depending on the exact form, it is also sometimes possible to reforumate the objective to explicitly include the constraint, e.g. see #355
aegonwolf commentedon Nov 12, 2022
Thanks a lot for the link to this issue, I'll close mine! May I ask for this suggestion, in the example from the examples, that would be including the constrained directly into the black-box function is that correct?
t-muser commentedon Nov 13, 2022
Hi @aegonwolf,
I don't think I quite understand. Could you expand on your suggestion?
bwheelz36 commentedon Nov 18, 2022
Hey @Till-M - I think they mean that we should put this advice in the examples as well - which we should - I am not finding much time to spend on this lately but hopefully will have some more time soon!