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

Is there any way to set an equality constraint? #24

Closed
Zeng1998 opened this issue Jul 6, 2022 · 2 comments
Closed

Is there any way to set an equality constraint? #24

Zeng1998 opened this issue Jul 6, 2022 · 2 comments

Comments

@Zeng1998
Copy link

Zeng1998 commented Jul 6, 2022

Describe the bug

import math
import numpy as np
from math import prod, fabs
from openbox import sp


def mishra(config: sp.Configuration):
    config_dict = config.get_dictionary()
    ws = np.array([config_dict['w%d' % i] for i in range(3)])
    # if fabs(sum(ws)-1)>1e-6:
    #     return {
    #         "objs":[math.inf,],
    #     }

    # if sum(ws) > 1:
    #     return {
    #         "objs": [math.inf, ]
    #     }

    result = dict()
    result['objs'] = [-1 * (ws[0] * 2 + ws[1] * 3 + ws[2]), ]
    # ?
    result["constraints"] = [fabs(sum(ws) - 1) - 1e-6, ]
    return result


N = 3
df = 1 / N
params = {
    'float': {
        f"w{i}": (0, 1, df) for i in range(N)
    }
}
space = sp.Space()
space.add_variables([
    sp.Real(name, *para) for name, para in params['float'].items()
])

from openbox import Optimizer

opt = Optimizer(
    mishra,
    space,
    num_objs=1,
    num_constraints=1,
    surrogate_type='gp',
    acq_optimizer_type='random_scipy',
    max_runs=50,
    time_limit_per_trial=10,
    task_id='soc',
)
history = opt.run()

print(history)

Here is my code, I want the sum of these three parameters to be 1, I tried adding constraints of both 1-sum(ws) and sum(ws)-1 but it doesn't work

@salty-fish-97
Copy link
Member

Thanks for your issue. It's very hard for black-box optimization to support black-box equality constraints, as the inner algorithm does not know how the constraint values are exactly computed. For your question, while the constraint value is directly computed by the input values (ws[0-2]), we suggest defining only 2 parameters in the search space, so that the third parameter can be represented by 1-ws[0]-ws[1]. We hope this can solve your problem.

@Zeng1998
Copy link
Author

Thanks.

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

No branches or pull requests

2 participants