EmuKit / emukit

A Python-based toolbox of various methods in decision making, uncertainty quantification and statistical emulation: multi-fidelity, experimental design, Bayesian optimisation, Bayesian quadrature, etc.
https://emukit.github.io/emukit/
Apache License 2.0
593 stars 128 forks source link

unknown constraint loop usage and "Y_constraint not found in results object" #248

Open zhangsiyu1103 opened 4 years ago

zhangsiyu1103 commented 4 years ago

I tried to use the UnknownConstraintBayesianOptimizationLoop to optimize my objective function with constraint. However, I have a couple of issues with this loop.

First, when I initialize the loop with two gpy models and try to run it with "run_loop", I found the error "Y_constraint not found in results object". Tracing back to the function, I am wondering is there anything wrong with the create_loop_state function called in UnknownConstraintBayesianOptimizationLoop constructor.

Moreover, since run_loop only takes the objective function as a input, it can only update the objective model. However, from my understanding for the https://arxiv.org/abs/1403.5607 paper, the constraint model should also be updated with each iteration when a new constrained value calculated, but I don't see a implementation on this. Therefore, I am wondering that whether this part has not be implemented yet or I misunderstood it?

apaleyes commented 4 years ago

Can you share the code you are trying to run, and full stack trace of the error? Otherwise it would be very difficult to diagnose

zhangsiyu1103 commented 4 years ago

Here is the code.

parameter_space = ParameterSpace([DiscreteParameter('x1',input_data["maxtrain"] ), DiscreteParameter('x2', input_data["epochs"])])
num_data_points = 8
design = LatinDesign(parameter_space)
X = design.get_samples(num_data_points)
Y = f(X)
constrains = g(X)
model_gpy = GPRegression(X,Y)
model_cons_gpy = GPRegression(X,constrains)
#the line below is equivalent to exact_feval = true in gpyopt
model_gpy.Gaussian_noise.constrain_fixed(1e-6, warning=False)
model_cons_gpy.Gaussian_noise.constrain_fixed(1e-6, warning=False)
model_gpy.optimize()
model_cons_gpy.optimize()
model_emukit = GPyModelWrapper(model_gpy)
model_cons = GPyModelWrapper(model_cons_gpy)

expected_improvement = ExpectedImprovement(model = model_emukit)

bayesopt_loop =UnknownConstraintBayesianOptimizationLoop(
    model_objective = model_emukit,
    model_constraint = model_cons,
    space = parameter_space,
    acquisition = expected_improvement,
    batch_size = 1)

print("running optimization")
bayesopt_loop.run_loop(f, 100)
result = bayesopt_loop.get_results()

Here's the full stack trace

Traceback (most recent call last): File "emukit_test.py", line 331, in main() File "emukit_test.py", line 50, in main x_opt, fx_opt = BayesianOPT(input_data,target_perf,cons_perf,list_knobs,cons) File "emukit_test.py", line 165, in BayesianOPT bayesopt_loop.run_loop(f, 100) File "/h2/szhang/.local/lib/python3.6/site-packages/emukit/core/loop/outer_loop.py", line 84, in run_loop self._update_models() File "/h2/szhang/.local/lib/python3.6/site-packages/emukit/core/loop/outer_loop.py", line 97, in _update_models model_updater.update(self.loop_state) File "/h2/szhang/.local/lib/python3.6/site-packages/emukit/core/loop/model_updaters.py", line 58, in update targets = self.targets_extractor_fcn(loop_state) File "/h2/szhang/.local/lib/python3.6/site-packages/emukit/bayesian_optimization/loops/unknown_constraint_bayesian_optimization_loop.py", line 53, in lambda state: state.Y_constraint) File "/h2/szhang/.local/lib/python3.6/site-packages/emukit/core/loop/loop_state.py", line 61, in getattr raise ValueError('{} not found in results object'.format(item)) ValueError: Y_constraint not found in results object

zhangsiyu1103 commented 4 years ago

I realized that the "Y_constraint not found" error occurs because after the first optimization run, the results of loop_state does not include the extra_output because there is no extra_output in the run_loop function. Essentially, the reason is that the constraint function is not evaluated in the loop such that the loop_state for constraint is not updated.

I am trying modify both the update functions in loop_state and the run_loop function in the outer_loop to include the constraint function.