Outcome constraints using constraint parameter in CEI #2117
-
In a lot of applications there is an outcome constraint (lower and or upper bound) on the black box function that you are wanting to optimize Using the forrester function as the objective function, I am attempting to find a set of points that minimize this function. import torch
from botorch.acquisition import ConstrainedExpectedImprovement
from botorch.fit import fit_gpytorch_model
from botorch.models import SingleTaskGP
from botorch.optim import optimize_acqf
from gpytorch.mlls import ExactMarginalLogLikelihood
def objective_fn(x):
return -((x + 1) ** 2) * torch.sin(2 * x + 2) / 5 + 1 + x / 3
lb, ub = -5, 5
bounds = torch.tensor([[lb], [ub]], dtype=torch.float)
xs = torch.linspace(lb, ub, 100).unsqueeze(1)
ys = -objective_fn(xs) # minimize --> flip the sign
n = 10
train_x = bounds[0] + (bounds[1] - bounds[0]) * torch.rand(n, 1)
train_y = -objective_fn(train_x) # minimize --> flip the sign
plt.figure(figsize=(4, 2))
plt.plot(xs, ys, label="Objective")
plt.scatter(train_x, train_y, color="black", label="Observations")
plt.axhline(y=0, color="k", linestyle="--", label="Upper Bound")
plt.legend(); Since BoTorch assumes maximization I flip the objective and set an upper bound outcome constraint of 0.0. Thus, I use I am getting this idea from the docstring of N_ITERATIONS = 20
for iteration in range(N_ITERATIONS):
model = SingleTaskGP(train_x, train_y)
mll = ExactMarginalLogLikelihood(model.likelihood, model)
fit_gpytorch_model(mll)
constraints = {0: (None, 0.0)} # 0th output should be non-negative
cEI = ConstrainedExpectedImprovement(
model,
best_f=train_y[train_y < 0].max(),
objective_index=1,
constraints=constraints
)
new_x, _ = optimize_acqf(
acq_function=cEI,
bounds=bounds,
q=1,
num_restarts=5,
raw_samples=20,
)
new_y = -objective_fn(new_x)
train_x = torch.cat([train_x, new_x])
train_y = torch.cat([train_y, new_y]) However, when running this example, I get the following error:
I think the issue is because this class is expecting the model to be multi-outcome. Indeed, the Closed-loop batch, constrained BO in BoTorch uses two GPs: one for the objective function and one for the constraint function. However, the docstring of CEI makes it seem as though you can use a Most of the tutorials I see for outcome constraints assume we know the cost function and thus train a GP for both the objective and cost function. However, I am interested in cases where all I know about the outcome constraint is the lower and upper bound of the domain according to technical specifications. Is it possible to achieve this without having to train two GPs: one for the objective function and one for the cost function? Thanks for your time and help! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 7 replies
-
So to distill your setup into a single sentence: You want to optimize some black box function If so, then this is something that we haven't really considered (typically you want to constrain on something other than the objective), which is likely why you're seeing this low-level failure. If you confirm that this is indeed what you want to do we can look into how to make that work. |
Beta Was this translation helpful? Give feedback.
should do the trick, no?