Skip to content

Constraint_value not found in Suggest-Evaluate-Register Paradigm #375

Closed

Description

@Dongzhikang

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!

Activity

  1. t-muser commented on Nov 5, 2022

    @t-muser
    Member

    Hi @Dongzhikang,

    Looks like there might be a typo -- I think it should be constraint_value, not constrain_value.

    Let me know if this fixes your problem.

  2. Dongzhikang commented on Nov 5, 2022

    @Dongzhikang
    Author

    Hi @Dongzhikang,

    Looks like there might be a typo -- I think it should be constraint_value, not constrain_value.

    Let me know if this fixes your problem.

    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!

  3. t-muser commented on Nov 5, 2022

    @t-muser
    Member

    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.

  4. Dongzhikang commented on Nov 6, 2022

    @Dongzhikang
    Author

    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?

  5. bwheelz36 commented on Nov 7, 2022

    @bwheelz36
    Collaborator

    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

    def objective(**stuff):
        if constraint_violated:
            return low_value  # this is cheap
        else:
           true_objective = expensive_calculation()
            return true_objective
  6. aegonwolf commented on Nov 12, 2022

    @aegonwolf

    pip install git+https://github.com/fmfn/BayesianOptimization

    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?

  7. t-muser commented on Nov 13, 2022

    @t-muser
    Member

    Hi @aegonwolf,

    I don't think I quite understand. Could you expand on your suggestion?

  8. bwheelz36 commented on Nov 18, 2022

    @bwheelz36
    Collaborator

    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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions